RTOS Implementation Yeon YoonMo
Focus RTOS context switch process from the bottom up used example The FreeRTOS Atmel AVR microcontroller port WinAVR development tools
The RTOS Tick time measurement is needed sleeping task : waking time blocked task : maximum waiting time tickmeasure time using a tick count variable
Tick ISR
GCC Signal Attribute interrupts written in C A compare match event on the AVR timer 1 peripheral __attribute__((signal)) modifiedregister that gets modified during the ISR is restored to its original value when the ISR exits a 'return from interrupt' instruction (RETI) to be used
code output
GCC Naked Attribute entire contextPerforming a context switch requires the entire context to be saved explicitly save all registers → some registers being saved twice The 'naked' attribute prevents the compiler generating any function entry or exit code
code output (naked)
save & restore context portSAVE_CONTEXT() portRESTORE_CONTEXT()
FreeRTOS Tick Code void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal, naked ) ); void vPortYieldFromTick( void ) __attribute__ ( ( naked ) ); void SIG_OUTPUT_COMPARE1A( void ) { vPortYieldFromTick(); asm volatile ( "reti" ); } void vPortYieldFromTick( void ) { portSAVE_CONTEXT(); vTaskIncrementTick(); vTaskSwitchContext(); portRESTORE_CONTEXT(); asm volatile ( "ret" ); }
The AVR Context
Saving the Context
Restoring the Context
Detailed Example a context switch on the AVR microcontroller two tasks low priority : task A high priority :task B
Step 1
Step 2
Step 3
Step 4 Incrementing the Tick Count after the TaskA context has been saved vTaskIncrementTick(); TaskBreadyTaskB(higher priority) to become ready to run vTaskSwitchContext() ;
Step 5
Step 6
Step 7