In my S12X code I have implemented an interrupt service routine that I call STANDBY. The point of STANDBY is to "softly" stop any ongoing operation and return to a state resembling the state after running the startup code.
Currently, STANDBY disables bunch of PIT timers and initializes most important variables and then returns to the execution of the code. Usually, my program will successfully return to the for-loop inside the main function and the variables are correctly initialized. But sometimes, probably due to bad luck with timing, the ISR returns inside a function that will modify the variables that were just initialized with STANDBY.
So I am looking for a way to return from my STANDBY ISR to the for-loop inside the main function, without running any of the code that was executing while STANDBY was issued.
Does anyone have any suggestions or examples how to achieve this? I guess one option would be to somehow clear the call stack and then use the goto command to jump into the main function. But I do not have any idea how to safely modify the call stack...
PS. I already have implemented a "REBOOT" command, that causes the MCU to reset by writing an invalid value to the ARMCOP register. But I would like to have also this faster and softer STANDBY command.
EDIT: After discussing about this problem with a colleague, we have an idea how this could be achieved by a couple of assembly instructions:
The plan is to somehow capture the Program Counter (PC) and Stack Pointer (SP) values in the beginning of the for-loop where I want to force the CPU. Ideally this is somehow done in the compiler, or then this will be stored in memory during run-time.
In the end of the STANDBY ISR the SP will be modified to point to the same value as in the beginning of the main function's for-loop. I am considering using the GLDS instruction for this. Then, the PC will be updated to the desired value with JMP instruction.
I guess I will have to also call CLI just before JMP, or the future interrupts will stay disabled? Is there anything else that I should do before the JMP instruction? E.g. something with the IPL?
I will try the above idea in practice and let you know what happens.