From: Nathael Pajani Date: Thu, 13 Aug 2015 23:01:25 +0000 (+0200) Subject: Some timers fixes. X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=edf0f79b2ef2068894badfc1f467c260e38115ad;p=soft%2Flpc122x%2Fcore Some timers fixes. --- diff --git a/drivers/timers.c b/drivers/timers.c index bf4529a..183956d 100644 --- a/drivers/timers.c +++ b/drivers/timers.c @@ -39,7 +39,7 @@ struct timer_device struct lpc_timer* regs; uint32_t power_bit; uint32_t irq; - void (*callback)(uint8_t); /* Possible RX callback */ + void (*callback)(uint32_t); /* Possible RX callback */ }; static struct timer_device timer_devices[NUM_TIMERS] = { { LPC_TMR16B0, LPC_SYS_ABH_CLK_CTRL_CT16B0, TIMER0_IRQ }, @@ -53,11 +53,10 @@ static struct timer_device timer_devices[NUM_TIMERS] = { /* Handlers */ void TIMER_Handler(struct timer_device* timer) { - struct lpc_timer* regs = timer->regs; - uint32_t intr_flags = regs->int_reg; /* Backup the flags */ + uint32_t intr_flags = timer->regs->int_reg; /* Backup the flags */ /* Clear the interrupt */ - regs->int_reg = intr_flags; + timer->regs->int_reg = intr_flags; /* And call the user routine if one has been registered */ if (timer->callback != NULL) { timer->callback(intr_flags); @@ -107,8 +106,9 @@ void timer_stop(uint32_t timer_num) { if (timer_num >= NUM_TIMERS) return; - /* Remove timer enable flag */ - timer_devices[timer_num].regs->timer_ctrl |= LPC_TIMER_COUNTER_RESET; + /* Remove timer enable flag and request reset */ + timer_devices[timer_num].regs->timer_ctrl = LPC_TIMER_COUNTER_RESET; + /* Remove reset flag */ timer_devices[timer_num].regs->timer_ctrl = 0; } /* Resets the timer and lets it count again imediately */ @@ -116,9 +116,10 @@ void timer_restart(uint32_t timer_num) { if (timer_num >= NUM_TIMERS) return; - /* Set and remove timer reset flag */ - timer_devices[timer_num].regs->timer_ctrl |= LPC_TIMER_COUNTER_RESET; - timer_devices[timer_num].regs->timer_ctrl &= ~LPC_TIMER_COUNTER_RESET; + /* Set timer reset flag */ + timer_devices[timer_num].regs->timer_ctrl = LPC_TIMER_COUNTER_RESET; + /* Remove reset flag and start counter */ + timer_devices[timer_num].regs->timer_ctrl = LPC_TIMER_COUNTER_ENABLE; } uint32_t timer_get_capture_val(uint32_t timer_num, uint32_t channel) @@ -140,7 +141,7 @@ void timer_set_match(uint32_t timer_num, uint32_t channel, uint32_t val) { if (timer_num >= NUM_TIMERS) return; - if (channel > 2) + if (channel > 3) return; timer_devices[timer_num].regs->match_reg[channel] = val; @@ -225,7 +226,7 @@ int timer_setup(uint32_t timer_num, struct timer_config* conf) * the prescaler value. * Set clkrate to 0 to disable the prescaler. */ -void timer_on(uint32_t timer_num, uint32_t clkrate, void (*callback)(uint8_t)) +void timer_on(uint32_t timer_num, uint32_t clkrate, void (*callback)(uint32_t)) { struct timer_device* timer = NULL; uint32_t prescale; /* The clock divider for the counter */ diff --git a/include/core/lpc_regs_12xx.h b/include/core/lpc_regs_12xx.h index e993de7..5393bba 100644 --- a/include/core/lpc_regs_12xx.h +++ b/include/core/lpc_regs_12xx.h @@ -700,6 +700,7 @@ struct lpc_timer #define LPC_TMR16B1 ((struct lpc_timer *) LPC_TIMER1_BASE) #define LPC_TMR32B0 ((struct lpc_timer *) LPC_TIMER2_BASE) #define LPC_TMR32B1 ((struct lpc_timer *) LPC_TIMER3_BASE) +#define LPC_TIMER_REGS(x) ((struct lpc_timer *) (LPC_TIMER0_BASE + ((x) * 0x4000))) #define LPC_TIMER_COUNTER_ENABLE (1 << 0) /* CEN */ #define LPC_TIMER_COUNTER_RESET (1 << 1) /* CRST */ diff --git a/include/drivers/timers.h b/include/drivers/timers.h index 51acf48..8881398 100644 --- a/include/drivers/timers.h +++ b/include/drivers/timers.h @@ -111,7 +111,7 @@ int timer_setup(uint32_t timer_num, struct timer_config* conf); * config field in timer_config struct upon timer setup) * The interrupt flags are passed to the interrupt routine as argument. */ -void timer_on(uint32_t timer_num, uint32_t clkrate, void (*callback)(uint8_t)); +void timer_on(uint32_t timer_num, uint32_t clkrate, void (*callback)(uint32_t)); /* Removes the main clock from the selected timer block */ void timer_off(uint32_t timer_num);