Problem with code geting optimized out

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

Problem with code geting optimized out

Jump to solution
2,176 Views
davepfaltzgraff
Senior Contributor I

I am trying to debug an SPI implementation and found that in the pin_mux_SPI() routine where the code is:

 

     /* PORTD_PCR4 */

     PORT_HAL_SetMuxMode(PORTD,4u,kPortMuxAlt7);

     /* PORTD_PCR5 */

     PORT_HAL_SetMuxMode(PORTD,5u,kPortMuxAlt7);

     /* PORTD_PCR6 */

     PORT_HAL_SetMuxMode(PORTD,6u,kPortMuxAlt7);

     /* PORTD_PCR7 */

     PORT_HAL_SetMuxMode(PORTD,7u,kPortMuxAlt7);

 

Only the first PORT_HAL_SetMuXMode insatance is executed. It appears that the remaining lines get optimized out. I tested this by setting the optimization level to None and all the lines get executed.

 

Is there a setting that would allow me to say "Don't optimize this section"?

 

More importantly, can the compiler be fixed?

 

My environment is for the FRDM-K22F board and KDS version 3.0.0 with KSDK version 1.2.0 running on Windows 7.

 

Thanks

Labels (1)
1 Solution
1,639 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi,

please try if one of below solution can help:

Solution 1:

Use "KEEP" command to force the linker to  not  dead strip the unused symbols in the specified section in ld or lcf file.

Syntax

KEEP(*(sectionType))

Example

  KEEP Directive Usage

GROUP : { .text (TEXT) : {}  .mycode (TEXT) : {KEEP(*(.mycode))}  ...}


Solution2:

if you use GCC compiler, insert

asm ("nop");

to the repetition functions.

for example,

     PORT_HAL_SetMuxMode(PORTD,4u,kPortMuxAlt7);

     asm ("nop");

     PORT_HAL_SetMuxMode(PORTD,5u,kPortMuxAlt7);

          asm ("nop");

see how does it work?


Have a great day,
Zhang Jun

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

3 Replies
1,639 Views
joeseymour
Contributor III

> Is there a setting that would allow me to say "Don't optimize this section"?

You can use the optimize attribute to compile a particular function at a different optimization level: Common Function Attributes - Using the GNU Compiler Collection (GCC)

> More importantly, can the compiler be fixed?

The toolchain in KDS 3.0.0 is provided by the GCC ARM Embedded project. They encourage you to first ask a question on their launchpad page then file a bug: GCC ARM Embedded in Launchpad

Without a test case it's difficult to say, but sometimes functions/macros like those you quote are inlined and have very simple bodies (x |= 0xf00 for example) , as such the compiler is able to combine all four apparent function calls into a single sequence of instructions. The result is that if you try and step over them in a debugger it appears only the first is executed, when in fact all four are executed at the same time. If that's what you're observing, it would be worth checking the state of the target after this block of code, to see if it has executed correctly.

Hope that helps,

Joe

1,640 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi,

please try if one of below solution can help:

Solution 1:

Use "KEEP" command to force the linker to  not  dead strip the unused symbols in the specified section in ld or lcf file.

Syntax

KEEP(*(sectionType))

Example

  KEEP Directive Usage

GROUP : { .text (TEXT) : {}  .mycode (TEXT) : {KEEP(*(.mycode))}  ...}


Solution2:

if you use GCC compiler, insert

asm ("nop");

to the repetition functions.

for example,

     PORT_HAL_SetMuxMode(PORTD,4u,kPortMuxAlt7);

     asm ("nop");

     PORT_HAL_SetMuxMode(PORTD,5u,kPortMuxAlt7);

          asm ("nop");

see how does it work?


Have a great day,
Zhang Jun

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

1,639 Views
davepfaltzgraff
Senior Contributor I

Hi Zhang,

Adding the asm("nop") between each line of code took care of the problem. Since this code is executed only during initialization, the added instructions don't bother me.

This was the easiest solution for my case, but I have to wonder if others aren't seeing the same kind of thing since the code is right out of the KSDK pin_mux.c module.

Thanks,

Dave

0 Kudos