From 367fb9d40ef58d4ba4df156f41f356633319ae18 Mon Sep 17 00:00:00 2001 From: soypat Date: Sat, 29 Jan 2022 14:10:05 -0300 Subject: [PATCH] machine/rp2040: whole now correctly set at minimum 1 value for high frequency PWM --- src/machine/machine_rp2040_pwm.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/machine/machine_rp2040_pwm.go b/src/machine/machine_rp2040_pwm.go index 525ea731..a355be4c 100644 --- a/src/machine/machine_rp2040_pwm.go +++ b/src/machine/machine_rp2040_pwm.go @@ -278,8 +278,14 @@ func (pwm *pwmGroup) setPeriod(period uint64) error { rhs := 16 * period / ((1 + phc) * periodPerCycle * (1 + topStart)) // right-hand-side of equation, scaled so frac is not divided whole := rhs / 16 frac := rhs % 16 - if whole > 0xff { + switch { + case whole > 0xff: whole = 0xff + case whole == 0: + // whole calculation underflowed so setting to minimum + // permissible value in DIV_INT register. + whole = 1 + frac = 0 } // Step 2 is acquiring a better top value. Clearing the equation: