From 1e4eaf089e2b99737e825e41bf4373cd9fb76a5a Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sat, 17 Apr 2021 08:16:37 +0000 Subject: [PATCH] More SPI code, DMA fixes. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@14210 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- demos/RP/RT-RP2040-PICO/cfg/mcuconf.h | 4 ++ os/hal/ports/RP/LLD/DMAv1/rp_dma.c | 6 +- os/hal/ports/RP/LLD/DMAv1/rp_dma.h | 4 +- os/hal/ports/RP/LLD/SPIv1/hal_spi_lld.c | 64 ++++++++++++++++- os/hal/ports/RP/LLD/SPIv1/hal_spi_lld.h | 94 +++++++++++-------------- 5 files changed, 111 insertions(+), 61 deletions(-) diff --git a/demos/RP/RT-RP2040-PICO/cfg/mcuconf.h b/demos/RP/RT-RP2040-PICO/cfg/mcuconf.h index 3ab789399..a2e8454c9 100644 --- a/demos/RP/RT-RP2040-PICO/cfg/mcuconf.h +++ b/demos/RP/RT-RP2040-PICO/cfg/mcuconf.h @@ -62,6 +62,10 @@ */ #define RP_SPI_USE_SPI0 TRUE #define RP_SPI_USE_SPI1 TRUE +#define RP_SPI_SPI0_RX_DMA_CHANNEL RP_DMA_CHANNEL_ID_ANY +#define RP_SPI_SPI0_TX_DMA_CHANNEL RP_DMA_CHANNEL_ID_ANY +#define RP_SPI_SPI1_RX_DMA_CHANNEL RP_DMA_CHANNEL_ID_ANY +#define RP_SPI_SPI1_TX_DMA_CHANNEL RP_DMA_CHANNEL_ID_ANY #define RP_SPI_SPI0_DMA_PRIORITY 1 #define RP_SPI_SPI1_DMA_PRIORITY 1 diff --git a/os/hal/ports/RP/LLD/DMAv1/rp_dma.c b/os/hal/ports/RP/LLD/DMAv1/rp_dma.c index 9b3b31bd1..03f0fd30a 100644 --- a/os/hal/ports/RP/LLD/DMAv1/rp_dma.c +++ b/os/hal/ports/RP/LLD/DMAv1/rp_dma.c @@ -257,13 +257,13 @@ const rp_dma_channel_t *dmaChannelAllocI(uint32_t id, osalDbgCheckClassI(); - if (id < RP_DMA_STREAM_ID_ANY) { + if (id < RP_DMA_CHANNEL_ID_ANY) { startid = id; endid = id; } - else if (id == RP_DMA_STREAM_ID_ANY) { + else if (id == RP_DMA_CHANNEL_ID_ANY) { startid = 0U; - endid = RP_DMA_STREAM_ID_ANY - 1U; + endid = RP_DMA_CHANNEL_ID_ANY - 1U; } else { osalDbgCheck(false); diff --git a/os/hal/ports/RP/LLD/DMAv1/rp_dma.h b/os/hal/ports/RP/LLD/DMAv1/rp_dma.h index 25426d045..60c065fb2 100644 --- a/os/hal/ports/RP/LLD/DMAv1/rp_dma.h +++ b/os/hal/ports/RP/LLD/DMAv1/rp_dma.h @@ -58,7 +58,7 @@ /** * @brief Any channel selector. */ -#define RP_DMA_STREAM_ID_ANY RP_DMA_CHANNELS +#define RP_DMA_CHANNEL_ID_ANY RP_DMA_CHANNELS /** * @brief Returns a pointer to a @p rp_dma_channel_t structure. @@ -67,7 +67,7 @@ * @return A pointer to the @p rp_dma_channel_t constant structure * associated to the DMA channel. */ -#define RP_DMA_CHANNEL(id) (&__rp_dma_channels[id]) +#define RP_DMA_CHANNEL(id) (&__rp_dma_channels[id]) /*===========================================================================*/ /* Driver pre-compile time settings. */ diff --git a/os/hal/ports/RP/LLD/SPIv1/hal_spi_lld.c b/os/hal/ports/RP/LLD/SPIv1/hal_spi_lld.c index 4bcb0d5a6..59cd2c625 100644 --- a/os/hal/ports/RP/LLD/SPIv1/hal_spi_lld.c +++ b/os/hal/ports/RP/LLD/SPIv1/hal_spi_lld.c @@ -56,6 +56,30 @@ SPIDriver SPID1; /* Driver local functions. */ /*===========================================================================*/ +/** + * @brief Shared end-of-rx service routine. + * + * @param[in] spip pointer to the @p SPIDriver object + * @param[in] ct content of the CTRL_TRIG register + */ +static void spi_lld_serve_rx_interrupt(SPIDriver *spip, uint32_t ct) { + + (void)spip; + (void)ct; +} + +/** + * @brief Shared end-of-tx service routine. + * + * @param[in] spip pointer to the @p SPIDriver object + * @param[in] ct content of the CTRL_TRIG register + */ +static void spi_lld_serve_tx_interrupt(SPIDriver *spip, uint32_t ct) { + + (void)spip; + (void)ct; +} + /*===========================================================================*/ /* Driver interrupt handlers. */ /*===========================================================================*/ @@ -106,11 +130,45 @@ void spi_lld_start(SPIDriver *spip) { if (spip->state == SPI_STOP) { /* Enables the peripheral.*/ -#if RP_SPI_USE_SPI1 == TRUE - if (&SPID1 == spip) { - + if (false) { + } +#if RP_SPI_USE_SPI0 == TRUE + if (&SPID0 == spip) { + spip->dmarx = dmaChannelAllocI(RP_SPI_SPI0_RX_DMA_CHANNEL, + RP_IRQ_SPI0_PRIORITY, + (rp_dmaisr_t)spi_lld_serve_rx_interrupt, + (void *)spip); + osalDbgAssert(spip->dmarx != NULL, "unable to allocate stream"); + spip->dmatx = dmaChannelAllocI(RP_SPI_SPI0_TX_DMA_CHANNEL, + RP_IRQ_SPI0_PRIORITY, + (rp_dmaisr_t)spi_lld_serve_tx_interrupt, + (void *)spip); + osalDbgAssert(spip->dmatx != NULL, "unable to allocate stream"); + dmaChannelEnableInterruptX(spip->dmarx); + dmaChannelEnableInterruptX(spip->dmatx); + hal_lld_peripheral_unreset(RESETS_ALLREG_SPI0); } #endif +#if RP_SPI_USE_SPI1 == TRUE + if (&SPID1 == spip) { + spip->dmarx = dmaChannelAllocI(RP_SPI_SPI1_RX_DMA_CHANNEL, + RP_IRQ_SPI1_PRIORITY, + (rp_dmaisr_t)spi_lld_serve_rx_interrupt, + (void *)spip); + osalDbgAssert(spip->dmarx != NULL, "unable to allocate stream"); + spip->dmatx = dmaChannelAllocI(RP_SPI_SPI1_TX_DMA_CHANNEL, + RP_IRQ_SPI1_PRIORITY, + (rp_dmaisr_t)spi_lld_serve_tx_interrupt, + (void *)spip); + osalDbgAssert(spip->dmatx != NULL, "unable to allocate stream"); + dmaChannelEnableInterruptX(spip->dmarx); + dmaChannelEnableInterruptX(spip->dmatx); + hal_lld_peripheral_unreset(RESETS_ALLREG_SPI1); + } +#endif + else { + osalDbgAssert(false, "invalid SPI instance"); + } } /* Configures the peripheral.*/ diff --git a/os/hal/ports/RP/LLD/SPIv1/hal_spi_lld.h b/os/hal/ports/RP/LLD/SPIv1/hal_spi_lld.h index a05a25692..30c2eb08a 100644 --- a/os/hal/ports/RP/LLD/SPIv1/hal_spi_lld.h +++ b/os/hal/ports/RP/LLD/SPIv1/hal_spi_lld.h @@ -40,59 +40,6 @@ /* Driver pre-compile time settings. */ /*===========================================================================*/ -/** - * @name RP configuration options - * @{ - */ -/** - * @brief SPI0 driver enable switch. - * @details If set to @p TRUE the support for SPI1 is included. - * @note The default is @p FALSE. - */ -#if !defined(RP_SPI_USE_SPI0) || defined(__DOXYGEN__) -#define RP_SPI_USE_SPI0 FALSE -#endif - -/** - * @brief SPI1 driver enable switch. - * @details If set to @p TRUE the support for SPI1 is included. - * @note The default is @p FALSE. - */ -#if !defined(RP_SPI_USE_SPI1) || defined(__DOXYGEN__) -#define RP_SPI_USE_SPI1 FALSE -#endif - -/** - * @brief SPI0 interrupt priority level setting. - */ -#if !defined(RP_IRQ_SPI0_PRIORITY) || defined(__DOXYGEN__) -#define RP_IRQ_SPI0_PRIORITY 2 -#endif - -/** - * @brief SPI1 interrupt priority level setting. - */ -#if !defined(RP_IRQ_SPI1_PRIORITY) || defined(__DOXYGEN__) -#define RP_IRQ_SPI1_PRIORITY 2 -#endif - -/** - * @brief SPI0 DMA priority (0..1|lowest..highest). - * @note The priority level is used for both the TX and RX DMA streams. - */ -#if !defined(RP_SPI_SPI0_DMA_PRIORITY) || defined(__DOXYGEN__) -#define RP_SPI_SPI0_DMA_PRIORITY 1 -#endif - -/** - * @brief SPI1 DMA priority (0..1|lowest..highest). - * @note The priority level is used for both the TX and RX DMA streams. - */ -#if !defined(RP_SPI_SPI1_DMA_PRIORITY) || defined(__DOXYGEN__) -#define RP_SPI_SPI1_DMA_PRIORITY 1 -#endif -/** @} */ - /*===========================================================================*/ /* Derived constants and error checks. */ /*===========================================================================*/ @@ -106,6 +53,47 @@ #error "RP_HAS_SPI1 not defined in registry" #endif +/* Mcuconf.h checks.*/ +#if !defined(RP_SPI_USE_SPI0) +#error "RP_SPI_USE_SPI0 not defined in mcuconf.h" +#endif + +#if !defined(RP_SPI_USE_SPI1) +#error "RP_SPI_USE_SPI1 not defined in mcuconf.h" +#endif + +#if !defined(RP_IRQ_SPI0_PRIORITY) +#error "RP_IRQ_SPI0_PRIORITY not defined in mcuconf.h" +#endif + +#if !defined(RP_IRQ_SPI1_PRIORITY) +#error "RP_IRQ_SPI1_PRIORITY not defined in mcuconf.h" +#endif + +#if !defined(RP_SPI_SPI0_RX_DMA_CHANNEL) +#error "RP_SPI_SPI0_RX_DMA_CHANNEL not defined in mcuconf.h" +#endif + +#if !defined(RP_SPI_SPI0_TX_DMA_CHANNEL) +#error "RP_SPI_SPI0_TX_DMA_CHANNEL not defined in mcuconf.h" +#endif + +#if !defined(RP_SPI_SPI1_RX_DMA_CHANNEL) +#error "RP_SPI_SPI0_RX_DMA_CHANNEL not defined in mcuconf.h" +#endif + +#if !defined(RP_SPI_SPI1_TX_DMA_CHANNEL) +#error "RP_SPI_SPI0_TX_DMA_CHANNEL not defined in mcuconf.h" +#endif + +#if !defined(RP_SPI_SPI0_DMA_PRIORITY) +#error "RP_SPI_SPI0_DMA_PRIORITY not defined in mcuconf.h" +#endif + +#if !defined(RP_SPI_SPI1_DMA_PRIORITY) +#error "RP_SPI_SPI1_DMA_PRIORITY not defined in mcuconf.h" +#endif + /* Device selection checks.*/ #if RP_SPI_USE_SPI0 && !RP_HAS_SPI0 #error "SPI0 not present in the selected device"