Skip to content

Commit

Permalink
mbed-classic: BUGFIX for timer when using wait_ms from interrupt context
Browse files Browse the repository at this point in the history
Previously if a user used wait[_ms,_us] in interrupt context the device would
hang indefinitely. This was due to incrementing overflowCount from
interrupt context only.

This meant that if a user used wait[_ms,_us] in an ISR with
the same or greater interrupt priority, it would result in an infinite
loop as the overflowCount variable would never be incremented, and
wait[_ms,_us] would never return.

This patch simply applies a better solution for the race condition
mentioned in the previous commit. It instead disables the timer1
interrupt and increments the overflowCount variable, preventing
the race condition whilst supporting wait[_ms,_us] in interrupt
context.
  • Loading branch information
jamesadevine committed May 13, 2016
1 parent 0918819 commit 1fb8ab4
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,19 @@ void tmr1_stop(void)
*/
static inline uint64_t tmr1_getCounter64(void)
{
int o = 0;

NRF_TIMER1->TASKS_CAPTURE[2] = 1;

if (NRF_TIMER1->EVENTS_COMPARE[3])
o++;
NVIC_DisableIRQ(TIMER1_IRQn);

if (NRF_TIMER1->EVENTS_COMPARE[3])
{
NRF_TIMER1->EVENTS_COMPARE[3] = 0;
overflowCount++;
}

NVIC_EnableIRQ(TIMER1_IRQn);

return (((uint64_t)(overflowCount+o)) << 16) | (NRF_TIMER1->CC[2] & MAX_TMR1_COUNTER_VAL);
return (((uint64_t)(overflowCount)) << 16) | (NRF_TIMER1->CC[2] & MAX_TMR1_COUNTER_VAL);
}

/**
Expand Down

0 comments on commit 1fb8ab4

Please sign in to comment.