From 3115d698decd6259fb92e67b505990d4fe1f055b Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Thu, 19 Mar 2026 19:01:42 -0400 Subject: [PATCH] rp2040: Replace divide with a multiply in spi.c Rework the comparison to avoid using an expensive divide operation. Signed-off-by: Kevin O'Connor --- src/rp2040/Kconfig | 2 +- src/rp2040/spi.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/rp2040/Kconfig b/src/rp2040/Kconfig index 8415592df..3f40524c0 100644 --- a/src/rp2040/Kconfig +++ b/src/rp2040/Kconfig @@ -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 spi rate, i2c rate, hard_pwm rate + # Software divide needed on rp2040 in i2c rate, hard_pwm rate select HAVE_SOFTWARE_DIVIDE_REQUIRED if MACH_RP2040 config BOARD_DIRECTORY diff --git a/src/rp2040/spi.c b/src/rp2040/spi.c index 9303e703b..21d643c62 100644 --- a/src/rp2040/spi.c +++ b/src/rp2040/spi.c @@ -86,15 +86,15 @@ spi_setup(uint32_t bus, uint8_t mode, uint32_t rate) struct spi_config res = {spi_bus[bus].spi, 0, 0}; - uint8_t prescale; + uint32_t prescale; for (prescale = 2; prescale <= 254; prescale += 2) { - if (pclk < (prescale + 2) * 256 * rate) + if ((prescale + 2) * 256 * rate > pclk) break; } - uint8_t postdiv; + uint32_t postdiv; for (postdiv = 255; postdiv > 0; --postdiv) { - if ((pclk / (prescale * postdiv)) > rate) + if (prescale * postdiv * rate < pclk) break; }