timer.c
8.51 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (C) 2015 Rakendra Thapa <rakendrathapa@gmail.com
* 2015 Marc Poulhiès <dkm@kataplop.net>
*
* 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_lm4f120
* @{
*
* @file timer.c
* @brief Implementation of the low-level timer driver for the LM4F120
*
* @author Rakendra Thapa <rakendrathapa@gmail.com>
* Marc Poulhiès <dkm@kataplop.net>
*/
#include <stdint.h>
#include "cpu.h"
#include "periph_conf.h"
#include "periph/timer.h"
#include "mutex.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
/* guard file in case no timers are defined */
#if TIMER_NUMOF
/**
* @brief Struct holding the configuration data
* @{
*/
typedef struct {
timer_cb_t cb; /**< timeout callback */
void *arg; /**< argument to the callback */
unsigned int divisor; /**< software clock divisor */
} timer_conf_t;
static timer_conf_t config[TIMER_NUMOF];
/**@}*/
#include "hw_timer.h"
/* Missing from driverlib */
static inline unsigned long
PRIV_TimerPrescaleSnapshotGet(unsigned long ulbase, unsigned long ultimer) {
return((ultimer == TIMER_A) ? HWREG(ulbase + TIMER_O_TAPS) :
HWREG(ulbase + TIMER_O_TBPS));
}
static inline unsigned long long _scaled_to_ll_value(unsigned int uncorrected, unsigned int divisor)
{
const unsigned long long scaledv = (unsigned long long) uncorrected * divisor;
return scaledv;
}
static inline unsigned int _llvalue_to_scaled_value(unsigned long long corrected, unsigned int divisor)
{
const unsigned long long scaledv = corrected / divisor;
return scaledv;
}
int timer_init(tim_t dev, unsigned long freq, timer_cb_t cb, void *arg)
{
if (dev >= TIMER_NUMOF){
return -1;
}
config[dev].cb = cb;
config[dev].arg = arg;
config[dev].divisor = ROM_SysCtlClockGet() / freq;
unsigned int sysctl_timer;
unsigned int timer_base;
unsigned int timer_side = TIMER_A;
unsigned int timer_cfg = TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC_UP | TIMER_TAMR_TAMIE;
unsigned int timer_max_val;
unsigned int timer_intbit = TIMER_TIMA_TIMEOUT | TIMER_TIMA_MATCH;
switch(dev){
#if TIMER_0_EN
case TIMER_0:
sysctl_timer = SYSCTL_PERIPH_WTIMER0;
timer_base = WTIMER0_BASE;
timer_max_val = TIMER_0_MAX_VALUE;
break;
#endif
#if TIMER_1_EN
case TIMER_1:
sysctl_timer = SYSCTL_PERIPH_WTIMER1;
timer_base = WTIMER1_BASE;
timer_max_val = TIMER_1_MAX_VALUE;
break;
#endif
default:
return -1; /* unreachable */
}
ROM_SysCtlPeripheralEnable(sysctl_timer);
ROM_TimerDisable(timer_base, timer_side);
ROM_TimerConfigure(timer_base, timer_cfg);
unsigned long long lltimer_val_max = _scaled_to_ll_value(timer_max_val, config[dev].divisor);
ROM_TimerPrescaleSet(timer_base, timer_side, lltimer_val_max >> 32);
ROM_TimerLoadSet(timer_base, timer_side, lltimer_val_max & 0xFFFFFFFF);
ROM_TimerIntClear(timer_base, timer_intbit);
ROM_TimerIntEnable(timer_base, timer_intbit);
timer_irq_enable(dev);
timer_start(dev);
return 0;
}
int timer_set(tim_t dev, int channel, unsigned int timeout)
{
unsigned int corrected_now;
int retval;
if (dev >= TIMER_NUMOF){
return -1;
}
corrected_now = timer_read(dev);
retval = timer_set_absolute(dev, channel, corrected_now+timeout);
return retval;
}
int timer_set_absolute(tim_t dev, int channel, unsigned int value)
{
unsigned int timer_base;
unsigned int timer_side = TIMER_A;
unsigned long long scaledv;
if (dev >= TIMER_NUMOF){
return -1;
}
switch(dev){
#if TIMER_0_EN
case TIMER_0:
timer_base = WTIMER0_BASE;
break;
#endif
#if TIMER_1_EN
case TIMER_1:
timer_base = WTIMER1_BASE;
break;
#endif
default:
return -1; /* unreachable */
break;
}
ROM_TimerDisable(timer_base, timer_side);
scaledv = _scaled_to_ll_value(value, config[dev].divisor);
if (scaledv>>32){
ROM_TimerPrescaleMatchSet(timer_base, timer_side, scaledv >> 32);
}
else {
ROM_TimerPrescaleMatchSet(timer_base, timer_side, 0);
}
ROM_TimerMatchSet(timer_base, timer_side, (unsigned long) (scaledv & 0xFFFFFFFF));
ROM_TimerEnable(timer_base, timer_side);
return 1;
}
int timer_clear(tim_t dev, int channel)
{
unsigned int timer_intbit = TIMER_TIMA_TIMEOUT;
unsigned int timer_base;
if (dev >= TIMER_NUMOF){
return -1;
}
switch(dev){
#if TIMER_0_EN
case TIMER_0:
timer_base = WTIMER0_BASE;
break;
#endif
#if TIMER_1_EN
case TIMER_1:
timer_base = WTIMER1_BASE;
break;
#endif
default:
return -1; /* unreachable */
break;
}
ROM_TimerIntClear(timer_base, timer_intbit);
return 1;
}
unsigned int timer_read(tim_t dev)
{
unsigned int timer_base;
unsigned int timer_side = TIMER_A;
unsigned long long high_bits, high_bits_dup;
unsigned long long low_bits;
unsigned long long total;
unsigned int scaled_value;
if (dev >= TIMER_NUMOF){
return -1;
}
switch(dev){
#if TIMER_0_EN
case TIMER_0:
timer_base = WTIMER0_BASE;
break;
#endif
#if TIMER_1_EN
case TIMER_1:
timer_base = WTIMER1_BASE;
break;
#endif
default:
return -1; /* unreachable */
break;
}
/* handle overflow happening between the 2 register reads */
do {
high_bits = ((unsigned long long)PRIV_TimerPrescaleSnapshotGet(timer_base, timer_side)) << 32;
low_bits = (unsigned long long)ROM_TimerValueGet(timer_base, timer_side);
high_bits_dup = ((unsigned long long)PRIV_TimerPrescaleSnapshotGet(timer_base, timer_side)) << 32;
} while (high_bits != high_bits_dup);
total = high_bits + low_bits;
DEBUG("Combined %lx:%lx\n", (unsigned long) (total>>32), (unsigned long) (total & 0xFFFFFFFF));
scaled_value = _llvalue_to_scaled_value(total, config[dev].divisor);
return scaled_value;
}
void timer_start(tim_t dev)
{
unsigned int timer_base;
unsigned int timer_side = TIMER_A;
if (dev >= TIMER_NUMOF){
return ;
}
switch(dev){
#if TIMER_0_EN
case TIMER_0:
timer_base = WTIMER0_BASE;
break;
#endif
#if TIMER_1_EN
case TIMER_1:
timer_base = WTIMER1_BASE;
break;
#endif
default:
return; /* unreachable */
}
ROM_TimerEnable(timer_base, timer_side);
}
void timer_stop(tim_t dev)
{
unsigned int timer_base;
unsigned int timer_side = TIMER_A;
if (dev >= TIMER_NUMOF){
return;
}
switch(dev){
#if TIMER_0_EN
case TIMER_0:
timer_base = WTIMER0_BASE;
break;
#endif
#if TIMER_1_EN
case TIMER_1:
timer_base = WTIMER1_BASE;
break;
#endif
default:
return; /* unreachable */
}
ROM_TimerDisable(timer_base, timer_side);
}
void timer_irq_enable(tim_t dev)
{
unsigned int timer_intbase;
if (dev >= TIMER_NUMOF){
return;
}
switch(dev){
#if TIMER_0_EN
case TIMER_0:
timer_intbase = INT_WTIMER0A;
break;
#endif
#if TIMER_1_EN
case TIMER_1:
timer_intbase = INT_WTIMER1A;
break;
#endif
default:
return; /* unreachable */
}
ROM_IntPrioritySet(timer_intbase, 32);
ROM_IntEnable(timer_intbase);
}
void timer_irq_disable(tim_t dev)
{
unsigned int timer_base;
unsigned int timer_intbit = TIMER_TIMA_TIMEOUT;
unsigned int timer_intbase;
if (dev >= TIMER_NUMOF){
return;
}
switch(dev){
#if TIMER_0_EN
case TIMER_0:
timer_base = WTIMER0_BASE;
timer_intbase = INT_WTIMER0A;
break;
#endif
#if TIMER_1_EN
case TIMER_1:
timer_base = WTIMER1_BASE;
timer_intbase = INT_WTIMER1A;
break;
#endif
default:
return; /* unreachable */
}
ROM_IntEnable(timer_intbase);
ROM_TimerIntDisable(timer_base, timer_intbit);
}
#if TIMER_0_EN
void isr_wtimer0a(void)
{
/* Clears both IT */
ROM_TimerIntClear(WTIMER0_BASE, TIMER_TIMA_TIMEOUT | TIMER_TIMA_MATCH);
config[TIMER_0].cb(config[TIMER_0].arg, 0);
cortexm_isr_end();
}
#endif /* TIMER_0_EN */
#if TIMER_1_EN
void isr_wtimer1a(void)
{
ROM_TimerIntClear(WTIMER1_BASE, TIMER_TIMA_TIMEOUT | TIMER_TIMA_MATCH);
config[TIMER_1].cb(config[TIMER_0].arg, 0);
cortexm_isr_end();
}
#endif /* TIMER_1_EN */
#endif /* TIMER_NUMOF */
/** @} */