+/EE445L
embedded systems
git clone https://git.julia.blue/+/EE445L
5c1089c
master
1#include "timer.h" 2#include "tm4c123gh6pm.h" 3 4static void (* periodic_task0 )(void ); 5static void (* periodic_task1 )(void ); 6static void (* periodic_task2 )(void ); 7 8uint32_t ms_to_cycles (uint16_t ms ) { 9return ms * 80000 ; 10} 11 12void timer0a_handler (void ) { 13TIMER0_ICR_R = 1 ;// ACK 14periodic_task0 (); 15} 16 17void timer1a_handler (void ) { 18TIMER1_ICR_R = 1 ; 19periodic_task1 (); 20} 21 22void timer2a_handler (void ) { 23TIMER2_ICR_R = 1 ; 24periodic_task2 (); 25} 26 27void timer0_start (void (* task )(void ),uint32_t period ,bool repeating ) { 28SYSCTL_RCGCTIMER_R |=1 ;// activate timer 0 29periodic_task0 = task ;// user function (needed here for delay) 30TIMER0_CTL_R = 0 ;// disable timer A 31TIMER0_CFG_R = 0 ;// 32-bit timer mode 32TIMER0_TAMR_R = 1 <<repeating ;// periodic or one-shot 33TIMER0_TAILR_R = ms_to_cycles (period )- 1 ;// reload value 34TIMER0_TAPR_R = 0 ;// no prescale 35TIMER0_ICR_R = 1 ;// clear timeout flag 36TIMER0_IMR_R = 1 ;// enable timeout interrupt 37NVIC_PRI4_R = (NVIC_PRI4_R & 0x00FFFFFF ) |0x40000000 ;// priority 2 38NVIC_EN0_R = 1 <<19 ;// enable interrupt 19 39TIMER0_CTL_R = 1 ;// enable timer A 40} 41 42void timer0_stop (void ) { 43NVIC_DIS0_R = 1 <<19 ; 44} 45 46void timer1_start (void (* task )(void ),uint32_t period ,bool repeating ) { 47SYSCTL_RCGCTIMER_R |=2 ; 48periodic_task1 = task ; 49TIMER1_CTL_R = 0 ; 50TIMER1_CFG_R = 0 ; 51TIMER1_TAMR_R = 1 <<repeating ; 52TIMER1_TAILR_R = ms_to_cycles (period )- 1 ; 53TIMER1_TAPR_R = 0 ; 54TIMER1_ICR_R = 1 ; 55TIMER1_IMR_R = 1 ; 56NVIC_PRI5_R = (NVIC_PRI5_R & 0xFFFF00FF ) |0x00008000 ;// priority 4 57NVIC_EN0_R |=1 <<21 ; 58TIMER1_CTL_R = 1 ; 59} 60 61void timer1_stop (void ) { 62NVIC_DIS0_R = 1 <<21 ; 63} 64 65void timer2_start (void (* task )(void ),uint32_t period ,bool repeating ) { 66SYSCTL_RCGCTIMER_R |=4 ; 67periodic_task2 = task ; 68TIMER2_CTL_R = 0 ; 69TIMER2_CFG_R = 0 ; 70TIMER2_TAMR_R = 1 <<repeating ; 71TIMER2_TAILR_R = ms_to_cycles (period )- 1 ; 72TIMER2_TAPR_R = 0 ; 73TIMER2_ICR_R = 1 ; 74TIMER2_IMR_R = 1 ; 75NVIC_PRI5_R = (NVIC_PRI5_R & 0x00FFFFFF ) |0x80000000 ;// priority 4 76NVIC_EN0_R |=1 <<23 ; 77TIMER2_CTL_R = 1 ; 78} 79 80void timer2_stop (void ) { 81NVIC_DIS0_R = 1 <<23 ; 82}