From c0b104001d604648f7a0d1df4665159198b72f5a Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 3 Dec 2020 14:41:30 +0100 Subject: [PATCH 1/5] when DHCP fails the static IP can be appllied without problem --- src/utility/stm32_eth.cpp | 65 ++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/src/utility/stm32_eth.cpp b/src/utility/stm32_eth.cpp index 1148ab0..788f3cd 100644 --- a/src/utility/stm32_eth.cpp +++ b/src/utility/stm32_eth.cpp @@ -63,8 +63,8 @@ * They could be used for this library when available */ #ifndef DEFAULT_ETHERNET_TIMER - #define DEFAULT_ETHERNET_TIMER TIM14 - #warning "Default timer used to call ethernet scheduler at regular interval: TIM14" +#define DEFAULT_ETHERNET_TIMER TIM14 +#warning "Default timer used to call ethernet scheduler at regular interval: TIM14" #endif /* Ethernet configuration: user parameters */ @@ -93,8 +93,8 @@ static uint8_t DHCP_Started_by_user = 0; static uint32_t gEhtLinkTickStart = 0; #if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x01060100) - /* Handler for stimer */ - static stimer_t TimHandle; +/* Handler for stimer */ +static stimer_t TimHandle; #endif /*************************** Function prototype *******************************/ @@ -111,6 +111,7 @@ static void TIM_scheduler_Config(void); */ static void Netif_Config(void) { + netif_remove(&gnetif); /* Add the network interface */ netif_add(&gnetif, &(gconfig.ipaddr), &(gconfig.netmask), &(gconfig.gw), NULL, ðernetif_init, ðernet_input); @@ -137,11 +138,11 @@ static void Netif_Config(void) * @retval None */ #if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x01060100) - static void scheduler_callback(stimer_t *htim) +static void scheduler_callback(stimer_t *htim) #elif (STM32_CORE_VERSION <= 0x01080000) - static void scheduler_callback(HardwareTimer *htim) +static void scheduler_callback(HardwareTimer *htim) #else - static void scheduler_callback(void) +static void scheduler_callback(void) #endif { #if (STM32_CORE_VERSION <= 0x01080000) @@ -176,6 +177,7 @@ static void TIM_scheduler_Config(void) { /* Configure HardwareTimer */ HardwareTimer *EthTim = new HardwareTimer(DEFAULT_ETHERNET_TIMER); + EthTim->setMode(1, TIMER_OUTPUT_COMPARE); /* Timer set to 1ms */ EthTim->setOverflow(1000, MICROSEC_FORMAT); @@ -191,47 +193,48 @@ void stm32_eth_init(const uint8_t *mac, const uint8_t *ip, const uint8_t *gw, co if (!initDone) { /* Initialize the LwIP stack */ lwip_init(); + } - if (mac != NULL) { - ethernetif_set_mac_addr(mac); - } // else default value is used: MAC_ADDR0 ... MAC_ADDR5 + if (mac != NULL) { + ethernetif_set_mac_addr(mac); + } // else default value is used: MAC_ADDR0 ... MAC_ADDR5 - if (ip != NULL) { - IP_ADDR4(&(gconfig.ipaddr), ip[0], ip[1], ip[2], ip[3]); - } else { + if (ip != NULL) { + IP_ADDR4(&(gconfig.ipaddr), ip[0], ip[1], ip[2], ip[3]); + } else { #if LWIP_DHCP - ip_addr_set_zero_ip4(&(gconfig.ipaddr)); + ip_addr_set_zero_ip4(&(gconfig.ipaddr)); #else - IP_ADDR4(&(gconfig.ipaddr), IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3); + IP_ADDR4(&(gconfig.ipaddr), IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3); #endif /* LWIP_DHCP */ - } + } - if (gw != NULL) { - IP_ADDR4(&(gconfig.gw), gw[0], gw[1], gw[2], gw[3]); - } else { + if (gw != NULL) { + IP_ADDR4(&(gconfig.gw), gw[0], gw[1], gw[2], gw[3]); + } else { #if LWIP_DHCP - ip_addr_set_zero_ip4(&(gconfig.gw)); + ip_addr_set_zero_ip4(&(gconfig.gw)); #else - IP_ADDR4(&(gconfig.gw), GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); + IP_ADDR4(&(gconfig.gw), GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); #endif /* LWIP_DHCP */ - } + } - if (netmask != NULL) { - IP_ADDR4(&(gconfig.netmask), netmask[0], netmask[1], netmask[2], netmask[3]); - } else { + if (netmask != NULL) { + IP_ADDR4(&(gconfig.netmask), netmask[0], netmask[1], netmask[2], netmask[3]); + } else { #if LWIP_DHCP - ip_addr_set_zero_ip4(&(gconfig.netmask)); + ip_addr_set_zero_ip4(&(gconfig.netmask)); #else - IP_ADDR4(&(gconfig.netmask), NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3); + IP_ADDR4(&(gconfig.netmask), NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3); #endif /* LWIP_DHCP */ - } + } - /* Configure the Network interface */ - Netif_Config(); + /* Configure the Network interface */ + Netif_Config(); + if (!initDone) { // stm32_eth_scheduler() will be called every 1ms. TIM_scheduler_Config(); - initDone = 1; } From 52d40d72bc86fb3d931aad0af299bf9cfa599a3d Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 3 Dec 2020 15:28:17 +0100 Subject: [PATCH 2/5] adding timeout to client connection --- src/EthernetClient.cpp | 10 ++++++---- src/EthernetClient.h | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/EthernetClient.cpp b/src/EthernetClient.cpp index 5db2e47..4417851 100644 --- a/src/EthernetClient.cpp +++ b/src/EthernetClient.cpp @@ -64,18 +64,20 @@ int EthernetClient::connect(IPAddress ip, uint16_t port) _tcp_client->data.p = NULL; _tcp_client->data.available = 0; _tcp_client->state = TCP_NONE; - + + uint32_t startTime = millis(); ip_addr_t ipaddr; tcp_arg(_tcp_client->pcb, _tcp_client); - if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback)) { + if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback) + || ((millis() - startTime) >= _timeout)) { stop(); return 0; } - uint32_t startTime = millis(); + startTime = millis(); while (_tcp_client->state == TCP_NONE) { stm32_eth_scheduler(); - if ((_tcp_client->state == TCP_CLOSING) || ((millis() - startTime) >= 10000)) { + if ((_tcp_client->state == TCP_CLOSING) || ((millis() - startTime) >= _timeout)) { stop(); return 0; } diff --git a/src/EthernetClient.h b/src/EthernetClient.h index 251b580..b3b03f0 100644 --- a/src/EthernetClient.h +++ b/src/EthernetClient.h @@ -52,6 +52,9 @@ class EthernetClient : public Client { { return (_tcp_client->pcb->remote_port); }; + void setTimeout(uint16_t timeout){ + _timeout = timeout; + } friend class EthernetServer; @@ -59,6 +62,7 @@ class EthernetClient : public Client { private: struct tcp_struct *_tcp_client; + uint16_t _timeout = 10000; }; #endif From f80692357ad6ac9435409c41efa10c882bfd7153 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 4 Dec 2020 14:54:26 +0100 Subject: [PATCH 3/5] commit --- src/EthernetClient.cpp | 4 ++-- src/EthernetClient.h | 3 ++- src/utility/stm32_eth.cpp | 14 +++++++------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/EthernetClient.cpp b/src/EthernetClient.cpp index 4417851..489ef75 100644 --- a/src/EthernetClient.cpp +++ b/src/EthernetClient.cpp @@ -64,11 +64,11 @@ int EthernetClient::connect(IPAddress ip, uint16_t port) _tcp_client->data.p = NULL; _tcp_client->data.available = 0; _tcp_client->state = TCP_NONE; - + uint32_t startTime = millis(); ip_addr_t ipaddr; tcp_arg(_tcp_client->pcb, _tcp_client); - if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback) + if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback) || ((millis() - startTime) >= _timeout)) { stop(); return 0; diff --git a/src/EthernetClient.h b/src/EthernetClient.h index b3b03f0..c937a05 100644 --- a/src/EthernetClient.h +++ b/src/EthernetClient.h @@ -52,7 +52,8 @@ class EthernetClient : public Client { { return (_tcp_client->pcb->remote_port); }; - void setTimeout(uint16_t timeout){ + void setTimeout(uint16_t timeout) + { _timeout = timeout; } diff --git a/src/utility/stm32_eth.cpp b/src/utility/stm32_eth.cpp index 788f3cd..73c49a0 100644 --- a/src/utility/stm32_eth.cpp +++ b/src/utility/stm32_eth.cpp @@ -63,8 +63,8 @@ * They could be used for this library when available */ #ifndef DEFAULT_ETHERNET_TIMER -#define DEFAULT_ETHERNET_TIMER TIM14 -#warning "Default timer used to call ethernet scheduler at regular interval: TIM14" + #define DEFAULT_ETHERNET_TIMER TIM14 + #warning "Default timer used to call ethernet scheduler at regular interval: TIM14" #endif /* Ethernet configuration: user parameters */ @@ -93,8 +93,8 @@ static uint8_t DHCP_Started_by_user = 0; static uint32_t gEhtLinkTickStart = 0; #if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x01060100) -/* Handler for stimer */ -static stimer_t TimHandle; + /* Handler for stimer */ + static stimer_t TimHandle; #endif /*************************** Function prototype *******************************/ @@ -138,11 +138,11 @@ static void Netif_Config(void) * @retval None */ #if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x01060100) -static void scheduler_callback(stimer_t *htim) + static void scheduler_callback(stimer_t *htim) #elif (STM32_CORE_VERSION <= 0x01080000) -static void scheduler_callback(HardwareTimer *htim) + static void scheduler_callback(HardwareTimer *htim) #else -static void scheduler_callback(void) + static void scheduler_callback(void) #endif { #if (STM32_CORE_VERSION <= 0x01080000) From 23045302164432ce7632045aaa8e7a607e6a2c2b Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Fri, 11 Mar 2022 10:34:46 +0100 Subject: [PATCH 4/5] Update src/EthernetClient.cpp Co-authored-by: Alexandre Bourdiol <50730894+ABOSTM@users.noreply.github.com> --- src/EthernetClient.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/EthernetClient.cpp b/src/EthernetClient.cpp index 489ef75..82b756c 100644 --- a/src/EthernetClient.cpp +++ b/src/EthernetClient.cpp @@ -68,8 +68,7 @@ int EthernetClient::connect(IPAddress ip, uint16_t port) uint32_t startTime = millis(); ip_addr_t ipaddr; tcp_arg(_tcp_client->pcb, _tcp_client); - if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback) - || ((millis() - startTime) >= _timeout)) { +if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback)) { stop(); return 0; } From c668bdd0f3d61aea3188e030f319e2dcda3f23cb Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Fri, 11 Mar 2022 10:47:24 +0100 Subject: [PATCH 5/5] Update src/EthernetClient.cpp --- src/EthernetClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EthernetClient.cpp b/src/EthernetClient.cpp index 82b756c..4fd0a7f 100644 --- a/src/EthernetClient.cpp +++ b/src/EthernetClient.cpp @@ -68,7 +68,7 @@ int EthernetClient::connect(IPAddress ip, uint16_t port) uint32_t startTime = millis(); ip_addr_t ipaddr; tcp_arg(_tcp_client->pcb, _tcp_client); -if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback)) { + if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback)) { stop(); return 0; }