搜索
bottom↓
回复: 10

STM32F4的FPU编译配置(含库文件解析)

[复制链接]

出0入0汤圆

发表于 2013-2-21 00:52:11 | 显示全部楼层 |阅读模式
本帖最后由 Earthman 于 2013-2-21 01:24 编辑

首先说明,有多次寻找各种关于stm32f4开FPU的资料,最后得到一些总结,然后完成了规范的配置
前贴不错,点我传送



好了,下面正式开始写


环境MDK v4.70a,CMSIS 3.01

首先看一段代码

来自core_cm4.h
  1. /** __FPU_USED indicates whether an FPU is used or not. For this, __FPU_PRESENT has to be checked prior to making use of FPU specific registers and functions.
  2. */
  3. #if defined ( __CC_ARM )
  4.   #if defined __TARGET_FPU_VFP
  5.     #if (__FPU_PRESENT == 1)
  6.       #define __FPU_USED       1
  7.     #else
  8.       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
  9.       #define __FPU_USED       0
  10.     #endif
  11.   #else
  12.     #define __FPU_USED         0
  13.   #endif

  14. #elif defined ( __ICCARM__ )
  15.   #if defined __ARMVFP__
  16.     #if (__FPU_PRESENT == 1)
  17.       #define __FPU_USED       1
  18.     #else
  19.       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
  20.       #define __FPU_USED       0
  21.     #endif
  22.   #else
  23.     #define __FPU_USED         0
  24.   #endif

  25. #elif defined ( __TMS470__ )
  26.   #if defined __TI_VFP_SUPPORT__
  27.     #if (__FPU_PRESENT == 1)
  28.       #define __FPU_USED       1
  29.     #else
  30.       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
  31.       #define __FPU_USED       0
  32.     #endif
  33.   #else
  34.     #define __FPU_USED         0
  35.   #endif

  36. #elif defined ( __GNUC__ )
  37.   #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  38.     #if (__FPU_PRESENT == 1)
  39.       #define __FPU_USED       1
  40.     #else
  41.       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
  42.       #define __FPU_USED       0
  43.     #endif
  44.   #else
  45.     #define __FPU_USED         0
  46.   #endif

  47. #elif defined ( __TASKING__ )
  48.   #if defined __FPU_VFP__
  49.     #if (__FPU_PRESENT == 1)
  50.       #define __FPU_USED       1
  51.     #else
  52.       #error "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
  53.       #define __FPU_USED       0
  54.     #endif
  55.   #else
  56.     #define __FPU_USED         0
  57.   #endif
  58. #endif
复制代码
这里针对各种工具链设置了相应的条件编译选项,这里只说MDK的,其他工具链的用户请参考本文自行修改

最上层的开关是 __CC_ARM 这个宏,很明显这个开关是针对MDK工具链的,同理IAR工具链的开关就叫做 __ICCARM__ 了;


然后一个开关就是 __TARGET_FPU_VFP 了,这个开关要求编译出的文件使用FPU功能,但是m4的FPU并不是必装设备,某些厂家可能会不装这个东西,所以需要检查是否有FPU,即 __FPU_PRESENT 这个开关。在core_cm4.h中有这样的代码
  1.   #ifndef __FPU_PRESENT
  2.     #define __FPU_PRESENT             0
  3.     #warning "__FPU_PRESENT not defined in device header file; using default!"
  4.   #endif

  5.   #ifndef __MPU_PRESENT
  6.     #define __MPU_PRESENT             0
  7.     #warning "__MPU_PRESENT not defined in device header file; using default!"
  8.   #endif
复制代码
即CMSIS默认的定义是没有FPU的,所以需要我们手动添加FPU存在的定义
  1. #define __FPU_PRESENT        1
复制代码
这样FPU存在与否的检查就通过了,所以后面就自动定义了 __FPU_USED 这一开关
  1. #define __FPU_USED       1
复制代码
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

下面继续翻,因为m4增加了FPU,所以很多数学函数都应该用m4专用的,优化过的,而不是之前的通用函数

打开arm_math.h
可以看见开头的一大段说明

   * Each library project have differant pre-processor macros.
   *
   * <b>ARM_MATH_CMx:</b>
   * Define macro ARM_MATH_CM4 for building the library on Cortex-M4 target, ARM_MATH_CM3 for building library on Cortex-M3 target
   * and ARM_MATH_CM0 for building library on cortex-M0 target.
   *
   * <b>ARM_MATH_BIG_ENDIAN:</b>
   * Define macro ARM_MATH_BIG_ENDIAN to build the library for big endian targets. By default library builds for little endian targets.
   *
   * <b>ARM_MATH_MATRIX_CHECK:</b>
   * Define macro for checking on the input and output sizes of matrices
   *
   * <b>ARM_MATH_ROUNDING:</b>
   * Define macro for rounding on support functions
   *
   * <b>__FPU_PRESENT:</b>
   * Initialize macro __FPU_PRESENT = 1 when building on FPU supported Targets. Enable this macro for M4bf and M4lf libraries



里面说了,要用ARM的数学函数得定义ARM_MATH_CMx
m4的内核自然是
  1. #define ARM_MATH_CM4
复制代码
至于是大端机还是小端机,我可管不着,ST得给我搞定
最后关于__FPU_PRESENT的定义,前面已经说过了

最后总结如下
如果你执意使用FPU,而不管Project中的设置那么使用下面的代码
  1. #define __FPU_PRESENT 1  /* FPU开关 */

  2. #ifndef  __TARGET_FPU_VFP /*要求在生成的代码中使用FPU*/
  3. #define __TARGET_FPU_VFP
  4. #endif

  5. #ifndef  ARM_MATH_CM4 /*要求使用m4的数学库函数*/
  6. #define ARM_MATH_CM4
  7. #endif
复制代码

出0入0汤圆

 楼主| 发表于 2013-2-21 01:26:32 | 显示全部楼层
此外,我还在其他帖子中看到,要使用浮点数,应当使用 1.111f 而不是 1.111 这样的形式,不知对否,希望后面有跟帖有关于FPU用法的深入讨论

出0入0汤圆

发表于 2013-2-21 03:12:51 | 显示全部楼层
Earthman 发表于 2013-2-21 01:26
此外,我还在其他帖子中看到,要使用浮点数,应当使用 1.111f 而不是 1.111 这样的形式,不知对否,希望后 ...

1.111f -- float
1.111 --- double

出0入0汤圆

发表于 2013-6-23 16:47:21 | 显示全部楼层
  1. static void thread_fpu_entry(void* parameter)
  2. {
  3.     volatile float value = 1.1f;
  4.     while(1)
  5.     {
  6.         value = value * 2.1f;
  7.         rt_thread_delay(1);
  8.     }
  9. }
复制代码
写了个测试程序没有加f时,在IAR(6.5)中竟然当成双精度,去调试浮点库去完成操作。

出0入0汤圆

发表于 2013-10-8 22:00:13 | 显示全部楼层
多谢楼主分享,不知道在MDK的Options中开启了FPU功能后,是否还需要在修改.H的文件内容?

出0入0汤圆

发表于 2013-10-16 23:42:48 | 显示全部楼层
ST_ATMEL_NXP 发表于 2013-10-8 22:00
多谢楼主分享,不知道在MDK的Options中开启了FPU功能后,是否还需要在修改.H的文件内容? ...

请问你搞定没有?  我刚接触 搞了两天 没搞明白, 我拿官方例程编译的时候报错说: cannot open source input file "ARMCM4.h": No such file or directory, 然后在网上查了一下资料说要在 options --> C/C++ Define  添加   ARM_MATH_CM4, __FPU_PRESENT =1,__FPU_USED=1,__CC_ARM     可是添加了以后编译还是错误

出0入0汤圆

发表于 2013-10-17 08:13:06 | 显示全部楼层
tianyu1989 发表于 2013-10-16 23:42
请问你搞定没有?  我刚接触 搞了两天 没搞明白, 我拿官方例程编译的时候报错说: cannot open source i ...

没有搞定,更换了RTX了。可以正常运行。我也只是用了用任务切换和几个信号量。

出0入0汤圆

 楼主| 发表于 2013-12-20 18:53:38 | 显示全部楼层
tianyu1989 发表于 2013-10-16 23:42
请问你搞定没有?  我刚接触 搞了两天 没搞明白, 我拿官方例程编译的时候报错说: cannot open source i ...

像这样
  1. #define __FPU_USED       1
复制代码
在头文件里定义就行,反正我用mdk4.7x都可以的

出0入0汤圆

发表于 2014-3-16 09:03:59 | 显示全部楼层
最近公司高算法需要用到这个FPU,从楼主这里学习了!

出0入0汤圆

发表于 2014-6-19 01:29:46 | 显示全部楼层
好贴 要是能再介绍一下IAR开发环境下FPU就更好了

出0入0汤圆

发表于 2014-7-11 11:27:03 | 显示全部楼层
不错的,正好可能需要
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-20 14:34

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表