From 326d76ebdff7d188c276b338b47076a1134d297b Mon Sep 17 00:00:00 2001 From: Armando Date: Tue, 26 Jan 2021 20:18:52 +0800 Subject: [PATCH] spi: add dma channel auto-alloc feature on esp32 --- components/driver/include/driver/spi_common.h | 12 +- .../include/driver/spi_common_internal.h | 22 +--- components/driver/include/driver/spi_slave.h | 10 +- .../driver/include/driver/spi_slave_hd.h | 2 +- components/driver/spi_common.c | 104 +++++++++++++----- components/driver/spi_slave.c | 28 ++--- components/driver/spi_slave_hd.c | 26 +++-- 7 files changed, 120 insertions(+), 84 deletions(-) diff --git a/components/driver/include/driver/spi_common.h b/components/driver/include/driver/spi_common.h index da12bb36ae..cbb09ca7f8 100644 --- a/components/driver/include/driver/spi_common.h +++ b/components/driver/include/driver/spi_common.h @@ -105,11 +105,11 @@ typedef struct { * * @param host_id SPI peripheral that controls this bus * @param bus_config Pointer to a spi_bus_config_t struct specifying how the host should be initialized - * @param dma_chan Either channel 1 or 2, or 0 in the case when no DMA is required. Selecting a DMA channel - * for a SPI bus allows transfers on the bus to have sizes only limited by the amount of - * internal memory. Selecting no DMA channel (by passing the value 0) limits the amount of - * bytes transfered to a maximum of 64. Set to 0 if only the SPI flash uses - * this bus. + * @param dma_chan -1: auto dma allocate mode; 0: non-dma mode; 1 or 2: assign a specific DMA channel; + * Selecting a DMA channel for an SPI bus allows transfers on the bus to have sizes only + * limited by the amount of internal memory. Selecting no DMA channel (by passing the + * value 0) limits the amount of bytes transfered to a maximum of 64. Set to 0 if only + * the SPI flash uses this bus. Set -1 to let the driver to allocate the DMA channel. * * @warning If a DMA channel is selected, any transmit and receive buffer used should be allocated in * DMA-capable memory. @@ -120,7 +120,7 @@ typedef struct { * * @return * - ESP_ERR_INVALID_ARG if configuration is invalid - * - ESP_ERR_INVALID_STATE if host already is in use + * - ESP_ERR_INVALID_STATE if host already is in use or DMA channel is not available * - ESP_ERR_NO_MEM if out of memory * - ESP_OK on success */ diff --git a/components/driver/include/driver/spi_common_internal.h b/components/driver/include/driver/spi_common_internal.h index 23a2b90050..82d5b73c9d 100644 --- a/components/driver/include/driver/spi_common_internal.h +++ b/components/driver/include/driver/spi_common_internal.h @@ -115,19 +115,6 @@ bool spicommon_periph_in_use(spi_host_device_t host); */ bool spicommon_periph_free(spi_host_device_t host); -/** - * @brief Try to claim a SPI DMA channel - * - * Call this if your driver wants to use SPI with a DMA channnel. - * - * @param dma_chan channel to claim - * - * @note This public API is deprecated. - * - * @return True if success; false otherwise. - */ -bool spicommon_dma_chan_claim(int dma_chan); - /** * @brief Check whether the spi DMA channel is in use. * @@ -151,12 +138,13 @@ bool spicommon_dma_chan_in_use(int dma_chan); bool spicommon_dma_chan_free(int dma_chan); /** - * @brief Connect SPI and DMA peripherals + * @brief Try to claim a SPI DMA channel and connect it with SPI peripherals * - * @param host SPI peripheral - * @param dma_chan DMA channel + * @param host_id SPI host ID + * @param dma_chan -1: auto dma allocate mode; 0: non-dma mode; 1 or 2: assign a specific DMA channel; + * @param[out] out_actual_dma_chan Actual DMA channel (if you choose to assign a specific DMA channel, this will be the channel you assigned before) */ -void spicommon_connect_spi_and_dma(spi_host_device_t host, int dma_chan); +esp_err_t spicommon_alloc_dma(spi_host_device_t host_id, int dma_chan, uint32_t *out_actual_dma_chan); /** * @brief Connect a SPI peripheral to GPIO pins diff --git a/components/driver/include/driver/spi_slave.h b/components/driver/include/driver/spi_slave.h index 4263735a42..3bee13c264 100644 --- a/components/driver/include/driver/spi_slave.h +++ b/components/driver/include/driver/spi_slave.h @@ -93,9 +93,11 @@ struct spi_slave_transaction_t { * @param host SPI peripheral to use as a SPI slave interface * @param bus_config Pointer to a spi_bus_config_t struct specifying how the host should be initialized * @param slave_config Pointer to a spi_slave_interface_config_t struct specifying the details for the slave interface - * @param dma_chan Either 1 or 2. A SPI bus used by this driver must have a DMA channel associated with - * it. The SPI hardware has two DMA channels to share. This parameter indicates which - * one to use. + * @param dma_chan -1: auto dma allocate mode; 0: non-dma mode; 1 or 2: assign a specific DMA channel; + * Selecting a DMA channel for an SPI bus allows transfers on the bus to have sizes only + * limited by the amount of internal memory. Selecting no DMA channel (by passing the + * value 0) limits the amount of bytes transfered to a maximum of 64. Set to 0 if only + * the SPI flash uses this bus. Set -1 to let the driver to allocate the DMA channel. * * @warning If a DMA channel is selected, any transmit and receive buffer used should be allocated in * DMA-capable memory. @@ -106,7 +108,7 @@ struct spi_slave_transaction_t { * * @return * - ESP_ERR_INVALID_ARG if configuration is invalid - * - ESP_ERR_INVALID_STATE if host already is in use + * - ESP_ERR_INVALID_STATE if host already is in use or DMA channel is not available * - ESP_ERR_NO_MEM if out of memory * - ESP_OK on success */ diff --git a/components/driver/include/driver/spi_slave_hd.h b/components/driver/include/driver/spi_slave_hd.h index e847287362..639f7b3287 100644 --- a/components/driver/include/driver/spi_slave_hd.h +++ b/components/driver/include/driver/spi_slave_hd.h @@ -86,7 +86,7 @@ typedef struct { uint32_t address_bits; ///< address field bits, multiples of 8 and at least 8. uint32_t dummy_bits; ///< dummy field bits, multiples of 8 and at least 8. uint32_t queue_size; ///< Transaction queue size. This sets how many transactions can be 'in the air' (queued using spi_slave_hd_queue_trans but not yet finished using spi_slave_hd_get_trans_result) at the same time - uint32_t dma_chan; ///< DMA channel used + uint32_t dma_chan; ///< DMA channel used. -1: auto dma allocate mode; 0: non-dma mode; 1 or 2: assign a specific DMA channel; spi_slave_hd_callback_config_t cb_config; ///< Callback configuration } spi_slave_hd_slot_config_t; diff --git a/components/driver/spi_common.c b/components/driver/spi_common.c index 108a2625bb..2f64299b2c 100644 --- a/components/driver/spi_common.c +++ b/components/driver/spi_common.c @@ -100,6 +100,12 @@ static portMUX_TYPE spi_dma_spinlock = portMUX_INITIALIZER_UNLOCKED; static spicommon_bus_context_t s_mainbus = MAIN_BUS_DEFAULT(); static spicommon_bus_context_t* bus_ctx[SOC_SPI_PERIPH_NUM] = {&s_mainbus}; +#if CONFIG_IDF_TARGET_ESP32 || SOC_GDMA_SUPPORTED +//ESP32S2 does not support DMA channel auto-allocation +//Each bit stands for 1 dma channel, used for auto-alloc dma channel +static uint32_t spi_dma_channel_code; +#endif + //Returns true if this peripheral is successfully claimed, false if otherwise. bool spicommon_periph_claim(spi_host_device_t host, const char* source) @@ -159,7 +165,7 @@ static inline periph_module_t get_dma_periph(int dma_chan) #endif } -bool spicommon_dma_chan_claim(int dma_chan) +static bool spicommon_dma_chan_claim(int dma_chan) { bool ret = false; assert(dma_chan >= 1 && dma_chan <= SOC_SPI_DMA_CHAN_NUM); @@ -196,7 +202,7 @@ bool spicommon_dma_chan_free(int dma_chan) return true; } -void spicommon_connect_spi_and_dma(spi_host_device_t host, int dma_chan) +static void spicommon_connect_spi_and_dma(spi_host_device_t host, int dma_chan) { #if CONFIG_IDF_TARGET_ESP32 DPORT_SET_PERI_REG_BITS(DPORT_SPI_DMA_CHAN_SEL_REG, 3, dma_chan, (host * 2)); @@ -223,6 +229,50 @@ void spicommon_connect_spi_and_dma(spi_host_device_t host, int dma_chan) #endif //#elif SOC_GDMA_SUPPORTED } +esp_err_t spicommon_alloc_dma(spi_host_device_t host_id, int dma_chan, uint32_t *out_actual_dma_chan) +{ + uint32_t actual_dma_chan = 0; + +#if !SOC_GDMA_SUPPORTED + if (dma_chan < 0) { +#if CONFIG_IDF_TARGET_ESP32 + for (int i = 0; i < SOC_SPI_DMA_CHAN_NUM; i++) { + bool is_used = BIT(i) & spi_dma_channel_code; + if (!is_used) { + spi_dma_channel_code |= BIT(i); + actual_dma_chan = i+1; + break; + } + } + if (!actual_dma_chan) { + SPI_CHECK(false, "no available dma channel", ESP_ERR_INVALID_STATE); + } +#elif CONFIG_IDF_TARGET_ESP32S2 + //On ESP32S2, each SPI controller has its own DMA channel. So DMA channel auto-allocation is not supported + SPI_CHECK(false, "ESP32S2 does not support auto-alloc dma channel", ESP_ERR_INVALID_STATE); +#endif //#if CONFIG_IDF_TARGET_XXX + } else if (dma_chan > 0) { + actual_dma_chan = dma_chan; + } else { //dma_chan == 0 + // Program won't reach here + } + + bool dma_chan_claimed = spicommon_dma_chan_claim(actual_dma_chan); + if (!dma_chan_claimed) { + spicommon_periph_free(host_id); + SPI_CHECK(false, "dma channel already in use", ESP_ERR_INVALID_STATE); + } + + spicommon_connect_spi_and_dma(host_id, actual_dma_chan); + +#elif SOC_GDMA_SUPPORTED + +#endif + + *out_actual_dma_chan = actual_dma_chan; + return ESP_OK; +} + static bool bus_uses_iomux_pins(spi_host_device_t host, const spi_bus_config_t* bus_config) { if (bus_config->sclk_io_num>=0 && @@ -490,12 +540,16 @@ esp_err_t spi_bus_initialize(spi_host_device_t host_id, const spi_bus_config_t * esp_err_t err = ESP_OK; spicommon_bus_context_t *ctx = NULL; spi_bus_attr_t *bus_attr = NULL; + uint32_t actual_dma_chan = 0; + SPI_CHECK(is_valid_host(host_id), "invalid host_id", ESP_ERR_INVALID_ARG); SPI_CHECK(bus_ctx[host_id] == NULL, "SPI bus already initialized.", ESP_ERR_INVALID_STATE); #ifdef CONFIG_IDF_TARGET_ESP32 - SPI_CHECK( dma_chan >= 0 && dma_chan <= 2, "invalid dma channel", ESP_ERR_INVALID_ARG ); + SPI_CHECK( (dma_chan >= 0 && dma_chan <= 2) || dma_chan == -1, "invalid dma channel", ESP_ERR_INVALID_ARG ); #elif CONFIG_IDF_TARGET_ESP32S2 SPI_CHECK( dma_chan == 0 || dma_chan == host_id, "invalid dma channel", ESP_ERR_INVALID_ARG ); +#elif SOC_GDMA_SUPPORTED + SPI_CHECK( dma_chan == -1, "invalid dma channel, chip only support spi dma channel auto-alloc", ESP_ERR_INVALID_ARG ); #endif SPI_CHECK((bus_config->intr_flags & (ESP_INTR_FLAG_HIGH|ESP_INTR_FLAG_EDGE|ESP_INTR_FLAG_INTRDISABLED))==0, "intr flag not allowed", ESP_ERR_INVALID_ARG); #ifndef CONFIG_SPI_MASTER_ISR_IN_IRAM @@ -505,36 +559,23 @@ esp_err_t spi_bus_initialize(spi_host_device_t host_id, const spi_bus_config_t * bool spi_chan_claimed = spicommon_periph_claim(host_id, "spi master"); SPI_CHECK(spi_chan_claimed, "host_id already in use", ESP_ERR_INVALID_STATE); - if (dma_chan != 0) { - bool dma_chan_claimed = spicommon_dma_chan_claim(dma_chan); - if (!dma_chan_claimed) { - spicommon_periph_free(host_id); - SPI_CHECK(false, "dma channel already in use", ESP_ERR_INVALID_STATE); - } - - spicommon_connect_spi_and_dma(host_id, dma_chan); - } - //clean and initialize the context - ctx = (spicommon_bus_context_t*)malloc(sizeof(spicommon_bus_context_t)); + ctx = (spicommon_bus_context_t *)calloc(1, sizeof(spicommon_bus_context_t)); if (!ctx) { err = ESP_ERR_NO_MEM; goto cleanup; } - *ctx = (spicommon_bus_context_t) { - .host_id = host_id, - .bus_attr = { - .bus_cfg = *bus_config, - .dma_chan = dma_chan, - }, - }; - + ctx->host_id = host_id; bus_attr = &ctx->bus_attr; - if (dma_chan == 0) { - bus_attr->max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE; - bus_attr->dma_desc_num = 0; - } else { - //See how many dma descriptors we need and allocate them + bus_attr->bus_cfg = *bus_config; + + if (dma_chan != 0) { + err = spicommon_alloc_dma(host_id, dma_chan, &actual_dma_chan); + if (err != ESP_OK) { + return err; + } + bus_attr->dma_chan = actual_dma_chan; + int dma_desc_ct = lldesc_get_required_num(bus_config->max_transfer_sz); if (dma_desc_ct == 0) dma_desc_ct = 1; //default to 4k when max is not given @@ -546,6 +587,9 @@ esp_err_t spi_bus_initialize(spi_host_device_t host_id, const spi_bus_config_t * goto cleanup; } bus_attr->dma_desc_num = dma_desc_ct; + } else { + bus_attr->max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE; + bus_attr->dma_desc_num = 0; } spi_bus_lock_config_t lock_config = { @@ -565,7 +609,7 @@ esp_err_t spi_bus_initialize(spi_host_device_t host_id, const spi_bus_config_t * } #endif //CONFIG_PM_ENABLE - err = spicommon_bus_initialize_io(host_id, bus_config, dma_chan, SPICOMMON_BUSFLAG_MASTER | bus_config->flags, &bus_attr->flags); + err = spicommon_bus_initialize_io(host_id, bus_config, actual_dma_chan, SPICOMMON_BUSFLAG_MASTER | bus_config->flags, &bus_attr->flags); if (err != ESP_OK) { goto cleanup; } @@ -585,8 +629,8 @@ cleanup: free(bus_attr->dmadesc_rx); } free(ctx); - if (dma_chan) { - spicommon_dma_chan_free(dma_chan); + if (actual_dma_chan) { + spicommon_dma_chan_free(actual_dma_chan); } spicommon_periph_free(host_id); return err; diff --git a/components/driver/spi_slave.c b/components/driver/spi_slave.c index 91f512be2f..d91b3b3137 100644 --- a/components/driver/spi_slave.c +++ b/components/driver/spi_slave.c @@ -110,15 +110,18 @@ static inline void restore_cs(spi_slave_t *host) esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *bus_config, const spi_slave_interface_config_t *slave_config, int dma_chan) { - bool spi_chan_claimed, dma_chan_claimed; + bool spi_chan_claimed; + uint32_t actual_dma_chan = 0; esp_err_t ret = ESP_OK; esp_err_t err; //We only support HSPI/VSPI, period. SPI_CHECK(is_valid_host(host), "invalid host", ESP_ERR_INVALID_ARG); -#if defined(CONFIG_IDF_TARGET_ESP32) - SPI_CHECK( dma_chan >= 0 && dma_chan <= 2, "invalid dma channel", ESP_ERR_INVALID_ARG ); -#elif defined(CONFIG_IDF_TARGET_ESP32S2) +#ifdef CONFIG_IDF_TARGET_ESP32 + SPI_CHECK( (dma_chan >= 0 && dma_chan <= 2) || dma_chan == -1, "invalid dma channel", ESP_ERR_INVALID_ARG ); +#elif CONFIG_IDF_TARGET_ESP32S2 SPI_CHECK( dma_chan == 0 || dma_chan == host, "invalid dma channel", ESP_ERR_INVALID_ARG ); +#elif SOC_GDMA_SUPPORTED + SPI_CHECK( dma_chan == -1, "invalid dma channel, chip only support spi dma channel auto-alloc", ESP_ERR_INVALID_ARG ); #endif SPI_CHECK((bus_config->intr_flags & (ESP_INTR_FLAG_HIGH|ESP_INTR_FLAG_EDGE|ESP_INTR_FLAG_INTRDISABLED))==0, "intr flag not allowed", ESP_ERR_INVALID_ARG); #ifndef CONFIG_SPI_SLAVE_ISR_IN_IRAM @@ -129,15 +132,12 @@ esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *b spi_chan_claimed=spicommon_periph_claim(host, "spi slave"); SPI_CHECK(spi_chan_claimed, "host already in use", ESP_ERR_INVALID_STATE); - bool use_dma = dma_chan != 0; + bool use_dma = (dma_chan != 0); if (use_dma) { - dma_chan_claimed=spicommon_dma_chan_claim(dma_chan); - if ( !dma_chan_claimed ) { - spicommon_periph_free( host ); - SPI_CHECK(dma_chan_claimed, "dma channel already in use", ESP_ERR_INVALID_STATE); + err = spicommon_alloc_dma(host, dma_chan, &actual_dma_chan); + if (err != ESP_OK) { + return err; } - - spicommon_connect_spi_and_dma(host, dma_chan); } spihost[host] = malloc(sizeof(spi_slave_t)); @@ -149,7 +149,7 @@ esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *b memcpy(&spihost[host]->cfg, slave_config, sizeof(spi_slave_interface_config_t)); spihost[host]->id = host; - err = spicommon_bus_initialize_io(host, bus_config, dma_chan, SPICOMMON_BUSFLAG_SLAVE|bus_config->flags, &spihost[host]->flags); + err = spicommon_bus_initialize_io(host, bus_config, actual_dma_chan, SPICOMMON_BUSFLAG_SLAVE|bus_config->flags, &spihost[host]->flags); if (err!=ESP_OK) { ret = err; goto cleanup; @@ -162,7 +162,7 @@ esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *b if (use_dma) freeze_cs(spihost[host]); int dma_desc_ct = 0; - spihost[host]->dma_chan = dma_chan; + spihost[host]->dma_chan = actual_dma_chan; if (use_dma) { //See how many dma descriptors we need and allocate them dma_desc_ct = (bus_config->max_transfer_sz + SPI_MAX_DMA_LEN - 1) / SPI_MAX_DMA_LEN; @@ -242,7 +242,7 @@ cleanup: free(spihost[host]); spihost[host] = NULL; spicommon_periph_free(host); - if (dma_chan != 0) spicommon_dma_chan_free(dma_chan); + if (actual_dma_chan != 0) spicommon_dma_chan_free(actual_dma_chan); return ret; } diff --git a/components/driver/spi_slave_hd.c b/components/driver/spi_slave_hd.c index 1b3cd6b104..86a872320f 100644 --- a/components/driver/spi_slave_hd.c +++ b/components/driver/spi_slave_hd.c @@ -64,12 +64,17 @@ static void spi_slave_hd_intr_append(void *arg); esp_err_t spi_slave_hd_init(spi_host_device_t host_id, const spi_bus_config_t *bus_config, const spi_slave_hd_slot_config_t *config) { - bool spi_chan_claimed, dma_chan_claimed; + bool spi_chan_claimed; bool append_mode = (config->flags & SPI_SLAVE_HD_APPEND_MODE); + uint32_t actual_dma_chan = 0; esp_err_t ret = ESP_OK; SPIHD_CHECK(VALID_HOST(host_id), "invalid host", ESP_ERR_INVALID_ARG); +#if CONFIG_IDF_TARGET_ESP32S2 SPIHD_CHECK(config->dma_chan == 0 || config->dma_chan == host_id, "invalid dma channel", ESP_ERR_INVALID_ARG); +#elif SOC_GDMA_SUPPORTED + SPI_CHECK(dma_chan == -1, "invalid dma channel, chip only support spi dma channel auto-alloc", ESP_ERR_INVALID_ARG); +#endif #if !CONFIG_IDF_TARGET_ESP32S2 //Append mode is only supported on ESP32S2 now SPIHD_CHECK(append_mode == 0, "Append mode is only supported on ESP32S2 now", ESP_ERR_INVALID_ARG); @@ -78,14 +83,11 @@ esp_err_t spi_slave_hd_init(spi_host_device_t host_id, const spi_bus_config_t *b spi_chan_claimed = spicommon_periph_claim(host_id, "slave_hd"); SPIHD_CHECK(spi_chan_claimed, "host already in use", ESP_ERR_INVALID_STATE); - if ( config->dma_chan != 0 ) { - dma_chan_claimed = spicommon_dma_chan_claim(config->dma_chan); - if (!dma_chan_claimed) { - spicommon_periph_free(host_id); - SPIHD_CHECK(dma_chan_claimed, "dma channel already in use", ESP_ERR_INVALID_STATE); + if (config->dma_chan != 0) { + ret = spicommon_alloc_dma(host_id, config->dma_chan, &actual_dma_chan); + if (ret != ESP_OK) { + return ret; } - - spicommon_connect_spi_and_dma(host_id, config->dma_chan); } spi_slave_hd_slot_t* host = malloc(sizeof(spi_slave_hd_slot_t)); @@ -96,10 +98,10 @@ esp_err_t spi_slave_hd_init(spi_host_device_t host_id, const spi_bus_config_t *b spihost[host_id] = host; memset(host, 0, sizeof(spi_slave_hd_slot_t)); - host->dma_chan = config->dma_chan; + host->dma_chan = actual_dma_chan; host->int_spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED; - ret = spicommon_bus_initialize_io(host_id, bus_config, config->dma_chan, + ret = spicommon_bus_initialize_io(host_id, bus_config, actual_dma_chan, SPICOMMON_BUSFLAG_SLAVE | bus_config->flags, &host->flags); if (ret != ESP_OK) { goto cleanup; @@ -113,14 +115,14 @@ esp_err_t spi_slave_hd_init(spi_host_device_t host_id, const spi_bus_config_t *b .host_id = host_id, .dma_in = SPI_LL_GET_HW(host_id), .dma_out = SPI_LL_GET_HW(host_id), - .dma_chan = config->dma_chan, + .dma_chan = actual_dma_chan, .append_mode = append_mode, .mode = config->mode, .tx_lsbfirst = (config->flags & SPI_SLAVE_HD_RXBIT_LSBFIRST), .rx_lsbfirst = (config->flags & SPI_SLAVE_HD_TXBIT_LSBFIRST), }; - if (config->dma_chan != 0) { + if (actual_dma_chan != 0) { //Malloc for all the DMA descriptors uint32_t total_desc_size = spi_slave_hd_hal_get_total_desc_size(&host->hal, bus_config->max_transfer_sz); host->hal.dmadesc_tx = heap_caps_malloc(total_desc_size, MALLOC_CAP_DMA);