Why same code runs at different speed by mbed and KDS?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why same code runs at different speed by mbed and KDS?

726 Views
haizhouli
Contributor III

Hello all,

I have a ballistics algorithm running in our current product.  We plan to put that piece code into K63 later.  Then I did a quick preliminary evaluation half year ago by "mbed" using FRDM-k64F board right after FTF. The speed is great, every computation cycle only takes about 2 ms.

Now I transplanted that piece of code into a bare metal program by KDS. Exact same code, every cycle takes about 20 ms. It is about 10 times slower. I also tried the "arm_math.h". There is no difference at all.

Then I run my mbed code again. It is still 2 ms. I changed the float variables to volatile just in case the optimization. I also change the variables during the loop to avoid any optimization.  It still holds 2 ms.  The time was measured by an oscilloscope by toggle a IO right before and right after the calculation.

When I compared the calculation results from both mbed and KDS, the output are exactly same.

the processor setup is :  -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16

Anyone could know what I missed or KDS missed compared to mbed?

Thanks a lot!

Haizhou

0 Kudos
4 Replies

462 Views
ndavies
Contributor V

I don't know anything about the mbed code so this is just a guess.

Are you running both sets of code at the same clock frequency? Does the mbed environment use the PLL to speed up the CPU. Are you doing the same clock PLL initialization in your KDS code? To run at the full speed the CPU is capable of you need to configure the PLLs on power up. Depending on your crystal and PLL settings the base clock frequency could be 1/10 of what you're expecting.

0 Kudos

462 Views
haizhouli
Contributor III

Hey Norm,

Thanks for the reply! Sorry I did not mention the frequency setting in the first post.

I have no idea which frequency mbed running at. I bet it is 120MHz because it is really quick.

However, I do know which frequency I set in KDS.  I set the   SIM->CLKDIV1 = (uint32_t)0x01440000u and MCG->C6 = (uint8_t)0x46u.  I set the Flexbus CLKOUT pin and used oscilloscope to read it, it was 24MHz. It proves that the core was running at 120 MHz.  I even overclocked the C6 to 0x5A to 200MHz to improve the performance. It improved, but it is still slow.

Thanks!

Haizhou

0 Kudos

462 Views
BlackNight
NXP Employee
NXP Employee

have you turned on compiler optimizations? Try -O3.

Erich

0 Kudos

462 Views
haizhouli
Contributor III

Yes, I did turn on the -O3. with and without this setup, I can achieve about 10% speed increase. Thanks!

Haizhou

0 Kudos