From 0d5495aa25c5107b369724543e8b3410a170f7aa Mon Sep 17 00:00:00 2001 From: pmantoine Date: Mon, 23 Oct 2023 21:28:25 +0800 Subject: [PATCH 1/4] F429ZI edits for ethernet --- EthernetInterface.cpp | 63 +++++++++++++++++++++++++++++++++---------- EthernetInterface.h | 10 +++++++ platformio.ini | 18 ++++++++++++- version.h | 3 ++- 4 files changed, 78 insertions(+), 16 deletions(-) diff --git a/EthernetInterface.cpp b/EthernetInterface.cpp index 5cf531c..993d5d0 100644 --- a/EthernetInterface.cpp +++ b/EthernetInterface.cpp @@ -41,8 +41,11 @@ void EthernetInterface::setup() DIAG(F("Prog Error!")); return; } - if ((singleton=new EthernetInterface())) + DIAG(F("Ethernet Class setup, attempting to instantiate")); + if ((singleton=new EthernetInterface())) { + DIAG(F("Ethernet Class initialized")); return; + } DIAG(F("Ethernet not initialized")); }; @@ -55,24 +58,48 @@ void EthernetInterface::setup() */ EthernetInterface::EthernetInterface() { - byte mac[6]; - DCCTimer::getSimulatedMacAddress(mac); connected=false; - - #ifdef IP_ADDRESS - Ethernet.begin(mac, IP_ADDRESS); - #else - if (Ethernet.begin(mac) == 0) +#if defined(STM32_ETHERNET) + // Set a HOSTNAME for the DHCP request - a nice to have, but hard it seems on LWIP for STM32 + // The default is "lwip", which is **always** set in STM32Ethernet/src/utility/ethernetif.cpp + // for some reason. One can edit it to instead read: + // #if LWIP_NETIF_HOSTNAME + // /* Initialize interface hostname */ + // if (netif->hostname == NULL) + // netif->hostname = "lwip"; + // #endif /* LWIP_NETIF_HOSTNAME */ + // Which seems more useful! We should propose the patch... so the following line actually works! + netif_set_hostname(&gnetif, WIFI_HOSTNAME); // Should probably be passed in the contructor... + #ifdef IP_ADDRESS + if (Ethernet.begin(IP_ADDRESS) == 0) + #else + if (Ethernet.begin() == 0) + #endif // IP_ADDRESS { DIAG(F("Ethernet.begin FAILED")); return; } - #endif +#else // All other architectures + byte mac[6]= { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; + DIAG(F("Ethernet attempting to get MAC address")); + DCCTimer::getSimulatedMacAddress(mac); + DIAG(F("Ethernet got MAC address")); + #ifdef IP_ADDRESS + if (Ethernet.begin(IP_ADDRESS) == 0) + #else + if (Ethernet.begin(mac, IP_ADDRESS) == 0) + #endif + { + DIAG(F("Ethernet.begin FAILED")); + return; + } + if (Ethernet.hardwareStatus() == EthernetNoHardware) { DIAG(F("Ethernet shield not found or W5100")); } - - unsigned long startmilli = millis(); +#endif + + uint32_t startmilli = millis(); while ((millis() - startmilli) < 5500) { // Loop to give time to check for cable connection if (Ethernet.linkStatus() == LinkON) break; @@ -171,17 +198,25 @@ void EthernetInterface::loop2() { return; } // get client from the server + #if defined (STM32_ETHERNET) + // STM32Ethernet doesn't use accept(), just available() + EthernetClient client = server->available(); + #else EthernetClient client = server->accept(); - + #endif // check for new client if (client) { - if (Diag::ETHERNET) DIAG(F("Ethernet: New client ")); byte socket; for (socket = 0; socket < MAX_SOCK_NUM; socket++) { - if (!clients[socket]) + if (clients[socket]) { + if (clients[socket] == client) + break; + } + else //if (!clients[socket]) { + if (Diag::ETHERNET) DIAG(F("Ethernet: New client ")); // On accept() the EthernetServer doesn't track the client anymore // so we store it in our client array if (Diag::ETHERNET) DIAG(F("Socket %d"),socket); diff --git a/EthernetInterface.h b/EthernetInterface.h index 8078c3f..f4f18dd 100644 --- a/EthernetInterface.h +++ b/EthernetInterface.h @@ -35,8 +35,18 @@ #if defined (ARDUINO_TEENSY41) #include //TEENSY Ethernet Treiber #include + #define MAX_SOCK_NUM 4 +#elif defined (ARDUINO_NUCLEO_F429ZI) || defined (ARDUINO_NUCLEO_F439ZI) + #include +// #include "STM32lwipopts.h" + #include + #include + extern "C" struct netif gnetif; + #define STM32_ETHERNET + #define MAX_SOCK_NUM 10 #else #include "Ethernet.h" + #define MAX_SOCK_NUM 4 #endif #include "RingStream.h" diff --git a/platformio.ini b/platformio.ini index 8767ef1..e1ddfb5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -245,7 +245,23 @@ monitor_echo = yes ; Experimental - Ethernet work still in progress ; -; [env:Nucleo-F429ZI] +[env:Nucleo-F429ZI] +platform = ststm32 +board = nucleo_f429zi +framework = arduino +lib_deps = ${env.lib_deps} + arduino-libraries/Ethernet @ ^2.0.1 + stm32duino/STM32Ethernet @ ^1.3.0 + stm32duino/STM32duino LwIP @ ^2.1.2 +build_flags = -std=c++17 -Os -g2 -Wunused-variable +monitor_speed = 115200 +monitor_echo = yes +upload_protocol = stlink + +; Experimental - Ethernet work still in progress +; Commented out as the F439ZI also needs variant files +; +; [env:Nucleo-F439ZI] ; platform = ststm32 ; board = nucleo_f429zi ; framework = arduino diff --git a/version.h b/version.h index 4daafba..3c98c21 100644 --- a/version.h +++ b/version.h @@ -3,7 +3,8 @@ #include "StringFormatter.h" -#define VERSION "5.1.17" +#define VERSION "5.1.17eth" +// 5.1.17e - Initial ethernet code for STM32F429ZI and F439ZI boards // 5.1.17 - Divide out C for config and D for diag commands // 5.1.16 - Remove I2C address from EXTT_TURNTABLE macro to work with MUX, requires separate HAL macro to create // 5.1.15 - LCC/Adapter support and Exrail feature-compile-out. From 9437945745bcf5c861eb6df2c67d9cf8a45e1e5b Mon Sep 17 00:00:00 2001 From: pmantoine Date: Sun, 29 Oct 2023 17:34:51 +0800 Subject: [PATCH 2/4] STM32F439ZI updates --- DCCTimerSTM32.cpp | 2 +- I2CManager_STM32.h | 3 ++- platformio.ini | 24 ++++++++++++------------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/DCCTimerSTM32.cpp b/DCCTimerSTM32.cpp index f2d51ff..13694aa 100644 --- a/DCCTimerSTM32.cpp +++ b/DCCTimerSTM32.cpp @@ -52,7 +52,7 @@ HardwareSerial Serial6(PA12, PA11); // Rx=PA12, Tx=PA11 -- CN10 pins 12 and 14 HardwareSerial Serial3(PC11, PC10); // Rx=PC11, Tx=PC10 -- USART3 - F446RE HardwareSerial Serial5(PD2, PC12); // Rx=PC7, Tx=PC6 -- UART5 - F446RE // On the F446RE, Serial4 and Serial6 also use pins we can't readily map while using the Arduino pins -#elif defined(ARDUINO_NUCLEO_F412ZG) || defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F446ZE) +#elif defined(ARDUINO_NUCLEO_F412ZG) || defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F439ZI) || defined(ARDUINO_NUCLEO_F446ZE) // Nucleo-144 boards don't have Serial1 defined by default HardwareSerial Serial6(PG9, PG14); // Rx=PG9, Tx=PG14 -- USART6 // Serial3 is defined to use USART3 by default, but is in fact used as the diag console diff --git a/I2CManager_STM32.h b/I2CManager_STM32.h index 7e0f547..f386959 100644 --- a/I2CManager_STM32.h +++ b/I2CManager_STM32.h @@ -39,7 +39,8 @@ #if defined(I2C_USE_INTERRUPTS) && defined(ARDUINO_ARCH_STM32) #if defined(ARDUINO_NUCLEO_F401RE) || defined(ARDUINO_NUCLEO_F411RE) || defined(ARDUINO_NUCLEO_F446RE) \ || defined(ARDUINO_NUCLEO_F412ZG) || defined(ARDUINO_NUCLEO_F413ZH) \ - || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F446ZE) + || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F439ZI) \ + || defined(ARDUINO_NUCLEO_F446ZE) // Assume I2C1 for now - default I2C bus on Nucleo-F411RE and likely all Nucleo-64 // and Nucleo-144 variants I2C_TypeDef *s = I2C1; diff --git a/platformio.ini b/platformio.ini index e1ddfb5..f13d4f5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -261,18 +261,18 @@ upload_protocol = stlink ; Experimental - Ethernet work still in progress ; Commented out as the F439ZI also needs variant files ; -; [env:Nucleo-F439ZI] -; platform = ststm32 -; board = nucleo_f429zi -; framework = arduino -; lib_deps = ${env.lib_deps} -; arduino-libraries/Ethernet @ ^2.0.1 -; stm32duino/STM32Ethernet @ ^1.3.0 -; stm32duino/STM32duino LwIP @ ^2.1.2 -; build_flags = -std=c++17 -Os -g2 -Wunused-variable -; monitor_speed = 115200 -; monitor_echo = yes -; upload_protocol = stlink +[env:Nucleo-F439ZI] +platform = ststm32 +board = nucleo_f429zi +framework = arduino +lib_deps = ${env.lib_deps} + arduino-libraries/Ethernet @ ^2.0.1 + stm32duino/STM32Ethernet @ ^1.3.0 + stm32duino/STM32duino LwIP @ ^2.1.2 +build_flags = -std=c++17 -Os -g2 -Wunused-variable +monitor_speed = 115200 +monitor_echo = yes +upload_protocol = stlink [env:Teensy3_2] platform = teensy From 645c9f9f893b71b06e6caf263769ba94d08d0087 Mon Sep 17 00:00:00 2001 From: pmantoine Date: Sun, 29 Oct 2023 17:39:12 +0800 Subject: [PATCH 3/4] STM32 Use UID_BASE to get MAC address --- DCCTimerSTM32.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DCCTimerSTM32.cpp b/DCCTimerSTM32.cpp index 13694aa..17d4b44 100644 --- a/DCCTimerSTM32.cpp +++ b/DCCTimerSTM32.cpp @@ -215,9 +215,9 @@ void DCCTimer::clearPWM() { } void DCCTimer::getSimulatedMacAddress(byte mac[6]) { - volatile uint32_t *serno1 = (volatile uint32_t *)0x1FFF7A10; - volatile uint32_t *serno2 = (volatile uint32_t *)0x1FFF7A14; - // volatile uint32_t *serno3 = (volatile uint32_t *)0x1FFF7A18; + volatile uint32_t *serno1 = (volatile uint32_t *)UID_BASE; + volatile uint32_t *serno2 = (volatile uint32_t *)UID_BASE+4; + // volatile uint32_t *serno3 = (volatile uint32_t *)UID_BASE+8; volatile uint32_t m1 = *serno1; volatile uint32_t m2 = *serno2; From d703c42d79559d03b5eb967b7aa55eca19cdfd97 Mon Sep 17 00:00:00 2001 From: pmantoine Date: Thu, 30 Nov 2023 16:16:44 +0800 Subject: [PATCH 4/4] STM32 Ethernet fix lib_deps --- platformio.ini | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/platformio.ini b/platformio.ini index f13d4f5..93804de 100644 --- a/platformio.ini +++ b/platformio.ini @@ -30,7 +30,6 @@ include_dir = . [env] build_flags = -Wall -Wextra -; monitor_filters = time [env:samd21-dev-usb] platform = atmelsam @@ -250,7 +249,6 @@ platform = ststm32 board = nucleo_f429zi framework = arduino lib_deps = ${env.lib_deps} - arduino-libraries/Ethernet @ ^2.0.1 stm32duino/STM32Ethernet @ ^1.3.0 stm32duino/STM32duino LwIP @ ^2.1.2 build_flags = -std=c++17 -Os -g2 -Wunused-variable @@ -263,10 +261,9 @@ upload_protocol = stlink ; [env:Nucleo-F439ZI] platform = ststm32 -board = nucleo_f429zi +board = nucleo_f439zi framework = arduino lib_deps = ${env.lib_deps} - arduino-libraries/Ethernet @ ^2.0.1 stm32duino/STM32Ethernet @ ^1.3.0 stm32duino/STM32duino LwIP @ ^2.1.2 build_flags = -std=c++17 -Os -g2 -Wunused-variable