From 67638ca55e73dc9e8795743788205cdaca49f3f5 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Tue, 22 Mar 2022 23:23:28 +0200 Subject: fix(timer): Phase Correct mode overruns #119 --- src/peripherals/timer.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/peripherals/timer.ts') diff --git a/src/peripherals/timer.ts b/src/peripherals/timer.ts index 6167394..93b9d19 100644 --- a/src/peripherals/timer.ts +++ b/src/peripherals/timer.ts @@ -496,6 +496,11 @@ export class AVRTimer { } } + /** 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; @@ -637,7 +642,15 @@ export class AVRTimer { }; private phasePwmCount(value: u16, delta: u8) { - const { ocrA, ocrB, ocrC, hasOCRC, TOP, tcntUpdated } = this; + 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++; @@ -683,7 +696,7 @@ export class AVRTimer { } delta--; } - return value; + return value & MAX; } private timerUpdated(value: number, prevValue: number) { -- cgit v1.2.3