From fee7a5a4c10a6e0d330ccb2a57592aabb557c64e Mon Sep 17 00:00:00 2001 From: zxw <2680562856@qq.com> Date: Thu, 15 Oct 2026 17:37:39 +0800 Subject: [PATCH] feat: Add low-power white status LED for SenseCAP Solar P1 Pro Add a dimmed heartbeat for the SenseCAP Solar (P1 Pro) white LED in the repeater firmware, implemented entirely as a SenseCapSolarBoard::loop() override so no example main.cpp needs to be touched: - 100 ms pulse every 5 s, dimmed to 10% brightness via hardware PWM (NRF_PWM0 / HwPWM0, 1 kHz, COUNTERTOP 16000, compare 1600) - during the 4.9 s off phase the PWM peripheral is stopped (ENABLE=0), the pin is released (PSEL disconnect) and GPIO 11 is held LOW, so no HFCLK is demanded between pulses - on successful application-level "start ota" the PWM is stopped and the white LED goes steady full-brightness while BLE OTA is advertised; on failed start the heartbeat keeps blinking - powerOff() stops/releases the heartbeat PWM before shutting the LED off - non-blocking millis() timing with wrap-safe unsigned arithmetic - gated by SENSECAP_SOLAR_WHITE_LED, defined only in the SenseCap_Solar_repeater PlatformIO env, so the room-server and companion firmwares sharing this board class are unaffected No changes to LoRa/repeater forwarding, the blue TX LED, charging LEDs, GPS, power management, config storage or the bootloader. --- .../sensecap_solar/SenseCapSolarBoard.cpp | 77 +++++++++++++++++++ variants/sensecap_solar/SenseCapSolarBoard.h | 23 ++++++ variants/sensecap_solar/platformio.ini | 2 + 3 files changed, 102 insertions(+) diff --git a/variants/sensecap_solar/SenseCapSolarBoard.cpp b/variants/sensecap_solar/SenseCapSolarBoard.cpp index da7964c9..058c1620 100644 --- a/variants/sensecap_solar/SenseCapSolarBoard.cpp +++ b/variants/sensecap_solar/SenseCapSolarBoard.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "SenseCapSolarBoard.h" @@ -50,6 +51,9 @@ void SenseCapSolarBoard::begin() { #ifdef LED_WHITE pinMode(LED_WHITE, OUTPUT); digitalWrite(LED_WHITE, LOW); +#ifdef SENSECAP_SOLAR_WHITE_LED + white_led_last_pulse_at_ = millis(); // first heartbeat pulse 5 s after boot +#endif #endif #ifdef LED_BLUE pinMode(LED_BLUE, OUTPUT); @@ -67,3 +71,76 @@ void SenseCapSolarBoard::begin() { delay(10); // give sx1262 some time to power up } + +#ifdef SENSECAP_SOLAR_WHITE_LED +// Dimmed WHITE heartbeat using hardware PWM (repeater builds only). +// +// Peripheral audit for this firmware: no analogWrite()/tone() callers, no +// NeoPixel/Servo, Bluefruit52Lib uses no PWM, SoftDevice S140 does not own +// any PWM instance -> NRF_PWM0 (HwPWM0) is free. takeOwnership() fences the +// instance so a future analogWrite() caller cannot claim it. +// +// Timing: base clock 16 MHz (DIV_1), COUNTERTOP 16000 -> exactly 1 kHz. +// Duty: HardwarePWM non-inverted values place bit15 (active-HIGH polarity), +// so compare 1600 => HIGH for [0,1600)/16000 => 10% duty, active-high. +static const uint32_t WHITE_LED_PWM_TOKEN = 0x5031574C; // 'P1WL' +static const uint16_t WHITE_LED_PWM_TOP = 16000; // 16 MHz / 16000 = 1 kHz +static const uint16_t WHITE_LED_PWM_DUTY = 1600; // 1600 / 16000 = 10 % +static_assert(WHITE_LED_PWM_TOP <= 0x7FFF, "PWM compare values are 15-bit"); +static_assert(WHITE_LED_PWM_DUTY < WHITE_LED_PWM_TOP, "duty must stay below COUNTERTOP"); + +void SenseCapSolarBoard::whiteLedHeartbeatOn() { + HwPWM0.takeOwnership(WHITE_LED_PWM_TOKEN); // no-op once owned; keeps analogWrite away + HwPWM0.setClockDiv(PWM_PRESCALER_PRESCALER_DIV_1); // 16 MHz base clock + HwPWM0.setMaxValue(WHITE_LED_PWM_TOP); // 1 kHz period + HwPWM0.addPin(LED_WHITE); // drives pin LOW, connects PSEL + HwPWM0.writePin(LED_WHITE, WHITE_LED_PWM_DUTY); // enables + starts 10% PWM +} + +void SenseCapSolarBoard::whiteLedHeartbeatOff() { + HwPWM0.stop(); // ENABLE=0: waveform and its HFCLK demand stop + if (HwPWM0.checkPin(LED_WHITE)) { + HwPWM0.removePin(LED_WHITE); // disconnect PSEL; GPIO block drives again + } + pinMode(LED_WHITE, OUTPUT); + digitalWrite(LED_WHITE, LOW); // deterministic LOW during the 4900 ms off phase +} + +// 100 ms pulse every 5000 ms, non-blocking millis() with wrap-safe unsigned +// arithmetic. Called from loop(); frozen while the OTA steady-on owns the LED. +void SenseCapSolarBoard::updateWhiteStatusLed() { + if (white_led_ota_active_) return; + + const uint32_t now = millis(); + const uint32_t elapsed = (uint32_t)(now - white_led_last_pulse_at_); + if (white_led_pulse_active_ && elapsed >= 100U) { + whiteLedHeartbeatOff(); // stop PWM, pin LOW + white_led_pulse_active_ = false; + } + if (!white_led_pulse_active_ && elapsed >= 5000U) { + whiteLedHeartbeatOn(); // 1 kHz hardware PWM, 10% duty + white_led_pulse_active_ = true; + white_led_last_pulse_at_ = now; + } +} + +void SenseCapSolarBoard::loop() { + mesh::MainBoard::loop(); // keep base housekeeping (watchdog etc.) if it ever gains any + updateWhiteStatusLed(); +} + +// `start ota` keeps WHITE on for as long as the application is advertising +// its BLE OTA service. The heartbeat PWM is stopped and the pin released +// first, so steady-on is a plain full-brightness GPIO drive (no PWM vs +// digitalWrite conflict). On a failed start nothing changes: the heartbeat +// keeps its normal blinking. +bool SenseCapSolarBoard::startOTAUpdate(const char *id, char reply[]) { + const bool started = NRF52Board::startOTAUpdate(id, reply); + if (started) { + white_led_ota_active_ = true; + whiteLedHeartbeatOff(); + digitalWrite(LED_WHITE, LED_STATE_ON); + } + return started; +} +#endif // SENSECAP_SOLAR_WHITE_LED diff --git a/variants/sensecap_solar/SenseCapSolarBoard.h b/variants/sensecap_solar/SenseCapSolarBoard.h index 5a8861c2..1b4b5711 100644 --- a/variants/sensecap_solar/SenseCapSolarBoard.h +++ b/variants/sensecap_solar/SenseCapSolarBoard.h @@ -5,6 +5,19 @@ #include class SenseCapSolarBoard : public NRF52BoardDCDC { +#ifdef SENSECAP_SOLAR_WHITE_LED + // Dimmed white-LED heartbeat state (repeater builds only; enabled with + // -D SENSECAP_SOLAR_WHITE_LED in the PlatformIO env, so room-server and + // companion firmwares sharing this board class are unaffected). + bool white_led_ota_active_ = false; + bool white_led_pulse_active_ = false; + uint32_t white_led_last_pulse_at_ = 0; + + void whiteLedHeartbeatOn(); // start 1 kHz PWM at 10% duty on LED_WHITE + void whiteLedHeartbeatOff(); // stop/release PWM, hold LED_WHITE LOW + void updateWhiteStatusLed(); // non-blocking 100 ms pulse every 5000 ms +#endif + protected: #ifdef NRF52_POWER_MANAGEMENT void initiateShutdown(uint8_t reason) override; @@ -14,6 +27,13 @@ public: SenseCapSolarBoard() : NRF52Board("SENSECAP_SOLAR_OTA") {} void begin(); +#ifdef SENSECAP_SOLAR_WHITE_LED + // Runs the white-LED heartbeat; invoked by the example main loops via the + // existing board.loop() call (mesh::MainBoard::loop() is a no-op by default). + void loop() override; + bool startOTAUpdate(const char *id, char reply[]) override; +#endif + #if defined(P_LORA_TX_LED) void onBeforeTransmit() override { digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED on @@ -38,6 +58,9 @@ public: } void powerOff() override { +#ifdef SENSECAP_SOLAR_WHITE_LED + whiteLedHeartbeatOff(); // stop/release the heartbeat PWM before shutdown +#endif digitalWrite(LED_WHITE, LOW); digitalWrite(LED_BLUE, LOW); diff --git a/variants/sensecap_solar/platformio.ini b/variants/sensecap_solar/platformio.ini index a2a9c85a..60e11e9f 100644 --- a/variants/sensecap_solar/platformio.ini +++ b/variants/sensecap_solar/platformio.ini @@ -45,6 +45,8 @@ build_flags = -D ADVERT_LON=0.0 -D ADMIN_PASSWORD='"password"' -D MAX_NEIGHBOURS=50 +; dimmed white-LED heartbeat + OTA steady-on (repeater builds only) + -D SENSECAP_SOLAR_WHITE_LED=1 ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 build_src_filter = ${SenseCap_Solar.build_src_filter}