From bbcc35e2099d168a15e93b44960e8294a22148a3 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Sat, 13 Jun 2026 12:21:56 -0500 Subject: [PATCH] Stm32 general (#10700) * Attempt to generalize ARCH_STM32 * Trunk * One More ARCH_STM32 * Whoops, one snuck in there * Fix comment to reflect define change Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/FSCommon.h | 4 +- src/Power.cpp | 12 +++--- src/detect/ScanI2C.cpp | 4 +- src/detect/ScanI2CTwoWire.cpp | 2 +- src/gps/GPS.cpp | 8 ++-- src/graphics/TFTDisplay.cpp | 6 +-- src/main.cpp | 2 +- src/memGet.cpp | 2 +- src/modules/AdminModule.cpp | 2 +- src/motion/AccelerometerThread.h | 14 +++++- src/motion/MMC5983MASensor.cpp | 66 ++++++++++++++--------------- src/platform/stm32wl/architecture.h | 1 + src/power.h | 2 +- 13 files changed, 68 insertions(+), 57 deletions(-) diff --git a/src/FSCommon.h b/src/FSCommon.h index e2e77da9a..2f86928af 100644 --- a/src/FSCommon.h +++ b/src/FSCommon.h @@ -14,8 +14,8 @@ #define FILE_O_READ "r" #endif -#if defined(ARCH_STM32WL) -// STM32WL +#if defined(ARCH_STM32) +// STM32 #include "LittleFS.h" #define FSCom InternalFS #define FSBegin() FSCom.begin() diff --git a/src/Power.cpp b/src/Power.cpp index 42bdf3ce4..5b2d41567 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -48,7 +48,7 @@ #include "concurrency/LockGuard.h" #endif -#if defined(ARCH_STM32WL) && defined(BATTERY_PIN) +#if defined(ARCH_STM32) && defined(BATTERY_PIN) #include "stm32yyxx_ll_adc.h" /* Analog read resolution */ @@ -431,7 +431,7 @@ class AnalogBatteryLevel : public HasBatteryLevel float scaled = 0; battery_adcEnable(); -#ifdef ARCH_STM32WL +#ifdef ARCH_STM32 // STM32 ADC with VREFINT runtime calibration Vref = __LL_ADC_CALC_VREFANALOG_VOLTAGE(analogRead(AVREF), LL_ADC_RESOLUTION); raw = analogRead(BATTERY_PIN); @@ -608,7 +608,7 @@ class AnalogBatteryLevel : public HasBatteryLevel bool initial_read_done = false; float last_read_value = (OCV[NUM_OCV_POINTS - 1] * NUM_CELLS); uint32_t last_read_time_ms = 0; -#ifdef ARCH_STM32WL +#ifdef ARCH_STM32 // 3300mV placeholder for STM32 errata where VREFINT factory calibration may be missing // (e.g. STM32U0, see DS14756 Rev 3 ยง2.4.1 "VREFINT offset") uint32_t Vref = 3300; @@ -718,7 +718,7 @@ bool Power::analogInit() #define BATTERY_SENSE_RESOLUTION_BITS 10 #endif -#ifdef ARCH_STM32WL +#ifdef ARCH_STM32 analogReadResolution(BATTERY_SENSE_RESOLUTION_BITS); #elif defined(ARCH_ESP32) // ESP32 needs special analog stuff adc_oneshot_unit_init_cfg_t init_config = { @@ -749,7 +749,7 @@ bool Power::analogInit() // NRF52 ADC init moved to powerHAL_init in nrf52 platform -#if !defined(ARCH_ESP32) && !defined(ARCH_STM32WL) +#if !defined(ARCH_ESP32) && !defined(ARCH_STM32) analogReadResolution(BATTERY_SENSE_RESOLUTION_BITS); #endif @@ -838,7 +838,7 @@ void Power::reboot() } LOG_DEBUG("final reboot!"); ::reboot(); -#elif defined(ARCH_STM32WL) +#elif defined(ARCH_STM32) HAL_NVIC_SystemReset(); #else rebootAtMsec = -1; diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 2c839a38b..aa25ab5e1 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -37,8 +37,8 @@ ScanI2C::FoundDevice ScanI2C::firstKeyboard() const ScanI2C::FoundDevice ScanI2C::firstAccelerometer() const { - ScanI2C::DeviceType types[] = {MPU6050, LIS3DH, BMA423, LSM6DS3, BMX160, STK8BAXX, - ICM20948, QMA6100P, BMM150, BMI270, ICM42607P}; + ScanI2C::DeviceType types[] = {MPU6050, LIS3DH, BMA423, LSM6DS3, BMX160, STK8BAXX, + ICM20948, QMA6100P, BMM150, BMI270, ICM42607P}; return firstOfOrNONE(11, types); } diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index a3bdf1440..5a9ec9a65 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -8,7 +8,7 @@ #if defined(ARCH_PORTDUINO) #include "linux/LinuxHardwareI2C.h" #endif -#if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) +#if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32) #include "meshUtils.h" // vformat #endif diff --git a/src/gps/GPS.cpp b/src/gps/GPS.cpp index 8d6308076..9c5e16864 100644 --- a/src/gps/GPS.cpp +++ b/src/gps/GPS.cpp @@ -47,7 +47,7 @@ template std::size_t array_count(const T (&)[N]) #if defined(ARCH_NRF52) Uart *GPS::_serial_gps = &GPS_SERIAL_PORT; -#elif defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) || defined(ARCH_STM32WL) +#elif defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) || defined(ARCH_STM32) HardwareSerial *GPS::_serial_gps = &GPS_SERIAL_PORT; #elif defined(ARCH_RP2040) SerialUART *GPS::_serial_gps = &GPS_SERIAL_PORT; @@ -642,7 +642,7 @@ bool GPS::verifyCachedProbePresence() return false; } -#if defined(ARCH_NRF52) || defined(ARCH_PORTDUINO) || defined(ARCH_STM32WL) +#if defined(ARCH_NRF52) || defined(ARCH_PORTDUINO) || defined(ARCH_STM32) _serial_gps->end(); _serial_gps->begin(cachedProbeBaud); #elif defined(ARCH_RP2040) @@ -1564,7 +1564,7 @@ GnssModel_t GPS::probe(int serialSpeed) switch (currentStep) { case 0: { -#if defined(ARCH_NRF52) || defined(ARCH_PORTDUINO) || defined(ARCH_STM32WL) +#if defined(ARCH_NRF52) || defined(ARCH_PORTDUINO) || defined(ARCH_STM32) _serial_gps->end(); _serial_gps->begin(serialSpeed); #elif defined(ARCH_RP2040) @@ -1921,7 +1921,7 @@ std::unique_ptr GPS::createGps() #elif defined(ARCH_NRF52) _serial_gps->setPins(new_gps->rx_gpio, new_gps->tx_gpio); _serial_gps->begin(GPS_BAUDRATE); -#elif defined(ARCH_STM32WL) +#elif defined(ARCH_STM32) _serial_gps->setTx(new_gps->tx_gpio); _serial_gps->setRx(new_gps->rx_gpio); _serial_gps->begin(GPS_BAUDRATE); diff --git a/src/graphics/TFTDisplay.cpp b/src/graphics/TFTDisplay.cpp index d7f56f2d9..4ef707efe 100644 --- a/src/graphics/TFTDisplay.cpp +++ b/src/graphics/TFTDisplay.cpp @@ -1533,8 +1533,7 @@ bool TFTDisplay::hasTouch(void) { #ifdef RAK14014 return true; -#elif !defined(M5STACK) && !defined(HACKADAY_COMMUNICATOR) && !defined(HELTEC_MESH_NODE_T096) && \ - !defined(HELTEC_MESH_NODE_T1) +#elif !defined(M5STACK) && !defined(HACKADAY_COMMUNICATOR) && !defined(HELTEC_MESH_NODE_T096) && !defined(HELTEC_MESH_NODE_T1) return tft->touch() != nullptr; #else return false; @@ -1553,8 +1552,7 @@ bool TFTDisplay::getTouch(int16_t *x, int16_t *y) } else { return false; } -#elif !defined(M5STACK) && !defined(HACKADAY_COMMUNICATOR) && !defined(HELTEC_MESH_NODE_T096) && \ - !defined(HELTEC_MESH_NODE_T1) +#elif !defined(M5STACK) && !defined(HACKADAY_COMMUNICATOR) && !defined(HELTEC_MESH_NODE_T096) && !defined(HELTEC_MESH_NODE_T1) return tft->getTouch(x, y); #else return false; diff --git a/src/main.cpp b/src/main.cpp index d1703c82d..1ca52c059 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1191,7 +1191,7 @@ extern meshtastic_DeviceMetadata getDeviceMetadata() // No bluetooth on these targets (yet): // Pico W / 2W may get it at some point // Portduino and ESP32-C6 are excluded because we don't have a working bluetooth stacks integrated yet. -#if defined(ARCH_RP2040) || defined(ARCH_PORTDUINO) || defined(ARCH_STM32WL) || defined(CONFIG_IDF_TARGET_ESP32C6) +#if defined(ARCH_RP2040) || defined(ARCH_PORTDUINO) || defined(ARCH_STM32) || defined(CONFIG_IDF_TARGET_ESP32C6) deviceMetadata.excluded_modules |= meshtastic_ExcludedModules_BLUETOOTH_CONFIG; #endif diff --git a/src/memGet.cpp b/src/memGet.cpp index 42a3430f6..0e7267dfc 100644 --- a/src/memGet.cpp +++ b/src/memGet.cpp @@ -14,7 +14,7 @@ #include #include // sbrk -#ifdef ARCH_STM32WL +#if defined(ARCH_STM32) // Returns the uncommitted sbrk headroom: addressable space between the current heap // break and the stack pointer that has not yet been committed to the arena. static uint32_t sbrkHeadroom() diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 8473930e7..1511a709c 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -546,7 +546,7 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta #if HAS_SCREEN IF_SCREEN(screen->showSimpleBanner("Device is rebooting\ninto DFU mode.", 0)); #endif -#if defined(ARCH_NRF52) || defined(ARCH_RP2040) || defined(ARCH_STM32WL) +#if defined(ARCH_NRF52) || defined(ARCH_RP2040) || defined(ARCH_STM32) enterDfuMode(); #endif break; diff --git a/src/motion/AccelerometerThread.h b/src/motion/AccelerometerThread.h index 3d7341e58..8482dafe4 100755 --- a/src/motion/AccelerometerThread.h +++ b/src/motion/AccelerometerThread.h @@ -15,8 +15,8 @@ #endif #include "BMM150Sensor.h" #include "BMX160Sensor.h" -#include "ICM42607PSensor.h" #include "ICM20948Sensor.h" +#include "ICM42607PSensor.h" #include "LIS3DHSensor.h" #include "LSM6DS3Sensor.h" #include "MPU6050Sensor.h" @@ -92,32 +92,44 @@ class AccelerometerThread : public concurrency::OSThread sensor = new BMA423Sensor(device); break; #endif +#if __has_include() case ScanI2C::DeviceType::MPU6050: sensor = new MPU6050Sensor(device); break; +#endif case ScanI2C::DeviceType::BMX160: sensor = new BMX160Sensor(device); break; +#if __has_include() case ScanI2C::DeviceType::LIS3DH: sensor = new LIS3DHSensor(device); break; +#endif +#if __has_include() case ScanI2C::DeviceType::LSM6DS3: sensor = new LSM6DS3Sensor(device); break; +#endif #ifdef HAS_STK8XXX case ScanI2C::DeviceType::STK8BAXX: sensor = new STK8XXXSensor(device); break; #endif +#if __has_include() case ScanI2C::DeviceType::ICM20948: sensor = new ICM20948Sensor(device); break; +#endif +#if __has_include() case ScanI2C::DeviceType::ICM42607P: sensor = new ICM42607PSensor(device); break; +#endif +#if __has_include() case ScanI2C::DeviceType::BMM150: sensor = new BMM150Sensor(device); break; +#endif #ifdef HAS_BMI270 case ScanI2C::DeviceType::BMI270: sensor = new BMI270Sensor(device); diff --git a/src/motion/MMC5983MASensor.cpp b/src/motion/MMC5983MASensor.cpp index 8e77b4c0f..ba09f4648 100644 --- a/src/motion/MMC5983MASensor.cpp +++ b/src/motion/MMC5983MASensor.cpp @@ -59,40 +59,40 @@ bool MMC5983MASensor::readMagnetometer(float &xGauss, float &yGauss, float &zGau } int32_t MMC5983MASensor::runOnce() - { - float magX = 0, magY = 0, magZ = 0; - if (!readMagnetometer(magX, magY, magZ)) { - return MOTION_SENSOR_CHECK_INTERVAL_MS; - } - - #if !defined(MESHTASTIC_EXCLUDE_SCREEN) - if (doCalibration) { - beginCalibrationDisplay(showingScreen); - updateCalibrationExtrema(magX, magY, magZ, highestX, lowestX, highestY, lowestY, highestZ, lowestZ); - finishCalibrationIfExpired(showingScreen, compassCalibrationFileName, highestX, lowestX, highestY, lowestY, highestZ, - lowestZ); - } - #endif - - magX -= (highestX + lowestX) / 2; - magY -= (highestY + lowestY) / 2; - magZ -= (highestZ + lowestZ) / 2; - - #if !defined(MESHTASTIC_EXCLUDE_SCREEN) && HAS_SCREEN - float heading = atan2f(magY, magX) * RAD_TO_DEG + MMC5983MA_HEADING_OFFSET_DEG; - if (heading < 0.0f) { - heading += 360.0f; - } else if (heading >= 360.0f) { - heading -= 360.0f; - } - - heading = applyCompassOrientation(heading); - if (screen) { - screen->setHeading(heading); - } - #endif - +{ + float magX = 0, magY = 0, magZ = 0; + if (!readMagnetometer(magX, magY, magZ)) { return MOTION_SENSOR_CHECK_INTERVAL_MS; + } + +#if !defined(MESHTASTIC_EXCLUDE_SCREEN) + if (doCalibration) { + beginCalibrationDisplay(showingScreen); + updateCalibrationExtrema(magX, magY, magZ, highestX, lowestX, highestY, lowestY, highestZ, lowestZ); + finishCalibrationIfExpired(showingScreen, compassCalibrationFileName, highestX, lowestX, highestY, lowestY, highestZ, + lowestZ); + } +#endif + + magX -= (highestX + lowestX) / 2; + magY -= (highestY + lowestY) / 2; + magZ -= (highestZ + lowestZ) / 2; + +#if !defined(MESHTASTIC_EXCLUDE_SCREEN) && HAS_SCREEN + float heading = atan2f(magY, magX) * RAD_TO_DEG + MMC5983MA_HEADING_OFFSET_DEG; + if (heading < 0.0f) { + heading += 360.0f; + } else if (heading >= 360.0f) { + heading -= 360.0f; + } + + heading = applyCompassOrientation(heading); + if (screen) { + screen->setHeading(heading); + } +#endif + + return MOTION_SENSOR_CHECK_INTERVAL_MS; } void MMC5983MASensor::calibrate(uint16_t forSeconds) diff --git a/src/platform/stm32wl/architecture.h b/src/platform/stm32wl/architecture.h index e131a0a32..d269b3fa1 100644 --- a/src/platform/stm32wl/architecture.h +++ b/src/platform/stm32wl/architecture.h @@ -1,6 +1,7 @@ #pragma once #define ARCH_STM32WL +#define ARCH_STM32 // // defaults for STM32WL architecture diff --git a/src/power.h b/src/power.h index 993163c39..a4243a306 100644 --- a/src/power.h +++ b/src/power.h @@ -17,7 +17,7 @@ // Device specific curves go in variant.h #ifndef OCV_ARRAY -#if defined(ARCH_STM32WL) && BATTERY_PIN == AVBAT +#if defined(ARCH_STM32) && BATTERY_PIN == AVBAT // STM32 VDD/VBAT absolute maximum is 4V so use an LFP curve #define OCV_ARRAY 3650, 3400, 3340, 3320, 3300, 3280, 3270, 3260, 3240, 3200, 2500 #else