PSP/MSP Stack pointer switching implementation

Hi, I am using a NXP S32K118 Cortex M0+ processor 

I have a scheduler function used to call different tasks ( all is time scheduled ) and i need to do the following:

-> Use the main stack pointer (MSP) for this scheduler function

-> Use the process stack pointer (PSP) for the scheduled tasks

I have 3 stacks defined in ram ( main stack, secondary stack 1, secondary stack 2 ). The idea is that the scheduler uses the main stack, and the scheduled tasks use the secondary stack 1 or secondary stack 2, so i need to switch from MSP to PSP when i´m in the scheduer, before calling the corresponding task, and switch back from PSP to MSP once the corresponding task is completed and im back on the scheduler.

I know how to switch these pointers writing to PSP/MSP/CONTROL registers, but the problem I have is i dont know how to do this context switch/ what i need to do in addition of simply changing stack pointers in order to keep all the stacks working properly, so im here just finding for any help/tips if anyone knows how to implement this properly.

Thank you! :)

  • Hi,

    Since you would like to have a time scheduled function that invokes scheduled tasks, it is recommended to use a combination of setting up your SysTick and PendSV handlers to perform context switching operations. 

    Though, there are many possible implementations, here is one of the possible suggestion: 

    - SysTick timer can be used to trigger Systick exception that gets triggered on a regular time interval for time scheduled operations.

    - PendSV interrupt can be used to perform context switching between a scheduler function and scheduled tasks. A scheduler function can be part of your PendSV handler that will use Main stack.

    - Within the thread mode, depending on how you invoke a task, you could configure CONTROL register to use Secondary stack.

    In this way, you could segregate scheduled function to be invoked on a higher priority level via PendSV than your scheduled tasks that execute on thread mode with secondary stack.



  • Check out: The definitive guide to arm cortex-m3 and cortex-m4.

More questions in this forum