Second core Wake up sequence in SMP


I am porting armR8 for SMP freertos. Everything is working fine for the primary core. So my questions are regarding the secondary core.

In the initialization of the secondary core, I initialize its stack and memory, etc., and then send it into sleep mode using wfi instruction. Then after a while, the scheduler from the primary core wakes it up to schedule some tasks and sends the control to irq, which calls portSAVE_CONTEXT, vTaskSwitchContextConst, and portRESTORE_CONTEXT.

I know the abovementioned sequence is wrong as the secondary core never got to run vPortRestoreTaskContext. I also observed other SMP ports, and it seems they are running vTaskStartScheduler(); on all cores. Does this mean all cores are running complete main(); ?

My questions are:

  1. When should the secondary core be put to sleep, and when should it be woken up?
  2. What should the secondary core run first when it is woken up?

Thanks in advance

Hello @Zaid,

  1. The secondary core should be started at xPortStartScheduler(). FreeRTOS does not stop cores unless tickless idle is configured and active.
  2. The secondary core should also run the scheduler. See the RP2040 port for an example: FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/port.c at 30afc1a2c05157fcac1a46dc773b0125bd680b82 · FreeRTOS/FreeRTOS-Kernel · GitHub

This port example uses an API function on the RP2040 to start and run a function on core 1. This function sets up the ISR routine from core 0. On the timer tick, each core will sequentially run the scheduler and then trigger the next core’s interrupt. If a core calls a kernel function which reschedules, it simply runs scheduler immediately.

Combined with my previous understanding from this, the secondary core should be sent to sleep or an empty infinite loop after basic initialization.
Then it should resume processing at xPortStartScheduler(), where it should call vPortRestoreTaskContext() or an equivalent function. This function will set the idle task in the currentTCB and it will be the first task any core will execute.

Is my understanding correct?

Yes, that is correct.