+/EE445L

embedded systems

git clone https://git.julia.blue/+/EE445L

Joseph RyanAdd clock rendering5c1089c

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