Interrupts

07 Dec 2010

In the MBed compiler, can you use any of the interrupts that are supported by the device?  I'm having a hard time understanding how to turn on a specific interrupt and writing the ISR for that interrupt.   

I'm looking for some sample code how to enable a specific interrupt and then implement the ISR for that interrupt. 

In particular I want to enable the CAN Interrupt and Capture Register (CAN1ICR - 0x4004 400C, CAN2ICR - 0x4004 800C) and have the ISR increment a counter if bit 3 (Data overrun) is set at the time the interrupt occured.

07 Dec 2010

If you look here, you can see the registers, and probably setup an interrupt by poking them around a little.

Lerche

07 Dec 2010

But you'd need to coordinate with interrupt VIC slots and so on that are used by the libraries.

Not sure if that's centrally documented.

07 Dec 2010

Hi Freddie,

To write and enable the ISR for one of the interrupt identifiers listed in LPC17xx.h, you can do something like:

void myroutine() { ... }

NVIC_SetVector(ADC_IRQn, (uint32_t)&myroutine);
NVIC_EnableIRQ(ADC_IRQn);

Note, this just enables it in the interrupt controller, which is not the same as enabling interrupts to be generated by the peripheral (you'll need to do both). But I think this answers your question.

Simon

07 Dec 2010

You may have a bit of a problem here. Are you using the Mbed library CAN?

The reason I ask is it has a method .attach() that allows you to attach an interupt callback. However, it only calls back on receive. A bit of investigation shows that bit0 of the IER register is set when you call .attach() as you might expect. So, in theory, you could just do

    LPC_CAN1->IER |= (1UL << 3);

which would also enable the interrupt you are seeking to trap and count. And here's the problem. According to the manual the CR register bits 0 through 10 are reset when the register is read. So, if the Mbed library reads this register before making the callback to your function you will never see bit3 as set. Conversely, if it doesn't and your handler then reads the CR register to test bit3, your ISR will reset bit0 meaning the Mbed library will not see bit0 set.

So, it's down to if you are using the Mbed library or not. Seems to me if you want to extend the Mbed lib to add extra functionality then you may come stuck and have to do your own implementation.

07 Dec 2010 . Edited: 07 Dec 2010

Andy,

You have a very good point here!!!!!  I was planning to use the Mbed CAN library so I will have this conflict with the CR register.  It's gonna be a race condition to see who reads the CR register first.  This may force me to not use the Mbed CAN library.

 

I wish that the Mbed CAN library could have one of the following:

1) A flag that indicates there has been at least one missed messages since the last time the flag was cleared.  For instance, if the CAN Rx buffer overflows then this flag get set.

2) A running counter of how many times the CAN Rx buffer has overflowed since the last time it was cleared.

 

This would be helpful in a CAN logging utility to let the user know that there may have been missed messages.  This comes into play when you are logging CAN traffic that is at a very high bus load.