Hi everyone!
I’m working on a Cortex M7 microcontroller and, for educational/research purposes, I need to observe every interrupt on an oscilloscope. My approach is to replace each default handler with a wrapper function that raises a GPIO line before calling the “real” handler, and lowers it afterward. This works like a charm for all interrupts - except PendSV.
Whenever I try to wrap PendSV, I see weird behavior and the system doesn’t act as expected. I realize PendSV is special because FreeRTOS (and the Cortex architecture in general) use it for context switching. Before giving up on wrapping it, I wanted to ask: has anyone successfully done this or found a clever workaround? I’d love to keep the same approach for all interrupts, but PendSV is resisting my efforts!
Any advice or insights would be super appreciated. Thanks!
what exactly is the “weird behavior” you observe? Note that by its very definition, you can not “wrap” a context switching isr as it does not “return” to its previous execution point (that is what context switching is all about).
You will have to write this wrapper in assembly to ensure that compiler generated prologue does not clobber any register other than the hardware saved ones. You’ll also have to make sure that you only use hardware saved registers (r0-r3, r12) before branching to real PendSV handler. And you’ll have to use branch instruction and NOT branch and link to ensure LR is not clobbered.
Because PendSV handler calls vTaskSwitchContext, a much easier approach would be to use traceENTER_vTaskSwitchContext and traceEXIT_vTaskSwitchContext macros for your tracing purpose.
This would be the “in” end of the handler, ie the requirements for the code that pulls the GPIO UP. The “out” end (ie the code the pulls the GPIO back down) would need to go into the code fragment that restores a task context (assuming that there is always a 1:1 relationship between PendSV invocations and task rescheduling, which may or may not hold).
I agree with @aggarg that the preexisting hooks would be more appropriate places for what you [the OP] intend to accomplish.