-
Notifications
You must be signed in to change notification settings - Fork 73
/
timer.ts
804 lines (741 loc) · 22.7 KB
/
timer.ts
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/**
* AVR-8 Timers
* Part of AVR8js
* Reference: http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf
*
* Copyright (C) 2019, 2020, 2021 Uri Shaked
*/
import { AVRInterruptConfig, CPU } from '../cpu/cpu';
import { AVRIOPort, PinOverrideMode, portBConfig, portDConfig } from './gpio';
const timer01Dividers = {
0: 0,
1: 1,
2: 8,
3: 64,
4: 256,
5: 1024,
6: 0, // External clock - see ExternalClockMode
7: 0, // Ditto
};
enum ExternalClockMode {
FallingEdge = 6,
RisingEdge = 7,
}
type u8 = number;
type u16 = number;
interface TimerDividers {
0: number;
1: number;
2: number;
3: number;
4: number;
5: number;
6: number;
7: number;
}
export interface AVRTimerConfig {
bits: 8 | 16;
dividers: TimerDividers;
// Interrupt vectors
captureInterrupt: u8;
compAInterrupt: u8;
compBInterrupt: u8;
compCInterrupt: u8; // Optional, 0 = unused
ovfInterrupt: u8;
// Register addresses
TIFR: u8;
OCRA: u8;
OCRB: u8;
OCRC: u8; // Optional, 0 = unused
ICR: u8;
TCNT: u8;
TCCRA: u8;
TCCRB: u8;
TCCRC: u8;
TIMSK: u8;
// TIFR bits
TOV: u8;
OCFA: u8;
OCFB: u8;
OCFC: u8; // Optional, if compCInterrupt != 0
// TIMSK bits
TOIE: u8;
OCIEA: u8;
OCIEB: u8;
OCIEC: u8; // Optional, if compCInterrupt != 0
// Output compare pins
compPortA: u16;
compPinA: u8;
compPortB: u16;
compPinB: u8;
compPortC: u16; // Optional, 0 = unused
compPinC: u16;
// External clock pin (optional, 0 = unused)
externalClockPort: u16;
externalClockPin: u8;
}
/** These are differnet for some devices (e.g. ATtiny85) */
const defaultTimerBits = {
// TIFR bits
TOV: 1,
OCFA: 2,
OCFB: 4,
OCFC: 0, // Unused
// TIMSK bits
TOIE: 1,
OCIEA: 2,
OCIEB: 4,
OCIEC: 0, // Unused
};
export const timer0Config: AVRTimerConfig = {
bits: 8,
captureInterrupt: 0, // not available
compAInterrupt: 0x1c,
compBInterrupt: 0x1e,
compCInterrupt: 0,
ovfInterrupt: 0x20,
TIFR: 0x35,
OCRA: 0x47,
OCRB: 0x48,
OCRC: 0, // not available
ICR: 0, // not available
TCNT: 0x46,
TCCRA: 0x44,
TCCRB: 0x45,
TCCRC: 0, // not available
TIMSK: 0x6e,
dividers: timer01Dividers,
compPortA: portDConfig.PORT,
compPinA: 6,
compPortB: portDConfig.PORT,
compPinB: 5,
compPortC: 0, // Not available
compPinC: 0,
externalClockPort: portDConfig.PORT,
externalClockPin: 4,
...defaultTimerBits,
};
export const timer1Config: AVRTimerConfig = {
bits: 16,
captureInterrupt: 0x14,
compAInterrupt: 0x16,
compBInterrupt: 0x18,
compCInterrupt: 0,
ovfInterrupt: 0x1a,
TIFR: 0x36,
OCRA: 0x88,
OCRB: 0x8a,
OCRC: 0, // not available
ICR: 0x86,
TCNT: 0x84,
TCCRA: 0x80,
TCCRB: 0x81,
TCCRC: 0x82,
TIMSK: 0x6f,
dividers: timer01Dividers,
compPortA: portBConfig.PORT,
compPinA: 1,
compPortB: portBConfig.PORT,
compPinB: 2,
compPortC: 0, // Not available
compPinC: 0,
externalClockPort: portDConfig.PORT,
externalClockPin: 5,
...defaultTimerBits,
};
export const timer2Config: AVRTimerConfig = {
bits: 8,
captureInterrupt: 0, // not available
compAInterrupt: 0x0e,
compBInterrupt: 0x10,
compCInterrupt: 0,
ovfInterrupt: 0x12,
TIFR: 0x37,
OCRA: 0xb3,
OCRB: 0xb4,
OCRC: 0, // not available
ICR: 0, // not available
TCNT: 0xb2,
TCCRA: 0xb0,
TCCRB: 0xb1,
TCCRC: 0, // not available
TIMSK: 0x70,
dividers: {
0: 0,
1: 1,
2: 8,
3: 32,
4: 64,
5: 128,
6: 256,
7: 1024,
},
compPortA: portBConfig.PORT,
compPinA: 3,
compPortB: portDConfig.PORT,
compPinB: 3,
compPortC: 0, // Not available
compPinC: 0,
externalClockPort: 0, // Not available
externalClockPin: 0,
...defaultTimerBits,
};
/* All the following types and constants are related to WGM (Waveform Generation Mode) bits: */
enum TimerMode {
Normal,
PWMPhaseCorrect,
CTC,
FastPWM,
PWMPhaseFrequencyCorrect,
Reserved,
}
enum TOVUpdateMode {
Max,
Top,
Bottom,
}
enum OCRUpdateMode {
Immediate,
Top,
Bottom,
}
const TopOCRA = 1;
const TopICR = 2;
type TimerTopValue = 0xff | 0x1ff | 0x3ff | 0xffff | typeof TopOCRA | typeof TopICR;
type WGMConfig = [TimerMode, TimerTopValue, OCRUpdateMode, TOVUpdateMode, number];
// Enable Toggle mode for OCxA in PWM Wave Generation mode
const OCToggle = 1;
const { Normal, PWMPhaseCorrect, CTC, FastPWM, Reserved, PWMPhaseFrequencyCorrect } = TimerMode;
const wgmModes8Bit: WGMConfig[] = [
/*0*/ [Normal, 0xff, OCRUpdateMode.Immediate, TOVUpdateMode.Max, 0],
/*1*/ [PWMPhaseCorrect, 0xff, OCRUpdateMode.Top, TOVUpdateMode.Bottom, 0],
/*2*/ [CTC, TopOCRA, OCRUpdateMode.Immediate, TOVUpdateMode.Max, 0],
/*3*/ [FastPWM, 0xff, OCRUpdateMode.Bottom, TOVUpdateMode.Max, 0],
/*4*/ [Reserved, 0xff, OCRUpdateMode.Immediate, TOVUpdateMode.Max, 0],
/*5*/ [PWMPhaseCorrect, TopOCRA, OCRUpdateMode.Top, TOVUpdateMode.Bottom, OCToggle],
/*6*/ [Reserved, 0xff, OCRUpdateMode.Immediate, TOVUpdateMode.Max, 0],
/*7*/ [FastPWM, TopOCRA, OCRUpdateMode.Bottom, TOVUpdateMode.Top, OCToggle],
];
// Table 16-4 in the datasheet
const wgmModes16Bit: WGMConfig[] = [
/*0 */ [Normal, 0xffff, OCRUpdateMode.Immediate, TOVUpdateMode.Max, 0],
/*1 */ [PWMPhaseCorrect, 0x00ff, OCRUpdateMode.Top, TOVUpdateMode.Bottom, 0],
/*2 */ [PWMPhaseCorrect, 0x01ff, OCRUpdateMode.Top, TOVUpdateMode.Bottom, 0],
/*3 */ [PWMPhaseCorrect, 0x03ff, OCRUpdateMode.Top, TOVUpdateMode.Bottom, 0],
/*4 */ [CTC, TopOCRA, OCRUpdateMode.Immediate, TOVUpdateMode.Max, 0],
/*5 */ [FastPWM, 0x00ff, OCRUpdateMode.Bottom, TOVUpdateMode.Top, 0],
/*6 */ [FastPWM, 0x01ff, OCRUpdateMode.Bottom, TOVUpdateMode.Top, 0],
/*7 */ [FastPWM, 0x03ff, OCRUpdateMode.Bottom, TOVUpdateMode.Top, 0],
/*8 */ [PWMPhaseFrequencyCorrect, TopICR, OCRUpdateMode.Bottom, TOVUpdateMode.Bottom, 0],
/*9 */ [PWMPhaseFrequencyCorrect, TopOCRA, OCRUpdateMode.Bottom, TOVUpdateMode.Bottom, OCToggle],
/*10*/ [PWMPhaseCorrect, TopICR, OCRUpdateMode.Top, TOVUpdateMode.Bottom, 0],
/*11*/ [PWMPhaseCorrect, TopOCRA, OCRUpdateMode.Top, TOVUpdateMode.Bottom, OCToggle],
/*12*/ [CTC, TopICR, OCRUpdateMode.Immediate, TOVUpdateMode.Max, 0],
/*13*/ [Reserved, 0xffff, OCRUpdateMode.Immediate, TOVUpdateMode.Max, 0],
/*14*/ [FastPWM, TopICR, OCRUpdateMode.Bottom, TOVUpdateMode.Top, OCToggle],
/*15*/ [FastPWM, TopOCRA, OCRUpdateMode.Bottom, TOVUpdateMode.Top, OCToggle],
];
type CompBitsValue = 0 | 1 | 2 | 3;
function compToOverride(comp: CompBitsValue) {
switch (comp) {
case 1:
return PinOverrideMode.Toggle;
case 2:
return PinOverrideMode.Clear;
case 3:
return PinOverrideMode.Set;
default:
return PinOverrideMode.Enable;
}
}
// Force Output Compare (FOC) bits
const FOCA = 1 << 7;
const FOCB = 1 << 6;
const FOCC = 1 << 5;
export class AVRTimer {
private readonly MAX = this.config.bits === 16 ? 0xffff : 0xff;
private lastCycle = 0;
private ocrA: u16 = 0;
private nextOcrA: u16 = 0;
private ocrB: u16 = 0;
private nextOcrB: u16 = 0;
private hasOCRC = this.config.OCRC > 0;
private ocrC: u16 = 0;
private nextOcrC: u16 = 0;
private ocrUpdateMode = OCRUpdateMode.Immediate;
private tovUpdateMode = TOVUpdateMode.Max;
private icr: u16 = 0; // only for 16-bit timers
private timerMode: TimerMode;
private topValue: TimerTopValue;
private tcnt: u16 = 0;
private tcntNext: u16 = 0;
private compA: CompBitsValue;
private compB: CompBitsValue;
private compC: CompBitsValue;
private tcntUpdated = false;
private updateDivider = false;
private countingUp = true;
private divider = 0;
private externalClockPort?: AVRIOPort;
private externalClockRisingEdge = false;
// This is the temporary register used to access 16-bit registers (section 16.3 of the datasheet)
private highByteTemp: u8 = 0;
// Interrupts
private OVF: AVRInterruptConfig = {
address: this.config.ovfInterrupt,
flagRegister: this.config.TIFR,
flagMask: this.config.TOV,
enableRegister: this.config.TIMSK,
enableMask: this.config.TOIE,
};
private OCFA: AVRInterruptConfig = {
address: this.config.compAInterrupt,
flagRegister: this.config.TIFR,
flagMask: this.config.OCFA,
enableRegister: this.config.TIMSK,
enableMask: this.config.OCIEA,
};
private OCFB: AVRInterruptConfig = {
address: this.config.compBInterrupt,
flagRegister: this.config.TIFR,
flagMask: this.config.OCFB,
enableRegister: this.config.TIMSK,
enableMask: this.config.OCIEB,
};
private OCFC: AVRInterruptConfig = {
address: this.config.compCInterrupt,
flagRegister: this.config.TIFR,
flagMask: this.config.OCFC,
enableRegister: this.config.TIMSK,
enableMask: this.config.OCIEC,
};
constructor(private cpu: CPU, private config: AVRTimerConfig) {
this.updateWGMConfig();
this.cpu.readHooks[config.TCNT] = (addr: u8) => {
this.count(false);
if (this.config.bits === 16) {
this.cpu.data[addr + 1] = this.tcnt >> 8;
}
return (this.cpu.data[addr] = this.tcnt & 0xff);
};
this.cpu.writeHooks[config.TCNT] = (value: u8) => {
this.tcntNext = (this.highByteTemp << 8) | value;
this.countingUp = true;
this.tcntUpdated = true;
this.cpu.updateClockEvent(this.count, 0);
if (this.divider) {
this.timerUpdated(this.tcntNext, this.tcntNext);
}
};
this.cpu.writeHooks[config.OCRA] = (value: u8) => {
this.nextOcrA = (this.highByteTemp << 8) | value;
if (this.ocrUpdateMode === OCRUpdateMode.Immediate) {
this.ocrA = this.nextOcrA;
}
};
this.cpu.writeHooks[config.OCRB] = (value: u8) => {
this.nextOcrB = (this.highByteTemp << 8) | value;
if (this.ocrUpdateMode === OCRUpdateMode.Immediate) {
this.ocrB = this.nextOcrB;
}
};
if (this.hasOCRC) {
this.cpu.writeHooks[config.OCRC] = (value: u8) => {
this.nextOcrC = (this.highByteTemp << 8) | value;
if (this.ocrUpdateMode === OCRUpdateMode.Immediate) {
this.ocrC = this.nextOcrC;
}
};
}
if (this.config.bits === 16) {
this.cpu.writeHooks[config.ICR] = (value: u8) => {
this.icr = (this.highByteTemp << 8) | value;
};
const updateTempRegister = (value: u8) => {
this.highByteTemp = value;
};
const updateOCRHighRegister = (value: u8, old: u8, addr: u16) => {
this.highByteTemp = value & (this.ocrMask >> 8);
cpu.data[addr] = this.highByteTemp;
return true;
};
this.cpu.writeHooks[config.TCNT + 1] = updateTempRegister;
this.cpu.writeHooks[config.OCRA + 1] = updateOCRHighRegister;
this.cpu.writeHooks[config.OCRB + 1] = updateOCRHighRegister;
if (this.hasOCRC) {
this.cpu.writeHooks[config.OCRC + 1] = updateOCRHighRegister;
}
this.cpu.writeHooks[config.ICR + 1] = updateTempRegister;
}
cpu.writeHooks[config.TCCRA] = (value) => {
this.cpu.data[config.TCCRA] = value;
this.updateWGMConfig();
return true;
};
cpu.writeHooks[config.TCCRB] = (value) => {
if (!config.TCCRC) {
this.checkForceCompare(value);
value &= ~(FOCA | FOCB);
}
this.cpu.data[config.TCCRB] = value;
this.updateDivider = true;
this.cpu.clearClockEvent(this.count);
this.cpu.addClockEvent(this.count, 0);
this.updateWGMConfig();
return true;
};
if (config.TCCRC) {
cpu.writeHooks[config.TCCRC] = (value) => {
this.checkForceCompare(value);
};
}
cpu.writeHooks[config.TIFR] = (value) => {
this.cpu.data[config.TIFR] = value;
this.cpu.clearInterruptByFlag(this.OVF, value);
this.cpu.clearInterruptByFlag(this.OCFA, value);
this.cpu.clearInterruptByFlag(this.OCFB, value);
return true;
};
cpu.writeHooks[config.TIMSK] = (value) => {
this.cpu.updateInterruptEnable(this.OVF, value);
this.cpu.updateInterruptEnable(this.OCFA, value);
this.cpu.updateInterruptEnable(this.OCFB, value);
};
}
reset() {
this.divider = 0;
this.lastCycle = 0;
this.ocrA = 0;
this.nextOcrA = 0;
this.ocrB = 0;
this.nextOcrB = 0;
this.ocrC = 0;
this.nextOcrC = 0;
this.icr = 0;
this.tcnt = 0;
this.tcntNext = 0;
this.tcntUpdated = false;
this.countingUp = false;
this.updateDivider = true;
}
get TCCRA() {
return this.cpu.data[this.config.TCCRA];
}
get TCCRB() {
return this.cpu.data[this.config.TCCRB];
}
get TIMSK() {
return this.cpu.data[this.config.TIMSK];
}
get CS() {
return (this.TCCRB & 0x7) as 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
}
get WGM() {
const mask = this.config.bits === 16 ? 0x18 : 0x8;
return ((this.TCCRB & mask) >> 1) | (this.TCCRA & 0x3);
}
get TOP() {
switch (this.topValue) {
case TopOCRA:
return this.ocrA;
case TopICR:
return this.icr;
default:
return this.topValue;
}
}
get ocrMask() {
switch (this.topValue) {
case TopOCRA:
case TopICR:
return 0xffff;
default:
return this.topValue;
}
}
/** Expose the raw value of TCNT, for use by the unit tests */
get debugTCNT() {
return this.tcnt;
}
private updateWGMConfig() {
const { config, WGM } = this;
const wgmModes = config.bits === 16 ? wgmModes16Bit : wgmModes8Bit;
const TCCRA = this.cpu.data[config.TCCRA];
const [timerMode, topValue, ocrUpdateMode, tovUpdateMode, flags] = wgmModes[WGM];
this.timerMode = timerMode;
this.topValue = topValue;
this.ocrUpdateMode = ocrUpdateMode;
this.tovUpdateMode = tovUpdateMode;
const pwmMode =
timerMode === FastPWM ||
timerMode === PWMPhaseCorrect ||
timerMode === PWMPhaseFrequencyCorrect;
const prevCompA = this.compA;
this.compA = ((TCCRA >> 6) & 0x3) as CompBitsValue;
if (this.compA === 1 && pwmMode && !(flags & OCToggle)) {
this.compA = 0;
}
if (!!prevCompA !== !!this.compA) {
this.updateCompA(this.compA ? PinOverrideMode.Enable : PinOverrideMode.None);
}
const prevCompB = this.compB;
this.compB = ((TCCRA >> 4) & 0x3) as CompBitsValue;
if (this.compB === 1 && pwmMode) {
this.compB = 0; // Reserved, according to the datasheet
}
if (!!prevCompB !== !!this.compB) {
this.updateCompB(this.compB ? PinOverrideMode.Enable : PinOverrideMode.None);
}
if (this.hasOCRC) {
const prevCompC = this.compC;
this.compC = ((TCCRA >> 2) & 0x3) as CompBitsValue;
if (this.compC === 1 && pwmMode) {
this.compC = 0; // Reserved, according to the datasheet
}
if (!!prevCompC !== !!this.compC) {
this.updateCompC(this.compC ? PinOverrideMode.Enable : PinOverrideMode.None);
}
}
}
count = (reschedule = true, external = false) => {
const { divider, lastCycle, cpu } = this;
const { cycles } = cpu;
const delta = cycles - lastCycle;
if ((divider && delta >= divider) || external) {
const counterDelta = external ? 1 : Math.floor(delta / divider);
this.lastCycle += counterDelta * divider;
const val = this.tcnt;
const { timerMode, TOP } = this;
const phasePwm = timerMode === PWMPhaseCorrect || timerMode === PWMPhaseFrequencyCorrect;
const newVal = phasePwm
? this.phasePwmCount(val, counterDelta)
: (val + counterDelta) % (TOP + 1);
const overflow = val + counterDelta > TOP;
// A CPU write overrides (has priority over) all counter clear or count operations.
if (!this.tcntUpdated) {
this.tcnt = newVal;
if (!phasePwm) {
this.timerUpdated(newVal, val);
}
}
if (!phasePwm) {
if (timerMode === FastPWM && overflow) {
const { compA, compB } = this;
if (compA) {
this.updateCompPin(compA, 'A', true);
}
if (compB) {
this.updateCompPin(compB, 'B', true);
}
}
if (this.ocrUpdateMode == OCRUpdateMode.Bottom && overflow) {
// OCRUpdateMode.Top only occurs in Phase Correct modes, handled by phasePwmCount()
this.ocrA = this.nextOcrA;
this.ocrB = this.nextOcrB;
this.ocrC = this.nextOcrC;
}
// OCRUpdateMode.Bottom only occurs in Phase Correct modes, handled by phasePwmCount().
// Thus we only handle TOVUpdateMode.Top or TOVUpdateMode.Max here.
if (overflow && (this.tovUpdateMode == TOVUpdateMode.Top || TOP === this.MAX)) {
cpu.setInterruptFlag(this.OVF);
}
}
}
if (this.tcntUpdated) {
this.tcnt = this.tcntNext;
this.tcntUpdated = false;
if (
(this.tcnt === 0 && this.ocrUpdateMode === OCRUpdateMode.Bottom) ||
(this.tcnt === this.TOP && this.ocrUpdateMode === OCRUpdateMode.Top)
) {
this.ocrA = this.nextOcrA;
this.ocrB = this.nextOcrB;
this.ocrC = this.nextOcrC;
}
}
if (this.updateDivider) {
const { CS } = this;
const { externalClockPin } = this.config;
const newDivider = this.config.dividers[CS];
this.lastCycle = newDivider ? this.cpu.cycles : 0;
this.updateDivider = false;
this.divider = newDivider;
if (this.config.externalClockPort && !this.externalClockPort) {
this.externalClockPort = this.cpu.gpioByPort[this.config.externalClockPort];
}
if (this.externalClockPort) {
this.externalClockPort.externalClockListeners[externalClockPin] = null;
}
if (newDivider) {
cpu.addClockEvent(this.count, this.lastCycle + newDivider - cpu.cycles);
} else if (
this.externalClockPort &&
(CS === ExternalClockMode.FallingEdge || CS === ExternalClockMode.RisingEdge)
) {
this.externalClockPort.externalClockListeners[externalClockPin] =
this.externalClockCallback;
this.externalClockRisingEdge = CS === ExternalClockMode.RisingEdge;
}
return;
}
if (reschedule && divider) {
cpu.addClockEvent(this.count, this.lastCycle + divider - cpu.cycles);
}
};
private externalClockCallback = (value: boolean) => {
if (value === this.externalClockRisingEdge) {
this.count(false, true);
}
};
private phasePwmCount(value: u16, delta: u8) {
const { ocrA, ocrB, ocrC, hasOCRC, TOP, MAX, tcntUpdated } = this;
if (!value && !TOP) {
delta = 0;
if (this.ocrUpdateMode === OCRUpdateMode.Top) {
this.ocrA = this.nextOcrA;
this.ocrB = this.nextOcrB;
this.ocrC = this.nextOcrC;
}
}
while (delta > 0) {
if (this.countingUp) {
value++;
if (value === TOP && !tcntUpdated) {
this.countingUp = false;
if (this.ocrUpdateMode === OCRUpdateMode.Top) {
this.ocrA = this.nextOcrA;
this.ocrB = this.nextOcrB;
this.ocrC = this.nextOcrC;
}
}
} else {
value--;
if (!value && !tcntUpdated) {
this.countingUp = true;
this.cpu.setInterruptFlag(this.OVF);
if (this.ocrUpdateMode === OCRUpdateMode.Bottom) {
this.ocrA = this.nextOcrA;
this.ocrB = this.nextOcrB;
this.ocrC = this.nextOcrC;
}
}
}
if (!tcntUpdated) {
if (value === ocrA) {
this.cpu.setInterruptFlag(this.OCFA);
if (this.compA) {
this.updateCompPin(this.compA, 'A');
}
}
if (value === ocrB) {
this.cpu.setInterruptFlag(this.OCFB);
if (this.compB) {
this.updateCompPin(this.compB, 'B');
}
}
if (hasOCRC && value === ocrC) {
this.cpu.setInterruptFlag(this.OCFC);
if (this.compC) {
this.updateCompPin(this.compC, 'C');
}
}
}
delta--;
}
return value & MAX;
}
private timerUpdated(value: number, prevValue: number) {
const { ocrA, ocrB, ocrC, hasOCRC } = this;
const overflow = prevValue > value;
if (((prevValue < ocrA || overflow) && value >= ocrA) || (prevValue < ocrA && overflow)) {
this.cpu.setInterruptFlag(this.OCFA);
if (this.compA) {
this.updateCompPin(this.compA, 'A');
}
}
if (((prevValue < ocrB || overflow) && value >= ocrB) || (prevValue < ocrB && overflow)) {
this.cpu.setInterruptFlag(this.OCFB);
if (this.compB) {
this.updateCompPin(this.compB, 'B');
}
}
if (
hasOCRC &&
(((prevValue < ocrC || overflow) && value >= ocrC) || (prevValue < ocrC && overflow))
) {
this.cpu.setInterruptFlag(this.OCFC);
if (this.compC) {
this.updateCompPin(this.compC, 'C');
}
}
}
private checkForceCompare(value: number) {
if (
this.timerMode == TimerMode.FastPWM ||
this.timerMode == TimerMode.PWMPhaseCorrect ||
this.timerMode == TimerMode.PWMPhaseFrequencyCorrect
) {
// The FOCnA/FOCnB/FOCnC bits are only active when the WGMn3:0 bits specifies a non-PWM mode
return;
}
if (value & FOCA) {
this.updateCompPin(this.compA, 'A');
}
if (value & FOCB) {
this.updateCompPin(this.compB, 'B');
}
if (this.config.compPortC && value & FOCC) {
this.updateCompPin(this.compC, 'C');
}
}
private updateCompPin(compValue: CompBitsValue, pinName: 'A' | 'B' | 'C', bottom = false) {
let newValue: PinOverrideMode = PinOverrideMode.None;
const invertingMode = compValue === 3;
const isSet = this.countingUp === invertingMode;
switch (this.timerMode) {
case Normal:
case CTC:
newValue = compToOverride(compValue);
break;
case FastPWM:
if (compValue === 1) {
newValue = bottom ? PinOverrideMode.None : PinOverrideMode.Toggle;
} else {
newValue = invertingMode !== bottom ? PinOverrideMode.Set : PinOverrideMode.Clear;
}
break;
case PWMPhaseCorrect:
case PWMPhaseFrequencyCorrect:
if (compValue === 1) {
newValue = PinOverrideMode.Toggle;
} else {
newValue = isSet ? PinOverrideMode.Set : PinOverrideMode.Clear;
}
break;
}
if (newValue !== PinOverrideMode.None) {
if (pinName === 'A') {
this.updateCompA(newValue);
} else if (pinName === 'B') {
this.updateCompB(newValue);
} else {
this.updateCompC(newValue);
}
}
}
private updateCompA(value: PinOverrideMode) {
const { compPortA, compPinA } = this.config;
const port = this.cpu.gpioByPort[compPortA];
port?.timerOverridePin(compPinA, value);
}
private updateCompB(value: PinOverrideMode) {
const { compPortB, compPinB } = this.config;
const port = this.cpu.gpioByPort[compPortB];
port?.timerOverridePin(compPinB, value);
}
private updateCompC(value: PinOverrideMode) {
const { compPortC, compPinC } = this.config;
const port = this.cpu.gpioByPort[compPortC];
port?.timerOverridePin(compPinC, value);
}
}