From bf9587132bc085135038e61e9c9d2d28e0b8fdcd Mon Sep 17 00:00:00 2001 From: morris Date: Thu, 6 May 2021 20:16:12 +0800 Subject: [PATCH] esp_eth: restart negotiation in esp_eth_start --- components/esp_eth/src/esp_eth.c | 13 +++++++------ components/esp_eth/src/esp_eth_phy_dm9051.c | 10 +++++----- components/esp_eth/src/esp_eth_phy_dp83848.c | 10 +++++----- components/esp_eth/src/esp_eth_phy_ip101.c | 10 +++++----- components/esp_eth/src/esp_eth_phy_lan8720.c | 12 ++++++------ components/esp_eth/src/esp_eth_phy_rtl8201.c | 10 +++++----- 6 files changed, 33 insertions(+), 32 deletions(-) diff --git a/components/esp_eth/src/esp_eth.c b/components/esp_eth/src/esp_eth.c index a895b5b097..4a8147498f 100644 --- a/components/esp_eth/src/esp_eth.c +++ b/components/esp_eth/src/esp_eth.c @@ -255,6 +255,8 @@ esp_err_t esp_eth_start(esp_eth_handle_t hdl) esp_err_t ret = ESP_OK; esp_eth_driver_t *eth_driver = (esp_eth_driver_t *)hdl; ETH_CHECK(eth_driver, "ethernet driver handle can't be null", err, ESP_ERR_INVALID_ARG); + esp_eth_phy_t *phy = eth_driver->phy; + esp_eth_mac_t *mac = eth_driver->mac; // check if driver has started esp_eth_fsm_t expected_fsm = ESP_ETH_FSM_STOP; if (!atomic_compare_exchange_strong(ð_driver->fsm, &expected_fsm, ESP_ETH_FSM_START)) { @@ -262,14 +264,13 @@ esp_err_t esp_eth_start(esp_eth_handle_t hdl) ret = ESP_ERR_INVALID_STATE; goto err; } - ETH_CHECK(eth_driver->phy->reset(eth_driver->phy) == ESP_OK, "reset phy failed", err, ESP_FAIL); + ETH_CHECK(phy->negotiate(phy) == ESP_OK, "phy negotiation failed", err, ESP_FAIL); + ETH_CHECK(mac->start(mac) == ESP_OK, "start mac failed", err, ESP_FAIL); + ETH_CHECK(esp_event_post(ETH_EVENT, ETHERNET_EVENT_START, ð_driver, sizeof(eth_driver), 0) == ESP_OK, + "send ETHERNET_EVENT_START event failed", err, ESP_FAIL); + ETH_CHECK(phy->get_link(phy) == ESP_OK, "phy get link status failed", err, ESP_FAIL); ETH_CHECK(xTimerStart(eth_driver->check_link_timer, 0) == pdPASS, "start eth_link_timer failed", err, ESP_FAIL); - ETH_CHECK(esp_event_post(ETH_EVENT, ETHERNET_EVENT_START, ð_driver, sizeof(eth_driver), 0) == ESP_OK, - "send ETHERNET_EVENT_START event failed", err_event, ESP_FAIL); - return ESP_OK; -err_event: - xTimerStop(eth_driver->check_link_timer, 0); err: return ret; } diff --git a/components/esp_eth/src/esp_eth_phy_dm9051.c b/components/esp_eth/src/esp_eth_phy_dm9051.c index 437b1157c0..acda61b7e1 100644 --- a/components/esp_eth/src/esp_eth_phy_dm9051.c +++ b/components/esp_eth/src/esp_eth_phy_dm9051.c @@ -202,6 +202,8 @@ static esp_err_t dm9051_negotiate(esp_eth_phy_t *phy) { phy_dm9051_t *dm9051 = __containerof(phy, phy_dm9051_t, parent); esp_eth_mediator_t *eth = dm9051->eth; + /* in case any link status has changed, let's assume we're in link down status */ + dm9051->link_status = ETH_LINK_DOWN; /* Start auto negotiation */ bmcr_reg_t bmcr = { .speed_select = 1, /* 100Mbps */ @@ -215,8 +217,8 @@ static esp_err_t dm9051_negotiate(esp_eth_phy_t *phy) bmsr_reg_t bmsr; dscsr_reg_t dscsr; uint32_t to = 0; - for (to = 0; to < dm9051->autonego_timeout_ms / 10; to++) { - vTaskDelay(pdMS_TO_TICKS(10)); + for (to = 0; to < dm9051->autonego_timeout_ms / 100; to++) { + vTaskDelay(pdMS_TO_TICKS(100)); PHY_CHECK(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK, "read BMSR failed", err); PHY_CHECK(eth->phy_reg_read(eth, dm9051->addr, ETH_PHY_DSCSR_REG_ADDR, &(dscsr.val)) == ESP_OK, @@ -225,11 +227,9 @@ static esp_err_t dm9051_negotiate(esp_eth_phy_t *phy) break; } } - if (to >= dm9051->autonego_timeout_ms / 10) { + if (to >= dm9051->autonego_timeout_ms / 100) { ESP_LOGW(TAG, "Ethernet PHY auto negotiation timeout"); } - /* Updata information about link, speed, duplex */ - PHY_CHECK(dm9051_update_link_duplex_speed(dm9051) == ESP_OK, "update link duplex speed failed", err); return ESP_OK; err: return ESP_FAIL; diff --git a/components/esp_eth/src/esp_eth_phy_dp83848.c b/components/esp_eth/src/esp_eth_phy_dp83848.c index 5d4432b65f..f7d379e4b9 100644 --- a/components/esp_eth/src/esp_eth_phy_dp83848.c +++ b/components/esp_eth/src/esp_eth_phy_dp83848.c @@ -191,6 +191,8 @@ static esp_err_t dp83848_negotiate(esp_eth_phy_t *phy) { phy_dp83848_t *dp83848 = __containerof(phy, phy_dp83848_t, parent); esp_eth_mediator_t *eth = dp83848->eth; + /* in case any link status has changed, let's assume we're in link down status */ + dp83848->link_status = ETH_LINK_DOWN; /* Start auto negotiation */ bmcr_reg_t bmcr = { .speed_select = 1, /* 100Mbps */ @@ -204,8 +206,8 @@ static esp_err_t dp83848_negotiate(esp_eth_phy_t *phy) bmsr_reg_t bmsr; physts_reg_t physts; uint32_t to = 0; - for (to = 0; to < dp83848->autonego_timeout_ms / 10; to++) { - vTaskDelay(pdMS_TO_TICKS(10)); + for (to = 0; to < dp83848->autonego_timeout_ms / 100; to++) { + vTaskDelay(pdMS_TO_TICKS(100)); PHY_CHECK(eth->phy_reg_read(eth, dp83848->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK, "read BMSR failed", err); PHY_CHECK(eth->phy_reg_read(eth, dp83848->addr, ETH_PHY_STS_REG_ADDR, &(physts.val)) == ESP_OK, @@ -215,11 +217,9 @@ static esp_err_t dp83848_negotiate(esp_eth_phy_t *phy) } } /* Auto negotiation failed, maybe no network cable plugged in, so output a warning */ - if (to >= dp83848->autonego_timeout_ms / 10) { + if (to >= dp83848->autonego_timeout_ms / 100) { ESP_LOGW(TAG, "auto negotiation timeout"); } - /* Updata information about link, speed, duplex */ - PHY_CHECK(dp83848_update_link_duplex_speed(dp83848) == ESP_OK, "update link duplex speed failed", err); return ESP_OK; err: return ESP_FAIL; diff --git a/components/esp_eth/src/esp_eth_phy_ip101.c b/components/esp_eth/src/esp_eth_phy_ip101.c index fe37540bc4..7576c4b5fe 100644 --- a/components/esp_eth/src/esp_eth_phy_ip101.c +++ b/components/esp_eth/src/esp_eth_phy_ip101.c @@ -231,6 +231,8 @@ static esp_err_t ip101_negotiate(esp_eth_phy_t *phy) { phy_ip101_t *ip101 = __containerof(phy, phy_ip101_t, parent); esp_eth_mediator_t *eth = ip101->eth; + /* in case any link status has changed, let's assume we're in link down status */ + ip101->link_status = ETH_LINK_DOWN; /* Restart auto negotiation */ bmcr_reg_t bmcr = { .speed_select = 1, /* 100Mbps */ @@ -243,8 +245,8 @@ static esp_err_t ip101_negotiate(esp_eth_phy_t *phy) /* Wait for auto negotiation complete */ bmsr_reg_t bmsr; uint32_t to = 0; - for (to = 0; to < ip101->autonego_timeout_ms / 10; to++) { - vTaskDelay(pdMS_TO_TICKS(10)); + for (to = 0; to < ip101->autonego_timeout_ms / 100; to++) { + vTaskDelay(pdMS_TO_TICKS(100)); PHY_CHECK(eth->phy_reg_read(eth, ip101->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK, "read BMSR failed", err); if (bmsr.auto_nego_complete) { @@ -252,11 +254,9 @@ static esp_err_t ip101_negotiate(esp_eth_phy_t *phy) } } /* Auto negotiation failed, maybe no network cable plugged in, so output a warning */ - if (to >= ip101->autonego_timeout_ms / 10) { + if (to >= ip101->autonego_timeout_ms / 100) { ESP_LOGW(TAG, "auto negotiation timeout"); } - /* Updata information about link, speed, duplex */ - PHY_CHECK(ip101_update_link_duplex_speed(ip101) == ESP_OK, "update link duplex speed failed", err); return ESP_OK; err: return ESP_FAIL; diff --git a/components/esp_eth/src/esp_eth_phy_lan8720.c b/components/esp_eth/src/esp_eth_phy_lan8720.c index aa81ec36a0..f2c8d24b4b 100644 --- a/components/esp_eth/src/esp_eth_phy_lan8720.c +++ b/components/esp_eth/src/esp_eth_phy_lan8720.c @@ -276,6 +276,8 @@ static esp_err_t lan8720_negotiate(esp_eth_phy_t *phy) { phy_lan8720_t *lan8720 = __containerof(phy, phy_lan8720_t, parent); esp_eth_mediator_t *eth = lan8720->eth; + /* in case any link status has changed, let's assume we're in link down status */ + lan8720->link_status = ETH_LINK_DOWN; /* Restart auto negotiation */ bmcr_reg_t bmcr = { .speed_select = 1, /* 100Mbps */ @@ -287,9 +289,9 @@ static esp_err_t lan8720_negotiate(esp_eth_phy_t *phy) /* Wait for auto negotiation complete */ bmsr_reg_t bmsr; pscsr_reg_t pscsr; - int32_t to = 0; - for (to = 0; to < lan8720->autonego_timeout_ms / 10; to++) { - vTaskDelay(pdMS_TO_TICKS(10)); + uint32_t to = 0; + for (to = 0; to < lan8720->autonego_timeout_ms / 100; to++) { + vTaskDelay(pdMS_TO_TICKS(100)); PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK, "read BMSR failed", err); PHY_CHECK(eth->phy_reg_read(eth, lan8720->addr, ETH_PHY_PSCSR_REG_ADDR, &(pscsr.val)) == ESP_OK, @@ -299,11 +301,9 @@ static esp_err_t lan8720_negotiate(esp_eth_phy_t *phy) } } /* Auto negotiation failed, maybe no network cable plugged in, so output a warning */ - if (to >= lan8720->autonego_timeout_ms / 10) { + if (to >= lan8720->autonego_timeout_ms / 100) { ESP_LOGW(TAG, "auto negotiation timeout"); } - /* Updata information about link, speed, duplex */ - PHY_CHECK(lan8720_update_link_duplex_speed(lan8720) == ESP_OK, "update link duplex speed failed", err); return ESP_OK; err: return ESP_FAIL; diff --git a/components/esp_eth/src/esp_eth_phy_rtl8201.c b/components/esp_eth/src/esp_eth_phy_rtl8201.c index 9d89c338a8..f567e95240 100644 --- a/components/esp_eth/src/esp_eth_phy_rtl8201.c +++ b/components/esp_eth/src/esp_eth_phy_rtl8201.c @@ -185,6 +185,8 @@ static esp_err_t rtl8201_negotiate(esp_eth_phy_t *phy) { phy_rtl8201_t *rtl8201 = __containerof(phy, phy_rtl8201_t, parent); esp_eth_mediator_t *eth = rtl8201->eth; + /* in case any link status has changed, let's assume we're in link down status */ + rtl8201->link_status = ETH_LINK_DOWN; /* Restart auto negotiation */ bmcr_reg_t bmcr = { .speed_select = 1, /* 100Mbps */ @@ -197,8 +199,8 @@ static esp_err_t rtl8201_negotiate(esp_eth_phy_t *phy) /* Wait for auto negotiation complete */ bmsr_reg_t bmsr; uint32_t to = 0; - for (to = 0; to < rtl8201->autonego_timeout_ms / 10; to++) { - vTaskDelay(pdMS_TO_TICKS(10)); + for (to = 0; to < rtl8201->autonego_timeout_ms / 100; to++) { + vTaskDelay(pdMS_TO_TICKS(100)); PHY_CHECK(eth->phy_reg_read(eth, rtl8201->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK, "read BMSR failed", err); if (bmsr.auto_nego_complete) { @@ -206,11 +208,9 @@ static esp_err_t rtl8201_negotiate(esp_eth_phy_t *phy) } } /* Auto negotiation failed, maybe no network cable plugged in, so output a warning */ - if (to >= rtl8201->autonego_timeout_ms / 10) { + if (to >= rtl8201->autonego_timeout_ms / 100) { ESP_LOGW(TAG, "auto negotiation timeout"); } - /* Updata information about link, speed, duplex */ - PHY_CHECK(rtl8201_update_link_duplex_speed(rtl8201) == ESP_OK, "update link duplex speed failed", err); return ESP_OK; err: return ESP_FAIL;