Firstly, I've done some research:
- Getting EPC
- On PIC18Fxxx
- Compile time only
- TNKernel ports.h, showing inline asm context switching
I want to get able to get and set the MCU program counter. I'm playing around with some kernel code, and want to implement basic context switching where I store key registers and PC, to be restored later.
I'm using a PIC32MX340F512 device, on a uC32 dev board (ignore the fact is intended for Aurduino).
I can grab the EPC (exception program counter) value, but it only stores the PC value from the last exception. But you can write to it, and call eret
asm instruction to jump the PC to its location. If I can create a fake exception at the position of interest in the code, I could probably grab the value then? Seems hackish.
I've seen examples of people writing to the PC via the PCL and PCH registers, but these don't seem to be defined in my MPLAB workspace (XC compiler).
Any ideas? I've included a gist from TNKernel which does context switching for a PIC32 device, but its in assembly and I don't understand it unfortunately. But it does look like its done in an ISR, which makes sense. When we jump into an ISR, we have to context switch anyway.