pico-sdk icon indicating copy to clipboard operation
pico-sdk copied to clipboard

spi_set_baudrate does not work well for SPI slave functionality

Open jmole opened this issue 7 months ago • 0 comments

In the RP2040 data sheet, there is a paragraph that states:

In slave mode, the same maximum SSPCLK frequency of 133MHz can achieve a peak bit rate of 133 / 12 = ~11.083Mbps. The SSPCPSR register can be programmed with a value of 12, and the SCR[7:0] field in the SSPCR0 register can be programmed with a value of 0. Similarly, the ratio of SSPCLK maximum frequency to SSPCLKOUT minimum frequency is 254 × 256.

However, the existing spi_set_baudrate function, while useful for setting an accurate bit rate for an SPI master, makes it very hard to accomplish this for an SPI slave.

Could I recommend that a new method be added, like spi_slave_clock_init(), that just sets these exact values?

e.g.:

void spi_slave_clock_init(spi_inst_t *spi) {
    spi_get_hw(spi)->cpsr = 12;
    hw_write_masked(&spi_get_hw(spi)->cr0, 0, SPI_SSPCR0_SCR_BITS);
}

According to the docs, this would result in a maximum usable SPI slave speed of ~11 MHz, and minimum speed of 133MHz/254/256 ~= 2kHz, which seems like it would span the useful range for all relevant uses of this component.

jmole avatar Jul 06 '24 05:07 jmole