[En-Nut-Discussion] NutThreadSwitch and debug build

Pete Allinson pete at peteallinson.plus.com
Wed Feb 9 18:41:16 CET 2005


Hi,

I decided to make a debug build of Nut/OS by altering the CPFLAGS to 
include -g and no optimisation.
But this led to a problem in NutThreadSwitch as the compiler introduced 
some prologue and epilogue code:

NutThreadSwitch:
<unwanted prologue>
     push r28
     push r29
     in r28, 0x3d
     in r29, 0x3e
</unwanted prologue>
     push r2
     push r3
     push r4
     ... etc ...
thread_start:
     ... etc ...
     pop r4
     pop r3
     pop r2
<unwanted epilogue>
     pop r29
     pop r28
</unwanted epilogue>
     ret

As you can imagine, the epilogue and prologue mess up thread switching.

I fixed the problem by adding "__attribute__ ((naked)" to the declaration 
for NutThreadSwitch thus:

void NutThreadSwitch(void) __attribute__ ((noinline)) __attribute__ ((naked));

and putting a "ret" instruction in the second asm statement, thus:

                   "pop r4" "\n\t"
                   "pop r3" "\n\t"
                   "pop r2" "\n\t"
                   "ret" "\n\t"::"r" (runningThread->td_sp), "I" 
_SFR_IO_ADDR(SPL), "I" _SFR_IO_ADDR(SPH)
         );
}

I'm running Nut/OS 3.9.2 and WinAVR with avr-gcc version 3.3.1 on an 
Ethernut 2.1b.
I've not tested other configurations for any similar problem.

BTW.  I notice that some code, eg NutInit(), writes to 0x005D to set the 
ATmega128 stack pointer, whereas other code, eg NutThreadSwitch(), writes 
to 0x003D to do this.
Is this anything to do with the different instruction used in each case - 
out .v. sts ?

Cheers,
Pete Allinson




More information about the En-Nut-Discussion mailing list