Fix peripheral power shutdown and auto-detect Station G3 radio slots

This commit is contained in:
mikecarper committed 2026-10-15 22:30:37 -07:00
1 parent c6e43c5530
commit 8eced48d34
103 files changed
+4900 -194

No files matched your search

+3 -2
View File
@@ -1,4 +1,5 @@
#include "HeltecE213Board.h"
#include <helpers/ESP32GpioHold.h>
void HeltecE213Board::begin() {
ESP32Board::begin();
@@ -15,8 +16,8 @@ void HeltecE213Board::begin() {
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
+3 -2
View File
@@ -1,4 +1,5 @@
#include "HeltecE290Board.h"
#include <helpers/ESP32GpioHold.h>
void HeltecE290Board::begin() {
ESP32Board::begin();
@@ -15,8 +16,8 @@ void HeltecE290Board::begin() {
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
+3 -2
View File
@@ -1,4 +1,5 @@
#include "HeltecRC32Board.h"
#include <helpers/ESP32GpioHold.h>
void HeltecRC32Board::begin() {
ESP32Board::begin();
@@ -29,8 +30,8 @@ void HeltecRC32Board::begin() {
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
+3 -2
View File
@@ -1,4 +1,5 @@
#include "HeltecT190Board.h"
#include <helpers/ESP32GpioHold.h>
void HeltecT190Board::begin() {
ESP32Board::begin();
@@ -15,8 +16,8 @@ void HeltecT190Board::begin() {
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
@@ -1,4 +1,5 @@
#include "HeltecTrackerV2Board.h"
#include <helpers/ESP32GpioHold.h>
void HeltecTrackerV2Board::begin() {
ESP32Board::begin();
@@ -20,8 +21,8 @@ void HeltecTrackerV2Board::begin() {
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
@@ -42,8 +43,7 @@ void HeltecTrackerV2Board::begin() {
// only in variants/heltec_tracker_v2/platformio.ini). Same guard idiom
// LoRaFEMControl.cpp already uses for this macro.
#if defined(P_LORA_PA_POWER)
digitalWrite(P_LORA_PA_POWER, LOW);
rtc_gpio_hold_en((gpio_num_t)P_LORA_PA_POWER);
mesh::esp32::holdOutputForDeepSleep((gpio_num_t)P_LORA_PA_POWER, LOW);
#endif
}
@@ -1,4 +1,5 @@
#include "LoRaFEMControl.h"
#include <helpers/ESP32GpioHold.h>
#include <driver/rtc_io.h>
#include <esp_sleep.h>
#include <Arduino.h>
@@ -8,9 +9,9 @@ void LoRaFEMControl::init(void)
#if defined(P_LORA_PA_POWER) && defined(P_LORA_KCT8103L_PA_CSD) && defined(P_LORA_KCT8103L_PA_CTX)
pinMode(P_LORA_PA_POWER, OUTPUT);
digitalWrite(P_LORA_PA_POWER, HIGH);
rtc_gpio_hold_dis((gpio_num_t)P_LORA_PA_POWER);
rtc_gpio_hold_dis((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
rtc_gpio_hold_dis((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_PA_POWER);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
delay(1);
pinMode(P_LORA_KCT8103L_PA_CSD, OUTPUT);
digitalWrite(P_LORA_KCT8103L_PA_CSD, HIGH);
@@ -52,13 +53,13 @@ void LoRaFEMControl::setRxModeEnableWhenMCUSleep(void)
{
#if defined(P_LORA_KCT8103L_PA_CSD) && defined(P_LORA_KCT8103L_PA_CTX)
digitalWrite(P_LORA_KCT8103L_PA_CSD, HIGH);
rtc_gpio_hold_en((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
mesh::esp32::holdRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
if (lna_enabled) {
digitalWrite(P_LORA_KCT8103L_PA_CTX, LOW);
} else {
digitalWrite(P_LORA_KCT8103L_PA_CTX, HIGH);
}
rtc_gpio_hold_en((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
mesh::esp32::holdRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
#endif
}
+3 -2
View File
@@ -1,4 +1,5 @@
#pragma once
#include <helpers/ESP32GpioHold.h>
#include <Arduino.h>
#include <helpers/ESP32Board.h>
@@ -19,8 +20,8 @@ public:
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_0);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_0);
}
}
+3 -2
View File
@@ -1,4 +1,5 @@
#pragma once
#include <helpers/ESP32GpioHold.h>
#include <Arduino.h>
#include <helpers/RefCountedDigitalPin.h>
@@ -45,8 +46,8 @@ public:
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
+4 -4
View File
@@ -1,4 +1,5 @@
#include "HeltecV4Board.h"
#include <helpers/ESP32GpioHold.h>
void HeltecV4Board::begin() {
ESP32Board::begin();
@@ -17,8 +18,8 @@ void HeltecV4Board::begin() {
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
@@ -36,8 +37,7 @@ void HeltecV4Board::begin() {
ESP32Board::shutdownPeripherals();
// Turn off PA
digitalWrite(P_LORA_PA_POWER, LOW);
rtc_gpio_hold_en((gpio_num_t)P_LORA_PA_POWER);
mesh::esp32::holdOutputForDeepSleep((gpio_num_t)P_LORA_PA_POWER, LOW);
}
uint16_t HeltecV4Board::getBattMilliVolts() {
+8 -7
View File
@@ -1,4 +1,5 @@
#include "LoRaFEMControl.h"
#include <helpers/ESP32GpioHold.h>
#include <driver/rtc_io.h>
#include <esp_sleep.h>
#include <Arduino.h>
@@ -9,7 +10,7 @@ void LoRaFEMControl::init(void)
// atomic transition (no glitch on deep sleep wake).
pinMode(P_LORA_PA_POWER, OUTPUT);
digitalWrite(P_LORA_PA_POWER, HIGH);
rtc_gpio_hold_dis((gpio_num_t)P_LORA_PA_POWER);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_PA_POWER);
esp_reset_reason_t reason = esp_reset_reason();
if (reason != ESP_RST_DEEPSLEEP) {
@@ -19,7 +20,7 @@ void LoRaFEMControl::init(void)
// Auto-detect FEM type via shared GPIO2 default pull level.
// GC1109 CSD: internal pull-down -> reads LOW
// KCT8103L CSD: internal pull-up -> reads HIGH
rtc_gpio_hold_dis((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
pinMode(P_LORA_KCT8103L_PA_CSD, INPUT);
delay(1);
if(digitalRead(P_LORA_KCT8103L_PA_CSD)==HIGH) {
@@ -27,7 +28,7 @@ void LoRaFEMControl::init(void)
fem_type= KCT8103L_PA;
pinMode(P_LORA_KCT8103L_PA_CSD, OUTPUT);
digitalWrite(P_LORA_KCT8103L_PA_CSD, HIGH);
rtc_gpio_hold_dis((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
pinMode(P_LORA_KCT8103L_PA_CTX, OUTPUT);
digitalWrite(P_LORA_KCT8103L_PA_CTX, lna_enabled ? LOW : HIGH);
setLnaCanControl(true);
@@ -85,20 +86,20 @@ void LoRaFEMControl::setRxModeEnable(void)
void LoRaFEMControl::setRxModeEnableWhenMCUSleep(void)
{
digitalWrite(P_LORA_PA_POWER, HIGH);
rtc_gpio_hold_en((gpio_num_t)P_LORA_PA_POWER);
mesh::esp32::holdRtcGpioIfSupported((gpio_num_t)P_LORA_PA_POWER);
if(fem_type==GC1109_PA) {
digitalWrite(P_LORA_GC1109_PA_EN, HIGH);
rtc_gpio_hold_en((gpio_num_t)P_LORA_GC1109_PA_EN);
mesh::esp32::holdRtcGpioIfSupported((gpio_num_t)P_LORA_GC1109_PA_EN);
gpio_pulldown_en((gpio_num_t)P_LORA_GC1109_PA_TX_EN);
} else if(fem_type==KCT8103L_PA) {
digitalWrite(P_LORA_KCT8103L_PA_CSD, HIGH);
rtc_gpio_hold_en((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
mesh::esp32::holdRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
if(lna_enabled) {
digitalWrite(P_LORA_KCT8103L_PA_CTX, LOW); // LNA on
} else {
digitalWrite(P_LORA_KCT8103L_PA_CTX, HIGH); // LNA bypass
}
rtc_gpio_hold_en((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
mesh::esp32::holdRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
}
}
+4 -4
View File
@@ -1,4 +1,5 @@
#include "HeltecV4R8Board.h"
#include <helpers/ESP32GpioHold.h>
// Expansion Kit V2 control-pin notes (kept here beside the shared-reset
// sequencing): display/touch I2C is SDA 17 / SCL 18; GPIO 21 is the shared
@@ -30,8 +31,8 @@ void HeltecV4R8Board::begin() {
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
@@ -52,8 +53,7 @@ void HeltecV4R8Board::powerOff() {
void HeltecV4R8Board::shutdownPeripherals() {
ESP32Board::shutdownPeripherals();
loRaFEMControl.setSleepModeEnable();
digitalWrite(P_LORA_PA_POWER, LOW);
rtc_gpio_hold_en((gpio_num_t)P_LORA_PA_POWER);
mesh::esp32::holdOutputForDeepSleep((gpio_num_t)P_LORA_PA_POWER, LOW);
periph_power.release(); // Drop the permanent antenna-boost/VEXT claim from begin().
}
+7 -6
View File
@@ -1,4 +1,5 @@
#include "LoRaFEMControl.h"
#include <helpers/ESP32GpioHold.h>
#include <Arduino.h>
#include <driver/rtc_io.h>
@@ -7,15 +8,15 @@
void LoRaFEMControl::init(void) {
pinMode(P_LORA_PA_POWER, OUTPUT);
digitalWrite(P_LORA_PA_POWER, HIGH);
rtc_gpio_hold_dis((gpio_num_t)P_LORA_PA_POWER);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_PA_POWER);
esp_reset_reason_t reason = esp_reset_reason();
if (reason != ESP_RST_DEEPSLEEP) {
delay(1);
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
rtc_gpio_hold_dis((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
pinMode(P_LORA_KCT8103L_PA_CSD, OUTPUT);
digitalWrite(P_LORA_KCT8103L_PA_CSD, HIGH);
@@ -40,12 +41,12 @@ void LoRaFEMControl::setRxModeEnable(void) {
void LoRaFEMControl::setRxModeEnableWhenMCUSleep(void) {
digitalWrite(P_LORA_PA_POWER, HIGH);
rtc_gpio_hold_en((gpio_num_t)P_LORA_PA_POWER);
mesh::esp32::holdRtcGpioIfSupported((gpio_num_t)P_LORA_PA_POWER);
digitalWrite(P_LORA_KCT8103L_PA_CSD, HIGH);
rtc_gpio_hold_en((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
mesh::esp32::holdRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CSD);
digitalWrite(P_LORA_KCT8103L_PA_CTX, lna_enabled ? LOW : HIGH);
rtc_gpio_hold_en((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
mesh::esp32::holdRtcGpioIfSupported((gpio_num_t)P_LORA_KCT8103L_PA_CTX);
}
void LoRaFEMControl::setLNAEnable(bool enabled) {
@@ -17,8 +17,10 @@ void IkokaNrf52Board::begin() {
#endif
pinMode(PIN_VBAT, INPUT);
// Seeed's XIAO charging guidance requires READ_BAT_ENABLE to stay LOW.
// Preload the safe latch before enabling the output; do not pulse HIGH.
digitalWrite(VBAT_ENABLE, LOW);
pinMode(VBAT_ENABLE, OUTPUT);
digitalWrite(VBAT_ENABLE, HIGH);
// required button pullup is handled as part of button initilization
// in target.cpp
@@ -26,7 +26,7 @@ public:
// We can't drive VBAT_ENABLE to HIGH as long
// as we don't know whether we are charging or not ...
// this is a 3mA loss (4/1500)
// About 3 uA through the 1 MOhm + 510 kOhm divider, not 3 mA.
digitalWrite(VBAT_ENABLE, LOW);
int adcvalue = 0;
analogReadResolution(12);
+3 -6
View File
@@ -56,14 +56,11 @@ const uint32_t g_ADigitalPinMap[] = {
};
void initVariant() {
// Disable reading of the BAT voltage.
// Keep the battery divider enabled: HIGH can overvoltage the ADC while charging.
// https://wiki.seeedstudio.com/XIAO_BLE#q3-what-are-the-considerations-when-using-xiao-nrf52840-sense-for-battery-charging
pinMode(VBAT_ENABLE, OUTPUT);
// digitalWrite(VBAT_ENABLE, HIGH);
// This was taken from Seeed github butis not coherent with the doc,
// VBAT_ENABLE should be kept to LOW to protect P0.14, (1500/500)*(4.2-3.3)+3.3 = 3.9V > 3.6V
// This induces a 3mA current in the resistors :( but it's better than burning the nrf
// The 1 MOhm + 510 kOhm divider draws about 3 uA, not 3 mA.
digitalWrite(VBAT_ENABLE, LOW);
pinMode(VBAT_ENABLE, OUTPUT);
// disable xiao charging current, the handheld uses a tp4056 to charge
// instead of the onboard xiao charging circuit. This charges at a max of
@@ -9,8 +9,10 @@ void IkokaNanoNRFBoard::begin() {
NRF52Board::begin();
pinMode(PIN_VBAT, INPUT);
// Seeed's XIAO charging guidance requires READ_BAT_ENABLE to stay LOW.
// Preload the safe latch before enabling the output; do not pulse HIGH.
digitalWrite(VBAT_ENABLE, LOW);
pinMode(VBAT_ENABLE, OUTPUT);
digitalWrite(VBAT_ENABLE, HIGH);
#ifdef PIN_USER_BTN
pinMode(PIN_USER_BTN, INPUT_PULLUP);
+1 -1
View File
@@ -34,7 +34,7 @@ public:
// We can't drive VBAT_ENABLE to HIGH as long
// as we don't know whether we are charging or not ...
// this is a 3mA loss (4/1500)
// About 3 uA through the 1 MOhm + 510 kOhm divider, not 3 mA.
digitalWrite(VBAT_ENABLE, LOW);
int adcvalue = 0;
analogReadResolution(12);
+3 -6
View File
@@ -57,14 +57,11 @@ const uint32_t g_ADigitalPinMap[] =
void initVariant()
{
// Disable reading of the BAT voltage.
// Keep the battery divider enabled: HIGH can overvoltage the ADC while charging.
// https://wiki.seeedstudio.com/XIAO_BLE#q3-what-are-the-considerations-when-using-xiao-nrf52840-sense-for-battery-charging
pinMode(VBAT_ENABLE, OUTPUT);
//digitalWrite(VBAT_ENABLE, HIGH);
// This was taken from Seeed github butis not coherent with the doc,
// VBAT_ENABLE should be kept to LOW to protect P0.14, (1500/500)*(4.2-3.3)+3.3 = 3.9V > 3.6V
// This induces a 3mA current in the resistors :( but it's better than burning the nrf
// The 1 MOhm + 510 kOhm divider draws about 3 uA, not 3 mA.
digitalWrite(VBAT_ENABLE, LOW);
pinMode(VBAT_ENABLE, OUTPUT);
// Low charging current (50mA)
// https://wiki.seeedstudio.com/XIAO_BLE#battery-charging-current
@@ -9,8 +9,10 @@ void IkokaStickNRFBoard::begin() {
NRF52Board::begin();
pinMode(PIN_VBAT, INPUT);
// Seeed's XIAO charging guidance requires READ_BAT_ENABLE to stay LOW.
// Preload the safe latch before enabling the output; do not pulse HIGH.
digitalWrite(VBAT_ENABLE, LOW);
pinMode(VBAT_ENABLE, OUTPUT);
digitalWrite(VBAT_ENABLE, HIGH);
#ifdef PIN_USER_BTN
pinMode(PIN_USER_BTN, INPUT_PULLUP);
@@ -34,7 +34,7 @@ public:
// We can't drive VBAT_ENABLE to HIGH as long
// as we don't know whether we are charging or not ...
// this is a 3mA loss (4/1500)
// About 3 uA through the 1 MOhm + 510 kOhm divider, not 3 mA.
digitalWrite(VBAT_ENABLE, LOW);
int adcvalue = 0;
analogReadResolution(12);
+3 -6
View File
@@ -57,14 +57,11 @@ const uint32_t g_ADigitalPinMap[] =
void initVariant()
{
// Disable reading of the BAT voltage.
// Keep the battery divider enabled: HIGH can overvoltage the ADC while charging.
// https://wiki.seeedstudio.com/XIAO_BLE#q3-what-are-the-considerations-when-using-xiao-nrf52840-sense-for-battery-charging
pinMode(VBAT_ENABLE, OUTPUT);
//digitalWrite(VBAT_ENABLE, HIGH);
// This was taken from Seeed github butis not coherent with the doc,
// VBAT_ENABLE should be kept to LOW to protect P0.14, (1500/500)*(4.2-3.3)+3.3 = 3.9V > 3.6V
// This induces a 3mA current in the resistors :( but it's better than burning the nrf
// The 1 MOhm + 510 kOhm divider draws about 3 uA, not 3 mA.
digitalWrite(VBAT_ENABLE, LOW);
pinMode(VBAT_ENABLE, OUTPUT);
// Low charging current (50mA)
// https://wiki.seeedstudio.com/XIAO_BLE#battery-charging-current
+3 -2
View File
@@ -1,4 +1,5 @@
#include "TBeam1WBoard.h"
#include <helpers/ESP32GpioHold.h>
#include <helpers/radiolib/CustomSX1262Wrapper.h>
#include <driver/gpio.h>
#include <driver/rtc_io.h>
@@ -15,8 +16,8 @@ void TBeam1WBoard::begin() {
// can also hold the dedicated radio LDO off. These pad holds survive wake
// and can survive a subsequent software reset, so release them on every
// startup before RadioLib tries to select the SX1262.
esp_err_t nss_hold_result = rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
esp_err_t dio_result = rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
esp_err_t nss_hold_result = mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
esp_err_t dio_result = mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
esp_err_t power_hold_result = gpio_hold_dis((gpio_num_t)SX126X_POWER_EN);
MESH_DEBUG_PRINTLN("T-Beam 1W pad recovery: NSS=%d DIO1=%d LDO=%d",
nss_hold_result, dio_result, power_hold_result);
+4 -3
View File
@@ -1,4 +1,5 @@
#include <Arduino.h>
#include <helpers/ESP32GpioHold.h>
#include "TDeckBoard.h"
uint32_t deviceOnline = 0x00;
@@ -29,7 +30,7 @@ void TDeckBoard::begin() {
startup_reason = BD_STARTUP_RX_PACKET; // received a LoRa packet (while in deep sleep)
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
}
@@ -1,4 +1,5 @@
#pragma once
#include <helpers/ESP32GpioHold.h>
#include <Arduino.h>
@@ -28,8 +29,8 @@ public:
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
@@ -1,10 +1,12 @@
#include "MeshnologyW12Board.h"
#include <helpers/ESP32GpioHold.h>
void MeshnologyW12Board::begin() {
ESP32Board::begin();
rtc_gpio_hold_dis((gpio_num_t)P_LORA_LF_PA_POWER);
rtc_gpio_hold_dis((gpio_num_t)P_LORA_HF_PA_POWER);
// Keep both FEM rails off while releasing a retained terminal shutdown.
mesh::esp32::releaseOutputAfterDeepSleep((gpio_num_t)P_LORA_LF_PA_POWER, LOW);
mesh::esp32::releaseOutputAfterDeepSleep((gpio_num_t)P_LORA_HF_PA_POWER, LOW);
pinMode(PIN_ADC_CTRL, OUTPUT);
digitalWrite(PIN_ADC_CTRL, LOW); // Disable battery sense until required
@@ -22,8 +24,8 @@ void MeshnologyW12Board::begin() {
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
@@ -59,7 +61,7 @@ void MeshnologyW12Board::begin() {
rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY);
rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1);
rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS);
mesh::esp32::holdRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
if (pin_wake_btn < 0) {
esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet
@@ -79,10 +81,8 @@ void MeshnologyW12Board::begin() {
// A shutdown must not retain the packet-wake deep-sleep behavior above.
// Disable both external FEM rails, then use the base shutdown path so the
// display, GPS, radio, serial buffers, and stale wake sources are handled.
digitalWrite(P_LORA_LF_PA_POWER, LOW);
digitalWrite(P_LORA_HF_PA_POWER, LOW);
rtc_gpio_hold_en((gpio_num_t)P_LORA_LF_PA_POWER);
rtc_gpio_hold_en((gpio_num_t)P_LORA_HF_PA_POWER);
mesh::esp32::holdOutputForDeepSleep((gpio_num_t)P_LORA_LF_PA_POWER, LOW);
mesh::esp32::holdOutputForDeepSleep((gpio_num_t)P_LORA_HF_PA_POWER, LOW);
ESP32Board::powerOff();
}
+1
View File
@@ -10,6 +10,7 @@ build_flags =
-D RADIO_CLASS=CustomLR2021
-D WRAPPER_CLASS=CustomLR2021Wrapper
-D USE_LR2021
-D LR2021_USE_DCDC=1 ; verified W12 schematic: L5 2.2uH across LXA/LXB
-D NEOPIXEL_LED=46
-D NEOPIXEL_BRIGHTNESS=64
-D P_LORA_DIO_1=14
+8 -1
View File
@@ -20,11 +20,18 @@ public:
analogReference(AR_INTERNAL_3_0);
analogReadResolution(12);
delay(10);
float volts = (analogRead(BATTERY_PIN) * ADC_MULTIPLIER * AREF_VOLTAGE) / 4096;
const int raw = analogRead(BATTERY_PIN);
#ifdef PIN_BAT_ADC_EN
// The divider is only needed while sampling; do not leave it powered
// between battery checks, including after an invalid ADC result.
digitalWrite(PIN_BAT_ADC_EN, LOW);
#endif
analogReference(AR_DEFAULT); // put back to default
analogReadResolution(10);
if (raw < 0 || raw > 4095) return 0;
float volts = (raw * ADC_MULTIPLIER * AREF_VOLTAGE) / 4096;
return volts * 1000;
#else
return 0;
+3 -2
View File
@@ -1,4 +1,5 @@
#pragma once
#include <helpers/ESP32GpioHold.h>
#include <Arduino.h>
#include <helpers/RefCountedDigitalPin.h>
@@ -44,8 +45,8 @@ public:
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
+56 -5
View File
@@ -1,7 +1,10 @@
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include "RAK3401Board.h"
#include "target.h"
#include "../../src/helpers/nrf52/TerminalPeripheralShutdown.h"
#ifdef NRF52_POWER_MANAGEMENT
// Static configuration for power management
@@ -16,7 +19,8 @@ void RAK3401Board::initiateShutdown(uint8_t reason) {
// Disable SKY66122 FEM (CSD+CPS LOW = shutdown, <1 uA)
digitalWrite(SX126X_POWER_EN, LOW);
// Disable 3V3 switched peripherals and 5V boost
// Disable switched 3V3 peripherals and the RAK13302 VD boost (IO2 EN).
// Legacy VC hardware ties boost EN to VBAT unless its resistors are changed.
digitalWrite(PIN_3V3_EN, LOW);
if (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
@@ -28,6 +32,54 @@ void RAK3401Board::initiateShutdown(uint8_t reason) {
}
#endif
void RAK3401Board::shutdownPeripherals() {
// Runtime sleep/recovery must retain the RAK13302/shared rail. Only the
// terminal power-off/deep-sleep path reaches this virtual board hook.
const bool ethernet_stopped = mesh::nrf52::terminalEthernetShutdown();
// Ethernet cannot register on this board (same IO slot, SPI and IO3).
// Still fail closed if an unexpected owner appears: even base radio sleep
// would remap that live SPI bus and lowering the FEM would reset W5100S.
if (!ethernet_stopped) return;
const bool storage_power_safe = sensors.canRemoveSharedStoragePowerForDeepSleep();
NRF52Board::shutdownPeripherals();
#if ENV_INCLUDE_GPS && defined(PIN_GPS_TX) && defined(PIN_GPS_RX)
if (storage_power_safe) {
if (Serial1) Serial1.end(); // never end an unstarted Adafruit UART
pinMode(PIN_GPS_TX, INPUT);
pinMode(PIN_GPS_RX, INPUT);
}
#endif
Wire.end();
pinMode(PIN_WIRE_SDA, INPUT);
pinMode(PIN_WIRE_SCL, INPUT);
// The RAK13302 is an external, switched-supply radio. The base shutdown
// sleeps it but leaves SPI and NRESET/NSS driven; those signals can feed
// an unpowered module through its I/O protection. Release both controllers
// first (the base prepares SPI, while this radio uses SPI1). begin/end is
// deliberate: Adafruit SPI.end() asserts if its object has never begun.
if (storage_power_safe) {
SPI.end();
SPI1.begin();
SPI1.end();
pinMode(P_LORA_NSS, INPUT);
pinMode(P_LORA_RESET, INPUT);
pinMode(P_LORA_SCLK, INPUT);
pinMode(P_LORA_MOSI, INPUT);
pinMode(P_LORA_MISO, INPUT);
}
// Sleeping the SX1262 alone does not disable the SKY66122's CSD input.
// A legacy RAK13302 VC boost can remain supplied independently of IO2.
digitalWrite(SX126X_POWER_EN, LOW);
pinMode(SX126X_POWER_EN, OUTPUT);
// PoE/external 5V enters the base separately from the switched 3V3_S rail.
// An external NOR can still be recovering an unknown-WIP operation. Keep
// its shared supply and driven CS/header pads until qualified teardown.
if (storage_power_safe) {
digitalWrite(PIN_3V3_EN, LOW);
pinMode(PIN_3V3_EN, OUTPUT);
}
}
void RAK3401Board::begin() {
NRF52BoardDCDC::begin();
pinMode(PIN_VBAT_READ, INPUT);
@@ -50,10 +102,9 @@ void RAK3401Board::begin() {
checkBootVoltage(&power_config);
#endif
// WB_IO2 controls the shared 3V3_S peripheral rail and the RAK13302 boost
// converter. Keep it enabled during normal operation. Cycling this rail as
// part of radio recovery also resets fitted GPS and sensor modules and does
// not provide a reliable reset of the SX1262 core.
// WB_IO2 controls shared 3V3_S and the RAK13302 VD boost (or a modified VC).
// Keep it enabled normally. Cycling it during radio recovery also resets
// fitted GPS/sensors and does not reliably reset the SX1262 core.
pinMode(PIN_3V3_EN, OUTPUT);
digitalWrite(PIN_3V3_EN, HIGH);
+1
View File
@@ -19,6 +19,7 @@ protected:
public:
RAK3401Board() : NRF52Board("RAK3401_OTA") {}
void begin();
void shutdownPeripherals() override;
bool recoverRadio();
void enableRadioFrontend();
bool supportsRadioHardReset() const override { return true; }
+4 -1
View File
@@ -162,7 +162,10 @@ static const uint8_t AREF = PIN_AREF;
// Setting IO3 HIGH enables the FEM (LNA for RX, PA path for TX).
// CTX is connected to SX1262 DIO2 - the radio handles TX/RX switching
// in hardware via SetDIO2AsRfSwitchCtrl (microsecond-accurate, no GPIO needed).
// The 5V boost for the PA is enabled by WB_IO2 (P0.34 = PIN_3V3_EN).
// WB_IO2 (P1.02 = PIN_3V3_EN) switches the module's 3V3_S supply. The
// RAK13302 VD boost defaults to EN=IO2 (R27 DNC, R28 fitted). Legacy VC
// defaults to EN=VBAT (R27 fitted, R28 DNC), so IO2 can disable its booster
// only after the resistor modification. External 5V is not software-gated.
#define SX126X_POWER_EN (21) // P0.21 = IO3 -> SKY66122 CSD+CPS (FEM enable)
// CTX is driven by SX1262 DIO2, not a GPIO
+43 -3
View File
@@ -5,13 +5,15 @@
#include "nrf_soc.h"
#include "RAK4631Board.h"
#include "target.h"
#include "../../src/helpers/nrf52/SoftDeviceState.h"
#include "../../src/helpers/nrf52/TerminalPeripheralShutdown.h"
#ifdef ETHERNET_ENABLED
// Drive WB_IO2 HIGH as early as possible using direct register access.
// WB_IO2 (P1.02, Arduino pin 34) controls the WisBlock slot power switch.
// With POE through RAK13800, this must be enabled before the framework
// initializes or the board will brownout from insufficient power delivery.
// Preserve the existing early Ethernet slot-enable timing. RAK13800 PoE's
// 5V feeds VCC_IN separately; IO2 is not the core's upstream power gate.
// Priority 102 runs just after SystemInit.
static void __attribute__((constructor(102))) rak4631_early_poe_power() {
nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(1, 2)); // WB_IO2 = P1.02
@@ -29,7 +31,7 @@ const PowerMgtConfig power_config = {
};
void RAK4631Board::initiateShutdown(uint8_t reason) {
// Disable LoRa module power before shutdown
// Disable LoRa RF-switch power (SX1262 supply itself is not GPIO-gated).
digitalWrite(SX126X_POWER_EN, LOW);
if (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
@@ -41,6 +43,44 @@ void RAK4631Board::initiateShutdown(uint8_t reason) {
}
#endif // NRF52_POWER_MANAGEMENT
void RAK4631Board::shutdownPeripherals() {
// Explicit power-off/deep-sleep only, never ordinary event-driven sleep.
// Finish display/radio/GPS/crypto/USB shutdown before removing slot power.
// A combined owner's release hook may restore the radio. Finish it before
// common shutdown sleeps that radio. Slow DHCP may exceed the bounded wait;
// in that case do not remove power under a still-running SPI owner.
const bool ethernet_stopped = mesh::nrf52::terminalEthernetShutdown();
const bool storage_power_safe = sensors.canRemoveSharedStoragePowerForDeepSleep();
NRF52Board::shutdownPeripherals();
#if ENV_INCLUDE_GPS && defined(PIN_GPS_TX) && defined(PIN_GPS_RX)
// Provider stop() does not end UARTE. An unstarted Adafruit Uart::end()
// can wait forever, so use its real begun-state operator before teardown.
if (storage_power_safe) {
if (Serial1) Serial1.end();
pinMode(PIN_GPS_TX, INPUT);
pinMode(PIN_GPS_RX, INPUT);
}
#endif
// Wire.begin() leaves MCU pull-ups enabled. Stop the controller before
// removing sensor power, then release its pads without internal pulls.
// Base-board pull-ups to unswitched VDD are a separate hardware path.
Wire.end();
pinMode(PIN_WIRE_SDA, INPUT);
pinMode(PIN_WIRE_SCL, INPUT);
digitalWrite(SX126X_POWER_EN, LOW);
pinMode(SX126X_POWER_EN, OUTPUT);
// RAK19007 Q7 switches only 3V3_S. RAK13800/RAK19018 PoE feeds the base
// separately through IO connector pin 9 (VCC_IN), not this switched rail.
// Ethernet capability therefore must not keep every fitted sensor awake
// after explicit power-off, even in combined companion builds.
// A detected NOR may retain a pending/unknown-WIP operation or driven CS31
// and header pads. Keep it powered until a dedicated storage handoff exists.
if (ethernet_stopped && storage_power_safe) {
digitalWrite(WB_IO2, LOW);
pinMode(WB_IO2, OUTPUT);
}
}
void RAK4631Board::begin() {
#ifdef RAK4631_COMPANION_FORCE_LDO
// Some RAK4631 battery/board combinations have proved unstable with the
+1
View File
@@ -17,6 +17,7 @@ protected:
public:
RAK4631Board() : NRF52Board("RAK4631_OTA") {}
void begin();
void shutdownPeripherals() override;
#define BATTERY_SAMPLES 8
+3 -2
View File
@@ -1,4 +1,5 @@
#pragma once
#include <helpers/ESP32GpioHold.h>
#include <Arduino.h>
#include <helpers/ESP32Board.h>
@@ -15,8 +16,8 @@ public:
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
+14 -11
View File
@@ -1,20 +1,21 @@
#include "LoRaFEMControl.h"
#include <helpers/ESP32GpioHold.h>
#include <Arduino.h>
#include <driver/rtc_io.h>
#include "StationG3RadioSlot.h"
void LoRaFEMControl::init() {
#ifdef P_PA1_EN
rtc_gpio_hold_dis((gpio_num_t)P_PA1_EN);
pinMode(P_PA1_EN, OUTPUT);
mesh::esp32::releaseOutputAfterDeepSleep((gpio_num_t)P_PA1_EN,
pa_gain_enabled ? P_PA1_EN_ACTIVE : !P_PA1_EN_ACTIVE);
applyPAGain();
#endif
#ifdef P_PRIMARY_LNA_EN
rtc_gpio_hold_dis((gpio_num_t)P_PRIMARY_LNA_EN);
pinMode(P_PRIMARY_LNA_EN, OUTPUT);
setRxModeEnable();
#endif
// Neither socket's receive amplifier is enabled before radio detection.
for (uint8_t slot = 1; slot <= 2; ++slot)
mesh::esp32::releaseOutputAfterDeepSleep(
(gpio_num_t)station_g3::pinsForSlot(slot).lna, HIGH);
}
void LoRaFEMControl::setSleepModeEnable() {
@@ -23,8 +24,9 @@ void LoRaFEMControl::setSleepModeEnable() {
digitalWrite(P_PA1_EN, !P_PA1_EN_ACTIVE);
#endif
#ifdef P_PRIMARY_LNA_EN
// Preserve the existing Station G3 power-off state.
digitalWrite(P_PRIMARY_LNA_EN, P_PRIMARY_LNA_EN_ACTIVE);
// LNA P/S are active LOW. Bypass/disable the receive amplifier on shutdown,
// rather than retaining the same enabled state used during normal receive.
digitalWrite(station_g3::activePins().lna, !P_PRIMARY_LNA_EN_ACTIVE);
#endif
}
@@ -34,13 +36,14 @@ void LoRaFEMControl::setTxModeEnable() {
// the PA is still driven at full input power.
applyPAGain();
#ifdef P_PRIMARY_LNA_EN
digitalWrite(P_PRIMARY_LNA_EN, !P_PRIMARY_LNA_EN_ACTIVE);
digitalWrite(station_g3::activePins().lna, !P_PRIMARY_LNA_EN_ACTIVE);
#endif
}
void LoRaFEMControl::setRxModeEnable() {
#ifdef P_PRIMARY_LNA_EN
digitalWrite(P_PRIMARY_LNA_EN, lna_enabled ? P_PRIMARY_LNA_EN_ACTIVE : !P_PRIMARY_LNA_EN_ACTIVE);
digitalWrite(station_g3::activePins().lna,
lna_enabled ? P_PRIMARY_LNA_EN_ACTIVE : !P_PRIMARY_LNA_EN_ACTIVE);
#endif
}
+25 -2
View File
@@ -1,15 +1,18 @@
#include "StationG3Board.h"
#include <helpers/ESP32GpioHold.h>
void StationG3Board::shutdownPeripherals() {
ESP32Board::shutdownPeripherals();
loRaFEMControl.setSleepModeEnable();
#ifdef P_PA1_EN
rtc_gpio_hold_en((gpio_num_t)P_PA1_EN);
mesh::esp32::holdOutputForDeepSleep((gpio_num_t)P_PA1_EN, !P_PA1_EN_ACTIVE);
#endif
#ifdef P_PRIMARY_LNA_EN
rtc_gpio_hold_en((gpio_num_t)P_PRIMARY_LNA_EN);
for (uint8_t slot = 1; slot <= 2; ++slot)
mesh::esp32::holdOutputForDeepSleep(
(gpio_num_t)station_g3::pinsForSlot(slot).lna, !P_PRIMARY_LNA_EN_ACTIVE);
#endif
}
@@ -68,6 +71,26 @@ bool StationG3Board::handleCommand(const char* command, uint32_t sender_timestam
return false;
}
if (strcmp(command, "get radio.slot") == 0) {
const auto& state = station_g3::slotState();
snprintf(reply, 160, "> %s; active=%u; detected=%s%s%s",
station_g3::slotModeName(state.savedMode), state.active,
state.detected == 3 ? "1,2" : state.detected == 1 ? "1" : state.detected == 2 ? "2" : "none",
state.savedMode != state.bootMode ? "; reboot required" : "",
state.settingsHealthy ? "" : "; WARN settings unavailable/invalid");
return true;
}
if (strncmp(command, "set radio.slot ", 15) == 0) {
const char* value = command + 15;
const int mode = strcmp(value, "auto") == 0 ? 0 : strcmp(value, "1") == 0 ? 1 : strcmp(value, "2") == 0 ? 2 : -1;
if (mode < 0) strcpy(reply, "Error: radio.slot must be auto, 1, or 2");
else if (!station_g3::saveSlotMode(static_cast<uint8_t>(mode)))
strcpy(reply, "Error: radio.slot could not be saved; running slot unchanged");
else snprintf(reply, 160, "OK - radio.slot %s saved; reboot to apply (active=%u)",
station_g3::slotModeName(mode), station_g3::slotState().active);
return true;
}
if (strcmp(command, "get radio.fem.rxgain") == 0) {
if (!loRaFEMControl.canControlLNA()) {
strcpy(reply, "Error: unsupported");
+21 -3
View File
@@ -1,9 +1,11 @@
#pragma once
#include <helpers/ESP32GpioHold.h>
#include <Arduino.h>
#include <helpers/ESP32Board.h>
#include <driver/rtc_io.h>
#include "LoRaFEMControl.h"
#include "StationG3RadioSlot.h"
class StationG3Board : public ESP32Board {
KeyValueStore* _prefs = NULL;
@@ -12,21 +14,37 @@ public:
LoRaFEMControl loRaFEMControl;
void begin() {
station_g3::loadSlotSettings();
const auto& slotSettings = station_g3::slotState();
station_g3::slotState().candidate = slotSettings.bootMode == 2 ||
(slotSettings.bootMode == 0 && slotSettings.last == 2) ? 2 : 1;
ESP32Board::begin();
// Release holds from EITHER previously selected socket, before probing.
for (uint8_t slot = 1; slot <= 2; ++slot) {
const auto& pins = station_g3::pinsForSlot(slot);
mesh::esp32::releaseOutputAfterDeepSleep((gpio_num_t)pins.nss, HIGH);
mesh::esp32::releaseOutputAfterDeepSleep((gpio_num_t)pins.lna, HIGH);
}
loRaFEMControl.init();
esp_reset_reason_t reason = esp_reset_reason();
if (reason == ESP_RST_DEEPSLEEP) {
uint64_t wakeup_source = esp_sleep_get_ext1_wakeup_status();
if (wakeup_source & (1ULL << P_LORA_DIO_1)) {
// A newly saved override may differ from the socket which woke us.
const uint64_t radio_wake_mask = (1ULL << station_g3::pinsForSlot(1).irq) |
(1ULL << station_g3::pinsForSlot(2).irq);
if (wakeup_source & radio_wake_mask) {
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
for (uint8_t slot = 1; slot <= 2; ++slot)
mesh::esp32::deinitRtcGpioIfSupported(
(gpio_num_t)station_g3::pinsForSlot(slot).irq);
}
}
uint32_t getIRQGpio() override { return station_g3::activePins().irq; }
void attachDynamicPrefs(KeyValueStore* prefs);
bool handleCommand(const char* command, uint32_t sender_timestamp, char* reply) override;
@@ -0,0 +1,167 @@
#include "StationG3BootRecovery.h"
#include "StationG3Board.h"
#include <helpers/UsbLogging.h>
#include <cstring>
namespace station_g3 {
namespace {
constexpr size_t kRxBudget = 64;
constexpr size_t kLineCapacity = 64;
constexpr size_t kReplyCapacity = 256;
constexpr char kPrefix[] = "!g3 ";
constexpr char kNotice[] =
"\r\nStation G3 radio unavailable; USB recovery is active.\r\n"
"Press Enter, then send !g3 help (no erase required).\r\n";
constexpr char kHelp[] =
"!g3 get radio.slot\r\n"
"!g3 set radio.slot auto|1|2\r\n"
"!g3 reboot\r\n"
"Slot changes are saved for the next boot. Recovery does not erase settings.\r\n";
struct RecoveryState {
char line[kLineCapacity] = {};
size_t length = 0;
bool discardLine = false;
bool kissQuarantine = false;
bool immediatelyAfterFend = false;
char output[kReplyCapacity] = {};
size_t outputLength = 0;
size_t outputOffset = 0;
void clearInput() {
length = 0;
discardLine = false;
kissQuarantine = false;
immediatelyAfterFend = false;
}
void queue(const char* text) {
size_t count = 0;
while (count + 1 < sizeof(output) && text[count]) ++count;
memcpy(output, text, count);
output[count] = 0;
outputLength = count;
outputOffset = 0;
}
bool flushOnce(Stream& port) {
if (outputOffset < outputLength) {
const size_t remaining = outputLength - outputOffset;
const size_t sent = port.write(
reinterpret_cast<const uint8_t*>(output + outputOffset), remaining);
outputOffset += sent < remaining ? sent : remaining;
}
return outputOffset == outputLength;
}
};
RecoveryState state;
void dispatch(StationG3Board& board) {
state.line[state.length] = 0;
if (strncmp(state.line, kPrefix, sizeof(kPrefix) - 1) != 0) return;
const char* command = state.line + sizeof(kPrefix) - 1;
if (strcmp(command, "help") == 0) {
state.queue(kHelp);
return;
}
if (strcmp(command, "reboot") == 0) {
board.reboot();
return;
}
const bool allowed = strcmp(command, "get radio.slot") == 0
|| strcmp(command, "set radio.slot auto") == 0
|| strcmp(command, "set radio.slot 1") == 0
|| strcmp(command, "set radio.slot 2") == 0;
if (!allowed) {
state.queue("Error: recovery accepts only radio.slot, help and reboot.\r\n");
return;
}
char reply[kReplyCapacity] = {};
if (!board.handleCommand(command, 0, reply)) {
state.queue("Error: radio.slot recovery unavailable.\r\n");
return;
}
// The board owns persistence and its failure reply. Never turn a failed save
// into success or reboot automatically before the operator has seen it.
reply[sizeof(reply) - 3] = 0;
const size_t length = strlen(reply);
memcpy(reply + length, "\r\n", 3);
state.queue(reply);
}
} // namespace
void serviceBootRecovery(StationG3Board& board, bool firstNotice) {
if (firstNotice) {
state.clearInput();
state.queue(kNotice);
}
mesh::serviceUsbLoggingPort();
mesh::serviceUsbTerminalPort();
if (mesh::takeUsbTerminalSessionReset()) {
// An unfinished command must not be completed by a different USB session.
state.clearInput();
state.queue(kNotice);
}
if (!mesh::tryCompleteUsbTerminalSessionReset()) return;
Stream& port = mesh::usbConsolePort();
if (!state.flushOnce(port)) return;
for (size_t count = 0; count < kRxBudget && port.available() > 0; ++count) {
const int input = port.read();
if (input < 0) break;
const uint8_t byte = static_cast<uint8_t>(input);
if (byte == 0xc0) {
// KISS can contain arbitrary newlines and even recovery-looking text.
// Ignore everything through a FEND followed directly by the operator's
// Enter. This also tolerates adjacent frames sharing one FEND delimiter.
state.length = 0;
state.discardLine = false;
state.kissQuarantine = true;
state.immediatelyAfterFend = true;
continue;
}
if (state.kissQuarantine) {
if ((byte == '\r' || byte == '\n') && state.immediatelyAfterFend) {
state.clearInput();
} else {
state.immediatelyAfterFend = false;
}
continue;
}
if (byte == '\r' || byte == '\n') {
const bool complete = !state.discardLine && state.length != 0;
if (complete) dispatch(board);
state.length = 0;
state.discardLine = false;
if (complete) {
(void)state.flushOnce(port);
return; // At most one command per call, including rejected commands.
}
continue;
}
if (byte < 0x20 || byte > 0x7e) {
state.discardLine = true;
state.length = 0;
continue;
}
if (state.discardLine) continue;
if (state.length + 1 >= sizeof(state.line)) {
state.discardLine = true;
state.length = 0;
continue;
}
state.line[state.length++] = static_cast<char>(byte);
}
}
} // namespace station_g3
@@ -0,0 +1,15 @@
#pragma once
class StationG3Board;
namespace station_g3 {
// Call only while radio initialization has failed, with primary USB already
// started. Every command needs the literal "!g3 " prefix. This is deliberately
// not a replacement for the working role's CLI or binary KISS protocol.
// firstNotice starts a new recovery interval and forgets any partial input.
// Each call performs bounded, zero-wait USB work; the retry loop still owns
// watchdog servicing, yield/delay and the next attempt to initialize the radio.
void serviceBootRecovery(StationG3Board& board, bool firstNotice);
} // namespace station_g3
@@ -0,0 +1,54 @@
#include "StationG3RadioSlot.h"
#include <nvs.h>
namespace station_g3 {
namespace {
constexpr const char* SettingsNamespace = "g3-radio";
bool saveByte(const char* key, uint8_t value) {
nvs_handle_t handle;
if (nvs_open(SettingsNamespace, NVS_READWRITE, &handle) != ESP_OK) return false;
const bool ok = nvs_set_u8(handle, key, value) == ESP_OK && nvs_commit(handle) == ESP_OK;
nvs_close(handle);
return ok;
}
} // namespace
void loadSlotSettings() {
SlotState& state = slotState();
if (state.loaded) return;
state.loaded = true;
nvs_handle_t handle;
const esp_err_t opened = nvs_open(SettingsNamespace, NVS_READONLY, &handle);
if (opened == ESP_ERR_NVS_NOT_FOUND) return; // new installation: auto
if (opened != ESP_OK) { state.settingsHealthy = false; return; }
uint8_t value = 0;
esp_err_t result = nvs_get_u8(handle, "mode", &value);
if (result == ESP_OK && value <= 2) state.bootMode = state.savedMode = value;
else if (result != ESP_ERR_NVS_NOT_FOUND) state.settingsHealthy = false;
value = 0;
result = nvs_get_u8(handle, "last", &value);
if (result == ESP_OK && value <= 2) state.last = value;
else if (result != ESP_ERR_NVS_NOT_FOUND) state.settingsHealthy = false;
nvs_close(handle);
}
bool saveSlotMode(uint8_t mode) {
if (mode > 2 || !saveByte("mode", mode)) return false;
slotState().savedMode = mode;
// A mode write does not repair an invalid/unreadable "last" key. Preserve
// any boot warning until the next boot revalidates both stored settings.
return true;
}
void rememberActiveSlot(uint8_t slot) {
SlotState& state = slotState();
if (slot < 1 || slot > 2 || slot == state.last) return;
if (saveByte("last", slot)) state.last = slot;
else state.settingsHealthy = false;
}
const char* slotModeName(uint8_t mode) {
return mode == 1 ? "1" : mode == 2 ? "2" : "auto";
}
} // namespace station_g3
@@ -0,0 +1,57 @@
#pragma once
#include <stdint.h>
namespace station_g3 {
// Physical daughterboard sockets. This is unrelated to MeshCore's radio2 PHY.
struct RadioPins {
uint8_t nss, irq, reset, busy, sck, miso, mosi, lna;
};
inline const RadioPins& pinsForSlot(uint8_t slot) {
static const RadioPins primary = {11, 48, 21, 47, 12, 14, 13, 10};
static const RadioPins secondary = {43, 2, 44, 1, 39, 41, 40, 42};
return slot == 2 ? secondary : primary;
}
struct SlotState {
uint8_t bootMode = 0; // 0=auto, 1/2=explicit; frozen for this boot
uint8_t savedMode = 0; // next-boot setting, changes only after checked commit
uint8_t last = 0; // last successfully used socket
uint8_t candidate = 1; // pin binding during a no-transmit probe
uint8_t active = 0;
uint8_t detected = 0; // bit 0=primary, bit 1=secondary
bool loaded = false;
bool settingsHealthy = true;
uint8_t choose(uint8_t mask) const {
if (bootMode > 2) return 0;
if (bootMode != 0) return mask & (1U << (bootMode - 1)) ? bootMode : 0;
if (last >= 1 && last <= 2 && (mask & (1U << (last - 1)))) return last;
return mask & 1U ? 1 : (mask & 2U ? 2 : 0);
}
};
// Shared across translation units: board, driver, FEM and reset/wake logic
// must observe one binding, never separate header-static copies.
inline SlotState& slotState() {
static SlotState state;
return state;
}
inline const RadioPins& activePins() { return pinsForSlot(slotState().candidate); }
inline bool isRadioPin(uint8_t pin) {
for (uint8_t slot = 1; slot <= 2; ++slot) {
const RadioPins& p = pinsForSlot(slot);
if (pin == p.nss || pin == p.irq || pin == p.reset || pin == p.busy ||
pin == p.sck || pin == p.miso || pin == p.mosi || pin == p.lna) return true;
}
return false;
}
void loadSlotSettings();
bool saveSlotMode(uint8_t mode);
void rememberActiveSlot(uint8_t slot);
const char* slotModeName(uint8_t mode);
} // namespace station_g3
+8 -4
View File
@@ -1,6 +1,10 @@
; The motherboard has two RF daughterboard slots on separate GPIOs, mirrored by its
; "LNA P" / "LNA S" jumpers. Slot 1 is [Station_G3_ESP32], slot 2 is [Station_G3_ESP32_r2];
; the slot pins sit in their own sections so neither set of -D flags shadows the other.
; Both Station_G3_ESP32 and its legacy _r2 recipes now detect the populated SX1262
; daughterboard slot at boot. "r2" historically meant physical slot 2, not revision 2.
; Keep both environment names/identities for explicit builds and existing tooling;
; future bulk releases need only one image per role/profile. The pin flags below
; provide constructor defaults only; runtime discovery binds every radio GPIO.
; CLI: get/set radio.slot auto|1|2 (saved, reboot required). This physical selector
; is unrelated to the time-shared radio2 PHY profile.
[Station_G3_ESP32_common]
extends = esp32_base
board = station-g3-esp32
@@ -55,7 +59,7 @@ build_flags =
-D P_PRIMARY_LNA_EN=10 ; Primary Slot LNA Mode: LOW/open is LNA on, HIGH/short is LNA off.
-D P_PRIMARY_LNA_EN_ACTIVE=LOW
; Slot 2 ("LNA S"). P_PRIMARY_LNA_EN keeps its name because LoRaFEMControl knows only one LNA pin.
; Legacy slot-2 constructor defaults ("LNA S"); FEM control uses runtime slot pins.
[station_g3_esp32_r2_pins]
build_flags =
-D P_LORA_DIO_1=2
+125 -5
View File
@@ -1,5 +1,7 @@
#include <Arduino.h>
#include "target.h"
#include "StationG3BootRecovery.h"
#include <helpers/UsbLogging.h>
// The normal Station G3 roles retain the established RadioLib Arduino HAL.
// The KISS modem can opt into buffered SPI for its on-board dual-profile RX
@@ -10,6 +12,12 @@
StationG3Board board;
// Release qualification must prove the selector is in the linked application,
// not merely infer it from a board/environment name.
extern const char meshcore_capability_station_g3_auto_slots[];
const char meshcore_capability_station_g3_auto_slots[] __attribute__((used)) =
"meshcore.capability.station_g3.auto_slots.v1";
#if defined(P_LORA_SCLK)
static SPIClass spi;
#if defined(MC_SX1262_BUFFERED_RADIO_HAL) && MC_SX1262_BUFFERED_RADIO_HAL
@@ -40,16 +48,128 @@ AutoDiscoverRTCClock rtc_clock(fallback_clock);
MomentaryButton user_btn(PIN_USER_BTN, 1000, true);
#endif
namespace {
bool waitForProbeReady(const station_g3::RadioPins& pins) {
const uint32_t started = millis();
while (digitalRead(pins.busy)) {
if (static_cast<uint32_t>(millis() - started) >= 100) return false;
delay(1);
}
return true;
}
// Identification only: no SetTx, CAD, packet send, or saved profile changes.
// Use a small bounded transaction rather than RadioLib's 10 reset attempts
// (each may wait a second) on every empty socket.
bool probeRadioSlot(uint8_t slot) {
const auto& pins = station_g3::pinsForSlot(slot);
spi.end();
station_g3::slotState().candidate = slot;
digitalWrite(pins.nss, HIGH);
pinMode(pins.nss, OUTPUT);
digitalWrite(pins.lna, HIGH);
pinMode(pins.lna, OUTPUT);
pinMode(pins.irq, INPUT);
pinMode(pins.busy, INPUT_PULLDOWN);
digitalWrite(pins.reset, HIGH);
pinMode(pins.reset, OUTPUT);
digitalWrite(pins.reset, LOW);
delay(1);
digitalWrite(pins.reset, HIGH);
delay(10);
spi.begin(pins.sck, pins.miso, pins.mosi);
bool found = false;
for (unsigned attempt = 0; attempt < 3 && !found; ++attempt) {
if (!waitForProbeReady(pins)) break;
spi.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
digitalWrite(pins.nss, LOW);
spi.transfer(RADIOLIB_SX126X_CMD_SET_STANDBY);
spi.transfer(RADIOLIB_SX126X_STANDBY_RC);
digitalWrite(pins.nss, HIGH);
spi.endTransaction();
// SX126x BUSY can assert just after NSS rises (RadioLib uses the same guard).
delayMicroseconds(1);
if (!waitForProbeReady(pins)) break;
char version[16] = {};
spi.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
digitalWrite(pins.nss, LOW);
spi.transfer(RADIOLIB_SX126X_CMD_READ_REGISTER);
spi.transfer(RADIOLIB_SX126X_REG_VERSION_STRING >> 8);
spi.transfer(RADIOLIB_SX126X_REG_VERSION_STRING & 0xff);
spi.transfer(0); // status/dummy byte before register data
for (unsigned i = 0; i < sizeof(version); ++i) version[i] = spi.transfer(0);
digitalWrite(pins.nss, HIGH);
spi.endTransaction();
// SX1262 intentionally uses RadioLib's SX1261 silicon version signature.
found = memcmp(version, RADIOLIB_SX1262_CHIP_TYPE, 6) == 0;
if (!found) delay(10);
}
// Leave the unselected socket in reset, not receiving or driving an IRQ.
// The chosen socket's std_init will perform its own verified reset.
digitalWrite(pins.reset, LOW);
spi.end();
return found;
}
bool initializeSlot(uint8_t slot) {
spi.end();
station_g3::slotState().candidate = slot;
const auto& pins = station_g3::activePins();
Module* module = radio.getMod();
// The pinned RadioLib build already opts into GODMODE. Keep one stable
// Module/radio/wrapper address, and bind only before std_init/ISR startup.
module->csPin = pins.nss;
module->irqPin = pins.irq;
module->rstPin = pins.reset;
module->gpioPin = pins.busy;
spi.begin(pins.sck, pins.miso, pins.mosi);
if (!radio.std_init(&spi)) {
digitalWrite(pins.reset, LOW);
spi.end();
return false;
}
station_g3::slotState().active = slot;
station_g3::rememberActiveSlot(slot);
board.loRaFEMControl.setRxModeEnable();
return true;
}
} // namespace
bool radio_init() {
volatile const char* capability = meshcore_capability_station_g3_auto_slots;
(void)*capability;
auto& state = station_g3::slotState();
// Runtime recovery must never change physical sockets under live wrapper
// state/interrupts. Only the first boot discovery is allowed to select one.
if (state.active != 0) return radio.std_init(&spi);
fallback_clock.begin();
rtc_clock.begin(Wire);
bool firstNotice = true;
for (;;) {
state.detected = 0;
for (uint8_t slot = 1; slot <= 2; ++slot)
if (probeRadioSlot(slot)) state.detected |= 1U << (slot - 1);
const uint8_t preferred = state.choose(state.detected);
if (preferred && initializeSlot(preferred)) return true;
// Auto may recover using another detected, functional socket. Explicit
// overrides never silently fall back to an unintended antenna/PA.
if (state.bootMode == 0 && preferred && state.detected == 3 &&
initializeSlot(preferred == 1 ? 2 : 1)) return true;
#if defined(P_LORA_SCLK)
spi.begin(P_LORA_SCLK, P_LORA_MISO, P_LORA_MOSI);
return radio.std_init(&spi);
#else
return radio.std_init();
// No radio is not permission to generate a new identity, transmit, or
// reboot-loop before the operator can correct a saved hardware setting.
const uint32_t started = millis();
do {
#if MESH_USB_CONSOLE_COOPERATIVE
mesh::serviceUsbLoggingPort();
mesh::serviceUsbTerminalPort();
#endif
station_g3::serviceBootRecovery(board, firstNotice);
firstNotice = false;
delay(2);
} while (static_cast<uint32_t>(millis() - started) < 10000);
}
}
mesh::LocalIdentity radio_new_identity() {
@@ -1,4 +1,5 @@
#include "WioTrackerL2Board.h"
#include <helpers/ESP32GpioHold.h>
// TCA9535 register map
static const uint8_t TCA_REG_OUTPUT0 = 0x02;
@@ -136,8 +137,8 @@ void WioTrackerL2Board::begin() {
if (wakeup_source & (1 << P_LORA_DIO_1)) {
startup_reason = BD_STARTUP_RX_PACKET; // LoRa packet woke us from deep sleep
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
mesh::esp32::releaseRtcGpioIfSupported((gpio_num_t)P_LORA_NSS);
mesh::esp32::deinitRtcGpioIfSupported((gpio_num_t)P_LORA_DIO_1);
}
}
+4 -2
View File
@@ -18,8 +18,10 @@ void XiaoNrf52Board::initiateShutdown(uint8_t reason) {
bool enable_lpcomp = (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
reason == SHUTDOWN_REASON_BOOT_PROTECT);
// Keep the divider's lower leg grounded even for user shutdown: driving
// READ_BAT_ENABLE HIGH can overvoltage the ADC input during charging.
digitalWrite(VBAT_ENABLE, LOW);
pinMode(VBAT_ENABLE, OUTPUT);
digitalWrite(VBAT_ENABLE, enable_lpcomp ? LOW : HIGH);
if (enable_lpcomp) {
configureVoltageWake(power_config.lpcomp_ain_channel, power_config.lpcomp_refsel);
@@ -34,8 +36,8 @@ void XiaoNrf52Board::begin() {
// Configure battery voltage ADC
pinMode(PIN_VBAT, INPUT);
pinMode(VBAT_ENABLE, OUTPUT);
digitalWrite(VBAT_ENABLE, LOW); // Enable VBAT divider for reading
pinMode(VBAT_ENABLE, OUTPUT);
analogReadResolution(12);
analogReference(AR_INTERNAL_3_0);
delay(50); // Allow ADC to settle
+3 -6
View File
@@ -56,14 +56,11 @@ const uint32_t g_ADigitalPinMap[] = {
};
void initVariant() {
// Disable reading of the BAT voltage.
// Keep the battery divider enabled: HIGH can overvoltage the ADC while charging.
// https://wiki.seeedstudio.com/XIAO_BLE#q3-what-are-the-considerations-when-using-xiao-nrf52840-sense-for-battery-charging
pinMode(VBAT_ENABLE, OUTPUT);
// digitalWrite(VBAT_ENABLE, HIGH);
// This was taken from Seeed github butis not coherent with the doc,
// VBAT_ENABLE should be kept to LOW to protect P0.14, (1500/500)*(4.2-3.3)+3.3 = 3.9V > 3.6V
// This induces a 3mA current in the resistors :( but it's better than burning the nrf
// The 1 MOhm + 510 kOhm divider draws about 3 uA, not 3 mA.
digitalWrite(VBAT_ENABLE, LOW);
pinMode(VBAT_ENABLE, OUTPUT);
// Low charging current (50mA)
// https://wiki.seeedstudio.com/XIAO_BLE#battery-charging-current