Menu
![](/uploads/1/2/6/5/126563069/922455794.jpg)
![Software Software](/uploads/1/2/6/5/126563069/954365435.jpg)
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.
Looking Under The Hood: Software. In the last chapter, we learned about the PIC32 physical and virtual memory maps. Of these, the physical memory map is.
You can change the value of the EPC register only while the cpu is in kernel mode. The easiest way to do that is by implementing an ISR in assembly, as the interrupts are being handled in kernel mode. You can make a simple ISR function and get the assembly code for handling the interrupt from the disassembly window in MPLABX and right before the eret function is executed, you can set a new value into EPC register. When eret returns to the address thats inside the EPC register, all cpu registers will be restored at their previous values before entering the ISR, so you should backup $ra and $epc registers before returning from the ISR and do the rest by using another function, whose address is inside the EPC register.
![](/uploads/1/2/6/5/126563069/922455794.jpg)