# Trying to understand context switching at low level

**URL:** <https://forums.freertos.org/t/trying-to-understand-context-switching-at-low-level/21798>\
**Category:** Kernel\
**Tags:** debug\
**Created:** [November 7, 2024, 7:21am UTC](https://forums.freertos.org/t/trying-to-understand-context-switching-at-low-level/21798 "2024-11-07T07:21:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsmith786x](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jsmith786x](https://forums.freertos.org/u/jsmith786x)\
**Post date:** [November 7, 2024, 7:21am UTC](https://forums.freertos.org/t/trying-to-understand-context-switching-at-low-level/21798/1 "2024-11-07T07:21:54Z")

</div>

Hi,

I’m working with a RiscV CPU with FreeRTOS (esp32c3). I’m trying to understand what is happening in context switching.

Code: esp idf code on github  
components/freertos/FreeRTOS-Kernel/portable/riscv/port.c

I found the followings  
_Call hierarchy for ‘create the task’_  
xTaskCreatePinnedToCore  
prvInitialiseNewTask  
pxPortInitialiseStack  
uxInitialiseStackFrame (vPortTaskWrapper): this is the main\_task. Im trying to  
figure out how the main\_task code will be called

_Call hierarchy for ‘scheduler call’_  
vTaskStartScheduler  
xPortStartScheduler  
vPortYield  
esp\_crosscore\_int\_send\_yield: this will send the crosscore IT

ISR will be : crosscore IT  
uxInitialiseStackFrame adds a new stack frame to the stack top

But who will POP and how will the code executed? ISR has own handling routine.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 7, 2024, 11:03am UTC](https://forums.freertos.org/t/trying-to-understand-context-switching-at-low-level/21798/2 "2024-11-07T11:03:04Z")

</div>

> [@jsmith786x](#):
>
> But who will POP and how will the code executed? ISR has own handling routine.

In our port, xPortStartScheduler calls [xPortStartFirstTask](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/RISC-V/port.c#L193) which [pops this context](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/RISC-V/portASM.S#L232).

Your question seems to about Espressif’s port and I am not familiar with that. Reading your description, I’d assume that the ISR handler pops this context before returning. I’d suggest to reach out to Espressif for a definitive answer.
