Context switch in FreeRTOS

MajinFoo
Posts: 9
Joined: Sun Jan 21, 2018 9:06 pm

Context switch in FreeRTOS

Postby MajinFoo » Mon Jan 22, 2018 11:39 am

I am thinking about implementing my own preemptive scheduler for ESP32. I'm sure I can only program a context switch in assembler. Since I don't want to deal with assembler for ESP32, but I had the idea to reuse some functions from FreeRTOS. I looked at the libraries and found these two functions in xtensa_rtos. h:

void XT_RTOS_INT_ENTER (void)
void XT_RTOS_INT_EXIT (void)

Is it correct that I can call these functions in an interrupt to cause a context switch? XT_RTOS_INT_ENTER saves the context of the current task interrupted by the interrupt? XT_RTOS_INT_EXIT restores the last saved context?

The comments state that these functions can only be executed for a Call0 instruction. What's the meaning of this?

There is another file called xtensa_context.S with the two functions:

text_context_restore
text_context_save

What is the meaning of this S-file?

Thank you in advance for your help.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Context switch in FreeRTOS

Postby ESP_Angus » Mon Jan 22, 2018 11:43 pm

Probalby the best place to start is _frxt_int_enter / _frxt_int_exit in portasm.S which calls _xt_context_save / _xt_context_restore in xtensa_context.S.
MajinFoo wrote: The comments state that these functions can only be executed for a Call0 instruction. What's the meaning of this?
Most Xtensa CPUs support different ABIs. C code compiled with the ESP32 toolchain makes Windowed ABI calls (the register window is rotated as part of the call, to minimize overhead of pushing/popping registers on the stack.) call0 is the instruction for a non-windowed ABI function call (as used on ESP8266). Some "internal" RTOS scheduler functions use this because you don't want to be rotating the register window during the call (in call0 only a0 is clobbered, as it's set to the return address). You can only do this via assembler, where you get to choose which call instruction to use.

Reading the files mentioned above should shed some light on the issue, hopefully. :)

Who is online

Users browsing this forum: Bing [Bot] and 80 guests