본문 바로가기

Master/Embedded

MSP와 PSP

You are correct in a way, in the cortex m (which your stm32 is, though I can't say which variant unless you specify a part) there is one active stack pointer r13, this can however be either the MSP or PSP.

The reason for two is to enable the user to easily implement a multi tasking 'operating system'.

The idea is that the PSP or process stack pointer is used by the individual tasks, and the kernel uses the MSP.

When an exception happens, a stack frame gets pushed to the currently active stack pointer, and then switches to use the MSP for the exception handler. In a multi tasking system, if the scheduler caused the exception, it is at this point that you change where the PSP is pointing to be the stack pointer for the next task, and return from the exception. The destacking of the new stack pointer then returns execution to the next task.

If you wish to see an example of code using both, a scheduler I wrote for the Cortex M4f is available on BitBucket, it's not documented, but it is fairly straight forward. A svc instruction is used to start the scheduler, and then pendsv exceptions handle the task switching, the Tick function should be called from a timer to ensure task switching.

 

 

 

 

Main Stack Pointer(MSP) vs Process Stack Pointer(PSP)

I was reading the STM32 Programming manual and somewhere on "page 18 " i see these two kind of stack pointers. I always think that there is only ONE stack and therefore only ONE stack pointer to that

electronics.stackexchange.com

 

'Master > Embedded' 카테고리의 다른 글

UART 통신 이론, stop, start bit  (0) 2021.04.15
시리얼 통신 기초 (Serial Communication)  (0) 2021.04.15
ISR(Interrupt Service Routine)  (0) 2021.04.01
Interrupt (인터럽트), Exception (예외)  (0) 2021.04.01
HAL 라이브러리  (0) 2021.03.26