[En-Nut-Discussion] NutOs context switching

Ralph Mason ralph.mason at telogis.com
Thu Nov 20 10:00:35 CET 2003


You need to save the call saved registers.  I am not sure about ICCAVR but
under GCC it's considerably more than R28 &R29.

I raised this some months back. I have been running this code with no
problems on GCC

#elif defined __GNUC__
/*!
* \brief Switch to another thread.
*
* Stop the current thread, saving its context. Then start the
* one with the highest priority, which is ready to run.
*
* Application programs typically do not call this function.
*
* \note CPU interrupts must be disabled before calling this function.
*
*/
void NutThreadSwitch(void)
{
/*
* Save all Call Saved CPU registers.
*/
asm volatile ("push r2" "\n\t"
"push r3" "\n\t"
"push r4" "\n\t"
"push r5" "\n\t"
"push r6" "\n\t"
"push r7" "\n\t"
"push r8" "\n\t"
"push r9" "\n\t"
"push r10" "\n\t"
"push r11" "\n\t"
"push r12" "\n\t"
"push r13" "\n\t"
"push r14" "\n\t"
"push r15" "\n\t"
"push r16" "\n\t"
"push r17" "\n\t"
"push r28" "\n\t"
"push r29" "\n\t"
"in %A0, %1" "\n\t"
"in %B0, %2" "\n\t":"=r" (runningThread->td_sp)
:"I" _SFR_IO_ADDR(SPL), "I" _SFR_IO_ADDR(SPH)
);
/*
* This defines a global label, which may be called
* as an entry point into this function.
*/
asm volatile (".global thread_start\n" "thread_start:\n\t"::);
/*
* Reload CPU registers from the thread in front
* of the run queue.
*/
runningThread = runQueue;
runningThread->td_state = TDS_RUNNING;

asm volatile ("out %1, %A0" "\n\t"
"out %2, %B0" "\n\t"
"pop r29" "\n\t"
"pop r28" "\n\t"
"pop r17" "\n\t"
"pop r16" "\n\t"
"pop r15" "\n\t"
"pop r14" "\n\t"
"pop r13" "\n\t"
"pop r12" "\n\t"
"pop r11" "\n\t"
"pop r10" "\n\t"
"pop r9" "\n\t"
"pop r8" "\n\t"
"pop r7" "\n\t"
"pop r6" "\n\t"
"pop r5" "\n\t"
"pop r4" "\n\t"
"pop r3" "\n\t"
"pop r2" "\n\t"
::"r" (runningThread->td_sp),
"I" _SFR_IO_ADDR(SPL), "I" _SFR_IO_ADDR(SPH)
);
}
#endif

Ralph

-----Original Message-----
From: en-nut-discussion-bounces at egnite.de
[mailto:en-nut-discussion-bounces at egnite.de]On Behalf Of Lorenzo Midulla
Sent: Thursday, 20 November 2003 10:23
To: en-nut-discussion at egnite.de
Subject: [En-Nut-Discussion] NutOs context switching


Nut/os is a cooperative multitasking so I don't understand why in context
switching ALL registers are saved. In my opinion you have only to save
R28/R29 (Y reg in ICCAVR). The other registers are saved by compiler or you
don't need to save them (volatile regs).

What's your opinion?

Lorenzo Midulla




More information about the En-Nut-Discussion mailing list