From a29ebef6cec2cda75b1b44b30f56667dbd68bd96 Mon Sep 17 00:00:00 2001 From: Lucas Tamborrino Date: Thu, 15 Sep 2022 11:06:32 -0300 Subject: [PATCH] drivers: spi: esp32c3: add DMA support Add SPI DMA support for esp32c3. Signed-off-by: Lucas Tamborrino --- drivers/spi/spi_esp32_spim.c | 18 ++++++++++++++++-- drivers/spi/spi_esp32_spim.h | 6 ++++++ dts/riscv/espressif/esp32c3.dtsi | 2 ++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spi_esp32_spim.c b/drivers/spi/spi_esp32_spim.c index 4d92ab2066..fe328e47ac 100644 --- a/drivers/spi/spi_esp32_spim.c +++ b/drivers/spi/spi_esp32_spim.c @@ -19,6 +19,8 @@ LOG_MODULE_REGISTER(esp32_spi, CONFIG_SPI_LOG_LEVEL); #ifndef CONFIG_SOC_ESP32C3 #include #else +#include +#include #include #endif #include @@ -150,6 +152,7 @@ static int spi_esp32_init_dma(const struct device *dev) { const struct spi_esp32_config *cfg = dev->config; struct spi_esp32_data *data = dev->data; + uint8_t channel_offset; LOG_ERR("DMA"); if (clock_control_on(cfg->clock_dev, (clock_control_subsys_t)cfg->dma_clk_src)) { @@ -157,6 +160,17 @@ static int spi_esp32_init_dma(const struct device *dev) return -EIO; } +#ifdef CONFIG_SOC_ESP32C3 + gdma_hal_init(&data->hal_gdma, 0); + gdma_ll_enable_clock(data->hal_gdma.dev, true); + gdma_ll_tx_reset_channel(data->hal_gdma.dev, cfg->dma_host); + gdma_ll_rx_reset_channel(data->hal_gdma.dev, cfg->dma_host); + gdma_ll_tx_connect_to_periph(data->hal_gdma.dev, cfg->dma_host, 0); + gdma_ll_rx_connect_to_periph(data->hal_gdma.dev, cfg->dma_host, 0); + channel_offset = 0; +#else + channel_offset = 1; +#endif /* CONFIG_SOC_ESP32C3 */ #ifdef CONFIG_SOC_ESP32 /*Connect SPI and DMA*/ DPORT_SET_PERI_REG_BITS(DPORT_SPI_DMA_CHAN_SEL_REG, 3, cfg->dma_host + 1, @@ -166,8 +180,8 @@ static int spi_esp32_init_dma(const struct device *dev) data->hal_config.dma_in = (spi_dma_dev_t *)cfg->spi; data->hal_config.dma_out = (spi_dma_dev_t *)cfg->spi; data->hal_config.dma_enabled = true; - data->hal_config.tx_dma_chan = cfg->dma_host + 1; - data->hal_config.rx_dma_chan = cfg->dma_host + 1; + data->hal_config.tx_dma_chan = cfg->dma_host + channel_offset; + data->hal_config.rx_dma_chan = cfg->dma_host + channel_offset; data->hal_config.dmadesc_n = 1; data->hal_config.dmadesc_rx = &data->dma_desc_rx; data->hal_config.dmadesc_tx = &data->dma_desc_tx; diff --git a/drivers/spi/spi_esp32_spim.h b/drivers/spi/spi_esp32_spim.h index 7d69ca7ed6..f6242c9e71 100644 --- a/drivers/spi/spi_esp32_spim.h +++ b/drivers/spi/spi_esp32_spim.h @@ -9,6 +9,9 @@ #include #include +#ifdef CONFIG_SOC_ESP32C3 +#include +#endif #define SPI_MASTER_FREQ_8M (APB_CLK_FREQ/10) #define SPI_MASTER_FREQ_9M (APB_CLK_FREQ/9) /* 8.89MHz */ @@ -39,6 +42,9 @@ struct spi_esp32_data { struct spi_context ctx; spi_hal_context_t hal; spi_hal_config_t hal_config; +#ifdef CONFIG_SOC_ESP32C3 + gdma_hal_context_t hal_gdma; +#endif spi_hal_timing_conf_t timing_config; spi_hal_dev_config_t dev_config; spi_hal_trans_config_t trans_config; diff --git a/dts/riscv/espressif/esp32c3.dtsi b/dts/riscv/espressif/esp32c3.dtsi index 42b830365c..fdc04f20e5 100644 --- a/dts/riscv/espressif/esp32c3.dtsi +++ b/dts/riscv/espressif/esp32c3.dtsi @@ -189,6 +189,8 @@ interrupts = ; interrupt-parent = <&intc>; clocks = <&rtc ESP32_SPI2_MODULE>; + dma-clk = ; + dma-host = <1>; status = "disabled"; };