registering a new interrupt handler at runtime

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

registering a new interrupt handler at runtime

Post 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
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: registering a new interrupt handler at runtime

Post 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
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: registering a new interrupt handler at runtime

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: registering a new interrupt handler at runtime

Post 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
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: registering a new interrupt handler at runtime

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: registering a new interrupt handler at runtime

Post by Giovanni »

Hi,

Looks correct, the map file should confirm it.

Giovanni
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: registering a new interrupt handler at runtime

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: registering a new interrupt handler at runtime

Post by Giovanni »

Replace ram_vectors with the absolute address of your vectors table.

Giovanni
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: registering a new interrupt handler at runtime

Post by hazelnusse »

How do I know what the address is at compile time?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: registering a new interrupt handler at runtime

Post 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
Post Reply