diff --git a/src/rp2_common/hardware_spi/include/hardware/spi.h b/src/rp2_common/hardware_spi/include/hardware/spi.h index 595a922..f0d89c1 100644 --- a/src/rp2_common/hardware_spi/include/hardware/spi.h +++ b/src/rp2_common/hardware_spi/include/hardware/spi.h @@ -102,14 +102,14 @@ typedef enum { * Puts the SPI into a known state, and enable it. Must be called before other * functions. * - * \param spi SPI instance specifier, either \ref spi0 or \ref spi1 - * \param baudrate Baudrate required in Hz + * \note There is no guarantee that the baudrate requested can be achieved exactly; the nearest will be chosen + * and returned * - * \note There is no guarantee that the baudrate requested will be possible, the nearest will be chosen, - * and this function does not return any indication of this. You can use the \ref spi_set_baudrate function - * which will return the actual baudrate selected if this is important. + * \param spi SPI instance specifier, either \ref spi0 or \ref spi1 + * \param baudrate Baudrate requested in Hz + * \return the actual baud rate set */ -void spi_init(spi_inst_t *spi, uint baudrate); +uint spi_init(spi_inst_t *spi, uint baudrate); /*! \brief Deinitialise SPI instances * \ingroup hardware_spi diff --git a/src/rp2_common/hardware_spi/spi.c b/src/rp2_common/hardware_spi/spi.c index bc59845..e74aca7 100644 --- a/src/rp2_common/hardware_spi/spi.c +++ b/src/rp2_common/hardware_spi/spi.c @@ -18,11 +18,11 @@ static inline void spi_unreset(spi_inst_t *spi) { unreset_block_wait(spi == spi0 ? RESETS_RESET_SPI0_BITS : RESETS_RESET_SPI1_BITS); } -void spi_init(spi_inst_t *spi, uint baudrate) { +uint spi_init(spi_inst_t *spi, uint baudrate) { spi_reset(spi); spi_unreset(spi); - (void) spi_set_baudrate(spi, baudrate); + uint baud = spi_set_baudrate(spi, baudrate); spi_set_format(spi, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST); // Always enable DREQ signals -- harmless if DMA is not listening hw_set_bits(&spi_get_hw(spi)->dmacr, SPI_SSPDMACR_TXDMAE_BITS | SPI_SSPDMACR_RXDMAE_BITS); @@ -30,6 +30,7 @@ void spi_init(spi_inst_t *spi, uint baudrate) { // Finally enable the SPI hw_set_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_SSE_BITS); + return baud; } void spi_deinit(spi_inst_t *spi) {