Page 1 of 2

registering a new interrupt handler at runtime

Posted: Tue Dec 04, 2012 4:10 am
by hazelnusse
I would like to change (at runtime) which interrupt handler gets called depending on the state of my system. I know I could check the status of a global flag from within an interrupt handler and then call the appropriate function based upon the state of the flag, but I was wondering how to do it a bit more elegantly than that. I know it must involve writing the address of the handler function to the appropriate vector table entry, but I am unsure as to how to do this. Any recommendations?

Thanks,
Luke

Re: registering a new interrupt handler at runtime

Posted: Tue Dec 04, 2012 4:38 am
by hazelnusse
I think I've determined that in order to do what I am thinking, I need to relocate the vector table from flash to SRAM. Does anybody have example code for how to do this? I would assume this needs to happen pretty early on, maybe before ChibiOS starts?

Thanks,
Luke

Re: registering a new interrupt handler at runtime

Posted: Tue Dec 04, 2012 8:32 am
by hazelnusse
I noticed that SCB->VTOR gets set in chSysInit. If I relocate the vector table before this, then my change gets overwritten. Is it safe for me to overwrite relocate the vector table after chSysInit()? Should I halt the system before I do so? Or are there any other special considerations I should consider?

Thanks,
Luke

Re: registering a new interrupt handler at runtime

Posted: Tue Dec 04, 2012 8:57 am
by Giovanni
Hi,

You have to setup the VTOR macro value in chconf.h so the kernel will setup it for you.

I suggest that you keep the flash vector table and copy it in RAM before calling chSysInit(), this way the default vectors are already setup.

I don't have an example right now but you need to make a custom .ld file, and create a new section just after stacks and before data segment.

Anyway, you could use the same solution the DMA minidriver uses, it just call the ISR through a function pointer that can be redirected, see stm32_dma.c.

Giovanni

Re: registering a new interrupt handler at runtime

Posted: Tue Dec 04, 2012 11:24 am
by hazelnusse
Ok, if I understand you correctly, I should do something like:

Code: Select all

#define CORTEX_VTOR_INIT ram_vectors

in chconf.h. In the linker script, I should do something like:

Code: Select all

ram_startup : ALIGN(16) SUBALIGN(16)
{
    KEEP(*(ram_vectors))
} > ram

and then wherever I define my vector table, I should have

Code: Select all

__attribute__ ((section("ram_vectors")))
vectors_t _vectors_relocated = ...

And then before calling chSysInit(), I should perform the copy of the regular vector table to ram by doing:

Code: Select all

extern vectors_t _vectors;
_vectors_relocated = _vectors;

Does this seem correct?

Thanks,
Luke

Re: registering a new interrupt handler at runtime

Posted: Tue Dec 04, 2012 11:35 am
by Giovanni
Hi,

Looks correct, the map file should confirm it.

Giovanni

Re: registering a new interrupt handler at runtime

Posted: Wed Dec 05, 2012 8:17 pm
by hazelnusse
Giovanni,
I implemented the changes above but am getting this error:

Code: Select all

Compiling ./ChibiOS-RT/os/ports/GCC/ARMCMx/chcore_v7m.c
./ChibiOS-RT/os/ports/GCC/ARMCMx/chcore_v7m.c: In function '_port_init':
./ChibiOS-RT/os/ports/GCC/ARMCMx/chcore_v7m.c:114:14: error: 'ram_vectors' undeclared (first use in this function)
./ChibiOS-RT/os/ports/GCC/ARMCMx/chcore_v7m.c:114:14: note: each undeclared identifier is reported only once for each function it appears in
make: *** [build/obj/chcore_v7m.o] Error 1

If I comment out the following line in my chconf.h it goes away:

Code: Select all

#define CORTEX_VTOR_INIT ram_vectors

But then when chSysInit() runs, SCB_VTOR gets set back to the default CORTEX_VTOR_INIT (0x00000000) rather than whatever the address of my vector table ends up being. How can I fix this?

Thanks,
Luke

Re: registering a new interrupt handler at runtime

Posted: Wed Dec 05, 2012 8:28 pm
by Giovanni
Replace ram_vectors with the absolute address of your vectors table.

Giovanni

Re: registering a new interrupt handler at runtime

Posted: Wed Dec 05, 2012 8:29 pm
by hazelnusse
How do I know what the address is at compile time?

Re: registering a new interrupt handler at runtime

Posted: Wed Dec 05, 2012 8:36 pm
by Giovanni
Because you add it in your .ld file at a specific position, if you place it after stacks it will be at 0x2000000 + stacks_size.

You could also use the symbol _vectors_relocated as address but you don't have the type to declare an extern.

Giovanni