Saving the RTOS Task Context
[RTOS Implementation Building Blocks]

Each real time task has its own stack memory area so the context can be saved by simply pushing processor registers onto the task stack. Saving the AVR context is one place where assembly code is unavoidable.

portSAVE_CONTEXT() is implemented as a macro, the source code for which is given below:

#define portSAVE_CONTEXT() \ asm volatile ( \ "push r0 \n\t" \ (1) "in r0, __SREG__ \n\t" \ (2) "cli \n\t" \ (3) "push r0 \n\t" \ (4) "push r1 \n\t" \ (5) "clr r1 \n\t" \ (6) "push r2 \n\t" \ (7) "push r3 \n\t" \ "push r4 \n\t" \ "push r5 \n\t" \

: : :

"push r30 \n\t" \ "push r31 \n\t" \ "lds r26, pxCurrentTCB \n\t" \ (8) "lds r27, pxCurrentTCB + 1 \n\t" \ (9) "in r0, __SP_L__ \n\t" \ (10) "st x+, r0 \n\t" \ (11) "in r0, __SP_H__ \n\t" \ (12) "st x+, r0 \n\t" \ (13) );

Referring to the source code above:



Next: RTOS Implementation - Restoring The Context