rp2040: Avoid run-time division in hard_pwm.c

Use a MAX_PWM of 256 (instead of 255) to avoid an expensive run-time
divide operation.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor
2026-03-20 20:21:02 -04:00
parent bfc624ac07
commit 9fb7604353
3 changed files with 7 additions and 5 deletions

View File

@@ -14,7 +14,7 @@ config RPXXXX_SELECT
select HAVE_GPIO_HARD_PWM
select HAVE_STEPPER_OPTIMIZED_BOTH_EDGE
select HAVE_BOOTLOADER_REQUEST
# Software divide needed on rp2040 in i2c rate, hard_pwm rate
# Software divide needed on rp2040 for rate calculation in i2c.c
select HAVE_SOFTWARE_DIVIDE_REQUIRED if MACH_RP2040
config BOARD_DIRECTORY

View File

@@ -26,7 +26,7 @@ struct gpio_pwm {
uint8_t shift;
uint32_t mask;
};
struct gpio_pwm gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint8_t val);
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);
struct gpio_adc {

View File

@@ -13,11 +13,12 @@
#include "hardware/structs/iobank0.h" // iobank0_hw
#include "hardware/regs/resets.h" // RESETS_RESET_PWM_BITS
#define MAX_PWM 255
#define MAX_PWM 256
DECL_CONSTANT("PWM_MAX", MAX_PWM);
struct gpio_pwm
gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint8_t val) {
gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint32_t val)
{
if(pin >= 30)
shutdown("invalid gpio pin");
@@ -88,6 +89,7 @@ gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint8_t val) {
}
void
gpio_pwm_write(struct gpio_pwm g, uint32_t val) {
gpio_pwm_write(struct gpio_pwm g, uint32_t val)
{
hw_write_masked((uint32_t*)g.reg, val << g.shift, g.mask);
}