diff --git a/patches/0233-emac-stop-start-dhcp-on-interface-bringup.patch b/patches/0233-emac-stop-start-dhcp-on-interface-bringup.patch new file mode 100644 index 000000000..85dd4c9e0 --- /dev/null +++ b/patches/0233-emac-stop-start-dhcp-on-interface-bringup.patch @@ -0,0 +1,30 @@ +From 5114c7f56a82c37021cdd92f07bc1a2c62e85e64 Mon Sep 17 00:00:00 2001 +From: maidnl +Date: Mon, 8 Jul 2024 12:07:40 +0200 +Subject: [PATCH 233/234] emac: stop/start dhcp on interface bringup + +--- + connectivity/lwipstack/source/LWIPInterface.cpp | 6 +++++- + 1 file changed, 5 insertions(+), 1 deletion(-) + +diff --git a/connectivity/lwipstack/source/LWIPInterface.cpp b/connectivity/lwipstack/source/LWIPInterface.cpp +index a1cfcf31c4..dfefebcb8b 100644 +--- a/connectivity/lwipstack/source/LWIPInterface.cpp ++++ b/connectivity/lwipstack/source/LWIPInterface.cpp +@@ -169,8 +169,12 @@ nsapi_error_t LWIP::Interface::set_dhcp() + + #if LWIP_DHCP + if (dhcp_has_to_be_set) { ++ if(dhcp_started) { ++ dhcp_stop(&netif); ++ dhcp_started = false; ++ } ++ + err_t err = dhcp_start(&netif); +- dhcp_has_to_be_set = false; + if (err) { + connected = NSAPI_STATUS_DISCONNECTED; + if (client_callback) { +-- +2.45.2 + diff --git a/patches/0234-emac-stm32-check-inteface-status-before-link_out.patch b/patches/0234-emac-stm32-check-inteface-status-before-link_out.patch new file mode 100644 index 000000000..16024188b --- /dev/null +++ b/patches/0234-emac-stm32-check-inteface-status-before-link_out.patch @@ -0,0 +1,147 @@ +From e0cad5c4277b3c5ee9cb01a9529df9333612bbeb Mon Sep 17 00:00:00 2001 +From: maidnl +Date: Mon, 8 Jul 2024 12:08:48 +0200 +Subject: [PATCH 234/234] emac: stm32: check inteface status before link_out() + +This allows handling of HAL_ETH_STATE_ERROR, usually on interface teardown/up with strict timings +--- + .../drivers/emac/TARGET_STM/stm32xx_emac.cpp | 27 +++++++++++++++++++ + .../drivers/emac/TARGET_STM/stm32xx_emac.h | 7 +++++ + .../lwipstack/source/LWIPInterfaceEMAC.cpp | 15 +++++++++-- + .../netsocket/include/netsocket/EMAC.h | 11 ++++++++ + .../STM32H7xx_HAL_Driver/stm32h7xx_hal_eth.c | 8 +----- + 5 files changed, 59 insertions(+), 9 deletions(-) + +diff --git a/connectivity/drivers/emac/TARGET_STM/stm32xx_emac.cpp b/connectivity/drivers/emac/TARGET_STM/stm32xx_emac.cpp +index 0230a90665..0e7d64ad34 100644 +--- a/connectivity/drivers/emac/TARGET_STM/stm32xx_emac.cpp ++++ b/connectivity/drivers/emac/TARGET_STM/stm32xx_emac.cpp +@@ -402,6 +402,33 @@ bool STM32_EMAC::low_level_init_successful() + } + #endif // ETH_IP_VERSION_V2 + ++/** ++ * This function get the state of emac interface ++ */ ++int STM32_EMAC::get_interface_status() { ++ return HAL_ETH_GetState(&EthHandle); ++} ++ ++/** ++ * This function returns true if the status of the interface is in the ++ * correct state for the trasmission ++ */ ++bool STM32_EMAC::is_ready_to_tx() { ++ return (HAL_ETH_GetState(&EthHandle) == HAL_ETH_STATE_READY); ++} ++ ++/** ++ * This function reset the emac interface in case the status is in error ++ * Apparently there was not anything to recover from an error state ++ */ ++void STM32_EMAC::restart() { ++ if(HAL_ETH_STATE_ERROR == HAL_ETH_GetState(&EthHandle)){ ++ HAL_ETH_Stop(&EthHandle); ++ HAL_ETH_Start(&EthHandle); ++ } ++} ++ ++ + /** + * This function should do the actual transmission of the packet. The packet is + * contained in the memory buffer chain that is passed to the function. +diff --git a/connectivity/drivers/emac/TARGET_STM/stm32xx_emac.h b/connectivity/drivers/emac/TARGET_STM/stm32xx_emac.h +index cfa6752177..ecc280b2f8 100644 +--- a/connectivity/drivers/emac/TARGET_STM/stm32xx_emac.h ++++ b/connectivity/drivers/emac/TARGET_STM/stm32xx_emac.h +@@ -148,6 +148,13 @@ public: + */ + virtual void set_memory_manager(EMACMemoryManager &mem_mngr); + ++ /* return the status of the interface as integer */ ++ int get_interface_status() override; ++ /* return true if the interface is in the correct state to transmit */ ++ bool is_ready_to_tx() override; ++ /* restart only if the interface is in error state */ ++ void restart() override; ++ + // Called from driver functions + ETH_HandleTypeDef EthHandle; + osThreadId_t thread; /**< Processing thread */ +diff --git a/connectivity/lwipstack/source/LWIPInterfaceEMAC.cpp b/connectivity/lwipstack/source/LWIPInterfaceEMAC.cpp +index 56fbcc0d90..ed4397879e 100644 +--- a/connectivity/lwipstack/source/LWIPInterfaceEMAC.cpp ++++ b/connectivity/lwipstack/source/LWIPInterfaceEMAC.cpp +@@ -27,15 +27,26 @@ + + #if LWIP_ETHERNET + ++ + err_t LWIP::Interface::emac_low_level_output(struct netif *netif, struct pbuf *p) + { ++ bool ret = false; + /* Increase reference counter since lwip stores handle to pbuf and frees + it after output */ + pbuf_ref(p); + + LWIP::Interface *mbed_if = static_cast(netif->state); +- bool ret = mbed_if->emac->link_out(p); +- return ret ? ERR_OK : ERR_IF; ++ ++ if(mbed_if->emac->is_ready_to_tx()) { ++ ret = mbed_if->emac->link_out(p); ++ } ++ else { ++ mbed_if->emac->restart(); ++ ret = mbed_if->emac->link_out(p); ++ } ++ ++ err_t rv = ret ? ERR_OK : ERR_IF; ++ return rv; + } + + void LWIP::Interface::emac_input(emac_mem_buf_t *buf) +diff --git a/connectivity/netsocket/include/netsocket/EMAC.h b/connectivity/netsocket/include/netsocket/EMAC.h +index 515629b5a6..885bc92c01 100644 +--- a/connectivity/netsocket/include/netsocket/EMAC.h ++++ b/connectivity/netsocket/include/netsocket/EMAC.h +@@ -176,6 +176,17 @@ public: + * @param mem_mngr Pointer to memory manager + */ + virtual void set_memory_manager(EMACMemoryManager &mem_mngr) = 0; ++ ++ virtual bool is_ready_to_tx() { ++ return true; ++ } ++ ++ virtual void restart() { ++ } ++ ++ virtual int get_interface_status() { ++ return -1; ++ } + }; + + +diff --git a/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_eth.c b/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_eth.c +index decff79455..df797092fc 100644 +--- a/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_eth.c ++++ b/targets/TARGET_STM/TARGET_STM32H7/STM32Cube_FW/STM32H7xx_HAL_Driver/stm32h7xx_hal_eth.c +@@ -2341,13 +2341,7 @@ HAL_StatusTypeDef HAL_ETH_SetWakeUpFilter(ETH_HandleTypeDef *heth, uint32_t *pFi + */ + HAL_ETH_StateTypeDef HAL_ETH_GetState(ETH_HandleTypeDef *heth) + { +- HAL_ETH_StateTypeDef ret; +- HAL_ETH_StateTypeDef gstate = heth->gState; +- HAL_ETH_StateTypeDef rxstate =heth->RxState; +- +- ret = gstate; +- ret |= rxstate; +- return ret; ++ return heth->gState; + } + + /** +-- +2.45.2 +