]> granicus.if.org Git - esp-idf/commitdiff
ethernet: bugfix and optimize
authorsuda-morris <362953310@qq.com>
Wed, 28 Aug 2019 08:04:50 +0000 (16:04 +0800)
committersuda-morris <362953310@qq.com>
Tue, 3 Sep 2019 03:01:16 +0000 (11:01 +0800)
1. check frame length before pass to stack
2. replace semaphore with task notify

components/esp_eth/Kconfig
components/esp_eth/src/esp_eth_mac_dm9051.c
components/esp_eth/src/esp_eth_mac_esp32.c

index 40e7ee36dc218cd2dd4e1dcc9da07ac76bce366a..8e894d397d633037cf7ef903938080f31d58267e 100644 (file)
@@ -114,7 +114,7 @@ menu "Ethernet"
 
         config ETH_DMA_RX_BUFFER_NUM
             int "Amount of Ethernet DMA Rx buffers"
-            range 3 20
+            range 3 30
             default 10
             help
                 Number of DMA receive buffers. Each buffer's size is ETH_DMA_BUFFER_SIZE.
@@ -122,7 +122,7 @@ menu "Ethernet"
 
         config ETH_DMA_TX_BUFFER_NUM
             int "Amount of Ethernet DMA Tx buffers"
-            range 3 20
+            range 3 30
             default 10
             help
                 Number of DMA transmit buffers. Each buffer's size is ETH_DMA_BUFFER_SIZE.
index b76c5a4fd542334c4e5cb4fc1b10b72ed95fcaf5..b2878cb1344124431b9dc03f23ee1effb92b7bb0 100644 (file)
@@ -511,7 +511,11 @@ static void emac_dm9051_task(void *arg)
                     buffer = (uint8_t *)heap_caps_malloc(ETH_MAX_PACKET_SIZE, MALLOC_CAP_DMA);
                     if (emac->parent.receive(&emac->parent, buffer, &length) == ESP_OK) {
                         /* pass the buffer to stack (e.g. TCP/IP layer) */
-                        emac->eth->stack_input(emac->eth, buffer, length);
+                        if (length) {
+                            emac->eth->stack_input(emac->eth, buffer, length);
+                        } else {
+                            free(buffer);
+                        }
                     } else {
                         free(buffer);
                     }
index cea8868a3b30a60d839235d3ed7800aa8cf256c2..bbc3df22da505499da48c39c68f04743a5c36612 100644 (file)
@@ -48,7 +48,6 @@ typedef struct {
     esp_eth_mediator_t *eth;
     emac_hal_context_t hal;
     intr_handle_t intr_hdl;
-    SemaphoreHandle_t rx_counting_sem;
     TaskHandle_t rx_task_hdl;
     uint32_t sw_reset_timeout_ms;
     uint32_t frames_remain;
@@ -237,18 +236,20 @@ static void emac_esp32_rx_task(void *arg)
     uint8_t *buffer = NULL;
     uint32_t length = 0;
     while (1) {
-        if (xSemaphoreTake(emac->rx_counting_sem, pdMS_TO_TICKS(RX_QUEUE_WAIT_MS)) == pdTRUE) {
-            buffer = (uint8_t *)malloc(ETH_MAX_PACKET_SIZE);
-            if (emac_esp32_receive(&emac->parent, buffer, &length) == ESP_OK) {
-                /* pass the buffer to stack (e.g. TCP/IP layer) */
-                emac->eth->stack_input(emac->eth, buffer, length);
-            } else {
-                free(buffer);
-            }
-        }
-        /* there might be some frames left in DMA buffer */
-        else if (emac->frames_remain) {
-            xSemaphoreGive(emac->rx_counting_sem);
+        if (ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(RX_QUEUE_WAIT_MS))) {
+            do {
+                buffer = (uint8_t *)malloc(ETH_MAX_PACKET_SIZE);
+                if (emac_esp32_receive(&emac->parent, buffer, &length) == ESP_OK) {
+                    /* pass the buffer to stack (e.g. TCP/IP layer) */
+                    if (length) {
+                        emac->eth->stack_input(emac->eth, buffer, length);
+                    } else {
+                        free(buffer);
+                    }
+                } else {
+                    free(buffer);
+                }
+            } while (emac->frames_remain);
         }
     }
     vTaskDelete(NULL);
@@ -331,7 +332,6 @@ static esp_err_t emac_esp32_del(esp_eth_mac_t *mac)
     emac_esp32_t *emac = __containerof(mac, emac_esp32_t, parent);
     esp_intr_free(emac->intr_hdl);
     vTaskDelete(emac->rx_task_hdl);
-    vSemaphoreDelete(emac->rx_counting_sem);
     int i = 0;
     for (i = 0; i < CONFIG_ETH_DMA_RX_BUFFER_NUM; i++) {
         free(emac->hal.rx_buf[i]);
@@ -416,17 +416,12 @@ esp_eth_mac_t *esp_eth_mac_new_esp32(const eth_mac_config_t *config)
     MAC_CHECK(esp_intr_alloc(ETS_ETH_MAC_INTR_SOURCE, ESP_INTR_FLAG_IRAM, emac_esp32_isr_handler,
                              &emac->hal, &(emac->intr_hdl)) == ESP_OK,
               "alloc emac interrupt failed", err_intr, NULL);
-    /* create counting semaphore */
-    emac->rx_counting_sem = xSemaphoreCreateCounting(config->queue_len, 0);
-    MAC_CHECK(emac->rx_counting_sem, "create semaphore failed", err_sem, NULL);
     /* create rx task */
     BaseType_t xReturned = xTaskCreate(emac_esp32_rx_task, "emac_rx", config->rx_task_stack_size, emac,
                                        config->rx_task_prio, &emac->rx_task_hdl);
     MAC_CHECK(xReturned == pdPASS, "create emac_rx task failed", err_task, NULL);
     return &(emac->parent);
 err_task:
-    vSemaphoreDelete(emac->rx_counting_sem);
-err_sem:
     esp_intr_free(emac->intr_hdl);
 err_intr:
     for (int i = 0; i < CONFIG_ETH_DMA_TX_BUFFER_NUM; i++) {
@@ -448,8 +443,8 @@ void emac_hal_rx_complete_cb(void *arg)
     emac_hal_context_t *hal = (emac_hal_context_t *)arg;
     emac_esp32_t *emac = __containerof(hal, emac_esp32_t, hal);
     BaseType_t high_task_wakeup;
-    /* send message to rx thread */
-    xSemaphoreGiveFromISR(emac->rx_counting_sem, &high_task_wakeup);
+    /* notify receive task */
+    vTaskNotifyGiveFromISR(emac->rx_task_hdl, &high_task_wakeup);
     if (high_task_wakeup == pdTRUE) {
         emac->isr_need_yield = true;
     }
@@ -460,8 +455,8 @@ void emac_hal_rx_unavail_cb(void *arg)
     emac_hal_context_t *hal = (emac_hal_context_t *)arg;
     emac_esp32_t *emac = __containerof(hal, emac_esp32_t, hal);
     BaseType_t high_task_wakeup;
-    /* send message to rx thread */
-    xSemaphoreGiveFromISR(emac->rx_counting_sem, &high_task_wakeup);
+    /* notify receive task */
+    vTaskNotifyGiveFromISR(emac->rx_task_hdl, &high_task_wakeup);
     if (high_task_wakeup == pdTRUE) {
         emac->isr_need_yield = true;
     }