Skip to content

Commit 7f0673f

Browse files
committed
Merge branch 'refactor/emac_alloc_dma_buffer_v5.3' into 'release/v5.3'
refactor(emac): use heap component API to allocate cached aligned DMA buffer (v5.3) See merge request espressif/esp-idf!31457
2 parents 54f30cc + c349247 commit 7f0673f

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

components/esp_eth/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,5 @@ if(CONFIG_ETH_ENABLED)
6868
if(CONFIG_ETH_USE_SPI_ETHERNET)
6969
idf_component_optional_requires(PUBLIC esp_driver_spi)
7070
endif()
71-
idf_component_optional_requires(PRIVATE esp_netif esp_pm)
72-
if(CONFIG_SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE)
73-
idf_component_optional_requires(PRIVATE esp_mm)
74-
endif()
71+
idf_component_optional_requires(PRIVATE esp_netif esp_pm esp_mm)
7572
endif()

components/esp_eth/src/mac/esp_eth_mac_esp_dma.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
*/
66

77
#include "esp_check.h"
8-
#include "esp_dma_utils.h"
98
#include "sdkconfig.h"
109
#include "soc/soc_caps.h"
11-
#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
1210
#include "esp_cache.h"
13-
#endif
1411
#include "hal/emac_hal.h"
12+
#include "esp_heap_caps.h"
1513
#include "esp_private/eth_mac_esp_dma.h"
1614

1715
#define ETH_CRC_LENGTH (4)
@@ -41,8 +39,7 @@
4139

4240
static const char *TAG = "esp.emac.dma";
4341

44-
struct emac_esp_dma_t
45-
{
42+
struct emac_esp_dma_t {
4643
emac_hal_context_t hal;
4744
uint32_t tx_desc_flags;
4845
uint32_t rx_desc_flags;
@@ -58,14 +55,14 @@ typedef struct {
5855
uint32_t magic_id;
5956
#endif // NDEBUG
6057
uint32_t copy_len;
61-
}__attribute__((packed)) emac_esp_dma_auto_buf_info_t;
58+
} __attribute__((packed)) emac_esp_dma_auto_buf_info_t;
6259

6360
void emac_esp_dma_reset(emac_esp_dma_handle_t emac_esp_dma)
6461
{
6562
/* reset DMA descriptors */
6663
emac_esp_dma->rx_desc = (eth_dma_rx_descriptor_t *)(emac_esp_dma->descriptors);
6764
emac_esp_dma->tx_desc = (eth_dma_tx_descriptor_t *)(emac_esp_dma->descriptors +
68-
sizeof(eth_dma_rx_descriptor_t) * CONFIG_ETH_DMA_RX_BUFFER_NUM);
65+
sizeof(eth_dma_rx_descriptor_t) * CONFIG_ETH_DMA_RX_BUFFER_NUM);
6966
/* init rx chain */
7067
for (int i = 0; i < CONFIG_ETH_DMA_RX_BUFFER_NUM; i++) {
7168
/* Set Own bit of the Rx descriptor Status: DMA */
@@ -227,7 +224,7 @@ uint32_t emac_esp_dma_transmit_multiple_buf_frame(emac_esp_dma_handle_t emac_esp
227224
buffs_cnt--;
228225
ptr = *(++buffs);
229226
lastlen = *(++lengths);
230-
/* There is only limited available space in the current descriptor, use it all */
227+
/* There is only limited available space in the current descriptor, use it all */
231228
} else {
232229
/* copy data from uplayer stack buffer */
233230
memcpy((void *)(desc_iter->Buffer1Addr + (CONFIG_ETH_DMA_BUFFER_SIZE - avail_len)), ptr, avail_len);
@@ -236,7 +233,7 @@ uint32_t emac_esp_dma_transmit_multiple_buf_frame(emac_esp_dma_handle_t emac_esp
236233
/* If lastlen is not zero, input buff will be fragmented over multiple descriptors */
237234
if (lastlen > 0) {
238235
ptr += avail_len;
239-
/* Input buff fully fits the descriptor, move to the next input buff */
236+
/* Input buff fully fits the descriptor, move to the next input buff */
240237
} else {
241238
/* Update processed input buffers info */
242239
buffs_cnt--;
@@ -386,7 +383,7 @@ uint32_t emac_esp_dma_receive_frame(emac_esp_dma_handle_t emac_esp_dma, uint8_t
386383

387384
if (copy_len) {
388385
eth_dma_rx_descriptor_t *desc_iter = emac_esp_dma->rx_desc;
389-
while(copy_len > CONFIG_ETH_DMA_BUFFER_SIZE) {
386+
while (copy_len > CONFIG_ETH_DMA_BUFFER_SIZE) {
390387
DMA_CACHE_INVALIDATE(desc_iter->Buffer1Addr, CONFIG_ETH_DMA_BUFFER_SIZE);
391388
memcpy(buf, (void *)(desc_iter->Buffer1Addr), CONFIG_ETH_DMA_BUFFER_SIZE);
392389
buf += CONFIG_ETH_DMA_BUFFER_SIZE;
@@ -449,7 +446,7 @@ esp_err_t emac_esp_del_dma(emac_esp_dma_handle_t emac_esp_dma)
449446
return ESP_OK;
450447
}
451448

452-
esp_err_t emac_esp_new_dma(const emac_esp_dma_config_t* config, emac_esp_dma_handle_t *ret_handle)
449+
esp_err_t emac_esp_new_dma(const emac_esp_dma_config_t *config, emac_esp_dma_handle_t *ret_handle)
453450
{
454451
esp_err_t ret = ESP_OK;
455452
*ret_handle = NULL;
@@ -459,20 +456,15 @@ esp_err_t emac_esp_new_dma(const emac_esp_dma_config_t* config, emac_esp_dma_han
459456
/* alloc memory for ethernet dma descriptor */
460457
uint32_t desc_size = CONFIG_ETH_DMA_RX_BUFFER_NUM * sizeof(eth_dma_rx_descriptor_t) +
461458
CONFIG_ETH_DMA_TX_BUFFER_NUM * sizeof(eth_dma_tx_descriptor_t);
462-
esp_dma_mem_info_t dma_mem_info = {
463-
.extra_heap_caps = MALLOC_CAP_INTERNAL,
464-
.dma_alignment_bytes = 4,
465-
};
466-
esp_dma_capable_calloc(1, desc_size, &dma_mem_info, (void*)&emac_esp_dma->descriptors, NULL);
467-
459+
emac_esp_dma->descriptors = heap_caps_aligned_calloc(4, 1, desc_size, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
468460
ESP_GOTO_ON_FALSE(emac_esp_dma->descriptors, ESP_ERR_NO_MEM, err, TAG, "no mem for descriptors");
469461
/* alloc memory for ethernet dma buffer */
470462
for (int i = 0; i < CONFIG_ETH_DMA_RX_BUFFER_NUM; i++) {
471-
esp_dma_capable_calloc(1, CONFIG_ETH_DMA_BUFFER_SIZE, &dma_mem_info, (void*)&emac_esp_dma->rx_buf[i], NULL);
463+
emac_esp_dma->rx_buf[i] = heap_caps_aligned_calloc(4, 1, CONFIG_ETH_DMA_BUFFER_SIZE, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
472464
ESP_GOTO_ON_FALSE(emac_esp_dma->rx_buf[i], ESP_ERR_NO_MEM, err, TAG, "no mem for RX DMA buffers");
473465
}
474466
for (int i = 0; i < CONFIG_ETH_DMA_TX_BUFFER_NUM; i++) {
475-
esp_dma_capable_calloc(1, CONFIG_ETH_DMA_BUFFER_SIZE, &dma_mem_info, (void*)&emac_esp_dma->tx_buf[i], NULL);
467+
emac_esp_dma->tx_buf[i] = heap_caps_aligned_calloc(4, 1, CONFIG_ETH_DMA_BUFFER_SIZE, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
476468
ESP_GOTO_ON_FALSE(emac_esp_dma->tx_buf[i], ESP_ERR_NO_MEM, err, TAG, "no mem for TX DMA buffers");
477469
}
478470
emac_hal_init(&emac_esp_dma->hal);

0 commit comments

Comments
 (0)