cancel
Showing results for 
Search instead for 
Did you mean: 

STM32MAT MATLAB generated code for sine cosine not using ARM sine cosine instructions

Ahmed Zahran
Associate II
Posted on May 29, 2017 at 10:26

I faced problem when I tried to generate sine using Matlab Embedded coder as it is appear with normal format without using ARM instructions. I installed ARM CORTEX M through matlab package installer but it didn't appear in the replacement functions menu. any body can help as my code calculate a lot of sine cosine operations and the execution time is too big!

Thanks in advance    

0690X000006078GQAQ.png0690X000006078LQAQ.png0690X0000060786QAA.png0690X000006078BQAQ.png

#stm32-mat/target-matlab #matlab
11 REPLIES 11
AvaTar
Lead
Posted on May 29, 2017 at 13:37

The FPU of the Cortex M has no sin or cos instructions, the need to be emulated.

And, your generated code consistently uses double variables and constants. Neither is supported natively, only single precision (except some of the very latest Cortex M7 variants).

And some Cortex M cores (M0 .. M3) have no FPU at all, so any fp operation must be emulated.

Posted on May 29, 2017 at 14:51

Hi Ahmed,

'ARM CMSIS SIN COS' CRL doesn't provide code replacement for all inputs type.

Only, single, int16 and int32.

Can you please cast result of function called into sin to single.

By default it is computed as double and there is no sin CRL matching function.

For example :

cos(cast(theta-2*pi/3,

'single'

))

will be replaced but not without cast.

Best regards

Pascal

Posted on May 29, 2017 at 19:30

The original Cortex-M7 design had doubles from the outset, Atmel built such a part and delivered it in production quantities before ST, who instead chose a stripped down single precision variant.

Not aware of ARM support for transcendental functions in the FPU like there were with 80x87 and 6888x designs, so no sin, cos, log, etc

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on May 30, 2017 at 11:29

but my controller support FPU as it is STM32F429? I tried also to use sine wave function generator but the same result has been obtained!  

Posted on May 30, 2017 at 12:03

but my controller support FPU as it is STM32F429?

Read the answers carefully.

No hardware support of 'double' type, no hardware support of trigonometric functions.

Posted on June 02, 2017 at 11:25

Thanks for your assistance. the problem was in the data type. after I adjusted my model to deal with single variables instead of double everything went well! as you can find in the image below, but I faced another problem as the sine wave generated frequency is 35 hz instead of 50hz. I think that happen due to the long execution time. kindly note that my PWM frequency is 10k and sampling time for the software is 10-4 sec.

0690X00000607D6QAI.png
Posted on June 02, 2017 at 11:38

'

so

any

fp operation must be emulated

' do you mean by using look-up table as example for the 

trigonometric functions? also after I converted my variables to be single the replacement code function worked fine but still the execution time is high as the sine wave frequency appear by 35 hz in the hardware instead of 50hz do you know any way to calculate the execution time exactly for my code through matlab or keil (kindly note that I using Nucleo

STM32F429  )  

Thanks in advance

Posted on June 02, 2017 at 12:06

'

so

any

fp operation must be emulated

' do you mean by using look-up table as example for the 

trigonometric functions?

The statement would apply if you used a Cortex M0 or Cortex M3. This is not the case for the F429.

You would not need to write any of this 'emulation code' yourself, the appropriate 'libm.a' version would do that for you.

Don't know about Matlab/Simulink. With my shallow knowledge of Matlab, I would suggest to start by modifying model to use single-precision instead of double.

... but still the execution time is high ...

Not sure if this has to do with the math/trigonometric code alone. You can measure the runtime of certain code (functions) either with the support of the toolchain (so-called 'profiling'), or add GPIO toggles before/after the code and measure with a scope. Not so familiar with Keil, but I believe it has extensive code profiling support.

But I suspect your runtime issue is rather with code organization than with the math code itself. Just for comparison, I achieved a runtime of 2.5ms to 4.0ms for a 2048-point FFT, using the CMSIS DSP lib code.

As additional hints:

 - check that your MCU application actually uses the FPU

 - turn on optimization for specific routines

 - don't forget to convert constants to float;

3.14

is double and involves a implicit conversion;

3.14

f

would be float
Posted on June 02, 2017 at 12:41

I guess my comment above about profiling still applies.

Check out where the runtime is actually spent - I am not so sure it is you sine wave generation.

kindly note that my PWM frequency is 10k and sampling time for the software is 10-4 sec.

Assuming 10-4 sec means 100us (10^-4 s), your code might be in trouble because you overload an interrupt routine.