Skip to content

Commit b916440

Browse files
LucasTamborcarlescufi
authored andcommitted
drivers: dma: esp32xx: Remove dynamic allocation for invalid buffers
According to the coding guidelines "dynamic allocation is not allowed". This commit removes handling invalid DMA capable buffers by allocating temporary buffer in a valid memory region, considering them as errors. Signed-off-by: Lucas Tamborrino <[email protected]>
1 parent a35dd4b commit b916440

File tree

1 file changed

+16
-37
lines changed

1 file changed

+16
-37
lines changed

drivers/dma/dma_esp32_gdma.c

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ struct dma_esp32_channel {
5353
dma_callback_t cb;
5454
void *user_data;
5555
dma_descriptor_t desc;
56-
uint8_t *buf_temp;
57-
uint8_t *buf_original;
5856
#if defined(CONFIG_SOC_ESP32S3)
5957
struct intr_handle_data_t *intr_handle;
6058
#endif
@@ -82,9 +80,6 @@ static void IRAM_ATTR dma_esp32_isr_handle_rx(const struct device *dev,
8280

8381
if (intr_status & (GDMA_LL_EVENT_RX_SUC_EOF | GDMA_LL_EVENT_RX_DONE)) {
8482
intr_status &= ~(GDMA_LL_EVENT_RX_SUC_EOF | GDMA_LL_EVENT_RX_DONE);
85-
if (rx->buf_temp) {
86-
memcpy(rx->buf_original, rx->buf_temp, rx->desc.dw0.length);
87-
}
8883
}
8984

9085
if (rx->cb) {
@@ -162,21 +157,13 @@ static int dma_esp32_disable_interrupt(const struct device *dev,
162157
static int dma_esp32_config_rx_descriptor(struct dma_esp32_channel *dma_channel,
163158
struct dma_block_config *block)
164159
{
165-
memset(&dma_channel->desc, 0, sizeof(dma_channel->desc));
166-
dma_channel->desc.buffer = (void *)block->dest_address;
167-
dma_channel->buf_original = (uint8_t *)block->dest_address;
168-
k_free(dma_channel->buf_temp);
169-
dma_channel->buf_temp = NULL;
170160
if (!esp_ptr_dma_capable((uint32_t *)block->dest_address)) {
171-
LOG_DBG("Rx buffer not in DMA capable memory: %p", (uint32_t *)block->dest_address);
172-
dma_channel->buf_temp = k_aligned_alloc(32, block->block_size);
173-
if (!dma_channel->buf_temp) {
174-
LOG_ERR("Not able to allocate mem");
175-
return -ENOMEM;
176-
}
177-
memset(dma_channel->buf_temp, 0, block->block_size);
178-
dma_channel->desc.buffer = dma_channel->buf_temp;
161+
LOG_ERR("Rx buffer not in DMA capable memory: %p", (uint32_t *)block->dest_address);
162+
return -EINVAL;
179163
}
164+
165+
memset(&dma_channel->desc, 0, sizeof(dma_channel->desc));
166+
dma_channel->desc.buffer = (void *)block->dest_address;
180167
dma_channel->desc.dw0.size = block->block_size;
181168
dma_channel->desc.dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
182169

@@ -222,21 +209,14 @@ static int dma_esp32_config_rx(const struct device *dev, struct dma_esp32_channe
222209
static int dma_esp32_config_tx_descriptor(struct dma_esp32_channel *dma_channel,
223210
struct dma_block_config *block)
224211
{
225-
memset(&dma_channel->desc, 0, sizeof(dma_channel->desc));
226-
dma_channel->desc.buffer = (void *)block->source_address;
227-
k_free(dma_channel->buf_temp);
228212
if (!esp_ptr_dma_capable((uint32_t *)block->source_address)) {
229-
LOG_DBG("Tx buffer not in DMA capable memory");
230-
dma_channel->buf_temp = k_malloc(block->block_size);
231-
if (!dma_channel->buf_temp) {
232-
LOG_ERR("Not able to allocate mem");
233-
return -ENOMEM;
234-
}
235-
memcpy(dma_channel->buf_temp, (uint8_t *)block->source_address, block->block_size);
236-
dma_channel->buf_original = (uint8_t *)block->source_address;
237-
dma_channel->desc.buffer = dma_channel->buf_temp;
213+
LOG_ERR("Tx buffer not in DMA capable memory: %p",
214+
(uint32_t *)block->source_address);
215+
return -EINVAL;
238216
}
239217

218+
memset(&dma_channel->desc, 0, sizeof(dma_channel->desc));
219+
dma_channel->desc.buffer = (void *)block->source_address;
240220
dma_channel->desc.dw0.size = block->block_size;
241221
dma_channel->desc.dw0.length = block->block_size;
242222
dma_channel->desc.dw0.suc_eof = 1;
@@ -286,6 +266,7 @@ static int dma_esp32_config(const struct device *dev, uint32_t channel,
286266
struct dma_esp32_config *config = (struct dma_esp32_config *)dev->config;
287267
struct dma_esp32_data *data = (struct dma_esp32_data *const)(dev)->data;
288268
struct dma_esp32_channel *dma_channel = &config->dma_channel[channel];
269+
int ret = 0;
289270

290271
if (channel >= config->dma_channel_max) {
291272
LOG_ERR("Unsupported channel");
@@ -326,21 +307,21 @@ static int dma_esp32_config(const struct device *dev, uint32_t channel,
326307
dma_channel_rx->periph_id = dma_channel->periph_id;
327308
dma_channel_tx->periph_id = dma_channel->periph_id;
328309

329-
dma_esp32_config_rx(dev, dma_channel_rx, config_dma);
330-
dma_esp32_config_tx(dev, dma_channel_tx, config_dma);
310+
ret = dma_esp32_config_rx(dev, dma_channel_rx, config_dma);
311+
ret = dma_esp32_config_tx(dev, dma_channel_tx, config_dma);
331312
break;
332313
case PERIPHERAL_TO_MEMORY:
333-
dma_esp32_config_rx(dev, dma_channel, config_dma);
314+
ret = dma_esp32_config_rx(dev, dma_channel, config_dma);
334315
break;
335316
case MEMORY_TO_PERIPHERAL:
336-
dma_esp32_config_tx(dev, dma_channel, config_dma);
317+
ret = dma_esp32_config_tx(dev, dma_channel, config_dma);
337318
break;
338319
default:
339320
LOG_ERR("Invalid Channel direction");
340321
return -EINVAL;
341322
}
342323

343-
return 0;
324+
return ret;
344325
}
345326

346327
static int dma_esp32_start(const struct device *dev, uint32_t channel)
@@ -555,8 +536,6 @@ static int dma_esp32_init(const struct device *dev)
555536

556537
for (uint8_t i = 0; i < DMA_MAX_CHANNEL * 2; i++) {
557538
dma_channel = &config->dma_channel[i];
558-
dma_channel->buf_original = NULL;
559-
dma_channel->buf_temp = NULL;
560539
dma_channel->cb = NULL;
561540
dma_channel->dir = DMA_UNCONFIGURED;
562541
dma_channel->periph_id = GDMA_TRIG_PERIPH_INVALID;

0 commit comments

Comments
 (0)