+/rtos

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

Vivek Malneediremove tm4c header1cc2297

master
3.3 KiB106 linesraw
1#include "tivaware/timer.h"
2#include "timer.h"
3#include "tivaware/hw_memmap.h"
4#include "tivaware/rom.h"
5
6static void (*tasks[12])(void);
7
8#define TIMERHANDLER(n)                                                        \
9    void timer##n##a_handler(void) {                                           \
10        ROM_TimerIntClear(TIMER##n##_BASE, TIMER_A);                           \
11        (*tasks[n])();                                                         \
12    }
13#define WTIMERHANDLER(n)                                                       \
14    void wtimer##n##a_handler(void) {                                          \
15        ROM_TimerIntClear(WTIMER##n##_BASE, TIMER_A);                          \
16        (*tasks[n + 6])();                                                     \
17    }
18
19TIMERHANDLER(0)
20TIMERHANDLER(1)
21TIMERHANDLER(2)
22TIMERHANDLER(3)
23TIMERHANDLER(4)
24TIMERHANDLER(5)
25WTIMERHANDLER(0)
26WTIMERHANDLER(1)
27WTIMERHANDLER(2)
28WTIMERHANDLER(3)
29WTIMERHANDLER(4)
30WTIMERHANDLER(5)
31
32uint32_t us(uint32_t us) {
33    return us * 80;
34}
35
36uint32_t ms(float ms) {
37    return us(ms * 1000);
38}
39
40uint32_t seconds(float s) {
41    return us(s * 1000000);
42}
43
44uint32_t to_us(uint32_t time) {
45    return time / 80;
46}
47
48float to_ms(uint32_t time) {
49    return time / 80000.f;
50}
51
52uint32_t hz(float hz) {
53    return 80000000 / hz;
54}
55
56float to_hz(uint32_t time) {
57    return time / 80000000.f;
58}
59
60uint32_t get_timer_reload(uint8_t timer_num) {
61    return ROM_TimerLoadGet(timers[timer_num].base, TIMER_A);
62}
63
64uint32_t get_timer_value(uint8_t timer_num) {
65    return ROM_TimerValueGet(timers[timer_num].base, TIMER_A);
66}
67
68void timer_enable(uint8_t timer_num, uint32_t period, void (*task)(void),
69                  uint8_t priority, bool periodic) {
70    TimerConfig config = timers[timer_num];
71    ROM_SysCtlPeripheralEnable(config.sysctl_periph);
72    ROM_TimerConfigure(config.base,
73                       periodic ? TIMER_CFG_PERIODIC : TIMER_CFG_ONE_SHOT);
74    ROM_TimerControlStall(config.base, TIMER_A, true);
75    ROM_TimerLoadSet(config.base, TIMER_A, period);
76    ROM_IntEnable(config.interrupt);
77    ROM_IntPrioritySet(config.interrupt,
78                       priority << 5); // priority is high 3 bits
79    tasks[timer_num] = task;
80    ROM_TimerIntEnable(config.base, TIMER_TIMA_TIMEOUT);
81    ROM_TimerEnable(config.base, TIMER_BOTH);
82}
83
84void busy_wait(uint8_t timer_num, uint32_t duration) {
85    TimerConfig config = timers[timer_num];
86    ROM_SysCtlPeripheralEnable(config.sysctl_periph);
87    ROM_TimerConfigure(config.base, TIMER_CFG_ONE_SHOT);
88    ROM_TimerLoadSet(config.base, TIMER_A, duration);
89    ROM_TimerEnable(config.base, TIMER_BOTH);
90    while (ROM_TimerValueGet(config.base, TIMER_A) != duration) {}
91}
92
93const TimerConfig timers[12] = {
94    {SYSCTL_PERIPH_TIMER0, INT_TIMER0A, TIMER0_BASE},
95    {SYSCTL_PERIPH_TIMER1, INT_TIMER1A, TIMER1_BASE},
96    {SYSCTL_PERIPH_TIMER2, INT_TIMER2A, TIMER2_BASE},
97    {SYSCTL_PERIPH_TIMER3, INT_TIMER3A, TIMER3_BASE},
98    {SYSCTL_PERIPH_TIMER4, INT_TIMER4A, TIMER4_BASE},
99    {SYSCTL_PERIPH_TIMER5, INT_TIMER5A, TIMER5_BASE},
100    {SYSCTL_PERIPH_WTIMER0, INT_WTIMER0A, WTIMER0_BASE},
101    {SYSCTL_PERIPH_WTIMER1, INT_WTIMER1A, WTIMER1_BASE},
102    {SYSCTL_PERIPH_WTIMER2, INT_WTIMER2A, WTIMER2_BASE},
103    {SYSCTL_PERIPH_WTIMER3, INT_WTIMER3A, WTIMER3_BASE},
104    {SYSCTL_PERIPH_WTIMER4, INT_WTIMER4A, WTIMER4_BASE},
105    {SYSCTL_PERIPH_WTIMER5, INT_WTIMER5A, WTIMER5_BASE},
106};