timer.c
3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (C) 2016 Leon George
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_cc26x0
* @{
*
* @file
* @brief Low-level timer driver implementation for the CC26x0
*
* @author Leon M. George <leon@georgemail.eu>
*
* @}
*/
#include <stdlib.h>
#include <stdio.h>
#include "cpu.h"
#include "periph_conf.h"
#include "periph/timer.h"
/**
* @brief Allocate memory for the interrupt context
*/
static timer_isr_ctx_t ctx[TIMER_NUMOF];
/**
* @brief Get the GPT register base for a timer
*
* @param[in] tim index of the timer
*
* @return base address
*/
static inline gpt_reg_t *dev(tim_t tim)
{
return timer_config[tim].dev;
}
int timer_init(tim_t tim, unsigned long freq, timer_cb_t cb, void *arg)
{
/* make sure given timer is valid */
if (tim >= TIMER_NUMOF) {
return -1;
}
/* enable the times clock */
PRCM->GPTCLKGR |= (1 << timer_config[tim].num);
PRCM->CLKLOADCTL = CLKLOADCTL_LOAD;
while (!(PRCM->CLKLOADCTL & CLKLOADCTL_LOADDONE)) {}
/* disable (and reset) timer */
dev(tim)->CTL = 0;
/* save context */
ctx[tim].cb = cb;
ctx[tim].arg = arg;
/* configure timer to 16-bit, periodic, up-counting */
dev(tim)->CFG = GPT_CFG_16T;
dev(tim)->TAMR = (GPT_TXMR_TXMR_PERIODIC | GPT_TXMR_TXCDIR_UP | GPT_TXMR_TXMIE);
/* set the timer speed */
dev(tim)->TAPR = (RCOSC48M_FREQ / freq) - 1;
/* enable global timer interrupt and start the timer */
timer_irq_enable(tim);
dev(tim)->CTL = GPT_CTL_TAEN;
return 0;
}
int timer_set(tim_t tim, int channel, unsigned int timeout)
{
return timer_set_absolute(tim, channel, timer_read(tim) + timeout);
}
int timer_set_absolute(tim_t tim, int channel, unsigned int value)
{
if (channel != 0) {
return -1;
}
dev(tim)->TAMATCHR = value;
dev(tim)->IMR |= GPT_IMR_TAMIM;
return 0;
}
int timer_clear(tim_t tim, int channel)
{
if (channel != 0) {
return -1;
}
dev(tim)->IMR &= ~(GPT_IMR_TAMIM);
return 0;
}
unsigned int timer_read(tim_t tim)
{
return dev(tim)->TAV & 0xFFFF;
}
void timer_stop(tim_t tim)
{
dev(tim)->CTL = 0;
}
void timer_start(tim_t tim)
{
dev(tim)->CTL = GPT_CTL_TAEN;
}
void timer_irq_enable(tim_t tim)
{
NVIC_EnableIRQ(GPTIMER_0A_IRQN + (2 * timer_config[tim].num));
}
void timer_irq_disable(tim_t tim)
{
NVIC_DisableIRQ(GPTIMER_0A_IRQN + (2 * timer_config[tim].num));
}
/**
* @brief handle interrupt for a timer
*
* @param[in] tim index of the timer
*/
static inline void isr_handler(tim_t tim)
{
uint32_t mis = dev(tim)->MIS;
dev(tim)->ICLR = mis;
if (mis & GPT_IMR_TAMIM) {
dev(tim)->IMR &= ~GPT_IMR_TAMIM;
ctx[tim].cb(ctx[tim].arg, 0);
}
cortexm_isr_end();
}
#ifdef TIMER_0_ISR
void TIMER_0_ISR(void)
{
isr_handler(0);
}
#endif
#ifdef TIMER_1_ISR
void TIMER_1_ISR(void)
{
isr_handler(1);
}
#endif
#ifdef TIMER_2_ISR
void TIMER_2_ISR(void)
{
isr_handler(2);
}
#endif
#ifdef TIMER_3_ISR
void TIMER_3_ISR(void)
{
isr_handler(3);
}
#endif