Interrupts

Status
Not open for further replies.

dpharris

Well-known member
I am trying to use interrupts in a CAN library. How does one specify interrupt routines for these chips? There are several CAN interrupt vectors, including for rx, tx, and error reporting. I assume one would write seoerate routines for each of these, can one combine them into one toutine, if so, how does one do that?

Thanks,
David
 
Thanks, I am aware of that library ... it is very good, but does not use interrupts ... hence my question.
David
 
To use an interrupt, you need to create a function which will be called. There's 2 ways. The simplest is to use the pre-defined names in kinetis.h.

Code:
extern void can0_message_isr(void);
extern void can0_bus_off_isr(void);
extern void can0_error_isr(void);
extern void can0_tx_warn_isr(void);
extern void can0_rx_warn_isr(void);
extern void can0_wakeup_isr(void);

The interrupt table is created with these names, so all you need to do is create a function with this name and it'll automatically be associated with the interrupt.

The other way involves using attachInterruptVector(IRQ, function). This overrides whatever's already in the table. Sometime this can be useful, but usually it's simpler to just use the pre-defined names.

The IRQ number is a constant, also defined in kinetis.h. While you could just use the number, it's best to use these names, so your code will automatically work when/if other boards are made with this same CAN hardware, but the interrupts assigned to different numbers.

Code:
        IRQ_CAN0_MESSAGE
        IRQ_CAN0_BUS_OFF
        IRQ_CAN0_ERROR
        IRQ_CAN0_TX_WARN
        IRQ_CAN0_RX_WARN
        IRQ_CAN0_WAKEUP

After you've created the function, you need to enable the interrupt. For that, use NVIC_ENABLE_IRQ(IRQ), where "IRQ" is one of those numbers for the interrupt you want enabled.

You will also need to enable generating the interrupt within the CAN controller hardware.

Another optional step involves the interrupt priority. Normally you should just leave this alone. But if your interrupt is very urgent and you want it to be able to interrupt other interrupts, you can make it a higher priority. Or if your interrupt spends quite a lot of time doing work, you might wish to give it a lower priority, so it doesn't block others from running. Priorities are assigned with NVIC_SET_PRIORITY(IRQ, priority), where the priority is a number from 0 to 255. Lower numbers mean higher priority. The default on Teensy is 128.
 
To use an interrupt, ... the simplest is to use the pre-defined names in kinetis.h.
...
The other way involves using attachInterruptVector(IRQ, function). 6
...
Paul --
Thank you for the complete answer, and this should cover my questions, I think. The CAN peripheral is very complicated. I am trying to adapt the library to be compatible with SocketCAN with FIFOs if wanted. The chip has a 6 message optional hardware FIFO for received frames (messages), but no hardware transmit FIFO.

David
 
Status
Not open for further replies.
Back
Top