From ecc002cb7abe142f48b3a4d7a2e262a340224f2a Mon Sep 17 00:00:00 2001 From: Rene Greiner Date: Tue, 11 May 2021 11:58:13 +0200 Subject: [PATCH 1/7] added spi_get_baudrate() + some consistency changes - new function to query the current baudrate + consistency changes - return a boolean for functions with a "is; has; can" in name - return unsigned value for number of bytes read/written - some const correctness --- .../hardware_spi/include/hardware/spi.h | 49 +++++++++++++------ src/rp2_common/hardware_spi/spi.c | 33 +++++++------ 2 files changed, 54 insertions(+), 28 deletions(-) diff --git a/src/rp2_common/hardware_spi/include/hardware/spi.h b/src/rp2_common/hardware_spi/include/hardware/spi.h index 595a9226c..0669c5101 100644 --- a/src/rp2_common/hardware_spi/include/hardware/spi.h +++ b/src/rp2_common/hardware_spi/include/hardware/spi.h @@ -132,18 +132,29 @@ void spi_deinit(spi_inst_t *spi); */ uint spi_set_baudrate(spi_inst_t *spi, uint baudrate); +/*! \brief Get SPI baudrate + * \ingroup hardware_spi + * + * Get SPI baudrate which was set by \see spi_set_baudrate + * achieved rate. + * + * \param spi SPI instance specifier, either \ref spi0 or \ref spi1 + * \return The actual baudrate set + */ +uint spi_get_baudrate(const spi_inst_t *spi); + /*! \brief Convert SPI instance to hardware instance number * \ingroup hardware_spi * * \param spi SPI instance * \return Number of SPI, 0 or 1. */ -static inline uint spi_get_index(spi_inst_t *spi) { +static inline uint spi_get_index(const spi_inst_t *spi) { invalid_params_if(SPI, spi != spi0 && spi != spi1); return spi == spi1 ? 1 : 0; } -static inline spi_hw_t *spi_get_hw(spi_inst_t *spi) { +static inline spi_hw_t *spi_get_hw(const spi_inst_t *spi) { spi_get_index(spi); // check it is a hw spi return (spi_hw_t *)spi; } @@ -197,27 +208,37 @@ static inline void spi_set_slave(spi_inst_t *spi, bool slave) { * \ingroup hardware_spi * * \param spi SPI instance specifier, either \ref spi0 or \ref spi1 - * \return 0 if no space is available to write. Non-zero if a write is possible + * \return false if no space is available to write. True if a write is possible * * \note Although the controllers each have a 8 deep TX FIFO, the current HW implementation can only return 0 or 1 * rather than the space available. */ -static inline size_t spi_is_writable(spi_inst_t *spi) { +static inline bool spi_is_writable(const spi_inst_t *spi) { // PL022 doesn't expose levels directly, so return values are only 0 or 1 - return (spi_get_hw(spi)->sr & SPI_SSPSR_TNF_BITS) >> SPI_SSPSR_TNF_LSB; + return (spi_get_hw(spi)->sr & SPI_SSPSR_TNF_BITS); } /*! \brief Check whether a read can be done on SPI device * \ingroup hardware_spi * * \param spi SPI instance specifier, either \ref spi0 or \ref spi1 - * \return Non-zero if a read is possible i.e. data is present + * \return true if a read is possible i.e. data is present * * \note Although the controllers each have a 8 deep RX FIFO, the current HW implementation can only return 0 or 1 * rather than the data available. */ -static inline size_t spi_is_readable(spi_inst_t *spi) { - return (spi_get_hw(spi)->sr & SPI_SSPSR_RNE_BITS) >> SPI_SSPSR_RNE_LSB; +static inline bool spi_is_readable(const spi_inst_t *spi) { + return (spi_get_hw(spi)->sr & SPI_SSPSR_RNE_BITS); +} + +/*! \brief Check whether SPI is busy + * \ingroup hardware_spi + * + * \param spi SPI instance specifier, either \ref spi0 or \ref spi1 + * \return true if SPI is busy + */ +static inline bool spi_is_busy(const spi_inst_t *spi) { + return (spi_get_hw(spi)->sr & SPI_SSPSR_BSY_BITS); } /*! \brief Write/Read to/from an SPI device @@ -232,7 +253,7 @@ static inline size_t spi_is_readable(spi_inst_t *spi) { * \param len Length of BOTH buffers * \return Number of bytes written/read */ -int spi_write_read_blocking(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len); +size_t spi_write_read_blocking(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len); /*! \brief Write to an SPI device, blocking * \ingroup hardware_spi @@ -245,7 +266,7 @@ int spi_write_read_blocking(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, s * \param len Length of \p src * \return Number of bytes written/read */ -int spi_write_blocking(spi_inst_t *spi, const uint8_t *src, size_t len); +size_t spi_write_blocking(spi_inst_t *spi, const uint8_t *src, size_t len); /*! \brief Read from an SPI device * \ingroup hardware_spi @@ -262,7 +283,7 @@ int spi_write_blocking(spi_inst_t *spi, const uint8_t *src, size_t len); * \param len Length of buffer \p dst * \return Number of bytes written/read */ -int spi_read_blocking(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len); +size_t spi_read_blocking(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len); // ---------------------------------------------------------------------------- // SPI-specific operations and aliases @@ -283,7 +304,7 @@ int spi_read_blocking(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, s * \param len Length of BOTH buffers in halfwords * \return Number of halfwords written/read */ -int spi_write16_read16_blocking(spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len); +size_t spi_write16_read16_blocking(spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len); /*! \brief Write to an SPI device * \ingroup hardware_spi @@ -298,7 +319,7 @@ int spi_write16_read16_blocking(spi_inst_t *spi, const uint16_t *src, uint16_t * * \param len Length of buffers * \return Number of halfwords written/read */ -int spi_write16_blocking(spi_inst_t *spi, const uint16_t *src, size_t len); +size_t spi_write16_blocking(spi_inst_t *spi, const uint16_t *src, size_t len); /*! \brief Read from an SPI device * \ingroup hardware_spi @@ -317,7 +338,7 @@ int spi_write16_blocking(spi_inst_t *spi, const uint16_t *src, size_t len); * \param len Length of buffer \p dst in halfwords * \return Number of halfwords written/read */ -int spi_read16_blocking(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len); +size_t spi_read16_blocking(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len); #ifdef __cplusplus } diff --git a/src/rp2_common/hardware_spi/spi.c b/src/rp2_common/hardware_spi/spi.c index bc598452a..2b7358403 100644 --- a/src/rp2_common/hardware_spi/spi.c +++ b/src/rp2_common/hardware_spi/spi.c @@ -22,11 +22,10 @@ void spi_init(spi_inst_t *spi, uint baudrate) { spi_reset(spi); spi_unreset(spi); - (void) spi_set_baudrate(spi, baudrate); + 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); - spi_set_format(spi, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST); // Finally enable the SPI hw_set_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_SSE_BITS); @@ -65,9 +64,15 @@ uint spi_set_baudrate(spi_inst_t *spi, uint baudrate) { return freq_in / (prescale * postdiv); } +uint spi_get_baudrate(const spi_inst_t *spi) { + uint prescale = spi_get_hw(spi)->cpsr; + uint postdiv = ((spi_get_hw(spi)->cr0 & SPI_SSPCR0_SCR_BITS) >> SPI_SSPCR0_SCR_LSB) + 1; + return clock_get_hz(clk_peri) / (prescale * postdiv); +} + // Write len bytes from src to SPI. Simultaneously read len bytes from SPI to dst. // Note this function is guaranteed to exit in a known amount of time (bits sent * time per bit) -int __not_in_flash_func(spi_write_read_blocking)(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len) { +size_t __not_in_flash_func(spi_write_read_blocking)(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len) { invalid_params_if(SPI, 0 > (int)len); // Never have more transfers in flight than will fit into the RX FIFO, @@ -86,11 +91,11 @@ int __not_in_flash_func(spi_write_read_blocking)(spi_inst_t *spi, const uint8_t } } - return (int)len; + return len; } // Write len bytes directly from src to the SPI, and discard any data received back -int __not_in_flash_func(spi_write_blocking)(spi_inst_t *spi, const uint8_t *src, size_t len) { +size_t __not_in_flash_func(spi_write_blocking)(spi_inst_t *spi, const uint8_t *src, size_t len) { invalid_params_if(SPI, 0 > (int)len); // Write to TX FIFO whilst ignoring RX, then clean up afterward. When RX // is full, PL022 inhibits RX pushes, and sets a sticky flag on @@ -112,14 +117,14 @@ int __not_in_flash_func(spi_write_blocking)(spi_inst_t *spi, const uint8_t *src, // Don't leave overrun flag set spi_get_hw(spi)->icr = SPI_SSPICR_RORIC_BITS; - return (int)len; + return len; } // Read len bytes directly from the SPI to dst. // repeated_tx_data is output repeatedly on SO as data is read in from SI. // Generally this can be 0, but some devices require a specific value here, // e.g. SD cards expect 0xff -int __not_in_flash_func(spi_read_blocking)(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len) { +size_t __not_in_flash_func(spi_read_blocking)(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len) { invalid_params_if(SPI, 0 > (int)len); const size_t fifo_depth = 8; size_t rx_remaining = len, tx_remaining = len; @@ -135,11 +140,11 @@ int __not_in_flash_func(spi_read_blocking)(spi_inst_t *spi, uint8_t repeated_tx_ } } - return (int)len; + return len; } // Write len halfwords from src to SPI. Simultaneously read len halfwords from SPI to dst. -int __not_in_flash_func(spi_write16_read16_blocking)(spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len) { +size_t __not_in_flash_func(spi_write16_read16_blocking)(spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len) { invalid_params_if(SPI, 0 > (int)len); // Never have more transfers in flight than will fit into the RX FIFO, // else FIFO will overflow if this code is heavily interrupted. @@ -157,11 +162,11 @@ int __not_in_flash_func(spi_write16_read16_blocking)(spi_inst_t *spi, const uint } } - return (int)len; + return len; } // Write len bytes directly from src to the SPI, and discard any data received back -int __not_in_flash_func(spi_write16_blocking)(spi_inst_t *spi, const uint16_t *src, size_t len) { +size_t __not_in_flash_func(spi_write16_blocking)(spi_inst_t *spi, const uint16_t *src, size_t len) { invalid_params_if(SPI, 0 > (int)len); // Deliberately overflow FIFO, then clean up afterward, to minimise amount // of APB polling required per halfword @@ -181,12 +186,12 @@ int __not_in_flash_func(spi_write16_blocking)(spi_inst_t *spi, const uint16_t *s // Don't leave overrun flag set spi_get_hw(spi)->icr = SPI_SSPICR_RORIC_BITS; - return (int)len; + return len; } // Read len halfwords directly from the SPI to dst. // repeated_tx_data is output repeatedly on SO as data is read in from SI. -int __not_in_flash_func(spi_read16_blocking)(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len) { +size_t __not_in_flash_func(spi_read16_blocking)(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len) { invalid_params_if(SPI, 0 > (int)len); const size_t fifo_depth = 8; size_t rx_remaining = len, tx_remaining = len; @@ -202,5 +207,5 @@ int __not_in_flash_func(spi_read16_blocking)(spi_inst_t *spi, uint16_t repeated_ } } - return (int)len; + return len; } From f385948df3b7f3da0dc07aaa2b1f94af4ba3636e Mon Sep 17 00:00:00 2001 From: Rene Greiner Date: Tue, 11 May 2021 13:03:16 +0200 Subject: [PATCH 2/7] fixed docu --- src/rp2_common/hardware_spi/include/hardware/spi.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rp2_common/hardware_spi/include/hardware/spi.h b/src/rp2_common/hardware_spi/include/hardware/spi.h index 0669c5101..11716e56d 100644 --- a/src/rp2_common/hardware_spi/include/hardware/spi.h +++ b/src/rp2_common/hardware_spi/include/hardware/spi.h @@ -136,7 +136,6 @@ uint spi_set_baudrate(spi_inst_t *spi, uint baudrate); * \ingroup hardware_spi * * Get SPI baudrate which was set by \see spi_set_baudrate - * achieved rate. * * \param spi SPI instance specifier, either \ref spi0 or \ref spi1 * \return The actual baudrate set From b43ac69aa03354f3e2e1826ab8669cc3f0460a08 Mon Sep 17 00:00:00 2001 From: Rene Greiner Date: Wed, 12 May 2021 23:53:06 +0200 Subject: [PATCH 3/7] Update spi.h removed misleading docu --- src/rp2_common/hardware_spi/include/hardware/spi.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/rp2_common/hardware_spi/include/hardware/spi.h b/src/rp2_common/hardware_spi/include/hardware/spi.h index 11716e56d..aabcfe57f 100644 --- a/src/rp2_common/hardware_spi/include/hardware/spi.h +++ b/src/rp2_common/hardware_spi/include/hardware/spi.h @@ -209,11 +209,8 @@ static inline void spi_set_slave(spi_inst_t *spi, bool slave) { * \param spi SPI instance specifier, either \ref spi0 or \ref spi1 * \return false if no space is available to write. True if a write is possible * - * \note Although the controllers each have a 8 deep TX FIFO, the current HW implementation can only return 0 or 1 - * rather than the space available. */ static inline bool spi_is_writable(const spi_inst_t *spi) { - // PL022 doesn't expose levels directly, so return values are only 0 or 1 return (spi_get_hw(spi)->sr & SPI_SSPSR_TNF_BITS); } From 05224d2d99cb83d7851e6847e417551378d13eca Mon Sep 17 00:00:00 2001 From: Rene Greiner Date: Thu, 13 May 2021 00:12:21 +0200 Subject: [PATCH 4/7] another misleading docu removed --- src/rp2_common/hardware_spi/include/hardware/spi.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/rp2_common/hardware_spi/include/hardware/spi.h b/src/rp2_common/hardware_spi/include/hardware/spi.h index 359ccc483..03a555830 100644 --- a/src/rp2_common/hardware_spi/include/hardware/spi.h +++ b/src/rp2_common/hardware_spi/include/hardware/spi.h @@ -220,8 +220,6 @@ static inline bool spi_is_writable(const spi_inst_t *spi) { * \param spi SPI instance specifier, either \ref spi0 or \ref spi1 * \return true if a read is possible i.e. data is present * - * \note Although the controllers each have a 8 deep RX FIFO, the current HW implementation can only return 0 or 1 - * rather than the data available. */ static inline bool spi_is_readable(const spi_inst_t *spi) { return (spi_get_hw(spi)->sr & SPI_SSPSR_RNE_BITS); From 5a043a287aba299ee92211bb157560d815fb0603 Mon Sep 17 00:00:00 2001 From: Rene Greiner Date: Thu, 13 May 2021 19:33:30 +0200 Subject: [PATCH 5/7] improved const correctness --- .../hardware_spi/include/hardware/spi.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/rp2_common/hardware_spi/include/hardware/spi.h b/src/rp2_common/hardware_spi/include/hardware/spi.h index 03a555830..cb0f0d4a3 100644 --- a/src/rp2_common/hardware_spi/include/hardware/spi.h +++ b/src/rp2_common/hardware_spi/include/hardware/spi.h @@ -153,11 +153,16 @@ static inline uint spi_get_index(const spi_inst_t *spi) { return spi == spi1 ? 1 : 0; } -static inline spi_hw_t *spi_get_hw(const spi_inst_t *spi) { +static inline spi_hw_t *spi_get_hw(spi_inst_t *spi) { spi_get_index(spi); // check it is a hw spi return (spi_hw_t *)spi; } +static inline const spi_hw_t *spi_get_const_hw(const spi_inst_t *spi) { + spi_get_index(spi); // check it is a hw spi + return (spi_hw_t *)spi; +} + /*! \brief Configure SPI * \ingroup hardware_spi * @@ -208,10 +213,9 @@ static inline void spi_set_slave(spi_inst_t *spi, bool slave) { * * \param spi SPI instance specifier, either \ref spi0 or \ref spi1 * \return false if no space is available to write. True if a write is possible - * */ static inline bool spi_is_writable(const spi_inst_t *spi) { - return (spi_get_hw(spi)->sr & SPI_SSPSR_TNF_BITS); + return (spi_get_const_hw(spi)->sr & SPI_SSPSR_TNF_BITS); } /*! \brief Check whether a read can be done on SPI device @@ -219,10 +223,9 @@ static inline bool spi_is_writable(const spi_inst_t *spi) { * * \param spi SPI instance specifier, either \ref spi0 or \ref spi1 * \return true if a read is possible i.e. data is present - * */ static inline bool spi_is_readable(const spi_inst_t *spi) { - return (spi_get_hw(spi)->sr & SPI_SSPSR_RNE_BITS); + return (spi_get_const_hw(spi)->sr & SPI_SSPSR_RNE_BITS); } /*! \brief Check whether SPI is busy @@ -232,7 +235,7 @@ static inline bool spi_is_readable(const spi_inst_t *spi) { * \return true if SPI is busy */ static inline bool spi_is_busy(const spi_inst_t *spi) { - return (spi_get_hw(spi)->sr & SPI_SSPSR_BSY_BITS); + return (spi_get_const_hw(spi)->sr & SPI_SSPSR_BSY_BITS); } /*! \brief Write/Read to/from an SPI device From b0e2b1e73d35de3d381c5f2e2a7feb5dc22240db Mon Sep 17 00:00:00 2001 From: Rene Greiner Date: Fri, 14 May 2021 10:48:33 +0200 Subject: [PATCH 6/7] fixed kitchen_sink issues --- src/rp2_common/hardware_spi/include/hardware/spi.h | 2 +- src/rp2_common/hardware_spi/spi.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rp2_common/hardware_spi/include/hardware/spi.h b/src/rp2_common/hardware_spi/include/hardware/spi.h index cb0f0d4a3..f54275bf7 100644 --- a/src/rp2_common/hardware_spi/include/hardware/spi.h +++ b/src/rp2_common/hardware_spi/include/hardware/spi.h @@ -160,7 +160,7 @@ static inline spi_hw_t *spi_get_hw(spi_inst_t *spi) { static inline const spi_hw_t *spi_get_const_hw(const spi_inst_t *spi) { spi_get_index(spi); // check it is a hw spi - return (spi_hw_t *)spi; + return (const spi_hw_t *)spi; } /*! \brief Configure SPI diff --git a/src/rp2_common/hardware_spi/spi.c b/src/rp2_common/hardware_spi/spi.c index 22a6250ec..27d1c8887 100644 --- a/src/rp2_common/hardware_spi/spi.c +++ b/src/rp2_common/hardware_spi/spi.c @@ -66,8 +66,8 @@ uint spi_set_baudrate(spi_inst_t *spi, uint baudrate) { } uint spi_get_baudrate(const spi_inst_t *spi) { - uint prescale = spi_get_hw(spi)->cpsr; - uint postdiv = ((spi_get_hw(spi)->cr0 & SPI_SSPCR0_SCR_BITS) >> SPI_SSPCR0_SCR_LSB) + 1; + uint prescale = spi_get_const_hw(spi)->cpsr; + uint postdiv = ((spi_get_const_hw(spi)->cr0 & SPI_SSPCR0_SCR_BITS) >> SPI_SSPCR0_SCR_LSB) + 1; return clock_get_hz(clk_peri) / (prescale * postdiv); } From 5b2f444d917f0308f2941ce2fe63a7cd6245e902 Mon Sep 17 00:00:00 2001 From: Rene Greiner Date: Fri, 14 May 2021 18:58:37 +0200 Subject: [PATCH 7/7] reverted size_t return value to int --- .../hardware_spi/include/hardware/spi.h | 12 +++++----- src/rp2_common/hardware_spi/spi.c | 24 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/rp2_common/hardware_spi/include/hardware/spi.h b/src/rp2_common/hardware_spi/include/hardware/spi.h index f54275bf7..b766f74f1 100644 --- a/src/rp2_common/hardware_spi/include/hardware/spi.h +++ b/src/rp2_common/hardware_spi/include/hardware/spi.h @@ -250,7 +250,7 @@ static inline bool spi_is_busy(const spi_inst_t *spi) { * \param len Length of BOTH buffers * \return Number of bytes written/read */ -size_t spi_write_read_blocking(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len); +int spi_write_read_blocking(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len); /*! \brief Write to an SPI device, blocking * \ingroup hardware_spi @@ -263,7 +263,7 @@ size_t spi_write_read_blocking(spi_inst_t *spi, const uint8_t *src, uint8_t *dst * \param len Length of \p src * \return Number of bytes written/read */ -size_t spi_write_blocking(spi_inst_t *spi, const uint8_t *src, size_t len); +int spi_write_blocking(spi_inst_t *spi, const uint8_t *src, size_t len); /*! \brief Read from an SPI device * \ingroup hardware_spi @@ -280,7 +280,7 @@ size_t spi_write_blocking(spi_inst_t *spi, const uint8_t *src, size_t len); * \param len Length of buffer \p dst * \return Number of bytes written/read */ -size_t spi_read_blocking(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len); +int spi_read_blocking(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len); // ---------------------------------------------------------------------------- // SPI-specific operations and aliases @@ -301,7 +301,7 @@ size_t spi_read_blocking(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst * \param len Length of BOTH buffers in halfwords * \return Number of halfwords written/read */ -size_t spi_write16_read16_blocking(spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len); +int spi_write16_read16_blocking(spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len); /*! \brief Write to an SPI device * \ingroup hardware_spi @@ -316,7 +316,7 @@ size_t spi_write16_read16_blocking(spi_inst_t *spi, const uint16_t *src, uint16_ * \param len Length of buffers * \return Number of halfwords written/read */ -size_t spi_write16_blocking(spi_inst_t *spi, const uint16_t *src, size_t len); +int spi_write16_blocking(spi_inst_t *spi, const uint16_t *src, size_t len); /*! \brief Read from an SPI device * \ingroup hardware_spi @@ -335,7 +335,7 @@ size_t spi_write16_blocking(spi_inst_t *spi, const uint16_t *src, size_t len); * \param len Length of buffer \p dst in halfwords * \return Number of halfwords written/read */ -size_t spi_read16_blocking(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len); +int spi_read16_blocking(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len); #ifdef __cplusplus } diff --git a/src/rp2_common/hardware_spi/spi.c b/src/rp2_common/hardware_spi/spi.c index 27d1c8887..880b53431 100644 --- a/src/rp2_common/hardware_spi/spi.c +++ b/src/rp2_common/hardware_spi/spi.c @@ -73,7 +73,7 @@ uint spi_get_baudrate(const spi_inst_t *spi) { // Write len bytes from src to SPI. Simultaneously read len bytes from SPI to dst. // Note this function is guaranteed to exit in a known amount of time (bits sent * time per bit) -size_t __not_in_flash_func(spi_write_read_blocking)(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len) { +int __not_in_flash_func(spi_write_read_blocking)(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len) { invalid_params_if(SPI, 0 > (int)len); // Never have more transfers in flight than will fit into the RX FIFO, @@ -92,11 +92,11 @@ size_t __not_in_flash_func(spi_write_read_blocking)(spi_inst_t *spi, const uint8 } } - return len; + return (int)len; } // Write len bytes directly from src to the SPI, and discard any data received back -size_t __not_in_flash_func(spi_write_blocking)(spi_inst_t *spi, const uint8_t *src, size_t len) { +int __not_in_flash_func(spi_write_blocking)(spi_inst_t *spi, const uint8_t *src, size_t len) { invalid_params_if(SPI, 0 > (int)len); // Write to TX FIFO whilst ignoring RX, then clean up afterward. When RX // is full, PL022 inhibits RX pushes, and sets a sticky flag on @@ -118,14 +118,14 @@ size_t __not_in_flash_func(spi_write_blocking)(spi_inst_t *spi, const uint8_t *s // Don't leave overrun flag set spi_get_hw(spi)->icr = SPI_SSPICR_RORIC_BITS; - return len; + return (int)len; } // Read len bytes directly from the SPI to dst. // repeated_tx_data is output repeatedly on SO as data is read in from SI. // Generally this can be 0, but some devices require a specific value here, // e.g. SD cards expect 0xff -size_t __not_in_flash_func(spi_read_blocking)(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len) { +int __not_in_flash_func(spi_read_blocking)(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len) { invalid_params_if(SPI, 0 > (int)len); const size_t fifo_depth = 8; size_t rx_remaining = len, tx_remaining = len; @@ -141,11 +141,11 @@ size_t __not_in_flash_func(spi_read_blocking)(spi_inst_t *spi, uint8_t repeated_ } } - return len; + return (int)len; } // Write len halfwords from src to SPI. Simultaneously read len halfwords from SPI to dst. -size_t __not_in_flash_func(spi_write16_read16_blocking)(spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len) { +int __not_in_flash_func(spi_write16_read16_blocking)(spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len) { invalid_params_if(SPI, 0 > (int)len); // Never have more transfers in flight than will fit into the RX FIFO, // else FIFO will overflow if this code is heavily interrupted. @@ -163,11 +163,11 @@ size_t __not_in_flash_func(spi_write16_read16_blocking)(spi_inst_t *spi, const u } } - return len; + return (int)len; } // Write len bytes directly from src to the SPI, and discard any data received back -size_t __not_in_flash_func(spi_write16_blocking)(spi_inst_t *spi, const uint16_t *src, size_t len) { +int __not_in_flash_func(spi_write16_blocking)(spi_inst_t *spi, const uint16_t *src, size_t len) { invalid_params_if(SPI, 0 > (int)len); // Deliberately overflow FIFO, then clean up afterward, to minimise amount // of APB polling required per halfword @@ -187,12 +187,12 @@ size_t __not_in_flash_func(spi_write16_blocking)(spi_inst_t *spi, const uint16_t // Don't leave overrun flag set spi_get_hw(spi)->icr = SPI_SSPICR_RORIC_BITS; - return len; + return (int)len; } // Read len halfwords directly from the SPI to dst. // repeated_tx_data is output repeatedly on SO as data is read in from SI. -size_t __not_in_flash_func(spi_read16_blocking)(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len) { +int __not_in_flash_func(spi_read16_blocking)(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len) { invalid_params_if(SPI, 0 > (int)len); const size_t fifo_depth = 8; size_t rx_remaining = len, tx_remaining = len; @@ -208,5 +208,5 @@ size_t __not_in_flash_func(spi_read16_blocking)(spi_inst_t *spi, uint16_t repeat } } - return len; + return (int)len; }