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 0000000000..3af7ce9ab8 --- /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 0000000000..9628212e5b --- /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 f83f274f96..e6263de47b 100644 --- a/variants/thinknode_m6/ThinkNodeM6Board.cpp +++ b/variants/thinknode_m6/ThinkNodeM6Board.cpp @@ -1,13 +1,122 @@ #include "ThinkNodeM6Board.h" +#include "StatusLed.h" #include +#include #ifdef THINKNODE_M6 #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); +} + +// 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; +} + +// 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) { + 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); + } +} + void ThinkNodeM6Board::begin() { NRF52Board::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() + + recoverI2cBus(); Wire.begin(); #ifdef P_LORA_TX_LED @@ -15,12 +124,55 @@ void ThinkNodeM6Board::begin() { digitalWrite(P_LORA_TX_LED, LOW); #endif - pinMode(EXT_PWR_DETECT, INPUT); - pinMode(EXT_CHRG_DETECT, INPUT); - 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() { int adcvalue = 0; diff --git a/variants/thinknode_m6/ThinkNodeM6Board.h b/variants/thinknode_m6/ThinkNodeM6Board.h index 934794c9af..db31e8b27b 100644 --- a/variants/thinknode_m6/ThinkNodeM6Board.h +++ b/variants/thinknode_m6/ThinkNodeM6Board.h @@ -12,7 +12,31 @@ #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 +#define M6_POWERON_HOLD_MILLIS 3000 // Elecrow: hold 3 s to power on +#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; + 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; + + void checkPowerOnPress(); + protected: #if NRF52_POWER_MANAGEMENT void initiateShutdown(uint8_t reason) override; @@ -21,16 +45,17 @@ 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) + // 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"; @@ -65,14 +90,5 @@ class ThinkNodeM6Board : public NRF52BoardDCDC { return isSolarChargerActive() || isUsbPowered(); } - void powerOff() override { - - // 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 - - // power off board - NRF52Board::powerOff(); - } + void powerOff() override; };