2017-03-02 12:27 PM
I am using an STM32F407 (new to ST and this processor) and I need to be able to calculate sines, cosines, square roots, various other trig functions for a power conversion application.
Is there a math library I can download that contains these functions?
Thanks in advance for any assistance.
2017-03-02 12:35 PM
If you're using a C compiler (or pretty much any high-level language) then it should have trig functions built-in.
With C or C++ you'd have to #include <math.h>.
With 8-bit processors such as PIC16, I would often drop to assembly code. But for the size of project that would justify stm32, I find the benefits of C / C++ greatly outweigh the minor loss in ultimate achievable performance.
Hope this helps,
Danish
2017-03-02 12:47 PM
#include <math.h> ??
32-bit float variant, though FPU doesn't support transcendental functions natively
cosf(), sinf(), sqrtf(), powf()
2017-03-02 12:48 PM
Danish Ali, thanks for the prompt response. I've tried the math.h funciton and it takes too long (30us) to execute. Not sure if I'm doing something wrong here.
I guess I'm looking for something from a motor drive library, maybe something used for Park / Clark transformations.
2017-03-02 01:50 PM
Make sure to a) enable the FPU, and b) use the 32-bit float variants, the FPU doesn't do 64-bit doubles, or transcendentals like an 80x87 would.
If it still isn't fast enough, use tables.
2017-03-02 01:56 PM
the sinf funtion reduced execution down to about 1us, very helpful and thanks.
Since I'm new to the chip (coming from a TI fixed point world) how do I know if the FPU is enabled?
Assuming it is, lookup tables will be next if the speed isn't enough. Regardless, very helpful and thanks!!
2017-03-02 01:58 PM
In Keil it is a check box item (sets defines, etc), and then there is typically code in SystemInit() system_stm32f4xx.c
void SystemInit(void)
{ /* FPU settings ------------------------------------------------------------*/ &sharpif (__FPU_PRESENT == 1) && (__FPU_USED == 1) SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ &sharpendif...
2017-03-02 02:14 PM
I found the code you listed, confirmed that it's being executed, so I guess the FPU is active. If the 1us execution is too long then I'll implement a look-up table.
You've been very helpful Clive One, thanks!!