Implement power management features and update battery sensing configuration

This commit is contained in:
Lbibass 2026-03-14 10:38:50 -04:00
parent 1e435ae2d1
commit 20953e56d6
5 changed files with 51 additions and 101 deletions

View file

@ -3,44 +3,38 @@
#include "muzi_base_duoBoard.h"
// void muzi_base_duoBoard::begin() {
// NRF52BoardDCDC::begin();
// btn_prev_state = HIGH;
#ifdef NRF52_POWER_MANAGEMENT
const PowerMgtConfig power_config = {
.lpcomp_ain_channel = PWRMGT_LPCOMP_AIN,
.lpcomp_refsel = PWRMGT_LPCOMP_REFSEL,
.voltage_bootlock = PWRMGT_VOLTAGE_BOOTLOCK
};
// #ifdef BUTTON_PIN
// pinMode(BATTERY_PIN, INPUT);
// pinMode(BUTTON_PIN, INPUT);
// pinMode(LED_PIN, OUTPUT);
// #endif
void muzi_base_duoBoard::initiateShutdown(uint8_t reason) {
// Disable LoRa module power before shutdown
if (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
reason == SHUTDOWN_REASON_BOOT_PROTECT) {
configureVoltageWake(power_config.lpcomp_ain_channel, power_config.lpcomp_refsel);
}
// #if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL)
// Wire.setPins(PIN_BOARD_SDA, PIN_BOARD_SCL);
// #endif
enterSystemOff(reason);
}
#endif // NRF52_POWER_MANAGEMENT
// Wire.begin();
// delay(10); // give sx1262 some time to power up
// }
void muzi_base_duoBoard::begin() {
NRF52BoardDCDC::begin();
pinMode(BATTERY_PIN, INPUT);
#ifdef PIN_USER_BTN
pinMode(PIN_USER_BTN, INPUT);
#endif
#ifdef PIN_USER_BTN_ANA
pinMode(PIN_USER_BTN_ANA, INPUT_PULLUP);
pinMode(PIN_VBAT_READ, INPUT);
#ifdef PIN_USER_BTN
pinMode(PIN_USER_BTN, INPUT_PULLUP);
#endif
#if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL)
Wire.setPins(PIN_BOARD_SDA, PIN_BOARD_SCL);
#endif
Wire.begin();
#ifdef NRF52_POWER_MANAGEMENT
// Boot voltage protection check (may not return if voltage too low)
// We need to call this after we configure SX126X_POWER_EN as output but before we pull high
checkBootVoltage(&power_config);
#endif
delay(10); // give sx1262 some time to power up
}

View file

@ -17,72 +17,21 @@ public:
#define BATTERY_SAMPLES 8
uint16_t getBattMilliVolts() override {
MESH_DEBUG_PRINTLN("BaseDuo: Sampling battery");
analogReadResolution(12);
analogReference(AR_INTERNAL_3_0);
delay(1);
uint32_t raw = 0;
for (int i = 0; i < BATTERY_SAMPLES; i++) {
raw += analogRead(BATTERY_PIN);
raw += analogRead(PIN_VBAT_READ);
}
raw = raw / BATTERY_SAMPLES;
return (ADC_MULTIPLIER * raw) / 4096;
// ADC_MULTIPLIER is the voltage divider ratio
return (raw * ADC_MULTIPLIER * AREF_VOLTAGE) / 4.096;
}
const char* getManufacturerName() const override {
return "Muzi Base Duo";
}
// int buttonStateChanged() {
// #ifdef BUTTON_PIN
// uint8_t v = digitalRead(BUTTON_PIN);
// if (v != btn_prev_state) {
// btn_prev_state = v;
// return (v == LOW) ? 1 : -1;
// }
// #endif
// return 0;
// }
// void powerOff() override {
// // #ifdef HAS_GPS
// // digitalWrite(GPS_VRTC_EN, LOW);
// // digitalWrite(GPS_RESET, LOW);
// // digitalWrite(GPS_SLEEP_INT, LOW);
// // digitalWrite(GPS_RTC_INT, LOW);
// // digitalWrite(GPS_EN, LOW);
// // #endif
// // #ifdef BUZZER_EN
// // digitalWrite(BUZZER_EN, LOW);
// // #endif
// // #ifdef PIN_3V3_EN
// // digitalWrite(PIN_3V3_EN, LOW);
// // #endif
// // #ifdef PIN_3V3_ACC_EN
// // digitalWrite(PIN_3V3_ACC_EN, LOW);
// // #endif
// // #ifdef SENSOR_EN
// // digitalWrite(SENSOR_EN, LOW);
// // #endif
// // set led on and wait for button release before poweroff
// #ifdef LED_PIN
// digitalWrite(LED_PIN, HIGH);
// #endif
// #ifdef BUTTON_PIN
// while(digitalRead(BUTTON_PIN));
// #endif
// #ifdef LED_PIN
// digitalWrite(LED_PIN, LOW);
// #endif
// #ifdef BUTTON_PIN
// nrf_gpio_cfg_sense_input(BUTTON_PIN, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_HIGH);
// #endif
// sd_power_system_off();
// }
};

View file

@ -9,9 +9,10 @@ build_flags = ${nrf52_base.build_flags}
-I variants/muzi_base_duo
-I src/helpers/ui
-D muzi_base_duo
-D NRF52_POWER_MANAGEMENT
-D PIN_USER_BTN=10
-D USER_BTN_PRESSED=LOW
-D PIN_STATUS_LED=(32+3)
-D PIN_STATUS_LED=35
-D RADIO_CLASS=CustomLR1110
-D WRAPPER_CLASS=CustomLR1110Wrapper
-D LORA_TX_POWER=22

View file

@ -66,11 +66,12 @@ void initVariant()
// All pins output HIGH by default.
// https://github.com/Seeed-Studio/Adafruit_nRF52_Arduino/blob/fab7d30a997a1dfeef9d1d59bfb549adda73815a/cores/nRF5/wiring.c#L65-L69
pinMode(BATTERY_PIN, INPUT);
pinMode(EXT_CHRG_DETECT, INPUT);
pinMode(EXT_PWR_DETECT, INPUT);
pinMode(PIN_VBAT_READ, INPUT);
// pinMode(EXT_CHRG_DETECT, INPUT);
// pinMode(EXT_PWR_DETECT, INPUT);
pinMode(PIN_BUTTON1, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_PIN, LOW);
digitalWrite(LED_BLUE, LOW);
}

View file

@ -18,21 +18,26 @@
////////////////////////////////////////////////////////////////////////////////
// Power
#define NRF_APM // detect usb power
// #define PIN_3V3_EN (38) // P1.6 Power to Sensors no power
#define PIN_VBAT_READ (31) // P0.31
#define BATTERY_SENSE_RESOLUTION_BITS 12
#define BATTERY_SENSE_RESOLUTION 4096.0
#define AREF_VOLTAGE 3.0
#define VBAT_AR_INTERNAL AR_INTERNAL_3_0
#define ADC_MULTIPLIER 1.537
#define ADC_RESOLUTION 14
#define BATTERY_PIN (0 + 31) // P0.31
// #define BATTERY_CHARGING_INV (32 + 02) // P1.02
// #define BATTERY_IMMUTABLE
#define ADC_MULTIPLIER (1.537F)
// Power management boot protection threshold (millivolts)
#define PWRMGT_VOLTAGE_BOOTLOCK 3300 // Won't boot below this voltage (mV)
#define EXT_CHRG_DETECT (32+02) // P1.02 Muzi Base schematic shows there's a bat_chg status. That's stat2 on BQ25. if low, it's charging.
#define EXT_PWR_DETECT (27) // P0.5 stat1 (power detect, basically) on bq25 is connected to pin 0.27
// LPCOMP wake configuration (voltage recovery from SYSTEMOFF)
#define PWRMGT_LPCOMP_AIN 3
#define PWRMGT_LPCOMP_REFSEL 4 // 5/8 VDD (~3.13-3.44V)
#define ADC_RESOLUTION (14)
#define BATTERY_SENSE_RES (12)
// Other pins
#define PIN_AREF (2)
#define AREF_VOLTAGE (3.3)
static const uint8_t AREF = PIN_AREF;
////////////////////////////////////////////////////////////////////////////////
// Number of pins
@ -79,10 +84,10 @@
////////////////////////////////////////////////////////////////////////////////
// Builtin LEDs
#define LED_BUILTIN (-1)
#define LED_BLUE (32+4) // P1.04
#define LED_GREEN (32+3) // P1.03
#define LED_PIN LED_GREEN
#define LED_BUILTIN (35)
#define LED_BLUE (-1) // P1.04 turned off, because the blue LED was annoying.
// #define LED_GREEN (35) // P1.03
#define LED_PIN LED_BUILTIN
#define LED_STATE_ON LOW