cancel
Showing results for 
Search instead for 
Did you mean: 

Looking for a trig function library?

abarnett
Associate II
Posted on March 02, 2017 at 21:27

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.

7 REPLIES 7
Danish1
Lead II
Posted on March 02, 2017 at 21:35

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

Posted on March 02, 2017 at 21:47

#include <math.h> ??

32-bit float variant, though FPU doesn't support transcendental functions natively

cosf(), sinf(), sqrtf(), powf()

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
abarnett
Associate II
Posted on March 02, 2017 at 21:48

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.  

Posted on March 02, 2017 at 21:50

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.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on March 02, 2017 at 21:56

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!!

Posted on March 02, 2017 at 21:58

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

...

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on March 02, 2017 at 22:14

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!!