折腾了那么久,终于有点好东西拿出来跟大家分享分享咯
1.确认编译器时候选择了FPU
2.勾选IAR自带的DSP库,不知道干嘛一定要用这个自带库,我在MDK测试都没有这么坑,(调了很久
)
3.添加宏定义__FPU_USED
__ICCARM__
__FPU_PRESENT
ARM_MATH_CM7
ARM_MATH_MATRIX_CHECK
ARM_MATH_ROUNDING
4.以下是DSP lib的定义,STM32F7属于小端单浮点型模式
5.添加代码,记得把DSP库的头文件路径加上
- #include "math.h"
- #include "arm_math.h"
- #define DELTA 0.0001f
- uint8_t sin_cos_test(float angle,uint32_t times,uint8_t mode)
- {
- float sinx,cosx;
- float result;
- uint32_t i=0;
- if(mode==0)
- {
- for(i=0;i<times;i++)
- {
- cosx=cosf(angle); //不使用 DSP 优化的 sin,cos 函数
- sinx=sinf(angle);
- result=sinx*sinx+cosx*cosx; //计算结果应该等于 1
- result=fabsf(result-1.0f); //对比与 1 的差值
- if(result>DELTA)return 0XFF;//判断失败
- angle+=0.001f; //角度自增
- }
- }else
- {
- for(i=0;i<times;i++)
- {
- cosx=arm_cos_f32(angle); //使用 DSP 优化的 sin,cos 函数
- sinx=arm_sin_f32(angle);
- result=sinx*sinx+cosx*cosx; //计算结果应该等于 1
- result=fabsf(result-1.0f); //对比与 1 的差值
- if(result>DELTA)return 0XFF;//判断失败
- angle+=0.001f; //角度自增
- }
- }
- return 0;//任务完成
- }
- uint8_t timeout;//定时器溢出次数
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- TIM_HandleTypeDef TimHandle;
- void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
- {
- /*##-1- Enable peripheral clock #################################*/
- /* TIMx Peripheral clock enable */
- __HAL_RCC_TIM3_CLK_ENABLE();
-
- /*##-2- Configure the NVIC for TIMx ########################################*/
- /* Set the TIMx priority */
- HAL_NVIC_SetPriority(TIM3_IRQn, 3, 0);
- /* Enable the TIMx global Interrupt */
- HAL_NVIC_EnableIRQ(TIM3_IRQn);
- }
- void initTIM3(void)
- {
- /* Compute the prescaler value to have TIMx counter clock equal to 10000 Hz */
- /* Set TIMx instance */
- TimHandle.Instance = TIM3;
- /* Initialize TIMx peripheral as follows:
- + Period = 10000 - 1
- + Prescaler = ((SystemCoreClock / 2)/10000) - 1
- + ClockDivision = 0
- + Counter direction = Up
- */
- TimHandle.Init.Period = 65535;
- TimHandle.Init.Prescaler = (uint32_t)(HAL_RCC_GetSysClockFreq() / 2) ;
- TimHandle.Init.ClockDivision = 0;
- TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
- TimHandle.Init.RepetitionCounter = 0;
- if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK)
- {
- /* Initialization Error */
- Error_Handler();
- }
- /*##-2- Start the TIM Base generation in interrupt mode ####################*/
- /* Start Channel1 */
- if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- }
- /**
- * [url=home.php?mod=space&uid=159083]@brief[/url] Main program
- * @param None
- * @retval None
- */
- int main(void)
- {
- uint8_t res;
- float time;
- uint8_t buf[50];
- /* Enable the CPU Cache */
- CPU_CACHE_Enable();
- /* STM32F7xx HAL library initialization:
- - Configure the Flash ART accelerator on ITCM interface
- - Configure the Systick to generate an interrupt each 1 msec
- - Set NVIC Group Priority to 4
- - Global MSP (MCU Support Package) initialization
- */
- HAL_Init();
-
- /* Configure the system clock to 200 MHz */
- SystemClock_Config();
-
- /* Configure LED1 */
- BSP_LED_Init(LED1);
-
- /*##-1- Configure LCD ######################################################*/
- LCD_Config();
-
- /* Configure TAMPER Button */
- BSP_PB_Init(BUTTON_TAMPER, BUTTON_MODE_GPIO);
-
- BSP_LCD_Clear(LCD_COLOR_WHITE);
- // BSP_LCD_DisplayStringAtLine(8, (uint8_t*)" Please insert SD Card ");
- while(1)
- {
- initTIM3();
- timeout=0;
- res=sin_cos_test(PI/6,200000,0); //不使用DSP库
- time=TIM3->CNT+(uint32_t)65536*timeout;
- sprintf((char*)buf,"Runtime:%0.6fms",time/10);
- if(0==res)
- {
- BSP_LCD_DisplayStringAtLine(1, (uint8_t*)"No Use DSP");
- BSP_LCD_DisplayStringAtLine(2, (uint8_t*)buf);
-
- }
- else BSP_LCD_DisplayStringAtLine(8, (uint8_t*)"error");
-
- initTIM3();
- timeout=0;
- res=sin_cos_test(PI/6,200000,1); //使用DSP库
- time=TIM3->CNT+(uint32_t)65536*timeout;
- sprintf((char*)buf,"Runtime:%0.6fms",time/10);
- if(0==res)
- {
- BSP_LCD_DisplayStringAtLine(7, (uint8_t*)"Use DSP");
- BSP_LCD_DisplayStringAtLine(8, (uint8_t*)buf);
- }
- else BSP_LCD_DisplayStringAtLine(8, (uint8_t*)"error");
- }
- }
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- timeout++;
- BSP_LED_Toggle(LED1);
- }
- //部分代码参考,正点原子,如有侵权请联系删除
复制代码运行结果:
没有使用硬件浮点运算的效果
使用硬件浮点运算的效果