Ticker, interrupts and re-entrancy

10 Oct 2010

Ticker callbacks are triggered from a timer interrupt, right?  So they could occur at any time, even during a call to a library function.  Which library functions are safe to re-enter?

I've got a ticker that fires 100 times a second, reads a value from an analog input (is that safe to re-enter?), does some processing with some data that's private to the ticker routine, and then occasionally needs to send out some output to the network.  I've a feeling that will take longer than 1/100th of a second.  If my ticker routine runs for longer than 1/100th of a second, will it get called again before it's finished?  Argh!

Probably I should have the ticker pass a signal to the main code and have the main code send output to the network, which to be safe probably requires a buffer queue in between the ticker-routine and the output-sending routine, and that buffer queue will have data items that both the ticker-routine and the output-sending routine will need to access (head and tail of the queue, count of items).  If these were threads I'd use locks, but the ticker-routine can't sleep while it waits for a lock.

Can I temporarily disable the ticker interrupt while the output-sending routine accesses the buffer queue, to ensure the ticker doesn't touch the queue while the output-sender is?

Rob.

10 Oct 2010 . Edited: 10 Oct 2010

Hi Rob

All mbed tickers use the TIMER3 object, so you can create a critical section in your main code to access a buffer being written to by a Ticker handler:

NVIC_DisableIRQ(TIMER3_IRQn);
// critical section
 
NVIC_EnableIRQ(TIMER3_IRQn);

A typical strategy with this approach is to swap between the use of two buffers in the critical section (writing one on the interrupt handler and emptying the other in the main code), so you don't block the timer for long. I think the ticker events get queued up, so if they don't get processed in time, they don't go away but will eventually cause a queue overrun and a crash.

Regards
Daniel

 

10 Oct 2010

Thanks for the TIMER3 info, and the double-buffer tip - I'll give those a try.

Rob.

17 Nov 2011

Hey i've been using multiple high speed tickers have having the program crash randomly, managed to fix it with disable TIMER3_IRQn functions. I thought it was a serial problem with tickers!

in the onEvent()

NVIC_DisableIRQ(TIMER3_IRQn);

/ code

NVIC_EnableIRQ(TIMER3_IRQn);

this didnt work

onEvent() {

disable_irq();

/ code

enable_irq();