FreeRTOS capable to run multiple tasks by using tasks switching method. Scheduler is the kernel who doing the task switching operation. The scheduler will changing the task into running state when it is require. But when the yield, block or suspend functions is called, the scheduler will switching other task in ready condition into the running state. Whereby only one task able in running state at a time.

This week I spent the time on doing the context switching function in the FreeRTOS. There is a function called vPortYield which is used during the task is yield. When the current task is yielded, the vPortYield will call portSAVE_context macro to save all the register data into the stack and stack pointer will load into another TCB to do task switching. By review to other ported demo example, I found that the portSAVE_context macro are only reverse of the portRESTORE_context macro. So I used the same method to write my portSAVE_context macro. When I doing multiple task switching, I found that the scheduler able to switch from first task to another task, but it cannot return back into the first task. Through investigate into the simulator stack memory and registers store value. The register cannot retrieve the correct value during returning back to first task.

I found that during context switch back to first task, portRESTORE_context registers miss track to load the data. For example, R1 load R4 data, R2 load R5 data, R3 load R6 data. After that, an adjustment is done on portSAVE_context macro to save the register into more lower stack. Finally, with this adjustment on the portSAVE_context macro, the first task can be loaded back.

By referring to other demos, the portSAVE_context macro are exactly writing in reverse of portRESTORE_context macro. For now, I still cannot figure out why my porting demo save and restore context macro are different with each other.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.