Skip to content

Commit 3f65182

Browse files
eren-terziogluxiaoxiang781216
authored andcommitted
arch/risc-v: Change DMA functions with common layer approach for esp32[-c3|-c6|-h2]
Change DMA functions with common layer functions for risc-v based Espressif devices Signed-off-by: Eren Terzioglu <[email protected]>
1 parent 230714b commit 3f65182

File tree

4 files changed

+64
-93
lines changed

4 files changed

+64
-93
lines changed

arch/risc-v/src/common/espressif/esp_dma.c

Lines changed: 58 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
#include "soc/gdma_periph.h"
4747
#include "hal/gdma_hal.h"
48+
#include "hal/gdma_hal_ahb.h"
4849
#include "hal/gdma_types.h"
4950
#include "hal/gdma_ll.h"
5051
#include "periph_ctrl.h"
@@ -58,13 +59,23 @@
5859

5960
#define ESPRESSIF_DMA_CHAN_MAX (SOC_GDMA_PAIRS_PER_GROUP_MAX)
6061

62+
#if !SOC_RCC_IS_INDEPENDENT
63+
#define GDMA_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
64+
#else
65+
#define GDMA_RCC_ATOMIC()
66+
#endif
67+
6168
/****************************************************************************
6269
* Private Data
6370
****************************************************************************/
6471

6572
static bool g_dma_chan_used[ESPRESSIF_DMA_CHAN_MAX];
6673
static mutex_t g_dma_lock = NXMUTEX_INITIALIZER;
6774
static gdma_hal_context_t ctx;
75+
static gdma_hal_config_t cfg =
76+
{
77+
.group_id = 0
78+
};
6879

6980
/****************************************************************************
7081
* Public Functions
@@ -123,26 +134,27 @@ int32_t esp_dma_request(enum esp_dma_periph_e periph,
123134

124135
dmainfo("Allocated channel=%d\n", chan);
125136

126-
gdma_ll_rx_connect_to_periph(ctx.dev, chan, periph, periph);
127-
gdma_ll_tx_connect_to_periph(ctx.dev, chan, periph, periph);
137+
gdma_hal_connect_peri(&ctx, chan, GDMA_CHANNEL_DIRECTION_TX,
138+
periph, periph);
139+
gdma_hal_connect_peri(&ctx, chan, GDMA_CHANNEL_DIRECTION_RX,
140+
periph, periph);
128141

129142
if (burst_en)
130143
{
131144
/* Enable DMA TX/RX channels burst sending data */
132145

133-
gdma_ll_tx_enable_data_burst(ctx.dev, chan, true);
134-
gdma_ll_rx_enable_data_burst(ctx.dev, chan, true);
135-
136146
/* Enable DMA TX/RX channels burst reading descriptor link */
137147

138-
gdma_ll_tx_enable_descriptor_burst(ctx.dev, chan, true);
139-
gdma_ll_rx_enable_descriptor_burst(ctx.dev, chan, true);
148+
gdma_hal_enable_burst(&ctx, chan, GDMA_CHANNEL_DIRECTION_TX,
149+
true, true);
150+
gdma_hal_enable_burst(&ctx, chan, GDMA_CHANNEL_DIRECTION_RX,
151+
true, true);
140152
}
141153

142154
/* Set priority for DMA TX/RX channels */
143155

144-
gdma_ll_tx_set_priority(ctx.dev, chan, tx_prio);
145-
gdma_ll_rx_set_priority(ctx.dev, chan, rx_prio);
156+
gdma_hal_set_priority(&ctx, chan, GDMA_CHANNEL_DIRECTION_TX, tx_prio);
157+
gdma_hal_set_priority(&ctx, chan, GDMA_CHANNEL_DIRECTION_RX, rx_prio);
146158

147159
nxmutex_unlock(&g_dma_lock);
148160
return chan;
@@ -177,6 +189,8 @@ uint32_t esp_dma_setup(int chan, bool tx,
177189
uint8_t *pdata = pbuf;
178190
uint32_t data_len;
179191
uint32_t buf_len;
192+
gdma_channel_direction_t dir = tx == true ? GDMA_CHANNEL_DIRECTION_TX : \
193+
GDMA_CHANNEL_DIRECTION_RX;
180194
dma_descriptor_t *dma_desc = (dma_descriptor_t *)dmadesc;
181195

182196
DEBUGASSERT(chan >= 0);
@@ -212,22 +226,16 @@ uint32_t esp_dma_setup(int chan, bool tx,
212226
dma_desc[i].dw0.suc_eof = 1;
213227
dmadesc[i].next = NULL;
214228

229+
gdma_hal_reset(&ctx, chan, dir);
230+
215231
if (tx)
216232
{
217-
/* Reset DMA TX channel FSM and FIFO pointer */
218-
219-
gdma_ll_tx_reset_channel(ctx.dev, chan);
220-
221233
/* Set the descriptor link base address for TX channel */
222234

223235
gdma_ll_tx_set_desc_addr(ctx.dev, chan, (uint32_t)dmadesc);
224236
}
225237
else
226238
{
227-
/* Reset DMA RX channel FSM and FIFO pointer */
228-
229-
gdma_ll_rx_reset_channel(ctx.dev, chan);
230-
231239
/* Set the descriptor link base address for RX channel */
232240

233241
gdma_ll_rx_set_desc_addr(ctx.dev, chan, (uint32_t)dmadesc);
@@ -256,30 +264,14 @@ uint32_t esp_dma_setup(int chan, bool tx,
256264
void esp_dma_load(struct esp_dmadesc_s *dmadesc, int chan, bool tx)
257265
{
258266
uint32_t regval;
267+
gdma_channel_direction_t dir = tx == true ? GDMA_CHANNEL_DIRECTION_TX : \
268+
GDMA_CHANNEL_DIRECTION_RX;
259269

260270
DEBUGASSERT(chan >= 0);
261271
DEBUGASSERT(dmadesc != NULL);
262272

263-
if (tx)
264-
{
265-
/* Reset DMA TX channel FSM and FIFO pointer */
266-
267-
gdma_ll_rx_reset_channel(ctx.dev, chan);
268-
269-
/* Set the descriptor link base address for TX channel */
270-
271-
gdma_ll_tx_set_desc_addr(ctx.dev, chan, (uint32_t)dmadesc);
272-
}
273-
else
274-
{
275-
/* Reset DMA RX channel FSM and FIFO pointer */
276-
277-
gdma_ll_rx_reset_channel(ctx.dev, chan);
278-
279-
/* Set the descriptor link base address for RX channel */
280-
281-
gdma_ll_rx_set_desc_addr(ctx.dev, chan, (uint32_t)dmadesc);
282-
}
273+
gdma_hal_reset(&ctx, chan, dir);
274+
gdma_hal_start_with_desc(&ctx, chan, dir, (uint32_t)dmadesc);
283275
}
284276

285277
/****************************************************************************
@@ -301,14 +293,9 @@ void esp_dma_load(struct esp_dmadesc_s *dmadesc, int chan, bool tx)
301293

302294
void esp_dma_enable_interrupt(int chan, bool tx, uint32_t mask, bool en)
303295
{
304-
if (tx)
305-
{
306-
gdma_ll_tx_enable_interrupt(ctx.dev, chan, mask, en);
307-
}
308-
else
309-
{
310-
gdma_ll_rx_enable_interrupt(ctx.dev, chan, mask, en);
311-
}
296+
gdma_channel_direction_t dir = tx == true ? GDMA_CHANNEL_DIRECTION_TX : \
297+
GDMA_CHANNEL_DIRECTION_RX;
298+
gdma_hal_enable_intr(&ctx, chan, dir, mask, en);
312299
}
313300

314301
/****************************************************************************
@@ -329,17 +316,9 @@ void esp_dma_enable_interrupt(int chan, bool tx, uint32_t mask, bool en)
329316
int esp_dma_get_interrupt(int chan, bool tx)
330317
{
331318
uint32_t intr_status = 0;
332-
333-
if (tx)
334-
{
335-
intr_status = gdma_ll_tx_get_interrupt_status(ctx.dev, chan, false);
336-
}
337-
else
338-
{
339-
intr_status = gdma_ll_rx_get_interrupt_status(ctx.dev, chan, false);
340-
}
341-
342-
return intr_status;
319+
gdma_channel_direction_t dir = tx == true ? GDMA_CHANNEL_DIRECTION_TX : \
320+
GDMA_CHANNEL_DIRECTION_RX;
321+
return gdma_hal_read_intr_status(&ctx, chan, dir, false);
343322
}
344323

345324
/****************************************************************************
@@ -360,14 +339,9 @@ int esp_dma_get_interrupt(int chan, bool tx)
360339

361340
void esp_dma_clear_interrupt(int chan, bool tx, uint32_t mask)
362341
{
363-
if (tx)
364-
{
365-
gdma_ll_tx_clear_interrupt_status(ctx.dev, chan, mask);
366-
}
367-
else
368-
{
369-
gdma_ll_rx_clear_interrupt_status(ctx.dev, chan, mask);
370-
}
342+
gdma_channel_direction_t dir = tx == true ? GDMA_CHANNEL_DIRECTION_TX : \
343+
GDMA_CHANNEL_DIRECTION_RX;
344+
gdma_hal_clear_intr(&ctx, chan, dir, mask);
371345
}
372346

373347
/****************************************************************************
@@ -387,18 +361,9 @@ void esp_dma_clear_interrupt(int chan, bool tx, uint32_t mask)
387361

388362
int esp_dma_get_desc_addr(int chan, bool tx)
389363
{
390-
uint32_t desc_addr = 0;
391-
392-
if (tx)
393-
{
394-
desc_addr = gdma_ll_tx_get_eof_desc_addr(ctx.dev, chan);
395-
}
396-
else
397-
{
398-
desc_addr = gdma_ll_rx_get_success_eof_desc_addr(ctx.dev, chan);
399-
}
400-
401-
return desc_addr;
364+
gdma_channel_direction_t dir = tx == true ? GDMA_CHANNEL_DIRECTION_TX : \
365+
GDMA_CHANNEL_DIRECTION_RX;
366+
return gdma_hal_get_eof_desc_addr(&ctx, chan, dir, true);
402367
}
403368

404369
/****************************************************************************
@@ -445,14 +410,9 @@ void esp_dma_enable(int chan, bool tx)
445410

446411
void esp_dma_disable(int chan, bool tx)
447412
{
448-
if (tx)
449-
{
450-
gdma_ll_tx_stop(ctx.dev, chan);
451-
}
452-
else
453-
{
454-
gdma_ll_rx_stop(ctx.dev, chan);
455-
}
413+
gdma_channel_direction_t dir = tx == true ? GDMA_CHANNEL_DIRECTION_TX : \
414+
GDMA_CHANNEL_DIRECTION_RX;
415+
gdma_hal_stop(&ctx, chan, dir);
456416
}
457417

458418
/****************************************************************************
@@ -499,14 +459,9 @@ void esp_dma_wait_idle(int chan, bool tx)
499459

500460
void esp_dma_reset_channel(int chan, bool tx)
501461
{
502-
if (tx)
503-
{
504-
gdma_ll_tx_reset_channel(ctx.dev, chan);
505-
}
506-
else
507-
{
508-
gdma_ll_rx_reset_channel(ctx.dev, chan);
509-
}
462+
gdma_channel_direction_t dir = tx == true ? GDMA_CHANNEL_DIRECTION_TX : \
463+
GDMA_CHANNEL_DIRECTION_RX;
464+
gdma_hal_reset(&ctx, chan, dir);
510465
}
511466

512467
/****************************************************************************
@@ -526,7 +481,12 @@ void esp_dma_reset_channel(int chan, bool tx)
526481
void esp_dma_init(void)
527482
{
528483
periph_module_enable(PERIPH_GDMA_MODULE);
529-
ctx.dev = GDMA_LL_GET_HW(0);
484+
GDMA_RCC_ATOMIC()
485+
{
486+
gdma_ll_enable_bus_clock(0, true);
487+
}
488+
489+
gdma_ahb_hal_init(&ctx, &cfg);
530490
gdma_ll_force_enable_reg_clock(ctx.dev, true);
531491
}
532492

@@ -551,12 +511,17 @@ void esp_dma_deinit(void)
551511
/* Disable DMA clock gating */
552512

553513
gdma_ll_force_enable_reg_clock(ctx.dev, false);
514+
GDMA_RCC_ATOMIC()
515+
{
516+
gdma_ll_enable_bus_clock(0, false);
517+
}
554518

555519
/* Disable DMA module by gating the clock and asserting the reset
556520
* signal.
557521
*/
558522

559523
periph_module_disable(PERIPH_GDMA_MODULE);
524+
gdma_hal_deinit(&ctx);
560525

561526
nxmutex_unlock(&g_dma_lock);
562527
}

arch/risc-v/src/esp32c3/hal_esp32c3.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$
150150
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)cache_hal.c
151151
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)efuse_hal.c
152152
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)gpio_hal.c
153+
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)gdma_hal_ahb_v1.c
154+
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)gdma_hal_top.c
153155
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)hal_utils.c
154156
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)ledc_hal.c
155157
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)ledc_hal_iram.c

arch/risc-v/src/esp32c6/hal_esp32c6.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$
154154
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)brownout_hal.c
155155
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)efuse_hal.c
156156
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)gpio_hal.c
157+
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)gdma_hal_ahb_v1.c
158+
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)gdma_hal_top.c
157159
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)hal_utils.c
158160
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)ledc_hal.c
159161
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)ledc_hal_iram.c

arch/risc-v/src/esp32h2/hal_esp32h2.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$
146146
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)cache_hal.c
147147
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)efuse_hal.c
148148
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)gpio_hal.c
149+
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)gdma_hal_ahb_v1.c
150+
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)gdma_hal_top.c
149151
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)hal_utils.c
150152
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)ledc_hal.c
151153
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)ledc_hal_iram.c

0 commit comments

Comments
 (0)