Skip to content

Commit a29ebef

Browse files
LucasTamborcarlescufi
authored andcommitted
drivers: spi: esp32c3: add DMA support
Add SPI DMA support for esp32c3. Signed-off-by: Lucas Tamborrino <[email protected]>
1 parent af47b9b commit a29ebef

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

drivers/spi/spi_esp32_spim.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ LOG_MODULE_REGISTER(esp32_spi, CONFIG_SPI_LOG_LEVEL);
1919
#ifndef CONFIG_SOC_ESP32C3
2020
#include <zephyr/drivers/interrupt_controller/intc_esp32.h>
2121
#else
22+
#include <hal/gdma_hal.h>
23+
#include <hal/gdma_ll.h>
2224
#include <zephyr/drivers/interrupt_controller/intc_esp32c3.h>
2325
#endif
2426
#include <zephyr/drivers/clock_control.h>
@@ -150,13 +152,25 @@ static int spi_esp32_init_dma(const struct device *dev)
150152
{
151153
const struct spi_esp32_config *cfg = dev->config;
152154
struct spi_esp32_data *data = dev->data;
155+
uint8_t channel_offset;
153156

154157
LOG_ERR("DMA");
155158
if (clock_control_on(cfg->clock_dev, (clock_control_subsys_t)cfg->dma_clk_src)) {
156159
LOG_ERR("Could not enable DMA clock");
157160
return -EIO;
158161
}
159162

163+
#ifdef CONFIG_SOC_ESP32C3
164+
gdma_hal_init(&data->hal_gdma, 0);
165+
gdma_ll_enable_clock(data->hal_gdma.dev, true);
166+
gdma_ll_tx_reset_channel(data->hal_gdma.dev, cfg->dma_host);
167+
gdma_ll_rx_reset_channel(data->hal_gdma.dev, cfg->dma_host);
168+
gdma_ll_tx_connect_to_periph(data->hal_gdma.dev, cfg->dma_host, 0);
169+
gdma_ll_rx_connect_to_periph(data->hal_gdma.dev, cfg->dma_host, 0);
170+
channel_offset = 0;
171+
#else
172+
channel_offset = 1;
173+
#endif /* CONFIG_SOC_ESP32C3 */
160174
#ifdef CONFIG_SOC_ESP32
161175
/*Connect SPI and DMA*/
162176
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)
166180
data->hal_config.dma_in = (spi_dma_dev_t *)cfg->spi;
167181
data->hal_config.dma_out = (spi_dma_dev_t *)cfg->spi;
168182
data->hal_config.dma_enabled = true;
169-
data->hal_config.tx_dma_chan = cfg->dma_host + 1;
170-
data->hal_config.rx_dma_chan = cfg->dma_host + 1;
183+
data->hal_config.tx_dma_chan = cfg->dma_host + channel_offset;
184+
data->hal_config.rx_dma_chan = cfg->dma_host + channel_offset;
171185
data->hal_config.dmadesc_n = 1;
172186
data->hal_config.dmadesc_rx = &data->dma_desc_rx;
173187
data->hal_config.dmadesc_tx = &data->dma_desc_tx;

drivers/spi/spi_esp32_spim.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#include <zephyr/drivers/pinctrl.h>
1111
#include <hal/spi_hal.h>
12+
#ifdef CONFIG_SOC_ESP32C3
13+
#include <hal/gdma_hal.h>
14+
#endif
1215

1316
#define SPI_MASTER_FREQ_8M (APB_CLK_FREQ/10)
1417
#define SPI_MASTER_FREQ_9M (APB_CLK_FREQ/9) /* 8.89MHz */
@@ -39,6 +42,9 @@ struct spi_esp32_data {
3942
struct spi_context ctx;
4043
spi_hal_context_t hal;
4144
spi_hal_config_t hal_config;
45+
#ifdef CONFIG_SOC_ESP32C3
46+
gdma_hal_context_t hal_gdma;
47+
#endif
4248
spi_hal_timing_conf_t timing_config;
4349
spi_hal_dev_config_t dev_config;
4450
spi_hal_trans_config_t trans_config;

dts/riscv/espressif/esp32c3.dtsi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@
189189
interrupts = <SPI2_INTR_SOURCE>;
190190
interrupt-parent = <&intc>;
191191
clocks = <&rtc ESP32_SPI2_MODULE>;
192+
dma-clk = <ESP32_GDMA_MODULE>;
193+
dma-host = <1>;
192194
status = "disabled";
193195
};
194196

0 commit comments

Comments
 (0)