
1. Introduction
Some engineers have reported that they cannot use the STM32F4xx hardware floating point unit under Keil, resulting in excessively long computation times for floating point operations. Additionally, some people are unsure how to use the complex mathematical operations available in the chip, such as trigonometric functions. This article will provide a detailed introduction on how to use the hardware floating point unit and related mathematical operations.
2. Causes of the Problem
1. ——For Keil MDK Version 5, the compiler fully supports the STM32F4xx FPU (Floating Point Unit), allowing direct use of the chip’s internal floating point unit.
2. ——For Keil MDK Version 4, higher versions such as the current v4.74.0.0 available for download from the Keil website also support the FPU, allowing direct use of the chip’s internal floating point unit. However, if using a lower version like v4.23.0.0, corresponding software settings are required.
3. How to Solve the Problem
1. Check whether your current Keil version supports the FPU. The simplest way is to enter the Keil debugging interface and directly check the data at address 0xE000ED88. If it is 0x00F00000, it indicates that the FPU is supported, as shown in the figure below:

2. If the data at address 0xE000ED88 is 0x00000000, the following operations are required:
a. Add the following code in the system_stm32f4xx.c file within the systeminit() function:
/* FPU settings————————————————————*/
#if (__FPU_PRESENT == 1) &&(__FPU_USED == 1)
SCB->CPACR |= ((3UL << 10*2)|(3UL<< 11*2)); /* set CP10 and CP11 Full Access */
#endif
b. In the project options (Project->Options for target “XXXX”), add the following statements in the C/C++ tab under Define:
See the figure below:
__FPU_PRESENT=1,__FPU_USED=1.

c. This will add the code to enable the FPU during compilation, allowing the CPU to correctly and efficiently use the FPU for simple addition, subtraction, multiplication, and division.
3. Further Explanation on Using Complex Mathematical Operations
For complex operations, such as trigonometric functions and square roots, the following settings are required:
a. Include the arm_math.h header file.
b. In the project options under the C/C++ tab, continue to add the statement ARM_MATH_CM4 in the define section.
c. In the project options under the C/C++ tab, continue to add the statement __CC_ARM.
For example, to use sin and cos operations, you need to call arm_sin_f32() and arm_cos_f32(), which are defined in arm_sin_f32.c and arm_cos_f32.c, and these two C files need to be included in the project.
* The file directory in the ST library package is as follows:
\stm32f4_dsp_stdperiph_lib\STM32F4xx_DSP_StdPeriph_Lib_V1.1.0\Libraries\CMSIS\DSP_Lib\Source\FastMathFunctions
* The file directory in the Keil installation directory is as follows:
\Keil\ARM\CMSIS\DSP_Lib\Source\FastMathFunctions
When using more mathematical operations, such as square roots, trigonometric functions, absolute values, etc., users can also directly include the mathematical operation library arm_cortexM4lf_math.lib from ARM in the project, without needing to add each file individually.
* The directory in the ST library package is as follows:
\STM32F4xx_DSP_StdPeriph_Lib_V1.6.0\STM32F4xx_DSP_StdPeriph_Lib_V1.6.0\Libraries\CMSIS\Lib\ARM
* The directory in the Keil installation directory is as follows:
\Keil\ARM\CMSIS\Lib\ARM

4. Conclusion
From the testing results, when using the hardware floating point unit, mathematical calculations become simple and efficient, allowing the system more time to handle other control programs, effectively improving system efficiency and saving time.