Electronic – Define callbacks for interrupts

armcinterruptsstm32

I am working with an STM32 and I am a bit confused about interrupts, specifically the Nested Vectored Interrupts (NVI). As I understand there is a NVI vector (called NVIC) where each interrupt has a priority (sometimes settable), and an address (see page 157 of the ARM reference manual here).

Now I am assuming that for each type of interrupt it is possible to attach a callback function, and I suspect that the attached address to each interrupt is related to the address of the callback.

What exactly is the address attached to a given interrupt? How can I define (in C, say) the callback function for a given interrupt?

Best Answer

The ARMs implement an interrupt table to store the address for each interrupt handler (or callback, basically the same thing). Basically, all of the addresses of the interrupt handlers are stored in program memory at a predefined location. When an interrupt occurs, the processor knows where in the table that interrupt's entry is, grabs it and branches to the address stored there.

How do you populate this table? Usually all this information is contained in a startup file. Typically you just define a constant array of pointers, fill it with the addresses of the callbacks you want to use. Here is a website detailing how to create a startup file for a specific ARM microcontroller. The tricky thing about this is that almost all of the particulars of making the file depend heavily on the linker, compiler and chip you're using, so you'll either have to find an example out there or muddle through making your own.