[En-Nut-Discussion] GCC Context switching

Ralph Mason ralph.mason at telogis.com
Fri May 2 06:58:56 CEST 2003


I have been using this slimed down context switch for sometime now with no
ill effects. It saves some ram, some flash and is a little faster.

Ralph

/*!
 * \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)
        );
}
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.461 / Virus Database: 260 - Release Date: 10/03/2003




More information about the En-Nut-Discussion mailing list