-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimer.c
404 lines (317 loc) · 11.3 KB
/
timer.c
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/** @file timer.c
@author
@date 12 February 2008
@brief Timer counter routines for AT91SAM7 processors
*/
/* DOES NOT SUPPORT WAVEFORM MODE and does not include burst mode,
clock invert, or external clock configuration. */
#include "timer.h"
#include "bits.h"
#define TIMER_CHANNEL_0 0
#define TIMER_CHANNEL_1 1
#define TIMER_CHANNEL_2 2
#define EX_TRG_NONE 0
#define EX_TRG_RISING 1
#define EX_TRG_FALLING 2
#define EX_TRG_EACH 3
#define EX_TRG_TIOB 0
#define EX_TRG_TIOA 1
#define RC_COMP_NONE 0
#define RC_COMP_TRG 1
#define LDRA_TIOA_NONE 0
#define LDRA_TIOA_RISING 1
#define LDRA_TIOA_FALLING 2
#define LDRA_TIOA_EACH 3
#define LDRB_TIOA_NONE 0
#define LDRB_TIOA_RISING 1
#define LDRB_TIOA_FALLING 2
#define LDRB_TIOA_EACH 3
#define LDRB_CLK_NONE 0
#define LDRB_CLK_STOP 1
#define LDRB_CLK_DIS 2
#define LDRB_CLK_STOP_DIS 3
#define TIMER_TC_CV 0
#define TIMER_TC_RA 1
#define TIMER_TC_RB 2
#define TIMER_TC_RC 3
/* Prescale values:
2, 8, 32, 128, 1024.
2^1, 2^3, 2^5, 2^7, 2^10. */
static const uint8_t log_prescales[] = {1, 3, 5, 7, 10};
static inline AT91S_TC *
timer_base (uint8_t channel)
{
switch (channel)
{
case TIMER_CHANNEL_0:
return TC0;
case TIMER_CHANNEL_1:
return TC1;
case TIMER_CHANNEL_2:
return TC2;
default:
return 0;
}
}
uint8_t
timer_init (uint8_t channel)
{
/* Each timer has its own peripheral clock. */
return 1;
}
/* Initialises the timer for capture mode. */
uint8_t
timer_enable (uint8_t channel)
{
switch (channel)
{
case TIMER_CHANNEL_0:
/* Enable peripheral pins. */
*AT91C_PIOA_PDR = AT91C_PA0_TIOA0 | AT91C_PA1_TIOB0;
/* Disable pullups. */
*AT91C_PIOA_PPUDR = AT91C_PA0_TIOA0 | AT91C_PA1_TIOB0;
/* Enable peripheral clock. */
PMC->PMC_PCER = BIT (AT91C_ID_TC0);
break;
case TIMER_CHANNEL_1:
/* Enable peripheral pins. */
*AT91C_PIOA_PDR = AT91C_PA15_TIOA1 | AT91C_PA16_TIOB1;
/* Disable pullups. */
*AT91C_PIOA_PPUDR = AT91C_PA15_TIOA1 | AT91C_PA16_TIOB1;
/* Enable peripheral clock. */
PMC->PMC_PCER = BIT (AT91C_ID_TC1);
break;
case TIMER_CHANNEL_2:
/* Enable peripheral pins. */
*AT91C_PIOA_PDR = AT91C_PA26_TIOA2 | AT91C_PA27_TIOB2;
/* Disable pullups. */
*AT91C_PIOA_PPUDR = AT91C_PA26_TIOA2 | AT91C_PA27_TIOB2;
/* Enable peripheral clock. */
PMC->PMC_PCER = BIT (AT91C_ID_TC2);
break;
default:
return 0;
}
return 1;
}
/* Disable the selected timer. */
uint8_t
timer_disable (uint8_t channel)
{
switch (channel)
{
case TIMER_CHANNEL_0:
/* Disable peripheral pins. */
*AT91C_PIOA_PER = AT91C_PA0_TIOA0 | AT91C_PA1_TIOB0;
/* Disable peripheral clock. */
PMC->PMC_PCDR = BIT (AT91C_ID_TC0);
break;
case TIMER_CHANNEL_1:
/* Disable peripheral pins. */
*AT91C_PIOA_PER = AT91C_PA15_TIOA1 | AT91C_PA16_TIOB1;
/* Disable peripheral clock. */
PMC->PMC_PCDR = BIT (AT91C_ID_TC1);
break;
case TIMER_CHANNEL_2:
/* Disable peripheral pins. */
*AT91C_PIOA_PER = AT91C_PA26_TIOA2 | AT91C_PA27_TIOB2;
/* Disable peripheral clock. */
PMC->PMC_PCDR = BIT (AT91C_ID_TC2);
break;
default:
return 0;
}
return 1;
}
/* Selects and applies a suitable prescale for the clock, based on the
given period. The period is in (master) clocks. */
uint8_t
timer_prescaler_set (uint8_t channel, unsigned int period)
{
uint8_t i;
uint8_t log_prescale = 0;
AT91S_TC *pTC;
/* Choose smallest prescaler so that the period fits within 16 bits. */
for (i = 1; i < ARRAY_SIZE (log_prescales) && period > 65535; i++)
{
period >>= (log_prescales[i] - log_prescale);
log_prescale = log_prescales[i];
}
/* Select the channel peripheral base address. */
pTC = timer_base (channel);
if (!pTC)
return 0;
/* Set clock prescale. */
switch (log_prescale)
{
case 1:
BITS_INSERT (pTC->TC_CMR, AT91C_TC_CLKS_TIMER_DIV1_CLOCK, 0, 2);
break;
case 3:
BITS_INSERT (pTC->TC_CMR, AT91C_TC_CLKS_TIMER_DIV2_CLOCK, 0, 2);
break;
case 5:
BITS_INSERT (pTC->TC_CMR, AT91C_TC_CLKS_TIMER_DIV3_CLOCK, 0, 2);
break;
case 7:
BITS_INSERT (pTC->TC_CMR, AT91C_TC_CLKS_TIMER_DIV4_CLOCK, 0, 2);
break;
case 10:
BITS_INSERT (pTC->TC_CMR, AT91C_TC_CLKS_TIMER_DIV5_CLOCK, 0, 2);
break;
default:
return 0;
}
return 1;
}
/* Set the timer to run in capture mode. */
uint8_t
timer_capture_mode_set (uint8_t channel)
{
/* Select the channel peripheral base address. */
AT91S_TC *pTC;
pTC = timer_base (channel);
if (!pTC)
return 0;
/* Enable capture mode. */
BITS_INSERT (pTC->TC_CMR, 0, 15, 15);
return 1;
}
/* Timer channel external trigger configuration (in capture mode only) */
/* */
/* --------OPTIONS-------- */
/* ex_trg_edge: EX_TRG_NONE - No external trigger */
/* EX_TRG_RISING - Trigger on signal rising edge */
/* EX_TRG_FALLING - Trigger on signal falling edge */
/* EX_TRG_EACH - Trigger on both rising and falling edges */
/* ex_trg: EX_TRG_TIOB - Set external trigger to TIOB */
/* EX_TRG_TIOA - Set external trigger to TIOA */
/* rc_comp_trg: RC_COMP_NONE - RC compare has no effect */
/* RC_COMP_NONE - Trigger on RC compare */
uint8_t
timer_capture_trigger_config (uint8_t channel, uint8_t ex_trg_edge,
uint8_t ex_trg, uint8_t rc_comp_trg)
{
/* Select the channel peripheral base address. */
AT91S_TC *pTC;
pTC = timer_base (channel);
if (!pTC)
return 0;
/* Ensure capture mode is in use. */
if (BITS_EXTRACT (pTC->TC_CMR, 15, 15))
return 0;
/* Insert CMR register bits. */
BITS_INSERT (pTC->TC_CMR, ex_trg_edge, 8, 9);
BITS_INSERT (pTC->TC_CMR, ex_trg, 10, 10);
BITS_INSERT (pTC->TC_CMR, rc_comp_trg, 14, 14);
return 1;
}
/* Timer channel register loading configuration (in capture mode only) */
/* */
/* --------OPTIONS-------- */
/* ldra: LDRA_TIOA_NONE - never load RA */
/* LDRA_TIOA_RISING - load RA on TIOA signal rising edge */
/* LDRA_TIOA_FALLING - load RA on TIOA signal falling edge */
/* LDRA_TIOA_EACH - load RA on TIOA rising and falling edges */
/* ldrb: LDRB_TIOA_NONE - never load RB */
/* LDRB_TIOA_RISING - load RB on TIOA signal rising edge */
/* LDRB_TIOA_FALLING - load RB on TIOA signal falling edge */
/* LDRB_TIOA_EACH - load RB on TIOA rising and falling edges */
/* ldrb_clk: LDRB_CLK_STOP - stop the clock when RB is loaded */
/* LDRB_CLK_DIS - disable the clock when RB is loaded */
/* LDRB_CLK_STOP_DIS - stop and disable the clock when RB is loaded */
/* LDRB_CLK_NONE - clock continues as normal on RB loading */
/* RA is loaded only if it has not been loaded since the last trigger
* or if RB has been loaded since */
/* the last loading of RA. */
/* RB is loaded only if RA has been loaded since the last trigger or
* the last loading of RB. */
/* Loading RA or RB before the read of the last value loaded sets the
* Overrun Error Flag (LOVRS) */
/* in TC_SR (Status Register). In this case, the old value is overwritten. */
uint8_t
timer_capture_register_load_config (uint8_t channel, uint8_t ldra,
uint8_t ldrb, uint8_t ldrb_clk)
{
AT91S_TC *pTC;
pTC = timer_base (channel);
if (!pTC)
return 0;
/* Ensure capture mode is in use. */
if (BITS_EXTRACT (pTC->TC_CMR, 15, 15))
return 0;
/* Insert CMR register bits. */
BITS_INSERT (pTC->TC_CMR, ldra, 16, 17);
BITS_INSERT (pTC->TC_CMR, ldrb, 18, 19);
BITS_INSERT (pTC->TC_CMR, ldrb_clk, 6, 7);
return 1;
}
/* Enable the timer clock. */
uint8_t
timer_clock_enable (uint8_t channel)
{
AT91S_TC *pTC;
pTC = timer_base (channel);
if (!pTC)
return 0;
pTC->TC_CCR = AT91C_TC_CLKEN;
return 1;
}
/* Timer channel register reading */
/* */
/* --------OPTIONS-------- */
/* timer_register: TIMER_TC_CV - Current counter value */
/* TIMER_TC_RA - Current RA value */
/* TIMER_TC_RB - Current RB value */
/* TIMER_TC_RC - Current RC value */
uint16_t
timer_read (uint8_t channel, uint8_t timer_reg)
{
AT91S_TC *pTC;
pTC = timer_base (channel);
if (!pTC)
return 0;
switch (timer_reg)
{
case TIMER_TC_CV:
return (pTC->TC_CV);
case TIMER_TC_RA:
/* Check an RA loading has occurred. */
if (BITS_EXTRACT (pTC->TC_SR, 5, 5))
return (pTC->TC_RA);
else
return 0;
case TIMER_TC_RB:
/* Check an RB loading has occurred. */
if (BITS_EXTRACT (pTC->TC_SR, 6, 6))
return (pTC->TC_RA);
else
return 0;
case TIMER_TC_RC:
return (pTC->TC_RC);
default:
return 0;
}
}
/* Performs a software trigger. */
uint8_t
timer_reset (uint8_t channel)
{
AT91S_TC *pTC;
pTC = timer_base (channel);
if (!pTC)
return 0;
pTC->TC_CCR = AT91C_TC_SWTRG;
return 1;
}
/* Disable the timer. */
uint8_t
timer_clock_disable (uint8_t channel)
{
AT91S_TC *pTC;
pTC = timer_base (channel);
if (!pTC)
return 0;
pTC->TC_CCR = AT91C_TC_CLKDIS;
return 1;
}