timer: Avoid infinite cycle timer on TCNT write
authorJakob Gruber <jakob.gruber@gmail.com>
Thu, 23 Aug 2012 08:57:21 +0000 (10:57 +0200)
committerJakob Gruber <jakob.gruber@gmail.com>
Wed, 29 Aug 2012 12:53:18 +0000 (14:53 +0200)
commit27bab459856ad7e06c5c9801722628e77a03e33c
tree2f1c8acf73cbc6a90b5f04d4b8f797beb60f367d
parentcbc735e0138d2a358541a0ee7b7ddd0e203a06ef
timer: Avoid infinite cycle timer on TCNT write

In some situations, it was possible to enter an infinite cycle timer
loop. In avr_timer_tcnt_write, the tov cycle timer registration is not
protected and can register a timer with p->tov_cycles == 0.

The following atmega1280 program demonstrates this issue:

int main() {

TCCR1A = 0;
TCCR1B = (1<<WGM12) | (1<<ICES1);
OCR1B = OCR1A = 960;

/* Start */
TCNT1 = 0;
TCCR1B |= (1<<CS11);

/* Stop */
TCCR1B &= ~(1<<CS11);
TIFR1 |= (1<<OCF1A) | (1<<OCF1B);

/* Start */
TCNT1 = 0; /**< Registers timer with tov_cycles == 0. */
TCCR1B |= (1<<CS11);

while (1) ;
return 0;
}
simavr/sim/avr_timer.c