STM32F4-DISCO 学习之FPU/DSP

/ 0评 / 0

STM32F4有DSP和FPU,FPU很容易就能开启了,在system_stm32f4xx.c就有设置:

  /* FPU settings ------------------------------------------------------------*/
  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
    SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */
  #endif

 __FPU_PRESENT就是说芯片有没有FPU功能,明显F4是有的,然后__FPU_USED就是说编译器有没有用FPU,明显我们一直都有开启.编译器都很聪明的,知道哪些要FPU哪些不用.所以只要这么设置了,就可以了,万事大吉.

QQ截图20150929191428

但是DSP库就不一样了,在以前用TI的片子,叫IQMath库,感觉就是浮点转定点做,牺牲精度换取时间.这个浮点转定点,在于其他芯片,也是可以用的,只要是ARM核,在M4可以用DSP.[怀疑态度]

stsw-stm32068STM32F4-Discovery_FW_V1.1.0LibrariesCMSISDSP_Lib

 数学库是给我们预先编译好的,当然我们也可以自己编译.但是好像并没有必要.对于我们工程,做以下添加,就可以开始测试Math的东西了.

QQ截图20150929191428

然后,添加库:

QQ截图20150929191428

添加声明:

QQ截图20150929191428

另外还可以做Q标定,详细,看IQMath吧,这不是讨论的范围.在需要Math的地方添加arm_math.h就可以了.示例程序,以及打包:

#include "stm32f4xx.h"
#include "arm_math.h"
#define USE_STATIC_INIT
 /* ----------------------------------------------------------------------
** Global defines
** ------------------------------------------------------------------- */
#define TEST_LENGTH_SAMPLES 	(20*4)
/* ----------------------------------------------------------------------
** List of Marks scored by 20 students for 4 subjects
** ------------------------------------------------------------------- */
const float32_t testMarks_f32[TEST_LENGTH_SAMPLES] =
{
	42.000000,	37.000000,	81.000000,	28.000000,
	83.000000,	72.000000,	36.000000,	38.000000,
	32.000000,	51.000000,	63.000000,	64.000000,
	97.000000,	82.000000,	95.000000,	90.000000,
	66.000000,	51.000000,	54.000000,	42.000000,
	67.000000,	56.000000,	45.000000,	57.000000,
	67.000000,	69.000000,	35.000000,	52.000000,
	29.000000,	81.000000,	58.000000,	47.000000,
	38.000000,	76.000000,	100.000000,	29.000000,
	33.000000,	47.000000,	29.000000,	50.000000,
	34.000000,	41.000000,	61.000000,	46.000000,
	52.000000,	50.000000,	48.000000,	36.000000,
	47.000000,	55.000000,	44.000000,	40.000000,
	100.000000,	94.000000,	84.000000,	37.000000,
	32.000000,	71.000000,	47.000000,	77.000000,
	31.000000,	50.000000,	49.000000,	35.000000,
	63.000000,	67.000000,	40.000000,	31.000000,
	29.000000,	68.000000,	61.000000,	38.000000,
	31.000000,	28.000000,	28.000000,	76.000000,
	55.000000,	33.000000,	29.000000,	39.000000
};
/* ----------------------------------------------------------------------
* Number of subjects X 1
* ------------------------------------------------------------------- */
const float32_t testUnity_f32[4] =
{
	1.000,  1.000, 	1.000,  1.000
};
/* ----------------------------------------------------------------------
** f32 Output buffer
** ------------------------------------------------------------------- */
static float32_t testOutput[TEST_LENGTH_SAMPLES];
/* ------------------------------------------------------------------
* Global defines
*------------------------------------------------------------------- */
#define 	NUMSTUDENTS  20
#define     NUMSUBJECTS  4
/* ------------------------------------------------------------------
* Global variables
*------------------------------------------------------------------- */
uint32_t  	numStudents = 20;
uint32_t  	numSubjects = 4;
float32_t	max_marks, min_marks, mean, std, var;
uint32_t 	student_num;
/* ----------------------------------------------------------------------------------
* Main f32 test function.  It returns maximum marks secured and student number
* ------------------------------------------------------------------------------- */
int32_t main()
{
#ifndef  USE_STATIC_INIT
  	arm_matrix_instance_f32 srcA;
  	arm_matrix_instance_f32 srcB;
  	arm_matrix_instance_f32 dstC;
	/* Input and output matrices initializations */
	arm_mat_init_f32(&srcA, numStudents, numSubjects, (float32_t *)testMarks_f32);
	arm_mat_init_f32(&srcB, numSubjects, 1, (float32_t *)testUnity_f32);
	arm_mat_init_f32(&dstC, numStudents, 1, testOutput);
#else
	/* Static Initializations of Input and output matrix sizes and array */
	arm_matrix_instance_f32 srcA = {NUMSTUDENTS, NUMSUBJECTS, (float32_t *)testMarks_f32};
	arm_matrix_instance_f32 srcB = {NUMSUBJECTS, 1, (float32_t *)testUnity_f32};
	arm_matrix_instance_f32 dstC = {NUMSTUDENTS, 1, testOutput};
#endif
	/* ----------------------------------------------------------------------
	*Call the Matrix multiplication process function
	* ------------------------------------------------------------------- */
	arm_mat_mult_f32(&srcA, &srcB, &dstC);
	/* ----------------------------------------------------------------------
	** Call the Max function to calculate max marks among numStudents
	** ------------------------------------------------------------------- */
	arm_max_f32(testOutput, numStudents, &max_marks, &student_num);
	/* ----------------------------------------------------------------------
	** Call the Min function to calculate min marks among numStudents
	** ------------------------------------------------------------------- */
	arm_min_f32(testOutput, numStudents, &min_marks, &student_num);
	/* ----------------------------------------------------------------------
	** Call the Mean function to calculate mean
	** ------------------------------------------------------------------- */
	arm_mean_f32(testOutput, numStudents, &mean);
	/* ----------------------------------------------------------------------
	** Call the std function to calculate standard deviation
	** ------------------------------------------------------------------- */
	arm_std_f32(testOutput, numStudents, &std);
	/* ----------------------------------------------------------------------
	** Call the var function to calculate variance
	** ------------------------------------------------------------------- */
	arm_var_f32(testOutput, numStudents, &var);
    while(1);                             /* main function does not return */
}

 程序下载:DSP_FPU测试

想知道速度提升,可以通过翻转IO,下断点观察,就不详细说了.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注