At present, there are mainly high-end DSP (such as TI F28335/C6000/DM6XX/OMAP), general CPU(X87 math coprocessor) and advanced ARM+DSP processor.
STM32-F4 belongs to Cortex-M4F architecture, and its biggest difference from M0 and M3 is that it has an additional F-float, which supports floating-point instruction set, so it can be tens or even hundreds times higher than M0/M3 in mathematical operation, but some small settings are needed to give full play to the mathematical performance of FPU:
1. Compilation control option: Although the corresponding code is added in the system _ STM32F4XXx.c file of the routine of stm32f4xx firmware library, it is not in the STM32F4-Discovery routine used for user evaluation. Therefore, when MDK4.23 was writing a floating-point operation program, although the compiler correctly generated the V instruction to perform floating-point operation, the CPU just thought that it had encountered an illegal instruction and jumped to the HardFault_Handler () interrupt because the FPU was not enabled in the system_stm32f4XXX.c file. Therefore, in order to ensure that this error will not occur, you must add the following code to the system_init () function:
# if(_ _ FPU _ PRESENT = = 1)& amp; & amp(__FPU_USED == 1)
SCB->cpar | =((3UL & lt; & lt 10 * 2)|(3UL & lt; & lt 1 1*2));
#endif
Because this option is controlled by conditional compilation, it needs to be set in project options (project->; Add the following statements to the definition of C/C++ tab in the options of the target "XXXX": _ _ fpu _ present = 1, _ _ fpu _ used = 1. In this way, when compiling, the code for starting FPU is added, so that CPU can use FPU correctly and efficiently for simple addition, subtraction, multiplication and division.
But even this would not be enough For complex operations, such as trigonometric functions and square root operations, if the math.h header file is still used in programming, the efficiency will not be improved: because the math.h header file is aimed at all ARM processors, its operation function is based on fixed-point CPU and standard algorithm (IEEE-754), the use of FPU is not foreseen, and many instructions and complicated processes are needed to complete the operation, thus increasing the operation time. Therefore, in order to give full play to the floating-point function of M4F, we need to use the arm_math.h that comes with the firmware library. This file determines which function method to use according to the compilation control item (__FPU_USED == 1): If FPU is not used, call the function defined in keil's standard math.h header file; If you use FPU, you use the optimization function of firmware library to solve the problem.
At the beginning of arm_math, there are these compilation control information:
#ifndef _ARM_MATH_H
#define _ARM_MATH_H
#define __CMSIS_GENERIC
# If defined (ARM_MATH_CM4)
#include "core_cm4.h "
#elif already defined (ARM_MATH_CM3)
# contains "core_cm3.h"
#elif already defined (ARM_MATH_CM0)
#include "core_cm0.h "
# Otherwise
# including "ARMCM4.h"
#warning "define ARM_MATH_CM4 or ARM_MATH_CM3 ... It is built on ARM_MATH_CM4 by default ..."
#endif
#undef __CMSIS_GENERIC
# contains "string.h"
# contains "math.h"
In other words, if CMSIS not used, keil's own standard library functions will be called. Otherwise, use the definition of CMSIS Because STM32F4 is used here, you should use ARM_MATH_CM4 for control, that is, you should add core_cm4.h, otherwise you should use ARM CM4.h-but keil will prompt that this file cannot be found at compile time. Therefore, it is necessary to continue to add ARM_MATH_CM4 statement in the definition of C/C++ tab of engineering options.
After adding the above compiled controls, it is basically no problem to use advanced mathematical functions, such as the calculation of sine and cosine trigonometric functions. However, it should be noted that if functions such as sin (), cos () and sqrt () are directly used, the result is still to call math.h of keil. When debugging, you can look at the corresponding code, and its assembly instruction is BL. W __hardfp_xxx。 Therefore, in order to complete the calculation of trigonometric function, you must use arm_sin_f32 () or arm_cos_f32 (), and the usage remains unchanged. The prototypes of these two functions are in arm_sin_f32.c and arm_cos_f32.c respectively. Through the query and interpolation algorithm of 256-point trigonometric function table, the exact function value of any angle can be obtained, which is much faster than the original sin () and cos ().
Of course, some exceptions are the development function sqrt (), which is defined in arm_math.h:
static _ _ INLINE arm _ status arm _ sqrt _ f32(float 32 _ t in,float32_t *pOut)
{
If (at>0)
{
// #if __FPU_USED
# if(_ _ FPU _ USED = = 1)& amp; & defined (__CC_ARM)
* pOut = _ _ sqrtf(in);
# Otherwise
* pOut = sqrtf(in);
#endif
return(ARM _ MATH _ SUCCESS);
}
other
{
* pOut = 0.0f
Returns (arm _ math _ argument _ error);
}
}
That is to say, the function for prescribing is arm_sqrt_f32 (), in which it is first judged whether the book being developed is greater than 0, and only those that are greater than 0 can be operated, otherwise the output result is 0 and an "error" flag is returned. If it is greater than 0, and FPU and __CC_ARM controls are used, call __sqrtf () to complete the compilation, otherwise call sqrtf()- this sqrtf () can be found in math.h of keil, that is, call a subfunction to complete the operation, and __sqrtf ()? The new one, I believe everyone can guess what it is: yes, it is the VSQRT instruction! Therefore, in order to exert this performance, it is necessary to add the statement __CC_ARM to the definition of the C/C++ tab of engineering options. You can compare whether to join __CC_ARM, and the compiled assembly code is very different.
Of course, the arm_sqrt_f32 () function is still a bit troublesome. If the confirmation specification is greater than or equal to 0, then directly use the __sqrtf () function to complete the operation, which is a simple VSQRT instruction.
The STM32F4 firmware library also provides other useful mathematical functions, which are located in the DSP_Lib folder. Please explore slowly and discover!
Teaching summary of the first-grade math teacher 1
This semester, I undertook the task of mathematics teaching in senior one