Skip to main content
Ahmed Zahran
Associate II
May 29, 2017
Question

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

  • May 29, 2017
  • 2 replies
  • 3156 views
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
This topic has been closed for replies.

2 replies

AvaTar
Senior III
May 29, 2017
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.

Tesla DeLorean
Guru
May 29, 2017
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 VenmoUp vote any posts that you find helpful, it shows what's working..
Nickname12840_O
Associate
May 29, 2017
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

Ahmed Zahran
Associate II
June 2, 2017
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
AvaTar
Senior III
June 2, 2017
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.