I am trying to implement a scheduler for Arduino Mega 2560. I think I lack understanding of how to set the PC register to another instruction. Here is my simple approach so far:
void dummy(){
digitalWrite(LED_BUILTIN, HIGH);
}
in a naked ISR method:
SP = (uint16_t)(&dummy)
__asm volatile(
"reti \n\t"
)
However it is not working. How should the PC register be set to a certain function or to where it was in a previous process for a scheduler approach?
1 Answer 1
This:
SP = (uint16_t)(&dummy)
is changing the stack pointer. Not what you want. You want to instead
push
the destination address to the stack. However, rather than
simulating a function return, it should be simpler to perform an
indirect jump:
asm volatile("ijmp" :: "z"(dummy));
Don't forget that your naked ISR has to save all the call-used
registers, as well as SREG
, r0
and r1
, in its custom prologue. And
restore them in its epilogue. Check the AVR calling
conventions.
-
Thanks for you fast answer, I am saving all registsers r0, ..,r31 and SREG to the stack, however I am not sure how I can save the PC into the stack and reload it.Mustafa Otbah– Mustafa Otbah2019年09月01日 02:34:50 +00:00Commented Sep 1, 2019 at 2:34
-
@MustafaOtbah: The PC was already saved by the interrupt. You normally restore it with
reti
. Maybe you should expand your question and explain in more detail how you intend to manage saving and restoring task context, what you have done so far, and what is not working.Edgar Bonet– Edgar Bonet2019年09月01日 09:23:09 +00:00Commented Sep 1, 2019 at 9:23
Explore related questions
See similar questions with these tags.