Enable telemetry and I2C sensors on STM32WL (except accelerometers) (#7008)

* Update platformio inis for stm32 platform and wio-e5 variant for enabling i2c

* Don't reference timezone functions if MESHTASTIC_EXCLUDE_TZ is defined

* Use custom pow_of_two in RadioInterface instead of floating-point pow()

* First pass: enable sensors for STM32wL

* Fix AirQualityTelemetryModule being created if the PM25AQI header is missing

* Link in power sensor libraries

* more ini tweaks

* Add =1 to EXCLUDE defines, fix indentation.

* Drop HAS_WIRE in ini, it's defined in architecture.h

* Fix build when power sensor libraries are missing

Make MAX sensor integration into Power.cpp optional based on its library header existing.
Also make NullSensor expose a voltage and current sensor, because Power calls directly into these for INA sensors.
This lets us remove all the deps for the STM32WL platform.

* Change default I2C for RAK3172 to be I2C1, not I2C2

* Respect the laws of mathematics (oops)
This commit is contained in:
Chloe Bethel 2025-07-02 12:01:45 +01:00 committed by GitHub
parent 17f8303e01
commit d494c23a88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 71 additions and 38 deletions

View File

@ -16,14 +16,17 @@ build_flags =
${arduino_base.build_flags} ${arduino_base.build_flags}
-flto -flto
-Isrc/platform/stm32wl -g -Isrc/platform/stm32wl -g
-DMESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR -DMESHTASTIC_EXCLUDE_AUDIO=1
-DMESHTASTIC_EXCLUDE_INPUTBROKER -DMESHTASTIC_EXCLUDE_ATAK=1 ; ATAK is quite big, disable it for big flash savings.
-DMESHTASTIC_EXCLUDE_I2C -DMESHTASTIC_EXCLUDE_INPUTBROKER=1
-DMESHTASTIC_EXCLUDE_POWERMON -DMESHTASTIC_EXCLUDE_POWERMON=1
-DMESHTASTIC_EXCLUDE_SCREEN -DMESHTASTIC_EXCLUDE_SCREEN=1
-DMESHTASTIC_EXCLUDE_MQTT -DMESHTASTIC_EXCLUDE_MQTT=1
-DMESHTASTIC_EXCLUDE_BLUETOOTH -DMESHTASTIC_EXCLUDE_BLUETOOTH=1
-DMESHTASTIC_EXCLUDE_GPS -DMESHTASTIC_EXCLUDE_GPS=1
-DMESHTASTIC_EXCLUDE_WIFI=1
-DMESHTASTIC_EXCLUDE_TZ=1 ; Exclude TZ to save some flash space.
-DPIO_FRAMEWORK_ARDUINO_NANOLIB_FLOAT_PRINTF ; This is REQUIRED for at least traceroute debug prints - without it the length ends up uninitialized.
;-DDEBUG_MUTE ;-DDEBUG_MUTE
-fmerge-all-constants -fmerge-all-constants
-ffunction-sections -ffunction-sections
@ -39,9 +42,9 @@ debug_tool = stlink
lib_deps = lib_deps =
${env.lib_deps} ${env.lib_deps}
${radiolib_base.lib_deps} ${radiolib_base.lib_deps}
# renovate: datasource=git-refs depName=caveman99-stm32-Crypto packageName=https://github.com/caveman99/Crypto gitBranch=main # renovate: datasource=git-refs depName=caveman99-stm32-Crypto packageName=https://github.com/caveman99/Crypto gitBranch=main
https://github.com/caveman99/Crypto/archive/eae9c768054118a9399690f8af202853d1ae8516.zip https://github.com/caveman99/Crypto/archive/eae9c768054118a9399690f8af202853d1ae8516.zip
lib_ignore = lib_ignore =
mathertel/OneButton@2.6.1 mathertel/OneButton@2.6.1
Wire

View File

@ -103,7 +103,7 @@ NullSensor ina3221Sensor;
#endif #endif
#if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_STM32WL) #if !MESHTASTIC_EXCLUDE_I2C
#include "modules/Telemetry/Sensor/MAX17048Sensor.h" #include "modules/Telemetry/Sensor/MAX17048Sensor.h"
#include <utility> #include <utility>
extern std::pair<uint8_t, TwoWire *> nodeTelemetrySensorsMap[_meshtastic_TelemetrySensorType_MAX + 1]; extern std::pair<uint8_t, TwoWire *> nodeTelemetrySensorsMap[_meshtastic_TelemetrySensorType_MAX + 1];
@ -278,7 +278,7 @@ class AnalogBatteryLevel : public HasBatteryLevel
} }
#endif #endif
#if HAS_TELEMETRY && !defined(ARCH_STM32WL) && !defined(HAS_PMU) && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR #if HAS_TELEMETRY && !defined(HAS_PMU) && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
if (hasINA()) { if (hasINA()) {
return getINAVoltage(); return getINAVoltage();
} }
@ -456,8 +456,7 @@ class AnalogBatteryLevel : public HasBatteryLevel
#ifdef EXT_CHRG_DETECT #ifdef EXT_CHRG_DETECT
return digitalRead(EXT_CHRG_DETECT) == ext_chrg_detect_value; return digitalRead(EXT_CHRG_DETECT) == ext_chrg_detect_value;
#else #else
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_STM32WL) && \ #if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(DISABLE_INA_CHARGING_DETECTION)
!defined(DISABLE_INA_CHARGING_DETECTION)
if (hasINA()) { if (hasINA()) {
// get current flow from INA sensor - negative value means power flowing into the battery // get current flow from INA sensor - negative value means power flowing into the battery
// default assuming BATTERY+ <--> INA_VIN+ <--> SHUNT RESISTOR <--> INA_VIN- <--> LOAD // default assuming BATTERY+ <--> INA_VIN+ <--> SHUNT RESISTOR <--> INA_VIN- <--> LOAD
@ -503,7 +502,7 @@ class AnalogBatteryLevel : public HasBatteryLevel
} }
#endif #endif
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_STM32WL) #if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
uint16_t getINAVoltage() uint16_t getINAVoltage()
{ {
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219].first == config.power.device_battery_ina_address) { if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219].first == config.power.device_battery_ina_address) {
@ -1161,7 +1160,7 @@ bool Power::axpChipInit()
#endif #endif
} }
#if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) #if !MESHTASTIC_EXCLUDE_I2C && __has_include(<Adafruit_MAX1704X.h>)
/** /**
* Wrapper class for an I2C MAX17048 Lipo battery sensor. * Wrapper class for an I2C MAX17048 Lipo battery sensor.

View File

@ -244,11 +244,15 @@ bool perhapsSetRTC(RTCQuality q, struct tm &t)
*/ */
int32_t getTZOffset() int32_t getTZOffset()
{ {
#if MESHTASTIC_EXCLUDE_TZ
return 0;
#else
time_t now = getTime(false); time_t now = getTime(false);
struct tm *gmt; struct tm *gmt;
gmt = gmtime(&now); gmt = gmtime(&now);
gmt->tm_isdst = -1; gmt->tm_isdst = -1;
return (int32_t)difftime(now, mktime(gmt)); return (int32_t)difftime(now, mktime(gmt));
#endif
} }
/** /**

View File

@ -12,6 +12,12 @@
#include <pb_decode.h> #include <pb_decode.h>
#include <pb_encode.h> #include <pb_encode.h>
// Calculate 2^n without calling pow()
uint32_t pow_of_2(uint32_t n)
{
return 1 << n;
}
#define RDEF(name, freq_start, freq_end, duty_cycle, spacing, power_limit, audio_permitted, frequency_switching, wide_lora) \ #define RDEF(name, freq_start, freq_end, duty_cycle, spacing, power_limit, audio_permitted, frequency_switching, wide_lora) \
{ \ { \
meshtastic_Config_LoRaConfig_RegionCode_##name, freq_start, freq_end, duty_cycle, spacing, power_limit, audio_permitted, \ meshtastic_Config_LoRaConfig_RegionCode_##name, freq_start, freq_end, duty_cycle, spacing, power_limit, audio_permitted, \
@ -246,7 +252,7 @@ uint32_t RadioInterface::getRetransmissionMsec(const meshtastic_MeshPacket *p)
float channelUtil = airTime->channelUtilizationPercent(); float channelUtil = airTime->channelUtilizationPercent();
uint8_t CWsize = map(channelUtil, 0, 100, CWmin, CWmax); uint8_t CWsize = map(channelUtil, 0, 100, CWmin, CWmax);
// Assuming we pick max. of CWsize and there will be a client with SNR at half the range // Assuming we pick max. of CWsize and there will be a client with SNR at half the range
return 2 * packetAirtime + (pow(2, CWsize) + 2 * CWmax + pow(2, int((CWmax + CWmin) / 2))) * slotTimeMsec + return 2 * packetAirtime + (pow_of_2(CWsize) + 2 * CWmax + pow_of_2(int((CWmax + CWmin) / 2))) * slotTimeMsec +
PROCESSING_TIME_MSEC; PROCESSING_TIME_MSEC;
} }
@ -259,7 +265,7 @@ uint32_t RadioInterface::getTxDelayMsec()
float channelUtil = airTime->channelUtilizationPercent(); float channelUtil = airTime->channelUtilizationPercent();
uint8_t CWsize = map(channelUtil, 0, 100, CWmin, CWmax); uint8_t CWsize = map(channelUtil, 0, 100, CWmin, CWmax);
// LOG_DEBUG("Current channel utilization is %f so setting CWsize to %d", channelUtil, CWsize); // LOG_DEBUG("Current channel utilization is %f so setting CWsize to %d", channelUtil, CWsize);
return random(0, pow(2, CWsize)) * slotTimeMsec; return random(0, pow_of_2(CWsize)) * slotTimeMsec;
} }
/** The CW size to use when calculating SNR_based delays */ /** The CW size to use when calculating SNR_based delays */
@ -279,7 +285,7 @@ uint32_t RadioInterface::getTxDelayMsecWeightedWorst(float snr)
{ {
uint8_t CWsize = getCWsize(snr); uint8_t CWsize = getCWsize(snr);
// offset the maximum delay for routers: (2 * CWmax * slotTimeMsec) // offset the maximum delay for routers: (2 * CWmax * slotTimeMsec)
return (2 * CWmax * slotTimeMsec) + pow(2, CWsize) * slotTimeMsec; return (2 * CWmax * slotTimeMsec) + pow_of_2(CWsize) * slotTimeMsec;
} }
/** The delay to use when we want to flood a message */ /** The delay to use when we want to flood a message */
@ -296,7 +302,7 @@ uint32_t RadioInterface::getTxDelayMsecWeighted(float snr)
LOG_DEBUG("rx_snr found in packet. Router: setting tx delay:%d", delay); LOG_DEBUG("rx_snr found in packet. Router: setting tx delay:%d", delay);
} else { } else {
// offset the maximum delay for routers: (2 * CWmax * slotTimeMsec) // offset the maximum delay for routers: (2 * CWmax * slotTimeMsec)
delay = (2 * CWmax * slotTimeMsec) + random(0, pow(2, CWsize)) * slotTimeMsec; delay = (2 * CWmax * slotTimeMsec) + random(0, pow_of_2(CWsize)) * slotTimeMsec;
LOG_DEBUG("rx_snr found in packet. Setting tx delay:%d", delay); LOG_DEBUG("rx_snr found in packet. Setting tx delay:%d", delay);
} }
@ -596,7 +602,7 @@ void RadioInterface::applyModemConfig()
uint32_t RadioInterface::computeSlotTimeMsec() uint32_t RadioInterface::computeSlotTimeMsec()
{ {
float sumPropagationTurnaroundMACTime = 0.2 + 0.4 + 7; // in milliseconds float sumPropagationTurnaroundMACTime = 0.2 + 0.4 + 7; // in milliseconds
float symbolTime = pow(2, sf) / bw; // in milliseconds float symbolTime = pow_of_2(sf) / bw; // in milliseconds
if (myRegion->wideLora) { if (myRegion->wideLora) {
// CAD duration derived from AN1200.22 of SX1280 // CAD duration derived from AN1200.22 of SX1280

View File

@ -213,11 +213,14 @@ void setupModules()
#if HAS_TELEMETRY #if HAS_TELEMETRY
new DeviceTelemetryModule(); new DeviceTelemetryModule();
#endif #endif
// TODO: How to improve this?
#if HAS_SENSOR && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR #if HAS_SENSOR && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
new EnvironmentTelemetryModule(); new EnvironmentTelemetryModule();
#if __has_include("Adafruit_PM25AQI.h")
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_PMSA003I].first > 0) { if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_PMSA003I].first > 0) {
new AirQualityTelemetryModule(); new AirQualityTelemetryModule();
} }
#endif
#if !MESHTASTIC_EXCLUDE_HEALTH_TELEMETRY #if !MESHTASTIC_EXCLUDE_HEALTH_TELEMETRY
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_MAX30102].first > 0 || if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_MAX30102].first > 0 ||
nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_MLX90614].first > 0) { nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_MLX90614].first > 0) {

View File

@ -1,6 +1,6 @@
#include "MAX17048Sensor.h" #include "MAX17048Sensor.h"
#if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_STM32WL) && __has_include(<Adafruit_MAX1704X.h>) #if !MESHTASTIC_EXCLUDE_I2C && __has_include(<Adafruit_MAX1704X.h>)
MAX17048Singleton *MAX17048Singleton::GetInstance() MAX17048Singleton *MAX17048Singleton::GetInstance()
{ {

View File

@ -5,7 +5,7 @@
#include "configuration.h" #include "configuration.h"
#if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_STM32WL) && __has_include(<Adafruit_MAX1704X.h>) #if !MESHTASTIC_EXCLUDE_I2C && __has_include(<Adafruit_MAX1704X.h>)
// Samples to store in a buffer to determine if the battery is charging or discharging // Samples to store in a buffer to determine if the battery is charging or discharging
#define MAX17048_CHARGING_SAMPLES 3 #define MAX17048_CHARGING_SAMPLES 3

View File

@ -20,4 +20,15 @@ bool NullSensor::getMetrics(meshtastic_Telemetry *measurement)
{ {
return false; return false;
} }
uint16_t NullSensor::getBusVoltageMv()
{
return 0;
}
int16_t NullSensor::getCurrentMa()
{
return 0;
}
#endif #endif

View File

@ -4,9 +4,12 @@
#pragma once #pragma once
#include "../mesh/generated/meshtastic/telemetry.pb.h" #include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "TelemetrySensor.h"
class NullSensor : public TelemetrySensor #include "CurrentSensor.h"
#include "TelemetrySensor.h"
#include "VoltageSensor.h"
class NullSensor : public TelemetrySensor, VoltageSensor, CurrentSensor
{ {
protected: protected:
@ -17,6 +20,9 @@ class NullSensor : public TelemetrySensor
virtual int32_t runOnce() override; virtual int32_t runOnce() override;
virtual bool getMetrics(meshtastic_Telemetry *measurement) override; virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
int32_t runTrigger() { return 0; } int32_t runTrigger() { return 0; }
virtual uint16_t getBusVoltageMv() override;
virtual int16_t getCurrentMa() override;
}; };
#endif #endif

View File

@ -12,6 +12,9 @@
#ifndef HAS_TELEMETRY #ifndef HAS_TELEMETRY
#define HAS_TELEMETRY 1 #define HAS_TELEMETRY 1
#endif #endif
#ifndef HAS_WIRE
#define HAS_WIRE 1
#endif
// //
// set HW_VENDOR // set HW_VENDOR
@ -28,4 +31,4 @@
#define SX126X_CS 1000 #define SX126X_CS 1000
#define SX126X_DIO1 1001 #define SX126X_DIO1 1001
#define SX126X_RESET 1003 #define SX126X_RESET 1003
#define SX126X_BUSY 1004 #define SX126X_BUSY 1004

View File

@ -81,7 +81,7 @@ extern NullSensor ina3221Sensor;
#endif #endif
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_STM32WL) #if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
#if __has_include(<Adafruit_MAX1704X.h>) #if __has_include(<Adafruit_MAX1704X.h>)
#include "modules/Telemetry/Sensor/MAX17048Sensor.h" #include "modules/Telemetry/Sensor/MAX17048Sensor.h"
extern MAX17048Sensor max17048Sensor; extern MAX17048Sensor max17048Sensor;

View File

@ -5,6 +5,8 @@ board_upload.maximum_size = 233472 ; reserve the last 28KB for filesystem
build_flags = build_flags =
${stm32_base.build_flags} ${stm32_base.build_flags}
-Ivariants/rak3172 -Ivariants/rak3172
-DPIN_WIRE_SDA=PA11
-DPIN_WIRE_SCL=PA12
-DHAL_DAC_MODULE_ONLY -DHAL_DAC_MODULE_ONLY
-DHAL_RNG_MODULE_ENABLED -DHAL_RNG_MODULE_ENABLED
-DRADIOLIB_EXCLUDE_SX128X=1 -DRADIOLIB_EXCLUDE_SX128X=1
@ -18,6 +20,5 @@ build_flags =
-DMESHTASTIC_EXCLUDE_SCREEN=1 -DMESHTASTIC_EXCLUDE_SCREEN=1
-DMESHTASTIC_EXCLUDE_MQTT=1 -DMESHTASTIC_EXCLUDE_MQTT=1
-DMESHTASTIC_EXCLUDE_POWERMON=1 -DMESHTASTIC_EXCLUDE_POWERMON=1
;-DPIO_FRAMEWORK_ARDUINO_NANOLIB_FLOAT_PRINTF
;-DCFG_DEBUG ;-DCFG_DEBUG
upload_port = stlink upload_port = stlink

View File

@ -8,20 +8,17 @@ build_flags =
-DSERIAL_UART_INSTANCE=1 -DSERIAL_UART_INSTANCE=1
-DPIN_SERIAL_RX=PB7 -DPIN_SERIAL_RX=PB7
-DPIN_SERIAL_TX=PB6 -DPIN_SERIAL_TX=PB6
-DPIN_WIRE_SDA=PA15
-DPIN_WIRE_SCL=PB15
-DHAL_DAC_MODULE_ONLY -DHAL_DAC_MODULE_ONLY
-DHAL_RNG_MODULE_ENABLED -DHAL_RNG_MODULE_ENABLED
-DRADIOLIB_EXCLUDE_SX128X=1 -DRADIOLIB_EXCLUDE_SX128X=1
-DRADIOLIB_EXCLUDE_SX127X=1 -DRADIOLIB_EXCLUDE_SX127X=1
-DRADIOLIB_EXCLUDE_LR11X0=1 -DRADIOLIB_EXCLUDE_LR11X0=1
-DMESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR=1 -DHAS_SENSOR
-DMESHTASTIC_EXCLUDE_I2C=1
-DMESHTASTIC_EXCLUDE_WIFI=1
-DMESHTASTIC_EXCLUDE_BLUETOOTH=1
-DMESHTASTIC_EXCLUDE_GPS=1
-DMESHTASTIC_EXCLUDE_SCREEN=1
-DMESHTASTIC_EXCLUDE_MQTT=1
-DMESHTASTIC_EXCLUDE_POWERMON=1
;-DPIO_FRAMEWORK_ARDUINO_NANOLIB_FLOAT_PRINTF
;-DCFG_DEBUG
upload_port = stlink upload_port = stlink
lib_deps =
${stm32_base.lib_deps}
# Add your custom sensor here!