-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclbTimer2.cpp
More file actions
656 lines (543 loc) · 19 KB
/
clbTimer2.cpp
File metadata and controls
656 lines (543 loc) · 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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#include "clbTimer.h"
static clb::Timer2* s_active_timer2_instance = nullptr;
static uint32_t getPrescaler(clb::TAsynClock clock);
static uint64_t calculateTicks(uint32_t value, clb::TTimeUnit unit, uint32_t prescaler);
volatile uint8_t* getOcrRegister(clb::TOutputChannel channel);
uint8_t getOcFlagBit(clb::TOutputChannel channel);
static struct Timer2InterruptHandlers {
void (*compareMatchACallback)() = nullptr;
void (*compareMatchBCallback)() = nullptr;
void (*overflowCallback)() = nullptr;
} s_timer2_handlers;
//global ISRs for Timer2
ISR(TIMER2_COMPA_vect) {
if (s_active_timer2_instance && s_active_timer2_instance->_asyncDelayActive && s_active_timer2_instance->_asyncDelayActiveChannel == clb::TOutputChannel::A) {
s_active_timer2_instance->_asyncOverflowsCount--;
if (s_active_timer2_instance->_asyncOverflowsCount == 0) {
s_active_timer2_instance->_asyncDelayActive = false;
TIMSK2 &= ~(BIT0 << OCIE2A);
uint8_t temp_sreg = SREG;
cli();
TCCR2A = s_active_timer2_instance->_asyncSavedTCCR2A;
TCCR2B = s_active_timer2_instance->_asyncSavedTCCR2B;
TCNT2 = s_active_timer2_instance->_asyncSavedTCNT2;
OCR2A = s_active_timer2_instance->_asyncSavedOCR2A;
OCR2B = s_active_timer2_instance->_asyncSavedOCR2B;
TIMSK2 = s_active_timer2_instance->_asyncSavedTIMSK2;
TIFR2 = s_active_timer2_instance->_asyncSavedTIFR2;
SREG = temp_sreg;
if (s_timer2_handlers.compareMatchACallback) {
s_timer2_handlers.compareMatchACallback();
}
}
else {
if (s_active_timer2_instance->_asyncOverflowsCount == 1) {
OCR2A = s_active_timer2_instance->_asyncRemainingTicksValue;
}
else {
OCR2A = 255;
}
}
}
else {
if (s_timer2_handlers.compareMatchACallback) {
s_timer2_handlers.compareMatchACallback();
}
}
}
ISR(TIMER2_COMPB_vect) {
if (s_active_timer2_instance && s_active_timer2_instance->_asyncDelayActive && s_active_timer2_instance->_asyncDelayActiveChannel == clb::TOutputChannel::B) {
s_active_timer2_instance->_asyncOverflowsCount--;
if (s_active_timer2_instance->_asyncOverflowsCount == 0) {
s_active_timer2_instance->_asyncDelayActive = false;
TIMSK2 &= ~(BIT0 << OCIE2B);
uint8_t temp_sreg = SREG;
cli();
TCCR2A = s_active_timer2_instance->_asyncSavedTCCR2A;
TCCR2B = s_active_timer2_instance->_asyncSavedTCCR2B;
TCNT2 = s_active_timer2_instance->_asyncSavedTCNT2;
OCR2A = s_active_timer2_instance->_asyncSavedOCR2A;
OCR2B = s_active_timer2_instance->_asyncSavedOCR2B;
TIMSK2 = s_active_timer2_instance->_asyncSavedTIMSK2;
TIFR2 = s_active_timer2_instance->_asyncSavedTIFR2;
SREG = temp_sreg;
if (s_timer2_handlers.compareMatchBCallback) {
s_timer2_handlers.compareMatchBCallback();
}
}
else {
if (s_active_timer2_instance->_asyncOverflowsCount == 1) {
OCR2B = s_active_timer2_instance->_asyncRemainingTicksValue;
}
else {
OCR2B = 255;
}
}
}
else {
if (s_timer2_handlers.compareMatchBCallback) {
s_timer2_handlers.compareMatchBCallback();
}
}
}
// Timer2 constructor/destructor
clb::Timer2::Timer2() {
WARNING("Using Timer2 is not recommended since any change will basically break the tone() and noTone() functions.");
if (s_active_timer2_instance != nullptr) {
FATAL("There is already an instance of Timer2. Only one instance is allowed.");
}
s_active_timer2_instance = this;
_asyncDelayActive = false;
_asyncTargetTicks = 0;
_asyncOverflowsCount = 0;
_asyncRemainingTicksValue = 0;
_asyncDelayActiveChannel = clb::TOutputChannel::A;
_asyncSavedTCCR2A = 0;
_asyncSavedTCCR2B = 0;
_asyncSavedTCNT2 = 0;
_asyncSavedOCR2A = 0;
_asyncSavedOCR2B = 0;
_asyncSavedTIMSK2 = 0;
_asyncSavedTIFR2 = 0;
_asyncSavedSREG = 0;
}
clb::Timer2::~Timer2() {
deactivate();
}
void clb::Timer2::deactivate() {
if (_asyncDelayActive) {
stopAsyncDelay();
}
cli();
TIMSK2 = 0;
TIFR2 = (BIT0 << OCF2A) | (BIT0 << OCF2B) | (BIT0 << TOV2);
TCCR2B = 0;
TCCR2A = 0;
OCR2A = 0;
OCR2B = 0;
TCNT2 = 0;
sei();
s_active_timer2_instance = nullptr;
}
//set the mode in TCCR2A and TCCR2B
void clb::Timer2::setMode(TMode8 mode) {
uint8_t _TCCR2A = TCCR2A;
uint8_t _TCCR2B = TCCR2B;
uint8_t _mode = static_cast<uint8_t>(mode) & 0x07;
uint8_t _bit0 = (_mode >> 0) & BIT0;
uint8_t _bit1 = (_mode >> 1) & BIT0;
uint8_t _bit2 = (_mode >> 2) & BIT0;
_bit0 <<= WGM20;
_bit1 <<= WGM21;
_bit2 <<= WGM22;
_TCCR2A &= ~(BIT1 | BIT0);
_TCCR2B &= ~(BIT3);
_TCCR2A |= (_bit1 | _bit0);
_TCCR2B |= (_bit2);
TCCR2A = _TCCR2A;
TCCR2B = _TCCR2B;
}
//set the clock in TCCR2B
void clb::Timer2::setClock(TAsynClock clock) {
_clockSource = static_cast<uint8_t>(clock) & 0x07;
}
//set the compare match output mode for OC2A
void clb::Timer2::setCompareMatchOutputModeA(TCMOM mode) {
uint8_t _TCCR2A = TCCR2A;
uint8_t _mode = static_cast<uint8_t>(mode) & 0x03;
uint8_t _bit0 = (_mode >> 0) & BIT0;
uint8_t _bit1 = (_mode >> 1) & BIT0;
_bit0 <<= COM2A0;
_bit1 <<= COM2A1;
_TCCR2A &= ~(BIT7 | BIT6);
_TCCR2A |= (_bit1 | _bit0);
TCCR2A = _TCCR2A;
}
//set the compare match output mode for OC2B
void clb::Timer2::setCompareMatchOutputModeB(TCMOM mode) {
uint8_t _TCCR2A = TCCR2A;
uint8_t _mode = static_cast<uint8_t>(mode) & 0x03;
uint8_t _bit0 = (_mode >> 0) & BIT0;
uint8_t _bit1 = (_mode >> 1) & BIT0;
_bit0 <<= COM2B0;
_bit1 <<= COM2B1;
_TCCR2A &= ~(BIT5 | BIT4);
_TCCR2A |= (_bit1 | _bit0);
TCCR2A = _TCCR2A;
}
//set the compare match value in OCR2A
void clb::Timer2::setCompareMatchValueA(uint8_t value) { OCR2A = value; }
//set the compare match value in OCR2B
void clb::Timer2::setCompareMatchValueB(uint8_t value) { OCR2B = value; }
//set the interrupt callback for the timer
void clb::Timer2::setInterruptCallback(TInterrupt8 type, void (*callback)()) {
switch (type) {
case TInterrupt8::COMPMATCHA:
s_timer2_handlers.compareMatchACallback = callback;
break;
case TInterrupt8::COMPMATCHB:
s_timer2_handlers.compareMatchBCallback = callback;
break;
case TInterrupt8::OVERFLOW:
s_timer2_handlers.overflowCallback = callback;
break;
default:
CRITICAL("Invalid interrupt type for setting callback");
break;
}
}
void clb::Timer2::enableInterrupt(TInterrupt8 type) {
switch (type) {
case TInterrupt8::COMPMATCHA:
TIMSK2 |= BIT0 << OCIE2A;
break;
case TInterrupt8::COMPMATCHB:
TIMSK2 |= BIT0 << OCIE2B;
break;
case TInterrupt8::OVERFLOW:
TIMSK2 |= BIT0 << TOIE2;
break;
default:
CRITICAL("Invalid interrupt type for enabling interrupt");
break;
}
}
void clb::Timer2::disableInterrupt(TInterrupt8 type) {
switch (type) {
case TInterrupt8::COMPMATCHA:
TIMSK2 &= ~(BIT0 << OCIE2A);
break;
case TInterrupt8::COMPMATCHB:
TIMSK2 &= ~(BIT0 << OCIE2B);
break;
case TInterrupt8::OVERFLOW:
TIMSK2 &= ~(BIT0 << TOIE2);
break;
default:
CRITICAL("Invalid interrupt type for disabling interrupt");
break;
}
}
bool clb::Timer2::getInterruptFlag(TInterrupt8 type) {
switch (type) {
case TInterrupt8::COMPMATCHA:
return (TIFR2 & BIT0 << OCF2A);
case TInterrupt8::COMPMATCHB:
return (TIFR2 & BIT0 << OCF2B);
case TInterrupt8::OVERFLOW:
return (TIFR2 & BIT0 << TOV2);
default:
CRITICAL("Invalid interrupt type for getting interrupt flag");
break;
}
return false;
}
void clb::Timer2::clearInterruptFlag(TInterrupt8 type) {
switch (type) {
case TInterrupt8::COMPMATCHA:
TIFR2 |= BIT0 << OCF2A;
break;
case TInterrupt8::COMPMATCHB:
TIFR2 |= BIT0 << OCF2B;
break;
case TInterrupt8::OVERFLOW:
TIFR2 |= BIT0 << TOV2;
break;
default:
CRITICAL("Invalid interrupt type for clearing interrupt flag");
break;
}
}
void clb::Timer2::startTimer() {
if (_clockSource == 0) {
FATAL("Clock source was not set, timer doesn't start");
}
uint8_t _TCCR2B = TCCR2B;
_TCCR2B &= ~(BIT2 | BIT1 | BIT0);
_TCCR2B |= _clockSource;
TCCR2B = _TCCR2B;
}
void clb::Timer2::stopTimer() {
if (_asyncDelayActive) {
stopAsyncDelay();
}
uint8_t _TCCR2B = TCCR2B;
_TCCR2B &= ~(BIT2 | BIT1 | BIT0);
TCCR2B = _TCCR2B;
}
uint8_t clb::Timer2::getTimerValue8() { return TCNT2; }
void clb::Timer2::setTimerValue(uint8_t value) { TCNT2 = value; }
void clb::Timer2::forceOutputCompareA() { TCCR2B |= BIT0 << FOC2A; }
void clb::Timer2::forceOutputCompareB() { TCCR2B |= BIT0 << FOC2B; }
void clb::Timer2::setAsynchronousClock(clb::TACLK clk) {
cli();
uint8_t _CS2 = TCCR2B & (BIT0 | BIT1 | BIT2);
uint8_t _TCNT2 = TCNT2;
uint8_t _OCR2A = OCR2A;
uint8_t _OCR2B = OCR2B;
uint8_t _TCCR2A = TCCR2A;
uint8_t _TCCR2B = TCCR2B;
TCCR2B &= ~(BIT0 | BIT1 | BIT2);
switch (clk) {
case clb::TACLK::CLKIO:
ASSR &= ~(BIT0 << EXCLK);
ASSR &= ~(BIT0 << AS2);
break;
case clb::TACLK::OSC:
ASSR &= ~(BIT0 << EXCLK);
ASSR |= (BIT0 << AS2);
while (ASSR & ((BIT0 << TCN2UB) | (BIT0 << OCR2AUB) | (BIT0 << OCR2BUB) | (BIT0 << TCR2AUB) | (BIT0 << TCR2BUB))) {
// Wait for the registers to be updated
}
WARNING("Modifying TCNT2, OCR2A, OCR2B, TCCR2A, and TCCR2B must be done after polling their corresponding busy flags in async mode");
break;
case clb::TACLK::SQRWAVE:
ASSR |= (BIT0 << EXCLK);
ASSR |= (BIT0 << AS2);
while (ASSR & ((BIT0 << TCN2UB) | (BIT0 << OCR2AUB) | (BIT0 << OCR2BUB) | (BIT0 << TCR2AUB) | (BIT0 << TCR2BUB))) {
// Wait for the registers to be updated
}
WARNING("Modifying TCNT2, OCR2A, OCR2B, TCCR2A, and TCCR2B must be done after polling their corresponding busy flags in async mode");
break;
default:
CRITICAL("Invalid TACLK value for Timer2");
break;
}
TCNT2 = _TCNT2;
if (ASSR & (BIT0 << AS2)) {while (ASSR & (BIT0 << TCN2UB));}
OCR2A = _OCR2A;
if (ASSR & (BIT0 << AS2)) {while (ASSR & (BIT0 << OCR2AUB));}
OCR2B = _OCR2B;
if (ASSR & (BIT0 << AS2)) {while (ASSR & (BIT0 << OCR2BUB));}
TCCR2A = _TCCR2A;
if (ASSR & (BIT0 << AS2)) {while (ASSR & (BIT0 << TCR2AUB));}
TCCR2B = _TCCR2B | _CS2;
if (ASSR & (BIT0 << AS2)) {while (ASSR & (BIT0 << TCR2BUB));}
sei();
}
bool clb::Timer2::getBusyFlag(clb::TBusyFlag flag) {
return (ASSR & (BIT0 << static_cast<uint8_t>(flag))) != 0;
}
void clb::Timer2::syncDelay(uint32_t time) {
syncDelay(time, clb::TTimeUnit::MILLISECONDS, clb::TOutputChannel::B);
}
void clb::Timer2::syncDelay(uint32_t time, clb::TTimeUnit unit) {
syncDelay(time, unit, clb::TOutputChannel::B);
}
void clb::Timer2::syncDelay(uint32_t time, clb::TTimeUnit unit, clb::TOutputChannel channel) {
uint32_t _prescaler;
clb::TAsynClock _clockSourceEnum = static_cast<clb::TAsynClock>(this->_clockSource);
if (_clockSourceEnum == clb::TAsynClock::STOPPED) {
_prescaler = 256;
} else {
_prescaler = getPrescaler(_clockSourceEnum);
}
uint64_t _ticks = calculateTicks(time, unit, _prescaler);
syncDelayLogic(_ticks, channel);
}
void clb::Timer2::asyncDelay(uint32_t time) {
asyncDelay(time, clb::TTimeUnit::MILLISECONDS, clb::TOutputChannel::A);
}
void clb::Timer2::asyncDelay(uint32_t time, clb::TTimeUnit timeUnit) {
asyncDelay(time, timeUnit, clb::TOutputChannel::A);
}
void clb::Timer2::asyncDelay(uint32_t time, clb::TTimeUnit timeUnit, clb::TOutputChannel channel) {
if (channel != clb::TOutputChannel::A && channel != clb::TOutputChannel::B) {
CRITICAL("Timer2 only supports TOutputChannel::A and TOutputChannel::B for asyncDelay.");
return;
}
uint32_t _prescaler;
clb::TAsynClock _clockSourceEnum = static_cast<clb::TAsynClock>(this->_clockSource);
if (_clockSourceEnum == clb::TAsynClock::STOPPED) {
_prescaler = 64;
} else {
_prescaler = getPrescaler(_clockSourceEnum);
}
if (_prescaler == 0) {
FATAL("Calculated prescaler for Timer2 asyncDelay is 0 (STOPPED clock source), cannot calculate ticks.");
return;
}
uint64_t calculatedTicks = calculateTicks(time, timeUnit, _prescaler);
asyncDelayLogic(calculatedTicks, channel);
}
//helpers
void clb::Timer2::syncDelayLogic(uint64_t ticks, clb::TOutputChannel channel) {
uint8_t _sreg = SREG;
cli();
uint8_t _tccr2a = TCCR2A;
uint8_t _tccr2b = TCCR2B;
uint8_t _tcnt2 = TCNT2;
uint8_t _ocr2a = OCR2A;
uint8_t _ocr2b = OCR2B;
uint8_t _timsk2 = TIMSK2;
uint8_t _tifr2 = TIFR2;
volatile uint8_t* _ocr_reg = getOcrRegister(channel);
uint8_t _oc_flag_bit = getOcFlagBit(channel);
TCCR2A = 0;
TCCR2B = 0;
uint32_t _prescaler = getPrescaler(static_cast<clb::TAsynClock>(this->_clockSource));
if (_prescaler == 0) {
TCCR2B = (BIT0 << CS22);
}
else {
TCCR2B = this->_clockSource;
}
TCNT2 = 0;
TIFR2 = (BIT0 << _oc_flag_bit) | (BIT0 << TOV2);
const uint16_t MAX_TIMER2_TICKS = 256;
uint32_t _overflows = ticks / MAX_TIMER2_TICKS;
uint16_t _remaining_ticks = ticks % MAX_TIMER2_TICKS;
for (uint32_t i = 0; i < _overflows; i++) {
while (!(TIFR2 & (BIT0 << TOV2))) {
}
TIFR2 |= (BIT0 << TOV2);
}
if (_remaining_ticks > 0) {
*_ocr_reg = _remaining_ticks - 1;
while (!(TIFR2 & (BIT0 << _oc_flag_bit))) { }
TIFR2 |= (BIT0 << _oc_flag_bit);
}
TCCR2A = _tccr2a;
TCCR2B = _tccr2b;
TCNT2 = _tcnt2;
OCR2A = _ocr2a;
OCR2B = _ocr2b;
TIMSK2 = _timsk2;
TIFR2 = _tifr2;
SREG = _sreg;
}
void clb::Timer2::asyncDelayLogic(uint64_t ticks, clb::TOutputChannel channel) {
if (_asyncDelayActive) {
WARNING("An asynchronous delay is already active on Timer2. Cannot start a new one.");
return;
}
if (ticks == 0) {
WARNING("asyncDelay(0) called. Delay will complete immediately.");
_asyncDelayActive = false;
return;
}
_asyncSavedSREG = SREG;
cli();
_asyncSavedTCCR2A = TCCR2A;
_asyncSavedTCCR2B = TCCR2B;
_asyncSavedTCNT2 = TCNT2;
_asyncSavedOCR2A = OCR2A;
_asyncSavedOCR2B = OCR2B;
_asyncSavedTIMSK2 = TIMSK2;
_asyncSavedTIFR2 = TIFR2;
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
const uint16_t MAX_TIMER2_TICKS = 256;
_asyncTargetTicks = ticks;
uint32_t numFullCycles = ticks / MAX_TIMER2_TICKS;
uint8_t remainderTicks = ticks % MAX_TIMER2_TICKS;
if (remainderTicks == 0) {
_asyncOverflowsCount = numFullCycles;
_asyncRemainingTicksValue = MAX_TIMER2_TICKS - 1;
}
else {
_asyncOverflowsCount = numFullCycles + 1;
_asyncRemainingTicksValue = remainderTicks - 1;
}
TCCR2A |= (BIT0 << WGM21);
uint32_t prescaler_val_for_setup = getPrescaler(static_cast<clb::TAsynClock>(this->_clockSource));
if (prescaler_val_for_setup == 0) {
TCCR2B |= (BIT0 << CS21) | (BIT0 << CS20);
}
else {
TCCR2B |= this->_clockSource;
}
if (channel == clb::TOutputChannel::A) {
TIFR2 |= (BIT0 << OCF2A);
if (numFullCycles == 0 && remainderTicks > 0) {
OCR2A = _asyncRemainingTicksValue;
}
else {
OCR2A = MAX_TIMER2_TICKS - 1;
}
TIMSK2 |= (BIT0 << OCIE2A);
}
else {
TIFR2 |= (BIT0 << OCF2B);
if (numFullCycles == 0 && remainderTicks > 0) {
OCR2B = _asyncRemainingTicksValue;
}
else {
OCR2B = MAX_TIMER2_TICKS - 1;
}
TIMSK2 |= (BIT0 << OCIE2B);
}
_asyncDelayActive = true;
_asyncDelayActiveChannel = channel;
SREG = _asyncSavedSREG;
}
bool clb::Timer2::isAsyncDelayFinished() {
return !_asyncDelayActive;
}
void clb::Timer2::stopAsyncDelay() {
if (_asyncDelayActive) {
WARNING("Stopping active asynchronous delay on Timer2.");
uint8_t temp_sreg = SREG;
cli();
if (_asyncDelayActiveChannel == clb::TOutputChannel::A) {
TIMSK2 &= ~(BIT0 << OCIE2A);
TIFR2 |= (BIT0 << OCF2A);
}
else {
TIMSK2 &= ~(BIT0 << OCIE2B);
TIFR2 |= (BIT0 << OCF2B);
}
TCCR2A = _asyncSavedTCCR2A;
TCCR2B = _asyncSavedTCCR2B;
TCNT2 = _asyncSavedTCNT2;
OCR2A = _asyncSavedOCR2A;
OCR2B = _asyncSavedOCR2B;
TIMSK2 = _asyncSavedTIMSK2;
TIFR2 = _asyncSavedTIFR2;
SREG = _asyncSavedSREG;
_asyncDelayActive = false;
_asyncCurrentTicks = 0;
_asyncOverflowsCount = 0;
_asyncRemainingTicksValue = 0;
}
}
static uint32_t getPrescaler(clb::TAsynClock clock) {
switch (clock) {
case clb::TAsynClock::STOPPED: return 0;
case clb::TAsynClock::DIV_1: return 1;
case clb::TAsynClock::DIV_8: return 8;
case clb::TAsynClock::DIV_32: return 32;
case clb::TAsynClock::DIV_64: return 64;
case clb::TAsynClock::DIV_128: return 128;
case clb::TAsynClock::DIV_256: return 256;
case clb::TAsynClock::DIV_1024: return 1024;
default: return 1;
}
}
static uint64_t calculateTicks(uint32_t value, clb::TTimeUnit unit, uint32_t prescaler) {
uint64_t _microseconds = 0;
switch (unit) {
case clb::TTimeUnit::SECONDS: _microseconds = (uint64_t)value * 1000000UL; break;
case clb::TTimeUnit::MILLISECONDS: _microseconds = (uint64_t)value * 1000UL; break;
case clb::TTimeUnit::MICROSECONDS: _microseconds = (uint64_t)value; break;
default: return 0;
}
//ticks = (total_microseconds * F_CPU) / (prescaler_value * 1,000,000)
uint64_t _ticks = (_microseconds * F_CPU) / (prescaler * 1000000UL);
return _ticks;
}
volatile uint8_t* getOcrRegister(clb::TOutputChannel channel) {
switch (channel) {
case clb::TOutputChannel::A: return &OCR2A;
case clb::TOutputChannel::B: return &OCR2B;
default: return nullptr;
}
}
uint8_t getOcFlagBit(clb::TOutputChannel channel) {
switch (channel) {
case clb::TOutputChannel::A: return OCF2A;
case clb::TOutputChannel::B: return OCF2B;
default: return 0;
}
}