make spi_init return baud rate set (#296)

This commit is contained in:
Graham Sanderson 2021-05-01 14:34:28 -05:00 committed by GitHub
parent 5259693774
commit 4c83c10f01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 8 deletions

View File

@ -102,14 +102,14 @@ typedef enum {
* Puts the SPI into a known state, and enable it. Must be called before other * Puts the SPI into a known state, and enable it. Must be called before other
* functions. * functions.
* *
* \param spi SPI instance specifier, either \ref spi0 or \ref spi1 * \note There is no guarantee that the baudrate requested can be achieved exactly; the nearest will be chosen
* \param baudrate Baudrate required in Hz * and returned
* *
* \note There is no guarantee that the baudrate requested will be possible, the nearest will be chosen, * \param spi SPI instance specifier, either \ref spi0 or \ref spi1
* and this function does not return any indication of this. You can use the \ref spi_set_baudrate function * \param baudrate Baudrate requested in Hz
* which will return the actual baudrate selected if this is important. * \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 /*! \brief Deinitialise SPI instances
* \ingroup hardware_spi * \ingroup hardware_spi

View File

@ -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); 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_reset(spi);
spi_unreset(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); spi_set_format(spi, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
// Always enable DREQ signals -- harmless if DMA is not listening // 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); 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 // Finally enable the SPI
hw_set_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_SSE_BITS); hw_set_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_SSE_BITS);
return baud;
} }
void spi_deinit(spi_inst_t *spi) { void spi_deinit(spi_inst_t *spi) {