-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTimerTwo.cpp
More file actions
428 lines (375 loc) · 21 KB
/
Copy pathTimerTwo.cpp
File metadata and controls
428 lines (375 loc) · 21 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/******************************************************************************************************************************************************
* COPYRIGHT
* ---------------------------------------------------------------------------------------------------------------------------------------------------
* \verbatim
* Copyright (c) Andreas Burnickl All rights reserved.
*
* \endverbatim
* ---------------------------------------------------------------------------------------------------------------------------------------------------
* FILE DESCRIPTION
* -------------------------------------------------------------------------------------------------------------------------------------------------*/
/** \file TimerTwo.c
* \brief Main file of TimerTwo library
*
* \details Arduino library to use Timer 2
*
*
*****************************************************************************************************************************************************/
#define _TIMERTWO_SOURCE_
/******************************************************************************************************************************************************
* INCLUDES
*****************************************************************************************************************************************************/
#include "TimerTwo.h"
/******************************************************************************************************************************************************
* GLOBAL DATA
*****************************************************************************************************************************************************/
TimerTwo& Timer2 = TimerTwo::getInstance(); // pre-instantiate TimerTwo
/******************************************************************************************************************************************************
* C O N S T R U C T O R S
*****************************************************************************************************************************************************/
/******************************************************************************************************************************************************
CONSTRUCTOR OF TimerTwo
******************************************************************************************************************************************************/
/*! \brief TimerTwo constructor
* \details Instantiation of the TimerTwo library
*
* \return -
*****************************************************************************************************************************************************/
TimerTwo::TimerTwo()
{
State = STATE_INIT;
TimerIsrOverflowCallback = nullptr;
ClockSelectBitGroup = REG_CS_NO_CLOCK;
} /* TimerTwo */
/******************************************************************************************************************************************************
DESTRUCTOR OF TimerTwo
******************************************************************************************************************************************************/
TimerTwo::~TimerTwo()
{
} /* ~TimerTwo */
/******************************************************************************************************************************************************
COPY CONSTRUCTOR OF TimerTwo
******************************************************************************************************************************************************/
TimerTwo& TimerTwo::getInstance()
{
static TimerTwo SingletonInstance;
return SingletonInstance;
} /* getInstance */
/******************************************************************************************************************************************************
* P U B L I C F U N C T I O N S
*****************************************************************************************************************************************************/
/******************************************************************************************************************************************************
init()
******************************************************************************************************************************************************/
/*! \brief initialization of the Timer2 hardware
* \details this functions initializes the Timer2 hardware
*
* \param[in] Microseconds period of the timer overflow interrupt
* \param[in] sTimerOverflowCallback Callback function which should be called when timer overflow interrupt occurs
* \return E_OK
* E_NOT_OK - TimerTwo is already initialized or given period is out of bounds
* \pre Timer has to be in NONE state
*****************************************************************************************************************************************************/
StdReturnType TimerTwo::init(TimeType Microseconds, TimerIsrCallbackF_void sTimerOverflowCallback)
{
StdReturnType ReturnValue = E_NOT_OK;
if(STATE_INIT == State) {
ReturnValue = E_OK;
/* clear control register */
TCCR2A = 0u;
TCCR2B = 0u;
/* set mode 5: phase correct PWM */
writeBit(TCCR2A, WGM20, 1u);
writeBit(TCCR2A, WGM21, 0u);
writeBit(TCCR2B, WGM22, 1u);
if(setPeriod(Microseconds) == E_NOT_OK) { ReturnValue = E_NOT_OK; }
if(sTimerOverflowCallback != nullptr) { attachInterrupt(sTimerOverflowCallback); }
State = STATE_IDLE;
}
return ReturnValue;
} /* init */
/******************************************************************************************************************************************************
setPeriod()
******************************************************************************************************************************************************/
/*! \brief set period of Timer2 overflow interrupt
* \details this functions sets the period of the Timer2 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 - Given period is out of bound
*****************************************************************************************************************************************************/
StdReturnType TimerTwo::setPeriod(TimeType Microseconds)
{
StdReturnType ReturnValue{E_NOT_OK};
if(Microseconds <= getPeriodMax()) {
/* OCR2A is TOP in phase correct PWM mode */
OCR2A = getTimerCycles(Microseconds);
if(STATE_RUNNING == State) {
/* reset clock select register, and start the clock */
writeBitGroup(TCCR2B, TIMERTWO_REG_CS_GM, TIMERTWO_REG_CS_GP, ClockSelectBitGroup);
}
return E_OK;
}
return ReturnValue;
} /* setPeriod */
/******************************************************************************************************************************************************
enablePwm()
******************************************************************************************************************************************************/
/*! \brief enable Pwm on given Pin
* \details this function enables Pwm on given Pin with given duty cycle
*
* \param[in] PwmPin pin where pwm should be enabled
* \param[in] DutyCycle duty cycle of pwm
* \return E_OK
* E_NOT_OK - TimerTwo is not initialized or wrong PwmPin was given
* \pre Timer has to be in READY, RUNNING or STOPPED state
*****************************************************************************************************************************************************/
StdReturnType TimerTwo::enablePwm(PwmPinType PwmPin, byte DutyCycle)
{
StdReturnType ReturnValue{E_NOT_OK};
if((STATE_IDLE == State) || (STATE_RUNNING == State) || (STATE_STOPPED == State))
{
if(PWM_PIN_3 == PwmPin) {
ReturnValue = E_OK;
pinMode(PWM_PIN_3, OUTPUT);
/* activate compare output mode in timer control register */
writeBit(TCCR2A, COM2B1, 1u);
}
if(setPwmDuty(PwmPin, DutyCycle) == E_NOT_OK) { ReturnValue = E_NOT_OK; }
}
return ReturnValue;
} /* enablePwm */
/******************************************************************************************************************************************************
disablePwm()
******************************************************************************************************************************************************/
/*! \brief disable Pwm on given Pin
* \details
* \param[in] PwmPin pin where pwm should be disabled
* \return E_OK
* E_NOT_OK - Wrong PwmPin was given
*****************************************************************************************************************************************************/
StdReturnType TimerTwo::disablePwm(PwmPinType PwmPin)
{
StdReturnType returnValue{E_NOT_OK};
if(PWM_PIN_3 == PwmPin) {
returnValue = E_OK;
/* deactivate compare output mode in timer control register */
writeBit(TCCR2A, COM2B1, 0u);
}
return returnValue;
} /* disablePwm */
/******************************************************************************************************************************************************
setPwmDuty()
******************************************************************************************************************************************************/
/*! \brief set pwm duty cycle on given pin
* \details
*
* \param[in] PwmPin pin where pwm duty cycle should be set
* \param[in] DutyCycle duty cycle of pwm
* \return E_OK
* E_NOT_OK - TimerTwo is not initialized or wrong PwmPin was given
* \pre Timer has to be in READY, RUNNING or STOPPED STATE
*****************************************************************************************************************************************************/
StdReturnType TimerTwo::setPwmDuty(PwmPinType PwmPin, byte DutyCycle)
{
StdReturnType ReturnValue{E_NOT_OK};
if((STATE_IDLE == State) || (STATE_RUNNING == State) || (STATE_STOPPED == State)) {
/* duty cycle out of bound? */
if(DutyCycle <= TIMERTWO_RESOLUTION) {
// use rule of three to calculate duty cycle related to timer top value
// OCR2A is top value of timer
uint32_t DutyCycleTrans = OCR2A * DutyCycle;
DutyCycleTrans >>= TIMERTWO_NUMBER_OF_BITS;
/* set output compare register value for given Pwm pin */
if(PWM_PIN_3 == PwmPin) {
ReturnValue = E_OK;
OCR2B = DutyCycleTrans;
}
}
}
return ReturnValue;
} /* setPwmDuty */
/******************************************************************************************************************************************************
start()
******************************************************************************************************************************************************/
/*! \brief start timer
* \details
*
* \return E_OK
* E_NOT_OK - TimerTwo is not in correct state
* \pre Timer has to be in READY or STOPPED STATE
*****************************************************************************************************************************************************/
StdReturnType TimerTwo::start()
{
if((STATE_IDLE == State) || (STATE_STOPPED == State)) {
/* reset counter value */
TCNT2 = 0u;
/* start counter by setting clock select register */
writeBitGroup(TCCR2B, TIMERTWO_REG_CS_GM, TIMERTWO_REG_CS_GP, ClockSelectBitGroup);
/* set overflow interrupt, if callback is set */
if(TimerIsrOverflowCallback != nullptr) {
/* wait until timer moved on from zero, otherwise get phantom interrupt */
while (TCNT2 == 0u);
/* enable timer overflow interrupt */
writeBit(TIMSK2, TOIE2, 1u);
}
State = STATE_RUNNING;
return E_OK;
}
return E_NOT_OK;
} /* start */
/******************************************************************************************************************************************************
stop()
******************************************************************************************************************************************************/
/*! \brief stop timer
* \details
*
* \return -
*****************************************************************************************************************************************************/
void TimerTwo::stop()
{
/* stop counter by clearing clock select register */
writeBitGroup(TCCR2B, TIMERTWO_REG_CS_GM, TIMERTWO_REG_CS_GP, REG_CS_NO_CLOCK);
State = STATE_STOPPED;
} /* stop */
/******************************************************************************************************************************************************
resume()
******************************************************************************************************************************************************/
/*! \brief resume timer
* \details
*
* \return E_OK
* E_NOT_OK - TimerTwo is not in stopped state
* \pre Timer has to be in STOPPED STATE
*****************************************************************************************************************************************************/
StdReturnType TimerTwo::resume()
{
if(STATE_STOPPED == State) {
/* resume counter by setting clock select register */
writeBitGroup(TCCR2B, TIMERTWO_REG_CS_GM, TIMERTWO_REG_CS_GP, ClockSelectBitGroup);
return E_OK;
}
return E_NOT_OK;
} /* resume */
/******************************************************************************************************************************************************
attachInterrupt()
******************************************************************************************************************************************************/
/*! \brief set timer overflow interrupt callback
* \details
*
* \param[in] TimerOverflowCallback timer overflow callback function
* \return E_OK
* E_NOT_OK - Callback function is nullptr
*****************************************************************************************************************************************************/
StdReturnType TimerTwo::attachInterrupt(TimerIsrCallbackF_void TimerOverflowCallback)
{
if(TimerOverflowCallback != nullptr) {
TimerIsrOverflowCallback = TimerOverflowCallback;
/* enable timer overflow interrupt */
if(State == STATE_RUNNING) writeBit(TIMSK2, TOIE2, 1u);
return E_OK;
}
return E_NOT_OK;
} /* attachInterrupt */
/******************************************************************************************************************************************************
detachInterrupt()
******************************************************************************************************************************************************/
/*! \brief clear timer overflow interrupt callback
* \details
*
* \return -
*****************************************************************************************************************************************************/
void TimerTwo::detachInterrupt()
{
/* clears the timer overflow interrupt enable bit */
writeBit(TIMSK2, TOIE2, 0u);
} /* detachInterrupt */
/******************************************************************************************************************************************************
read()
******************************************************************************************************************************************************/
/*! \brief read current timer value in microseconds
* \details this function returns the current timer value in microseconds
*
* \param[out] Microseconds current timer value
* \return E_OK
* E_NOT_OK - TimerTwo is not in running state
* \pre Timer has to be in RUNNING STATE
*****************************************************************************************************************************************************/
StdReturnType TimerTwo::read(TimeType& Microseconds)
{
if(STATE_RUNNING == State) {
/* save current timer value */
uint16_t counterValue{TCNT2};
byte TCNT2_tmp{TCNT2};
/* wait one counter tick, needed to find out counter counting up or down */
do { TCNT2_tmp = TCNT2; } while (counterValue == TCNT2_tmp);
/* if counter counting down, add top value to current value */
if(TCNT2_tmp < counterValue) { counterValue = (OCR2A - counterValue) + OCR2A; }
/* transform counter value to microseconds in an efficient way */
Microseconds = ((counterValue * 1000uL) / (F_CPU / 1000uL)) << getPrescaleShiftScale();
return E_OK;
}
return E_NOT_OK;
} /* read */
/******************************************************************************************************************************************************
* P R I V A T E F U N C T I O N S
******************************************************************************************************************************************************/
/******************************************************************************************************************************************************
getPrescaleShiftScale()
******************************************************************************************************************************************************/
inline byte TimerTwo::getPrescaleShiftScale()
{
switch (ClockSelectBitGroup)
{
case REG_CS_NO_PRESCALER:
return 0u;
break;
case REG_CS_PRESCALE_8:
return 3u;
break;
case REG_CS_PRESCALE_32:
return 5u;
break;
case REG_CS_PRESCALE_64:
return 6u;
break;
case REG_CS_PRESCALE_128:
return 7u;
break;
case REG_CS_PRESCALE_256:
return 8u;
break;
case REG_CS_PRESCALE_1024:
return 10u;
break;
default:
return 0u;
}
}
/******************************************************************************************************************************************************
getPrescaleShiftScale()
******************************************************************************************************************************************************/
inline byte TimerTwo::getTimerCycles(TimeType Microseconds)
{
/* 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 pre-scaler */
if(TimerCycles < TIMERTWO_RESOLUTION) ClockSelectBitGroup = REG_CS_NO_PRESCALER;
else if((TimerCycles >>= 3u) < TIMERTWO_RESOLUTION) ClockSelectBitGroup = REG_CS_PRESCALE_8;
else if((TimerCycles >>= 2u) < TIMERTWO_RESOLUTION) ClockSelectBitGroup = REG_CS_PRESCALE_32;
else if((TimerCycles >>= 1u) < TIMERTWO_RESOLUTION) ClockSelectBitGroup = REG_CS_PRESCALE_64;
else if((TimerCycles >>= 1u) < TIMERTWO_RESOLUTION) ClockSelectBitGroup = REG_CS_PRESCALE_128;
else if((TimerCycles >>= 1u) < TIMERTWO_RESOLUTION) ClockSelectBitGroup = REG_CS_PRESCALE_256;
else if((TimerCycles >>= 2u) < TIMERTWO_RESOLUTION) ClockSelectBitGroup = REG_CS_PRESCALE_1024;
return TimerCycles;
}
/******************************************************************************************************************************************************
I S R F U N C T I O N S
******************************************************************************************************************************************************/
ISR(TIMER2_OVF_vect)
{
Timer2.callTimerIsrOverflowCallback();
}
/******************************************************************************************************************************************************
* E N D O F F I L E
*****************************************************************************************************************************************************/