diff options
author | Michael Kurz <michi.kurz@gmail.com> | 2021-08-22 13:58:27 +0200 |
---|---|---|
committer | KevinOConnor <kevin@koconnor.net> | 2021-08-23 21:29:29 -0400 |
commit | efbb704522ec246cc91039f5321f6ccfac88e627 (patch) | |
tree | b1381549b7e530dbf555fee06ec0bf762a82d079 /src | |
parent | 7f704c47494f3e24b39e12325b3adcfd56d1cb36 (diff) | |
download | kutter-efbb704522ec246cc91039f5321f6ccfac88e627.tar.gz kutter-efbb704522ec246cc91039f5321f6ccfac88e627.tar.xz kutter-efbb704522ec246cc91039f5321f6ccfac88e627.zip |
lpc176x: Fix wrong inital value at PWM setup
This fixes a PWM going to full scale output when a initial value != 0 was
given. The output was on 100% until another update occurred.
This change enables the PWM counter before setting the channel values.
Fixes KevinOConnor/klipper#4559
Signed-off-by: Michael Kurz <michi.kurz@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/lpc176x/hard_pwm.c | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/src/lpc176x/hard_pwm.c b/src/lpc176x/hard_pwm.c index 934ecf1a..184c3d5c 100644 --- a/src/lpc176x/hard_pwm.c +++ b/src/lpc176x/hard_pwm.c @@ -61,32 +61,31 @@ gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint8_t val) { enable_pclock(PCLK_PWM1); } - if (LPC_PWM1->TCR & 1) { + if (LPC_PWM1->TCR & 0x1) { if (LPC_PWM1->PR != prescaler) { shutdown("PWM already programmed at different speed"); } } else { + LPC_PWM1->PCR = 0; LPC_PWM1->PR = (uint16_t) prescaler; - LPC_PWM1->MCR = 2; LPC_PWM1->MR0 = MAX_PWM - 1; - LPC_PWM1->LER |= 1 << 0; - LPC_PWM1->TCR = 0x2; // Reset PWM - LPC_PWM1->TCR = 0x8; // Enable PWM mode + LPC_PWM1->TCR = 0x2; // Reset PWM TC and PC + LPC_PWM1->MCR = 0x2; // Reset Counter on MR0 match + LPC_PWM1->TCR = 0x8; // Enable PWM mode + LPC_PWM1->TCR |= 0x1; // Enable PWM peripheral } if (LPC_PWM1->PCR & (1 << (p->channel + 8))) { shutdown("PWM output already in use"); } + // Setup channel struct gpio_pwm channel = {.reg = p->reg, .channel = p->channel}; gpio_pwm_write(channel, val); // Enable channel LPC_PWM1->PCR |= (1 << (p->channel + 8)); - // Enable PWM peripheral - LPC_PWM1->TCR |= 0x1; - return channel; } |