From ebe62299d3a9c513ca5b28ad0af013d8b7743ea2 Mon Sep 17 00:00:00 2001 From: Michel Pollet Date: Thu, 24 Dec 2009 16:54:25 +0000 Subject: [PATCH] timer: No longer craksh when reading TCNT with timer off Thanks to Jon Escombe Signed-off-by: Michel Pollet --- simavr/sim/avr_timer.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/simavr/sim/avr_timer.c b/simavr/sim/avr_timer.c index 10bf174..22e9f45 100644 --- a/simavr/sim/avr_timer.c +++ b/simavr/sim/avr_timer.c @@ -103,14 +103,16 @@ static uint8_t avr_timer_tcnt_read(struct avr_t * avr, avr_io_addr_t addr, void avr_timer_t * p = (avr_timer_t *)param; // made to trigger potential watchpoints - uint64_t when = avr->cycle - p->tov_base; + if (p->tov_cycles) { + uint64_t when = avr->cycle - p->tov_base; - uint16_t tcnt = (when * p->tov_top) / p->tov_cycles; -// printf("%s-%c when = %d tcnt = %d/%d\n", __FUNCTION__, p->name, (uint32_t)when, tcnt, p->tov_top); + uint16_t tcnt = (when * p->tov_top) / p->tov_cycles; + // printf("%s-%c when = %d tcnt = %d/%d\n", __FUNCTION__, p->name, (uint32_t)when, tcnt, p->tov_top); - avr->data[p->r_tcnt] = tcnt; - if (p->r_tcnth) - avr->data[p->r_tcnth] = tcnt >> 8; + avr->data[p->r_tcnt] = tcnt; + if (p->r_tcnth) + avr->data[p->r_tcnth] = tcnt >> 8; + } return avr_core_watch_read(avr, addr); } -- 2.39.5