#include "timer.h" #include "tm4c123gh6pm.h" static void (*periodic_task0)(void); static void (*periodic_task1)(void); static void (*periodic_task2)(void); uint32_t ms_to_cycles(uint16_t ms) { return ms * 80000; } void timer0a_handler(void) { TIMER0_ICR_R = 1; // ACK periodic_task0(); } void timer1a_handler(void) { TIMER1_ICR_R = 1; periodic_task1(); } void timer2a_handler(void) { TIMER2_ICR_R = 1; periodic_task2(); } void timer0_start(void (*task)(void), uint32_t period, bool repeating) { SYSCTL_RCGCTIMER_R |= 1; // activate timer 0 periodic_task0 = task; // user function (needed here for delay) TIMER0_CTL_R = 0; // disable timer A TIMER0_CFG_R = 0; // 32-bit timer mode TIMER0_TAMR_R = 1 << repeating; // periodic or one-shot TIMER0_TAILR_R = ms_to_cycles(period) - 1; // reload value TIMER0_TAPR_R = 0; // no prescale TIMER0_ICR_R = 1; // clear timeout flag TIMER0_IMR_R = 1; // enable timeout interrupt NVIC_PRI4_R = (NVIC_PRI4_R & 0x00FFFFFF) | 0x40000000; // priority 2 NVIC_EN0_R = 1 << 19; // enable interrupt 19 TIMER0_CTL_R = 1; // enable timer A } void timer0_stop(void) { NVIC_DIS0_R = 1 << 19; } void timer1_start(void (*task)(void), uint32_t period, bool repeating) { SYSCTL_RCGCTIMER_R |= 2; periodic_task1 = task; TIMER1_CTL_R = 0; TIMER1_CFG_R = 0; TIMER1_TAMR_R = 1 << repeating; TIMER1_TAILR_R = ms_to_cycles(period) - 1; TIMER1_TAPR_R = 0; TIMER1_ICR_R = 1; TIMER1_IMR_R = 1; NVIC_PRI5_R = (NVIC_PRI5_R & 0xFFFF00FF) | 0x00008000; // priority 4 NVIC_EN0_R |= 1 << 21; TIMER1_CTL_R = 1; } void timer1_stop(void) { NVIC_DIS0_R = 1 << 21; } void timer2_start(void (*task)(void), uint32_t period, bool repeating) { SYSCTL_RCGCTIMER_R |= 4; periodic_task2 = task; TIMER2_CTL_R = 0; TIMER2_CFG_R = 0; TIMER2_TAMR_R = 1 << repeating; TIMER2_TAILR_R = ms_to_cycles(period) - 1; TIMER2_TAPR_R = 0; TIMER2_ICR_R = 1; TIMER2_IMR_R = 1; NVIC_PRI5_R = (NVIC_PRI5_R & 0x00FFFFFF) | 0x80000000; // priority 4 NVIC_EN0_R |= 1 << 23; TIMER2_CTL_R = 1; } void timer2_stop(void) { NVIC_DIS0_R = 1 << 23; }