diff --git a/src/rp2040/spi.c b/src/rp2040/spi.c index d2c8df95..57231966 100644 --- a/src/rp2040/spi.c +++ b/src/rp2040/spi.c @@ -10,6 +10,7 @@ #include "internal.h" // pclock, gpio_peripheral #include "hardware/structs/spi.h" // spi_hw_t #include "hardware/regs/resets.h" // RESETS_RESET_SPI*_BITS +#include "board/misc.h" // timer_is_before DECL_ENUMERATION("spi_bus", "spi0_gpio0_gpio3_gpio2", 0); @@ -115,10 +116,16 @@ spi_prepare(struct spi_config config) spi_hw_t *spi = config.spi; if (spi->cr0 == config.cr0 && spi->cpsr == config.cpsr) return; + uint32_t diff = spi->cr0 ^ config.cr0; spi->cr1 = 0; spi->cr0 = config.cr0; spi->cpsr = config.cpsr; spi->cr1 = SPI_SSPCR1_SSE_BITS; + // Give time for state to update before caller changes CS pin + uint32_t end = timer_read_time() + timer_from_us(1); + if (diff & SPI_SSPCR0_SPO_BITS) + while (timer_is_before(timer_read_time(), end)) + ; } void