Repository navigation
Expand file tree
/
Copy pathTimer.cpp
More file actions
356 lines (300 loc) · 19.2 KB
/
Copy pathTimer.cpp
File metadata and controls
356 lines (300 loc) · 19.2 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
/******************************************************************************************************************************************************
* COPYRIGHT
* ---------------------------------------------------------------------------------------------------------------------------------------------------
* \verbatim
* Copyright (c) Andreas Burnickl All rights reserved.
*
* \endverbatim
* ---------------------------------------------------------------------------------------------------------------------------------------------------
* FILE DESCRIPTION
* -------------------------------------------------------------------------------------------------------------------------------------------------*/
/** \file Timer.cpp
* \brief 16-bit Timer/Counter TC4/TC5 of the XMEGA E5 as periodic interrupt and PWM source
*
* \details The member functions are instantiated here, once per timer enabled in Timer.h,
* next to the overflow ISR of that timer.
*
******************************************************************************************************************************************************/
#define _TIMER_SOURCE_
/******************************************************************************************************************************************************
* I N C L U D E S
******************************************************************************************************************************************************/
#include "Timer.h"
#include <avr/interrupt.h>
#include <util/atomic.h>
/******************************************************************************************************************************************************
* L O C A L C O N S T A N T M A C R O S
******************************************************************************************************************************************************/
/* All Timers of XMEGA are 16 bit */
#define TIMER_NUMBER_OF_BITS 16u
#define TIMER_RESOLUTION (1uL << TIMER_NUMBER_OF_BITS)
#define TIMER_MAX_PRESCALER 1024u
#define TIMER_PWM_DUTY_BITS 10u
/******************************************************************************************************************************************************
* L O C A L F U N C T I O N M A C R O S
******************************************************************************************************************************************************/
/******************************************************************************************************************************************************
* L O C A L D A T A T Y P E S A N D S T R U C T U R E S
******************************************************************************************************************************************************/
/******************************************************************************************************************************************************
* P U B L I C F U N C T I O N S
******************************************************************************************************************************************************/
/******************************************************************************************************************************************************
init()
******************************************************************************************************************************************************/
template <typename TimerTraits>
StdReturnType Timer<TimerTraits>::init(uint32_t Microseconds, TimerIsrCallbackF_void sTimerOverflowCallback)
{
StdReturnType returnValue{E_NOT_OK};
if(STATE_NONE == State) {
returnValue = E_OK;
State = STATE_INIT;
/* the xmegaForArduino core leaves TC4/TC5 in byte mode with a running clock for analogWrite(),
so stop it and start from the reset values rather than only changing the waveform mode */
setClockSelect(TIMER_REG_CS_NO_CLOCK);
getTimer().CTRLGSET = TC45_CMD_RESET_gc;
/* set mode 7: Dual-slope PWM, overflow interrupt at BOTTOM */
writeBitGroup(getTimer().CTRLB, TC4_WGMODE_gm, TC45_WGMODE_DSBOTTOM_gc);
if(setPeriod(Microseconds) == E_NOT_OK) returnValue = E_NOT_OK;
if(sTimerOverflowCallback != nullptr) if(attachInterrupt(sTimerOverflowCallback) == E_NOT_OK) returnValue = E_NOT_OK;
State = STATE_READY;
}
return returnValue;
} /* init */
/******************************************************************************************************************************************************
setPeriod()
******************************************************************************************************************************************************/
/*! \brief set period of the timer overflow interrupt
* \details this functions sets the period of the timer overflow interrupt therefore
* prescaler and timer top value will be calculated
* \param[in] Microseconds period of the timer overflow interrupt
* \return E_OK
* E_NOT_OK
*****************************************************************************************************************************************************/
template <typename TimerTraits>
StdReturnType Timer<TimerTraits>::setPeriod(uint32_t Microseconds)
{
StdReturnType returnValue{E_NOT_OK};
/* was request out of bounds? */
if(Microseconds < ((TIMER_RESOLUTION / (F_CPU / 1000000uL)) * TIMER_MAX_PRESCALER * 2u)) {
returnValue = E_OK;
/* calculate timer cycles to reach timer period, counter runs backwards after TOP, interrupt is at BOTTOM so divide microseconds by 2 */
uint32_t timerCycles = (F_CPU / 2000000uL) * Microseconds;
/* calculate timer prescaler */
if(timerCycles < TIMER_RESOLUTION) ClockSelectBitGroup = TIMER_REG_CS_NO_PRESCALER;
else if((timerCycles >>= 1) < TIMER_RESOLUTION) ClockSelectBitGroup = TIMER_REG_CS_PRESCALE_2;
else if((timerCycles >>= 1) < TIMER_RESOLUTION) ClockSelectBitGroup = TIMER_REG_CS_PRESCALE_4;
else if((timerCycles >>= 1) < TIMER_RESOLUTION) ClockSelectBitGroup = TIMER_REG_CS_PRESCALE_8;
else if((timerCycles >>= 3) < TIMER_RESOLUTION) ClockSelectBitGroup = TIMER_REG_CS_PRESCALE_64;
else if((timerCycles >>= 2) < TIMER_RESOLUTION) ClockSelectBitGroup = TIMER_REG_CS_PRESCALE_256;
else if((timerCycles >>= 2) < TIMER_RESOLUTION) ClockSelectBitGroup = TIMER_REG_CS_PRESCALE_1024;
else {
/* request was out of bounds, set to maximum */
timerCycles = TIMER_RESOLUTION - 1u;
ClockSelectBitGroup = TIMER_REG_CS_PRESCALE_1024;
returnValue = E_NOT_OK;
}
/* Set TOP value of timer; a 16-bit write goes through the shared TEMP register, so an ISR
touching another 16-bit register of this timer must not get in between */
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { getTimer().PER = timerCycles; }
if(STATE_RUNNING == State) { setClockSelect(ClockSelectBitGroup); }
}
return returnValue;
} /* setPeriod */
/******************************************************************************************************************************************************
start()
******************************************************************************************************************************************************/
/*! \brief start the timer from zero
* \return E_OK
* E_NOT_OK timer was not initialized
*****************************************************************************************************************************************************/
template <typename TimerTraits>
StdReturnType Timer<TimerTraits>::start()
{
StdReturnType returnValue{E_NOT_OK};
if(STATE_READY == State || STATE_RUNNING == State || STATE_STOPPED == State) {
returnValue = E_OK;
/* RESTART clears the counter and the direction, so the first period is a whole one */
getTimer().CTRLGSET = TC45_CMD_RESTART_gc;
setClockSelect(ClockSelectBitGroup);
State = STATE_RUNNING;
}
return returnValue;
} /* start */
/******************************************************************************************************************************************************
stop()
******************************************************************************************************************************************************/
template <typename TimerTraits>
void Timer<TimerTraits>::stop()
{
setClockSelect(TIMER_REG_CS_NO_CLOCK);
if(STATE_RUNNING == State) { State = STATE_STOPPED; }
} /* stop */
/******************************************************************************************************************************************************
resume()
******************************************************************************************************************************************************/
/*! \brief continue a stopped timer where it stopped
* \return E_OK
* E_NOT_OK timer was not stopped
*****************************************************************************************************************************************************/
template <typename TimerTraits>
StdReturnType Timer<TimerTraits>::resume()
{
StdReturnType returnValue{E_NOT_OK};
if(STATE_STOPPED == State) {
returnValue = E_OK;
setClockSelect(ClockSelectBitGroup);
State = STATE_RUNNING;
}
return returnValue;
} /* resume */
/******************************************************************************************************************************************************
attachInterrupt()
******************************************************************************************************************************************************/
template <typename TimerTraits>
StdReturnType Timer<TimerTraits>::attachInterrupt(TimerIsrCallbackF_void sTimerOverflowCallback)
{
StdReturnType returnValue{E_NOT_OK};
if(sTimerOverflowCallback != nullptr) {
returnValue = E_OK;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { TimerOverflowCallback = sTimerOverflowCallback; }
/* low level, so the core's millis() on TCD5 (high level) keeps interrupting the callback */
writeBitGroup(getTimer().INTCTRLA, TC4_OVFINTLVL_gm, TC45_OVFINTLVL_LO_gc);
PMIC.CTRL |= PMIC_LOLVLEN_bm;
}
return returnValue;
} /* attachInterrupt */
/******************************************************************************************************************************************************
detachInterrupt()
******************************************************************************************************************************************************/
template <typename TimerTraits>
void Timer<TimerTraits>::detachInterrupt()
{
writeBitGroup(getTimer().INTCTRLA, TC4_OVFINTLVL_gm, TC45_OVFINTLVL_OFF_gc);
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { TimerOverflowCallback = nullptr; }
} /* detachInterrupt */
/******************************************************************************************************************************************************
read()
******************************************************************************************************************************************************/
/*! \brief read current timer value in microseconds
* \details this function returns the time since the last overflow interrupt in microseconds
*
* \param[out] Microseconds current timer value
* \return E_OK
* E_NOT_OK
* \pre Timer has to be in RUNNING or STOPPED STATE
*****************************************************************************************************************************************************/
template <typename TimerTraits>
StdReturnType Timer<TimerTraits>::read(uint32_t& Microseconds)
{
StdReturnType returnValue{E_NOT_OK};
if(STATE_RUNNING == State || STATE_STOPPED == State) {
uint32_t counterValue{0u};
uint32_t topValue{0u};
TimerCountDirection direction{TIMER_COUNT_DIRECTION_UP};
returnValue = E_OK;
/* counter and direction have to come from the same instant, or a turn at TOP in between
reads as a whole period too much */
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
counterValue = getTimer().CNT;
topValue = getTimer().PER;
direction = getCountingDirection();
}
/* if counter counting down, add top value to current value */
if(direction == TIMER_COUNT_DIRECTION_DOWN) { counterValue = (topValue - counterValue) + topValue; }
/* transform counter value to microseconds in an efficient way */
Microseconds = ((counterValue * 1000uL) / (F_CPU / 1000uL)) << toPrescalerShift(ClockSelectBitGroup);
}
return returnValue;
} /* read */
/******************************************************************************************************************************************************
handleOverflowInterrupt()
******************************************************************************************************************************************************/
/*! \brief called from the overflow ISR of this timer
* \details Unlike TC0/TC1 of the other XMEGAs, TC4/TC5 do not clear their interrupt flag when
* the vector executes; without the explicit clear the ISR is entered again at once.
*****************************************************************************************************************************************************/
template <typename TimerTraits>
void Timer<TimerTraits>::handleOverflowInterrupt()
{
getTimer().INTFLAGS = TC4_OVFIF_bm;
if(TimerOverflowCallback != nullptr) { TimerOverflowCallback(); }
} /* handleOverflowInterrupt */
/******************************************************************************************************************************************************
* P R I V A T E F U N C T I O N S
******************************************************************************************************************************************************/
/******************************************************************************************************************************************************
getCountingDirection
******************************************************************************************************************************************************/
template <typename TimerTraits>
typename Timer<TimerTraits>::TimerCountDirection Timer<TimerTraits>::getCountingDirection()
{
if(getTimer().CTRLGSET & TC4_DIR_bm) { return TIMER_COUNT_DIRECTION_DOWN; }
else { return TIMER_COUNT_DIRECTION_UP; }
} /* getCountingDirection */
/******************************************************************************************************************************************************
setClockSelect
******************************************************************************************************************************************************/
template <typename TimerTraits>
void Timer<TimerTraits>::setClockSelect(TimerClockSelectType ClockSelect)
{
writeBitGroup(getTimer().CTRLA, TC4_CLKSEL_gm, ClockSelect);
} /* setClockSelect */
/******************************************************************************************************************************************************
enablePwmChannel
******************************************************************************************************************************************************/
template <typename TimerTraits>
StdReturnType Timer<TimerTraits>::enablePwmChannel(TimerChannelType Channel, uint16_t Duty)
{
StdReturnType returnValue = setPwmDutyChannel(Channel, Duty);
if(E_OK == returnValue) {
TimerTraits::getPort().DIRSET = toPinMask(Channel);
writeBitGroup(getTimer().CTRLE, toChannelModeMask(Channel), toChannelModeCompare(Channel));
}
return returnValue;
} /* enablePwmChannel */
/******************************************************************************************************************************************************
disablePwmChannel
******************************************************************************************************************************************************/
template <typename TimerTraits>
StdReturnType Timer<TimerTraits>::disablePwmChannel(TimerChannelType Channel)
{
writeBitGroup(getTimer().CTRLE, toChannelModeMask(Channel), TC45_CCAMODE_DISABLE_gc);
/* the pin stays an output, it now shows its PORT.OUT value instead of the waveform */
return E_OK;
} /* disablePwmChannel */
/******************************************************************************************************************************************************
setPwmDutyChannel
******************************************************************************************************************************************************/
/*! \brief set duty of one compare channel, 0 .. PwmDutyMax
* \details Written to the buffer register, so the new compare value takes effect at the next
* BOTTOM and a running PWM never sees half a period of each.
*****************************************************************************************************************************************************/
template <typename TimerTraits>
StdReturnType Timer<TimerTraits>::setPwmDutyChannel(TimerChannelType Channel, uint16_t Duty)
{
StdReturnType returnValue{E_NOT_OK};
if(Duty <= PwmDutyMax) {
returnValue = E_OK;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
const uint32_t compareValue = (static_cast<uint32_t>(getTimer().PER) * Duty) >> TIMER_PWM_DUTY_BITS;
/* CCABUF..CCDBUF are consecutive in both TC4_t and TC5_t; the channel was range checked at compile time */
(&getTimer().CCABUF)[Channel] = static_cast<uint16_t>(compareValue);
}
}
return returnValue;
} /* setPwmDutyChannel */
/******************************************************************************************************************************************************
* I N S T A N C E S A N D I N T E R R U P T S
******************************************************************************************************************************************************/
#if (TIMER_USE_TCC4 == STD_ON)
template class Timer<TimerC4>;
ISR(TCC4_OVF_vect) { Timer<TimerC4>::getInstance().handleOverflowInterrupt(); }
#endif
#if (TIMER_USE_TCC5 == STD_ON)
template class Timer<TimerC5>;
ISR(TCC5_OVF_vect) { Timer<TimerC5>::getInstance().handleOverflowInterrupt(); }
#endif
/******************************************************************************************************************************************************
* E N D O F F I L E
******************************************************************************************************************************************************/