From ae571ddddfed09f93345d50ed9fdbc0d38743d1f Mon Sep 17 00:00:00 2001 From: shangke Date: Tue, 1 Aug 2017 11:59:57 +0800 Subject: [PATCH 1/4] fix rx buf --- components/ethernet/emac_main.c | 40 +++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/components/ethernet/emac_main.c b/components/ethernet/emac_main.c index 391ffcdb43..0343c2f82d 100644 --- a/components/ethernet/emac_main.c +++ b/components/ethernet/emac_main.c @@ -450,16 +450,17 @@ static void emac_process_rx_unavail(void) while (emac_config.cnt_rx < DMA_RX_BUF_NUM) { emac_config.cnt_rx++; - - //copy data to lwip - emac_config.emac_tcpip_input((void *)(emac_config.dma_erx[emac_config.dirty_rx].basic.desc2), - (((emac_config.dma_erx[emac_config.dirty_rx].basic.desc0) >> EMAC_DESC_FRAME_LENGTH_S) & EMAC_DESC_FRAME_LENGTH) , NULL); - if (emac_config.cnt_rx > DMA_RX_BUF_NUM) { ESP_LOGE(TAG, "emac rx unavail buf err !!\n"); } + uint32_t tmp_dirty = emac_config.dirty_rx; emac_config.dirty_rx = (emac_config.dirty_rx + 1) % DMA_RX_BUF_NUM; - } + + //copy data to lwip + emac_config.emac_tcpip_input((void *)(emac_config.dma_erx[tmp_dirty].basic.desc2), + (((emac_config.dma_erx[tmp_dirty].basic.desc0) >> EMAC_DESC_FRAME_LENGTH_S) & EMAC_DESC_FRAME_LENGTH) , NULL); + + } emac_enable_rx_intr(); emac_enable_rx_unavail_intr(); xSemaphoreGiveRecursive( emac_rx_xMutex ); @@ -479,16 +480,17 @@ static void emac_process_rx(void) while (((uint32_t) & (emac_config.dma_erx[emac_config.dirty_rx].basic.desc0) != cur_rx_desc) && emac_config.cnt_rx < DMA_RX_BUF_NUM ) { emac_config.cnt_rx++; - - //copy data to lwip - emac_config.emac_tcpip_input((void *)(emac_config.dma_erx[emac_config.dirty_rx].basic.desc2), - (((emac_config.dma_erx[emac_config.dirty_rx].basic.desc0) >> EMAC_DESC_FRAME_LENGTH_S) & EMAC_DESC_FRAME_LENGTH) , NULL); - if (emac_config.cnt_rx > DMA_RX_BUF_NUM ) { ESP_LOGE(TAG, "emac rx buf err!!\n"); } + uint32_t tmp_dirty = emac_config.dirty_rx; emac_config.dirty_rx = (emac_config.dirty_rx + 1) % DMA_RX_BUF_NUM; + + //copy data to lwip + emac_config.emac_tcpip_input((void *)(emac_config.dma_erx[tmp_dirty].basic.desc2), + (((emac_config.dma_erx[tmp_dirty].basic.desc0) >> EMAC_DESC_FRAME_LENGTH_S) & EMAC_DESC_FRAME_LENGTH) , NULL); + cur_rx_desc = emac_read_rx_cur_reg(); } } else { @@ -497,16 +499,17 @@ static void emac_process_rx(void) while (emac_config.cnt_rx < DMA_RX_BUF_NUM) { emac_config.cnt_rx++; - //copy data to lwip - emac_config.emac_tcpip_input((void *)(emac_config.dma_erx[emac_config.dirty_rx].basic.desc2), - (((emac_config.dma_erx[emac_config.dirty_rx].basic.desc0) >> EMAC_DESC_FRAME_LENGTH_S) & EMAC_DESC_FRAME_LENGTH) , NULL); - if (emac_config.cnt_rx > DMA_RX_BUF_NUM) { ESP_LOGE(TAG, "emac rx buf err!!!\n"); } - + uint32_t tmp_dirty = emac_config.dirty_rx; emac_config.dirty_rx = (emac_config.dirty_rx + 1) % DMA_RX_BUF_NUM; - } + + //copy data to lwip + emac_config.emac_tcpip_input((void *)(emac_config.dma_erx[tmp_dirty].basic.desc2), + (((emac_config.dma_erx[tmp_dirty].basic.desc0) >> EMAC_DESC_FRAME_LENGTH_S) & EMAC_DESC_FRAME_LENGTH) , NULL); + + } } } } @@ -963,6 +966,9 @@ esp_err_t esp_eth_init(eth_config_t *config) ret = ESP_FAIL; goto _exit; #endif + if (emac_config.emac_status != EMAC_RUNTIME_NOT_INIT) { + goto _exit; + } emac_init_default_data(); From 1e6f904d38dbc58b7b9c8ca844dcca8454be3502 Mon Sep 17 00:00:00 2001 From: shangke Date: Thu, 3 Aug 2017 14:44:22 +0800 Subject: [PATCH 2/4] add comments for rx list --- components/ethernet/emac_main.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/components/ethernet/emac_main.c b/components/ethernet/emac_main.c index 0343c2f82d..f57fc95cca 100644 --- a/components/ethernet/emac_main.c +++ b/components/ethernet/emac_main.c @@ -115,6 +115,25 @@ static void emac_set_rx_base_reg(void) REG_WRITE(EMAC_DMARXBASEADDR_REG, (uint32_t)(emac_config.dma_erx)); } +/* +* dirty_rx indicates the hardware has been fed with data packets and is the first node software needs to handle; +* +* cur_rx indicates the completion of software handling and is the last node hardware could use; +* +* cnt_rx is to count the numbers of packets handled by software, passed to protocol stack and not been freed. +* +* (1) Initializing the Linked List. Connect the numerable nodes to a circular linked list, appoint one of the nodes as the head node, mark* the dirty_rx and cur_rx into the node, and mount the node on the hardware base address. Initialize cnt_rx into 0. +* +* (2) When hardware receives packets, nodes of linked lists will be fed with data packets from the base address by turns, marks the node +* of linked lists as “HARDWARE UNUSABLE” and reports interrupts. +* +* (3) When the software receives the interrupts, it will handle the linked lists by turns from dirty_rx, send data packets to protocol +* stack. dirty_rx will deviate backwards by turns and cnt_rx will by turns ++. +* +* (4) After the protocol stack handles all the data and calls the free function, it will deviate backwards by turns from cur_rx, mark the * node of linked lists as “HARDWARE USABLE” and cnt_rx will by turns ——. +* +* (5) Cycle from Step 2 to Step 4 without break and build up circular linked list handling. +*/ static void emac_reset_dma_chain(void) { emac_config.cnt_tx = 0; From 05aa09fbb99b95ab98ca7cc2590f6b61e46e488d Mon Sep 17 00:00:00 2001 From: shangke Date: Fri, 4 Aug 2017 14:24:44 +0800 Subject: [PATCH 3/4] beautify the code --- components/ethernet/emac_main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/ethernet/emac_main.c b/components/ethernet/emac_main.c index f57fc95cca..b6989f0bbe 100644 --- a/components/ethernet/emac_main.c +++ b/components/ethernet/emac_main.c @@ -54,8 +54,8 @@ static struct emac_config_data emac_config; -static uint8_t emac_dma_rx_chain_buf[32 * DMA_RX_BUF_NUM]; -static uint8_t emac_dma_tx_chain_buf[32 * DMA_TX_BUF_NUM]; +static uint8_t emac_dma_rx_chain_buf[sizeof(struct dma_extended_desc) * DMA_RX_BUF_NUM]; +static uint8_t emac_dma_tx_chain_buf[sizeof(struct dma_extended_desc) * DMA_TX_BUF_NUM]; static uint8_t emac_dma_rx_buf[DMA_RX_BUF_SIZE * DMA_RX_BUF_NUM]; static uint8_t emac_dma_tx_buf[DMA_TX_BUF_SIZE * DMA_TX_BUF_NUM]; @@ -130,7 +130,7 @@ static void emac_set_rx_base_reg(void) * (3) When the software receives the interrupts, it will handle the linked lists by turns from dirty_rx, send data packets to protocol * stack. dirty_rx will deviate backwards by turns and cnt_rx will by turns ++. * -* (4) After the protocol stack handles all the data and calls the free function, it will deviate backwards by turns from cur_rx, mark the * node of linked lists as “HARDWARE USABLE” and cnt_rx will by turns ——. +* (4) After the protocol stack handles all the data and calls the free function, it will deviate backwards by turns from cur_rx, mark the * node of linked lists as “HARDWARE USABLE” and cnt_rx will by turns --. * * (5) Cycle from Step 2 to Step 4 without break and build up circular linked list handling. */ From 01a2c31e1ffb1072cd4db63ad28e8360e30e4e46 Mon Sep 17 00:00:00 2001 From: shangke Date: Fri, 4 Aug 2017 14:56:58 +0800 Subject: [PATCH 4/4] change copy right --- components/ethernet/emac_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/ethernet/emac_main.c b/components/ethernet/emac_main.c index b6989f0bbe..df614582d4 100644 --- a/components/ethernet/emac_main.c +++ b/components/ethernet/emac_main.c @@ -1,4 +1,4 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License.