Homepage  

Saving the Context
[RTOS Implementation Building Blocks]

Each real time task has it's 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


Copyright (C) 2003 - 2008 Richard Barry
Any and all data, files, source code, html content and documentation included in the FreeRTOS distribution or available on this site are the exclusive property of Richard Barry. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Richard Barry.