Skip to content

Commit c0d73ec

Browse files
authored
Merge pull request #1984 from samschlegel/samschlegel/rak3401-power-management
Add nrf52 power management for RAK3401
2 parents a0867a0 + bfdbcd8 commit c0d73ec

3 files changed

Lines changed: 40 additions & 13 deletions

File tree

variants/rak3401/RAK3401Board.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33

44
#include "RAK3401Board.h"
55

6+
#ifdef NRF52_POWER_MANAGEMENT
7+
// Static configuration for power management
8+
// Values set in variant.h defines
9+
const PowerMgtConfig power_config = {
10+
.lpcomp_ain_channel = PWRMGT_LPCOMP_AIN,
11+
.lpcomp_refsel = PWRMGT_LPCOMP_REFSEL,
12+
.voltage_bootlock = PWRMGT_VOLTAGE_BOOTLOCK
13+
};
14+
15+
void RAK3401Board::initiateShutdown(uint8_t reason) {
16+
// Disable SKY66122 FEM (CSD+CPS LOW = shutdown, <1 uA)
17+
digitalWrite(SX126X_POWER_EN, LOW);
18+
19+
// Disable 3V3 switched peripherals and 5V boost
20+
digitalWrite(PIN_3V3_EN, LOW);
21+
22+
if (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
23+
reason == SHUTDOWN_REASON_BOOT_PROTECT) {
24+
configureVoltageWake(power_config.lpcomp_ain_channel, power_config.lpcomp_refsel);
25+
}
26+
27+
enterSystemOff(reason);
28+
}
29+
#endif
30+
631
void RAK3401Board::begin() {
732
NRF52BoardDCDC::begin();
833
pinMode(PIN_VBAT_READ, INPUT);
@@ -31,18 +56,11 @@ void RAK3401Board::begin() {
3156
// HIGH = FEM active (LNA for RX, PA path available for TX).
3257
// TX/RX switching (CTX) is handled by SX1262 DIO2 via SetDIO2AsRfSwitchCtrl.
3358
pinMode(SX126X_POWER_EN, OUTPUT);
59+
#ifdef NRF52_POWER_MANAGEMENT
60+
// Boot voltage protection check (may not return if voltage too low)
61+
// We need to call this after we configure SX126X_POWER_EN as output but before we pull high
62+
checkBootVoltage(&power_config);
63+
#endif
3464
digitalWrite(SX126X_POWER_EN, HIGH);
3565
delay(1); // SKY66122 turn-on settling time (tON = 3us typ)
3666
}
37-
38-
#ifdef NRF52_POWER_MANAGEMENT
39-
void RAK3401Board::initiateShutdown(uint8_t reason) {
40-
// Disable SKY66122 FEM (CSD+CPS LOW = shutdown, <1 uA)
41-
digitalWrite(SX126X_POWER_EN, LOW);
42-
43-
// Disable 3V3 switched peripherals and 5V boost
44-
digitalWrite(PIN_3V3_EN, LOW);
45-
46-
enterSystemOff(reason);
47-
}
48-
#endif

variants/rak3401/platformio.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ build_flags = ${nrf52_base.build_flags}
66
${sensor_base.build_flags}
77
-I variants/rak3401
88
-D RAK_3401
9+
-D NRF52_POWER_MANAGEMENT
910
-D RADIO_CLASS=CustomSX1262
1011
-D WRAPPER_CLASS=CustomSX1262Wrapper
1112
-D LORA_TX_POWER=22
@@ -124,4 +125,4 @@ build_flags =
124125
;-D MESH_DEBUG=1
125126
build_src_filter = ${rak3401.build_src_filter}
126127
+<helpers/ui/SSD1306Display.cpp>
127-
+<../examples/simple_sensor>
128+
+<../examples/simple_sensor>

variants/rak3401/variant.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ extern "C"
7878
static const uint8_t A7 = PIN_A7;
7979
#define ADC_RESOLUTION 14
8080

81+
// Power management boot protection threshold (millivolts)
82+
// Set to 0 to disable boot protection
83+
#define PWRMGT_VOLTAGE_BOOTLOCK 3300 // Won't boot below this voltage (mV)
84+
// LPCOMP wake configuration (voltage recovery from SYSTEMOFF)
85+
// AIN3 = P0.05 = PIN_A0 / PIN_VBAT_READ
86+
#define PWRMGT_LPCOMP_AIN 3
87+
#define PWRMGT_LPCOMP_REFSEL 4 // 5/8 VDD (~3.13-3.44V)
88+
8189
// Other pins
8290
#define WB_I2C1_SDA (13) // SENSOR_SLOT IO_SLOT
8391
#define WB_I2C1_SCL (14) // SENSOR_SLOT IO_SLOT

0 commit comments

Comments
 (0)