From 988011475c84e7f02a7adb2a17a637cc8075a4b0 Mon Sep 17 00:00:00 2001 From: pmantoine Date: Tue, 20 Jun 2023 08:34:27 +0800 Subject: [PATCH] STM32 Serial port handling for WiFi --- DCCTimerSTM32.cpp | 16 +++++++++------- WifiInterface.cpp | 41 +++++++++++++++++++++++++++-------------- defines.h | 2 -- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/DCCTimerSTM32.cpp b/DCCTimerSTM32.cpp index 17ad84e..cc60547 100644 --- a/DCCTimerSTM32.cpp +++ b/DCCTimerSTM32.cpp @@ -36,23 +36,25 @@ #include "DIAG.h" #if defined(ARDUINO_NUCLEO_F411RE) -// Nucleo-64 boards don't have Serial1 defined by default +// Nucleo-64 boards don't have additional serial ports defined by default HardwareSerial Serial1(PB7, PA15); // Rx=PB7, Tx=PA15 -- CN7 pins 17 and 21 - F411RE // Serial2 is defined to use USART2 by default, but is in fact used as the diag console // via the debugger on the Nucleo-64. It is therefore unavailable for other DCC-EX uses like WiFi, DFPlayer, etc. // Let's define Serial6 as an additional serial port (the only other option for the Nucleo-64s) -HardwareSerial Serial3(PA12, PA11); // Rx=PA12, Tx=PA11 -- CN10 pins 12 and 14 - F411RE +HardwareSerial Serial6(PA12, PA11); // Rx=PA12, Tx=PA11 -- CN10 pins 12 and 14 - F411RE #elif defined(ARDUINO_NUCLEO_F446RE) -// Nucleo-64 boards don't have Serial1 defined by default +// Nucleo-64 boards don't have additional serial ports defined by default +// On the F446RE, Serial1 isn't really useable as it's Rx/Tx pair sit on already used D2/D10 pins // HardwareSerial Serial1(PA10, PB6); // Rx=PA10 (D2), Tx=PB6 (D10) -- CN10 pins 17 and 9 - F446RE // Serial2 is defined to use USART2 by default, but is in fact used as the diag console // via the debugger on the Nucleo-64. It is therefore unavailable for other DCC-EX uses like WiFi, DFPlayer, etc. -HardwareSerial Serial1(PC11, PC10); // Rx=PC11, Tx=PC10 -- USART3 - F446RE -HardwareSerial Serial3(PD2, PC12); // Rx=PC7, Tx=PC6 -- UART5 - F446RE -// NB: USART3 and USART6 are available but as yet undefined +// On the F446RE, Serial3 and Serial5 are easy to use: +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_F429ZI) || defined(ARDUINO_NUCLEO_F446ZE) // Nucleo-144 boards don't have Serial1 defined by default -HardwareSerial Serial1(PG9, PG14); // Rx=PG9, Tx=PG14 -- USART6 +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 // via the debugger on the Nucleo-144. It is therefore unavailable for other DCC-EX uses like WiFi, DFPlayer, etc. #else diff --git a/WifiInterface.cpp b/WifiInterface.cpp index 732b393..3a88aea 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -52,20 +52,32 @@ Stream * WifiInterface::wifiStream; #if (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)) #define NUM_SERIAL 3 +#define SERIAL1 Serial1 +#define SERIAL3 Serial3 +#endif + +#if defined(ARDUINO_ARCH_STM32) +// Handle serial ports availability on STM32 for variants! +// #undef NUM_SERIAL +#if defined(ARDUINO_NUCLEO_F411RE) +#define NUM_SERIAL 3 +#define SERIAL1 Serial1 +#define SERIAL3 Serial6 +#elif defined(ARDUINO_NUCLEO_F446RE) +#define NUM_SERIAL 3 +#define SERIAL1 Serial3 +#define SERIAL3 Serial5 +#elif defined(ARDUINO_NUCLEO_F412ZG) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F446ZE) +#define NUM_SERIAL 2 +#define SERIAL1 Serial6 +#endif #endif #ifndef NUM_SERIAL #define NUM_SERIAL 1 +#define SERIAL1 Serial1 #endif -// For STM32 we need to define Serial3 in the platform specific -// DCCTimerSTM32.cpp file, we here make the assumption that it -// exists to link against. -#ifdef ARDUINO_ARCH_STM32 -#if NUM_SERIAL > 2 -extern HardwareSerial Serial3; -#endif -#endif bool WifiInterface::setup(long serial_link_speed, const FSH *wifiESSID, const FSH *wifiPassword, @@ -84,14 +96,15 @@ bool WifiInterface::setup(long serial_link_speed, (void) port; (void) channel; #endif - + +// See if the WiFi is attached to the first serial port #if NUM_SERIAL > 0 && !defined(SERIAL1_COMMANDS) - Serial1.begin(serial_link_speed); - wifiUp = setup(Serial1, wifiESSID, wifiPassword, hostname, port, channel); + SERIAL1.begin(serial_link_speed); + wifiUp = setup(SERIAL1, wifiESSID, wifiPassword, hostname, port, channel); #endif // Other serials are tried, depending on hardware. -// Currently only the Arduino Mega 2560 has usable Serial2 +// Currently only the Arduino Mega 2560 has usable Serial2 (Nucleo-64 boards use Serial 2 for console!) #if defined(ARDUINO_AVR_MEGA2560) #if NUM_SERIAL > 1 && !defined(SERIAL2_COMMANDS) if (wifiUp == WIFI_NOAT) @@ -107,8 +120,8 @@ bool WifiInterface::setup(long serial_link_speed, #if NUM_SERIAL > 2 && !defined(SERIAL3_COMMANDS) if (wifiUp == WIFI_NOAT) { - Serial3.begin(serial_link_speed); - wifiUp = setup(Serial3, wifiESSID, wifiPassword, hostname, port, channel); + SERIAL3.begin(serial_link_speed); + wifiUp = setup(SERIAL3, wifiESSID, wifiPassword, hostname, port, channel); } #endif diff --git a/defines.h b/defines.h index 3f5c3ba..48f7c6b 100644 --- a/defines.h +++ b/defines.h @@ -147,8 +147,6 @@ #ifndef I2C_USE_WIRE #define I2C_USE_WIRE #endif - #undef NUM_SERIAL - #define NUM_SERIAL 3 /* TODO when ready #elif defined(ARDUINO_ARCH_RP2040)