From a751b4c37784a8816fc04e35cd28d57c10df446c Mon Sep 17 00:00:00 2001 From: Olivier Booklage Date: Mon, 23 Nov 2026 21:20:43 +0100 Subject: [PATCH 1/5] fix(thinknode_m6): Free the I2C bus before probing the RTC A reset during an RTC read left SDA held low, and the RTC probe then hung every boot. Up to nine clock pulses release the bus. --- variants/thinknode_m6/ThinkNodeM6Board.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/variants/thinknode_m6/ThinkNodeM6Board.cpp b/variants/thinknode_m6/ThinkNodeM6Board.cpp index f83f274f96b..cde7e70d628 100644 --- a/variants/thinknode_m6/ThinkNodeM6Board.cpp +++ b/variants/thinknode_m6/ThinkNodeM6Board.cpp @@ -5,9 +5,28 @@ #include +// A reset in the middle of an RTC read can leave the RTC holding SDA low, +// and the RTC probe at boot then hangs forever. Up to nine clock pulses let +// it finish its byte and release the bus. +static void recoverI2cBus() { + pinMode(PIN_WIRE_SDA, INPUT_PULLUP); + pinMode(PIN_WIRE_SCL, INPUT_PULLUP); + MESH_DEBUG_PRINTLN("I2C at boot: SDA=%d SCL=%d", digitalRead(PIN_WIRE_SDA), digitalRead(PIN_WIRE_SCL)); + pinMode(PIN_WIRE_SCL, OUTPUT); + digitalWrite(PIN_WIRE_SCL, HIGH); + for (int i = 0; i < 9 && digitalRead(PIN_WIRE_SDA) == LOW; i++) { + digitalWrite(PIN_WIRE_SCL, LOW); + delayMicroseconds(5); + digitalWrite(PIN_WIRE_SCL, HIGH); + delayMicroseconds(5); + } + pinMode(PIN_WIRE_SCL, INPUT_PULLUP); +} + void ThinkNodeM6Board::begin() { NRF52Board::begin(); + recoverI2cBus(); Wire.begin(); #ifdef P_LORA_TX_LED From b4dd93dfe1737e1e36a55e2233ca6f898da1d7a3 Mon Sep 17 00:00:00 2001 From: Olivier Booklage Date: Mon, 23 Nov 2026 21:20:55 +0100 Subject: [PATCH 2/5] feat(thinknode_m6): Add a 90 s watchdog It resets the board when the main loop stops, as Meshtastic does on the M6. It pauses while the CPU sleeps. --- variants/thinknode_m6/ThinkNodeM6Board.cpp | 19 +++++++++++++++++++ variants/thinknode_m6/ThinkNodeM6Board.h | 3 +++ 2 files changed, 22 insertions(+) diff --git a/variants/thinknode_m6/ThinkNodeM6Board.cpp b/variants/thinknode_m6/ThinkNodeM6Board.cpp index cde7e70d628..f7e994b9353 100644 --- a/variants/thinknode_m6/ThinkNodeM6Board.cpp +++ b/variants/thinknode_m6/ThinkNodeM6Board.cpp @@ -23,9 +23,24 @@ static void recoverI2cBus() { pinMode(PIN_WIRE_SCL, INPUT_PULLUP); } +// Resets the board when loop() stops feeding it, for example on an I2C read +// that never ends. Like Meshtastic: 90 s, paused while the CPU sleeps or a +// debugger halts it. A pin reset stops it, a soft reset does not. +static void startWatchdog() { + if (NRF_WDT->RUNSTATUS) { + return; // still running after a soft reset, and locked until the next one + } + NRF_WDT->CONFIG = (WDT_CONFIG_SLEEP_Pause << WDT_CONFIG_SLEEP_Pos) + | (WDT_CONFIG_HALT_Pause << WDT_CONFIG_HALT_Pos); + NRF_WDT->CRV = M6_WATCHDOG_SECS * 32768; // counts the 32.768 kHz clock + NRF_WDT->RREN = WDT_RREN_RR0_Msk; + NRF_WDT->TASKS_START = 1; +} + void ThinkNodeM6Board::begin() { NRF52Board::begin(); + startWatchdog(); // first, so that a hang later in the boot is caught too recoverI2cBus(); Wire.begin(); @@ -40,6 +55,10 @@ void ThinkNodeM6Board::begin() { delay(10); // give sx1262 some time to power up } +void ThinkNodeM6Board::loop() { + NRF_WDT->RR[0] = WDT_RR_RR_Reload; // feed the watchdog +} + uint16_t ThinkNodeM6Board::getBattMilliVolts() { int adcvalue = 0; diff --git a/variants/thinknode_m6/ThinkNodeM6Board.h b/variants/thinknode_m6/ThinkNodeM6Board.h index 934794c9af4..1f273a74f17 100644 --- a/variants/thinknode_m6/ThinkNodeM6Board.h +++ b/variants/thinknode_m6/ThinkNodeM6Board.h @@ -12,6 +12,8 @@ #define PIN_VBAT_READ BATTERY_PIN #define REAL_VBAT_MV_PER_LSB (VBAT_DIVIDER_COMP * VBAT_MV_PER_LSB) +#define M6_WATCHDOG_SECS 90 + class ThinkNodeM6Board : public NRF52BoardDCDC { protected: #if NRF52_POWER_MANAGEMENT @@ -21,6 +23,7 @@ class ThinkNodeM6Board : public NRF52BoardDCDC { public: ThinkNodeM6Board() : NRF52Board("THINKNODE_M6_OTA") {} void begin(); + void loop() override; uint16_t getBattMilliVolts() override; #if defined(P_LORA_TX_LED) From afa2fd9a4ccdefb3987ad468bf14ff2812ef033f Mon Sep 17 00:00:00 2001 From: Olivier Booklage Date: Mon, 23 Nov 2026 21:21:15 +0100 Subject: [PATCH 3/5] feat(thinknode_m6): Drive the LEDs and the button as in the Elecrow manual The red LED stays on for 10 s at boot, stays on with USB or while charging, flashes slowly at 3.5 V or less and gives a short flash every 3 s on battery. The blue LED flashes while sending and after each received packet. Holding the button 4 s and releasing it powers the board off; the button or the charger wakes it. --- .../test_m6_status_led/test_m6_status_led.cpp | 39 +++++++++++++++ variants/thinknode_m6/StatusLed.h | 37 +++++++++++++++ variants/thinknode_m6/ThinkNodeM6Board.cpp | 47 +++++++++++++++++++ variants/thinknode_m6/ThinkNodeM6Board.h | 47 +++++++++++++++++-- 4 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 test/test_m6_status_led/test_m6_status_led.cpp create mode 100644 variants/thinknode_m6/StatusLed.h diff --git a/test/test_m6_status_led/test_m6_status_led.cpp b/test/test_m6_status_led/test_m6_status_led.cpp new file mode 100644 index 00000000000..3af7ce9ab89 --- /dev/null +++ b/test/test_m6_status_led/test_m6_status_led.cpp @@ -0,0 +1,39 @@ +#include +#include "../../variants/thinknode_m6/StatusLed.h" + +TEST(M6RedLed, OnForTenSecondsAtBoot) { + EXPECT_TRUE(m6RedLed(4000, false, 3900)); + EXPECT_FALSE(m6RedLed(10500, false, 3900)); +} + +TEST(M6RedLed, SteadyWhileCharging) { + EXPECT_TRUE(m6RedLed(20000, true, 3900)); + EXPECT_TRUE(m6RedLed(20600, true, 3400)); +} + +TEST(M6RedLed, ShortFlashEveryThreeSecondsOnAGoodBattery) { + EXPECT_TRUE(m6RedLed(21000, false, 3900)); + EXPECT_TRUE(m6RedLed(21001, false, 3900)); + EXPECT_FALSE(m6RedLed(21002, false, 3900)); + EXPECT_FALSE(m6RedLed(22500, false, 3900)); + EXPECT_TRUE(m6RedLed(24000, false, 0)); // bad reading, not a low battery +} + +TEST(M6RedLed, SlowFlashingOnALowBattery) { + EXPECT_TRUE(m6RedLed(20000, false, 3450)); + EXPECT_FALSE(m6RedLed(20600, false, 3450)); + EXPECT_TRUE(m6RedLed(25100, false, 3500)); +} + +TEST(M6BlueLed, FlashesAtFiveHertzDuringData) { + EXPECT_TRUE(m6BlueLed(true, 0)); + EXPECT_TRUE(m6BlueLed(true, 99)); + EXPECT_FALSE(m6BlueLed(true, 100)); + EXPECT_TRUE(m6BlueLed(true, 200)); + EXPECT_FALSE(m6BlueLed(false, 0)); +} + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/variants/thinknode_m6/StatusLed.h b/variants/thinknode_m6/StatusLed.h new file mode 100644 index 00000000000..9628212e5b0 --- /dev/null +++ b/variants/thinknode_m6/StatusLed.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +// LED behaviour from the Elecrow ThinkNode M6 manual. +#define M6_BOOT_LED_MILLIS 10000 // red on for 10 s at power-on +#define M6_BATT_LOW_MV 3500 // low-battery alert at 3.5 V or less +#define M6_LOW_BATT_PERIOD_MILLIS 5000 // slow flashing at 0.2 Hz +#define M6_LOW_BATT_FLASH_MILLIS 500 // our choice: the manual gives no duty cycle +#define M6_DATA_BLINK_MILLIS 100 // rapid flashing at 5 Hz: 100 ms on, 100 ms off +#define M6_HEARTBEAT_MILLIS 3000 // not in the manual: a short sign of life on battery +#define M6_HEARTBEAT_FLASH_MILLIS 2 + +// Red PWR LED: on for 10 s at power-on, steady while charging, slow flashing +// when the battery is low, otherwise a 2 ms flash every 3 s. +inline bool m6RedLed(uint32_t now, bool charging, uint16_t batt_mv) { + if (now < M6_BOOT_LED_MILLIS) { + return true; + } + if (charging) { + return true; + } + bool low_battery = batt_mv > 1000 && batt_mv <= M6_BATT_LOW_MV; // below 1 V is a bad reading + if (low_battery) { + return now % M6_LOW_BATT_PERIOD_MILLIS < M6_LOW_BATT_FLASH_MILLIS; + } + return now % M6_HEARTBEAT_MILLIS < M6_HEARTBEAT_FLASH_MILLIS; +} + +// Blue DATA LED: rapid flashing while LoRa data is sent or received, +// starting with a flash. +inline bool m6BlueLed(bool data_active, uint32_t data_elapsed_ms) { + if (!data_active) { + return false; + } + return (data_elapsed_ms / M6_DATA_BLINK_MILLIS) % 2 == 0; +} diff --git a/variants/thinknode_m6/ThinkNodeM6Board.cpp b/variants/thinknode_m6/ThinkNodeM6Board.cpp index f7e994b9353..dae64c04ef1 100644 --- a/variants/thinknode_m6/ThinkNodeM6Board.cpp +++ b/variants/thinknode_m6/ThinkNodeM6Board.cpp @@ -1,5 +1,7 @@ #include "ThinkNodeM6Board.h" +#include "StatusLed.h" #include +#include #ifdef THINKNODE_M6 @@ -40,6 +42,8 @@ static void startWatchdog() { void ThinkNodeM6Board::begin() { NRF52Board::begin(); + digitalWrite(PIN_LED_RED, HIGH); // power-on sign, see loop() + startWatchdog(); // first, so that a hang later in the boot is caught too recoverI2cBus(); Wire.begin(); @@ -51,12 +55,55 @@ void ThinkNodeM6Board::begin() { pinMode(EXT_PWR_DETECT, INPUT); pinMode(EXT_CHRG_DETECT, INPUT); + pinMode(PIN_USER_BTN, INPUT_PULLUP); delay(10); // give sx1262 some time to power up } void ThinkNodeM6Board::loop() { + uint32_t now = millis(); + NRF_WDT->RR[0] = WDT_RR_RR_Reload; // feed the watchdog + + // Like Meshtastic, power off when the button is released after a long hold. + // Presses that start during boot are ignored: a floating pin could fake one. + if (digitalRead(PIN_USER_BTN) == LOW) { + if (_btn_down_at == 0) { + _btn_down_at = now; + } + } else { + bool long_press = _btn_down_at > M6_BTN_BOOT_HOLDOFF_MILLIS + && now - _btn_down_at >= M6_POWEROFF_HOLD_MILLIS; + if (long_press) { + powerOff(); // does not return + } + _btn_down_at = 0; + } + + if (_batt_read_at == 0 || now - _batt_read_at >= M6_BATT_READ_MILLIS) { + _batt_mv = getBattMilliVolts(); + _batt_read_at = now; + } + bool red_on = m6RedLed(now, isChargerActive(), _batt_mv); + if (red_on != _red_led_on) { + digitalWrite(PIN_LED_RED, red_on ? HIGH : LOW); + _red_led_on = red_on; + } + +#ifdef P_LORA_TX_LED + uint32_t rx_count = radio_driver.getPacketsRecv(); + if (rx_count != _rx_count) { + _rx_count = rx_count; + _rx_at = now; + _data_started_at = now; + } + bool data_active = _tx_active || now - _rx_at < M6_RX_FLASH_MILLIS; + bool blue_on = m6BlueLed(data_active, now - _data_started_at); + if (blue_on != _blue_led_on) { + digitalWrite(P_LORA_TX_LED, blue_on ? HIGH : LOW); + _blue_led_on = blue_on; + } +#endif } uint16_t ThinkNodeM6Board::getBattMilliVolts() { diff --git a/variants/thinknode_m6/ThinkNodeM6Board.h b/variants/thinknode_m6/ThinkNodeM6Board.h index 1f273a74f17..3563d57ba08 100644 --- a/variants/thinknode_m6/ThinkNodeM6Board.h +++ b/variants/thinknode_m6/ThinkNodeM6Board.h @@ -12,9 +12,24 @@ #define PIN_VBAT_READ BATTERY_PIN #define REAL_VBAT_MV_PER_LSB (VBAT_DIVIDER_COMP * VBAT_MV_PER_LSB) +#define M6_POWEROFF_HOLD_MILLIS 3900 // Meshtastic: hold time before power off +#define M6_BTN_BOOT_HOLDOFF_MILLIS 30000 // Meshtastic: ignore presses that start earlier +#define M6_BATT_READ_MILLIS 30000 #define M6_WATCHDOG_SECS 90 +#define M6_DFU_MAGIC_SKIP 0x6D // tells the bootloader to start the firmware at once +#define M6_RX_FLASH_MILLIS 400 // blue flashing after each received packet class ThinkNodeM6Board : public NRF52BoardDCDC { + bool _red_led_on = true; + bool _blue_led_on = false; + bool _tx_active = false; + uint32_t _data_started_at = 0; + uint32_t _rx_count = 0; + uint32_t _rx_at = 0; + uint32_t _batt_read_at = 0; + uint16_t _batt_mv = 0; + uint32_t _btn_down_at = 0; + protected: #if NRF52_POWER_MANAGEMENT void initiateShutdown(uint8_t reason) override; @@ -26,14 +41,14 @@ class ThinkNodeM6Board : public NRF52BoardDCDC { void loop() override; uint16_t getBattMilliVolts() override; -#if defined(P_LORA_TX_LED) + // loop() flashes the blue LED while _tx_active is set void onBeforeTransmit() override { - digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED on + _tx_active = true; + _data_started_at = millis(); } void onAfterTransmit() override { - digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED off + _tx_active = false; } -#endif const char* getManufacturerName() const override { return "Elecrow ThinkNode M6"; @@ -69,12 +84,36 @@ class ThinkNodeM6Board : public NRF52BoardDCDC { } void powerOff() override { + // Elecrow: the red LED flashes 5 times before power off + for (int i = 0; i < 5; i++) { + digitalWrite(PIN_LED_RED, HIGH); + delay(250); + digitalWrite(PIN_LED_RED, LOW); + delay(250); + } // turn off all leds, sd_power_system_off will not do this for us #ifdef P_LORA_TX_LED digitalWrite(P_LORA_TX_LED, LOW); #endif + // Like Meshtastic, wake on the button or when the charger starts. + // If the charger is already active, the board wakes again at once. + nrf_gpio_cfg_sense_input(g_ADigitalPinMap[PIN_USER_BTN], NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); + nrf_gpio_cfg_sense_input(g_ADigitalPinMap[EXT_CHRG_DETECT], NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); + + // The bootloader enters its update mode (blue LED pulsing) when the button + // is held at boot, and the button is what wakes the board. Like Meshtastic, + // ask it to skip that check on the next boot. + uint8_t sd_enabled = 0; + sd_softdevice_is_enabled(&sd_enabled); + if (sd_enabled) { + sd_power_gpregret_clr(0, 0xFF); + sd_power_gpregret_set(0, M6_DFU_MAGIC_SKIP); + } else { + NRF_POWER->GPREGRET = M6_DFU_MAGIC_SKIP; + } + // power off board NRF52Board::powerOff(); } From 2de82d4739b25c644ed0493408e8db49efedface Mon Sep 17 00:00:00 2001 From: Olivier Booklage Date: Mon, 23 Nov 2026 21:45:55 +0100 Subject: [PATCH 4/5] feat(thinknode_m6): Hold the button 3 s to power on The Elecrow manual asks for a long press to power on, with the red LED lighting after 3 s. A shorter press now puts the board back to sleep; a wake-up by the charger still boots at once. --- variants/thinknode_m6/ThinkNodeM6Board.cpp | 72 ++++++++++++++++++++-- variants/thinknode_m6/ThinkNodeM6Board.h | 39 ++---------- 2 files changed, 73 insertions(+), 38 deletions(-) diff --git a/variants/thinknode_m6/ThinkNodeM6Board.cpp b/variants/thinknode_m6/ThinkNodeM6Board.cpp index dae64c04ef1..c61367b24e6 100644 --- a/variants/thinknode_m6/ThinkNodeM6Board.cpp +++ b/variants/thinknode_m6/ThinkNodeM6Board.cpp @@ -39,9 +39,77 @@ static void startWatchdog() { NRF_WDT->TASKS_START = 1; } +// index 0 is GPREGRET, read by the bootloader; index 1 is GPREGRET2 +static void writeGpregret(uint8_t index, uint8_t value) { + uint8_t sd_enabled = 0; + sd_softdevice_is_enabled(&sd_enabled); + if (sd_enabled) { + sd_power_gpregret_clr(index, 0xFF); + sd_power_gpregret_set(index, value); + } else if (index == 0) { + NRF_POWER->GPREGRET = value; + } else { + NRF_POWER->GPREGRET2 = value; + } +} + +// Turns the LEDs off and sets what the next wake-up needs. Like Meshtastic, +// the button or the charger starting wakes the board; if the charger is +// already active, it wakes again at once. +static void prepareWakeUp() { + digitalWrite(PIN_LED_RED, LOW); + digitalWrite(PIN_LED_BLUE, LOW); + nrf_gpio_cfg_sense_input(g_ADigitalPinMap[PIN_USER_BTN], NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); + nrf_gpio_cfg_sense_input(g_ADigitalPinMap[EXT_CHRG_DETECT], NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); + + // The bootloader enters its update mode (blue LED pulsing) when the button + // is held at boot, and the button is what wakes the board: ask it to skip + // that check, as Meshtastic does. + writeGpregret(0, M6_DFU_MAGIC_SKIP); + writeGpregret(1, M6_POWEROFF_MARK); // see checkPowerOnPress() +} + +void ThinkNodeM6Board::powerOff() { + // Elecrow: the red LED flashes 5 times before power off + for (int i = 0; i < 5; i++) { + digitalWrite(PIN_LED_RED, HIGH); + delay(250); + digitalWrite(PIN_LED_RED, LOW); + delay(250); + } + prepareWakeUp(); + NRF52Board::powerOff(); +} + +// Elecrow manual: after a power off, the button must be held 3 s to power +// on, and the red LED then lights. A shorter press puts the board back to +// sleep. A wake-up by the charger boots at once. +void ThinkNodeM6Board::checkPowerOnPress() { + bool after_power_off = NRF_POWER->GPREGRET2 == M6_POWEROFF_MARK; // SoftDevice not started yet + writeGpregret(1, 0); + if (!after_power_off || isChargerActive()) { + return; + } + while (millis() < M6_POWERON_HOLD_MILLIS) { + if (digitalRead(PIN_USER_BTN) == HIGH) { + // Released too early. The radio still sleeps since the power off, but + // initVariant() powered the GPS on again. + digitalWrite(PIN_GPS_EN, LOW); + prepareWakeUp(); + NRF_POWER->SYSTEMOFF = POWER_SYSTEMOFF_SYSTEMOFF_Enter; + } + delay(10); + } +} + void ThinkNodeM6Board::begin() { NRF52Board::begin(); + pinMode(EXT_PWR_DETECT, INPUT); + pinMode(EXT_CHRG_DETECT, INPUT); + pinMode(PIN_USER_BTN, INPUT_PULLUP); + checkPowerOnPress(); + digitalWrite(PIN_LED_RED, HIGH); // power-on sign, see loop() startWatchdog(); // first, so that a hang later in the boot is caught too @@ -53,10 +121,6 @@ void ThinkNodeM6Board::begin() { digitalWrite(P_LORA_TX_LED, LOW); #endif - pinMode(EXT_PWR_DETECT, INPUT); - pinMode(EXT_CHRG_DETECT, INPUT); - pinMode(PIN_USER_BTN, INPUT_PULLUP); - delay(10); // give sx1262 some time to power up } diff --git a/variants/thinknode_m6/ThinkNodeM6Board.h b/variants/thinknode_m6/ThinkNodeM6Board.h index 3563d57ba08..dd7fbd9800b 100644 --- a/variants/thinknode_m6/ThinkNodeM6Board.h +++ b/variants/thinknode_m6/ThinkNodeM6Board.h @@ -18,6 +18,8 @@ #define M6_WATCHDOG_SECS 90 #define M6_DFU_MAGIC_SKIP 0x6D // tells the bootloader to start the firmware at once #define M6_RX_FLASH_MILLIS 400 // blue flashing after each received packet +#define M6_POWERON_HOLD_MILLIS 3000 // Elecrow: hold 3 s to power on +#define M6_POWEROFF_MARK 0x4D // in GPREGRET2: the board was powered off by powerOff() class ThinkNodeM6Board : public NRF52BoardDCDC { bool _red_led_on = true; @@ -30,6 +32,8 @@ class ThinkNodeM6Board : public NRF52BoardDCDC { uint16_t _batt_mv = 0; uint32_t _btn_down_at = 0; + void checkPowerOnPress(); + protected: #if NRF52_POWER_MANAGEMENT void initiateShutdown(uint8_t reason) override; @@ -83,38 +87,5 @@ class ThinkNodeM6Board : public NRF52BoardDCDC { return isSolarChargerActive() || isUsbPowered(); } - void powerOff() override { - // Elecrow: the red LED flashes 5 times before power off - for (int i = 0; i < 5; i++) { - digitalWrite(PIN_LED_RED, HIGH); - delay(250); - digitalWrite(PIN_LED_RED, LOW); - delay(250); - } - - // turn off all leds, sd_power_system_off will not do this for us - #ifdef P_LORA_TX_LED - digitalWrite(P_LORA_TX_LED, LOW); - #endif - - // Like Meshtastic, wake on the button or when the charger starts. - // If the charger is already active, the board wakes again at once. - nrf_gpio_cfg_sense_input(g_ADigitalPinMap[PIN_USER_BTN], NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); - nrf_gpio_cfg_sense_input(g_ADigitalPinMap[EXT_CHRG_DETECT], NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); - - // The bootloader enters its update mode (blue LED pulsing) when the button - // is held at boot, and the button is what wakes the board. Like Meshtastic, - // ask it to skip that check on the next boot. - uint8_t sd_enabled = 0; - sd_softdevice_is_enabled(&sd_enabled); - if (sd_enabled) { - sd_power_gpregret_clr(0, 0xFF); - sd_power_gpregret_set(0, M6_DFU_MAGIC_SKIP); - } else { - NRF_POWER->GPREGRET = M6_DFU_MAGIC_SKIP; - } - - // power off board - NRF52Board::powerOff(); - } + void powerOff() override; }; From 096cc4bff95ad37c12bbfa0340020bcb6d64be50 Mon Sep 17 00:00:00 2001 From: Olivier Booklage Date: Mon, 23 Nov 2026 22:16:06 +0100 Subject: [PATCH 5/5] fix(thinknode_m6): Put the radio to sleep after a short press A press too short to power on now goes back to sleep through the normal power-off path, so the radio sleeps again. The mark in GPREGRET2 is now SHUTDOWN_REASON_USER, and a release must last 20 ms. --- variants/thinknode_m6/ThinkNodeM6Board.cpp | 15 +++++++++------ variants/thinknode_m6/ThinkNodeM6Board.h | 5 ++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/variants/thinknode_m6/ThinkNodeM6Board.cpp b/variants/thinknode_m6/ThinkNodeM6Board.cpp index c61367b24e6..e6263de47b4 100644 --- a/variants/thinknode_m6/ThinkNodeM6Board.cpp +++ b/variants/thinknode_m6/ThinkNodeM6Board.cpp @@ -92,11 +92,14 @@ void ThinkNodeM6Board::checkPowerOnPress() { } while (millis() < M6_POWERON_HOLD_MILLIS) { if (digitalRead(PIN_USER_BTN) == HIGH) { - // Released too early. The radio still sleeps since the power off, but - // initVariant() powered the GPS on again. - digitalWrite(PIN_GPS_EN, LOW); - prepareWakeUp(); - NRF_POWER->SYSTEMOFF = POWER_SYSTEMOFF_SYSTEMOFF_Enter; + delay(20); + if (digitalRead(PIN_USER_BTN) == HIGH) { // still released, not a bounce + // initVariant() powered the GPS on again, and the reset may have woken + // the radio: the normal power-off path puts the radio back to sleep. + digitalWrite(PIN_GPS_EN, LOW); + prepareWakeUp(); + NRF52Board::powerOff(); + } } delay(10); } @@ -108,11 +111,11 @@ void ThinkNodeM6Board::begin() { pinMode(EXT_PWR_DETECT, INPUT); pinMode(EXT_CHRG_DETECT, INPUT); pinMode(PIN_USER_BTN, INPUT_PULLUP); + startWatchdog(); // first, so that a hang later in the boot is caught too checkPowerOnPress(); digitalWrite(PIN_LED_RED, HIGH); // power-on sign, see loop() - startWatchdog(); // first, so that a hang later in the boot is caught too recoverI2cBus(); Wire.begin(); diff --git a/variants/thinknode_m6/ThinkNodeM6Board.h b/variants/thinknode_m6/ThinkNodeM6Board.h index dd7fbd9800b..db31e8b27b0 100644 --- a/variants/thinknode_m6/ThinkNodeM6Board.h +++ b/variants/thinknode_m6/ThinkNodeM6Board.h @@ -19,7 +19,10 @@ #define M6_DFU_MAGIC_SKIP 0x6D // tells the bootloader to start the firmware at once #define M6_RX_FLASH_MILLIS 400 // blue flashing after each received packet #define M6_POWERON_HOLD_MILLIS 3000 // Elecrow: hold 3 s to power on -#define M6_POWEROFF_MARK 0x4D // in GPREGRET2: the board was powered off by powerOff() +#ifndef SHUTDOWN_REASON_USER + #define SHUTDOWN_REASON_USER 0x55 // same value as NRF52Board power management +#endif +#define M6_POWEROFF_MARK SHUTDOWN_REASON_USER // in GPREGRET2: powerOff() put the board to sleep class ThinkNodeM6Board : public NRF52BoardDCDC { bool _red_led_on = true;