rp2040: Improve accuracy of hardware pwm cycle time

Use a different method of setting the hardware pwm registers so that
the actual cycle_time is much closer to the requested cycle_time.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor committed 2026-03-21 11:23:01 -04:00
1 parent 9fb7604353
commit 594ec7e120
2 files changed
+19 -11

No files matched your search

+1 -2
View File
@@ -23,8 +23,7 @@ uint8_t gpio_in_read(struct gpio_in g);
struct gpio_pwm {
void *reg;
uint8_t shift;
uint32_t mask;
uint32_t hwpwm_ticks, shift, mask;
};
struct gpio_pwm gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint32_t val);
void gpio_pwm_write(struct gpio_pwm g, uint32_t val);
+18 -9
View File
@@ -13,7 +13,7 @@
#include "hardware/structs/iobank0.h" // iobank0_hw
#include "hardware/regs/resets.h" // RESETS_RESET_PWM_BITS
#define MAX_PWM 256
#define MAX_PWM (1<<15)
DECL_CONSTANT("PWM_MAX", MAX_PWM);
struct gpio_pwm
@@ -27,7 +27,6 @@ gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint32_t val)
pwm_slice_hw_t * slice = &pwm_hw->slice[(pin >> 1) & 0x7];
uint8_t channel = pin & 1;
// Map cycle_time to clock divider
// The rp2040 has an 8.4 fractional divider, so we'll map the requested
// cycle time into that. The cycle_time we receive from Klippy is in
// relation to the crystal frequency and so we need to scale it up to match
@@ -39,13 +38,21 @@ gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint32_t val)
// Host never sends large cycle_time, so no need for elaborate handling
cycle_16ticks_64 = 0x80000000;
uint32_t cycle_16ticks = cycle_16ticks_64;
uint32_t pwm_div = cycle_16ticks / MAX_PWM;
// Clamp range of the divider
// Convert requested cycle time (cycle_time/CLOCK_FREQ) to actual
// cycle time (hwpwm_ticks*(pwm_div/16)/FREQ_SYS).
uint32_t hwpwm_ticks, pwm_div, shift = 4;
for (;;) {
hwpwm_ticks = (cycle_16ticks + (1 << (shift-1))) >> shift;
pwm_div = 1 << shift;
if (hwpwm_ticks <= UINT16_MAX)
break;
shift += 1;
}
if (pwm_div > 0xfff)
pwm_div = 0xfff;
else if (pwm_div < 0x10)
pwm_div = 0x10;
if (hwpwm_ticks < 2)
hwpwm_ticks = 2;
// Enable clock
if (!is_enabled_pclock(RESETS_RESET_PWM_BITS))
@@ -58,12 +65,12 @@ gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint32_t val)
// as long as their cycle times are the same.
if(!(slice->csr & PWM_CH0_CSR_EN_BITS)) {
slice->div = pwm_div;
slice->top = MAX_PWM - 1;
slice->top = hwpwm_ticks - 1;
slice->ctr = PWM_CH0_CTR_RESET;
slice->cc = PWM_CH0_CC_RESET;
slice->csr = PWM_CH0_CSR_EN_BITS;
} else {
if (slice->div != pwm_div)
if (slice->div != pwm_div || slice->top != hwpwm_ticks - 1)
shutdown("PWM pin has different cycle time from another in "
"the same slice");
@@ -79,6 +86,7 @@ gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint32_t val)
struct gpio_pwm out;
out.reg = (void*)&slice->cc;
out.hwpwm_ticks = hwpwm_ticks;
out.shift = channel ? PWM_CH0_CC_B_LSB : PWM_CH0_CC_A_LSB;
out.mask = channel ? PWM_CH0_CC_B_BITS : PWM_CH0_CC_A_BITS;
@@ -91,5 +99,6 @@ gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint32_t val)
void
gpio_pwm_write(struct gpio_pwm g, uint32_t val)
{
hw_write_masked((uint32_t*)g.reg, val << g.shift, g.mask);
uint32_t r = DIV_ROUND_CLOSEST(val * g.hwpwm_ticks, MAX_PWM);
hw_write_masked((uint32_t*)g.reg, r << g.shift, g.mask);
}