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 committed 2026-03-20 19:54:57 -04:00
1 parent 6416d3e2ca
commit 3115d698de
2 files changed
+5 -5

No files matched your search

+1 -1
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
+4 -4
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;
}