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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor
2026-03-19 19:01:42 -04:00
parent 6416d3e2ca
commit 3115d698de
2 changed files with 5 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 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

View File

@@ -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;
}