Compare commits

...
4 changed files with 99 additions and 122 deletions
+20 -119
View File
@@ -68,11 +68,7 @@
#define ETH ETH2 #define ETH ETH2
#endif // HAS_ETHERNET #endif // HAS_ETHERNET
#endif #endif // MQTT
#ifndef DELAY_FOREVER
#define DELAY_FOREVER portMAX_DELAY
#endif
#if defined(BATTERY_PIN) && defined(ARCH_ESP32) #if defined(BATTERY_PIN) && defined(ARCH_ESP32)
@@ -94,34 +90,6 @@ static const adc_atten_t atten = ADC_ATTENUATION;
#endif #endif
#endif // BATTERY_PIN && ARCH_ESP32 #endif // BATTERY_PIN && ARCH_ESP32
#ifdef EXT_PWR_DETECT
#ifndef EXT_PWR_DETECT_MODE
#define EXT_PWR_DETECT_MODE INPUT
// If using internal pull resistors, we can infer EXT_PWR_DETECT_VALUE
#elif EXT_PWR_DETECT_MODE == INPUT_PULLUP
#define EXT_PWR_DETECT_VALUE LOW
#elif EXT_PWR_DETECT_MODE == INPUT_PULLDOWN
#define EXT_PWR_DETECT_VALUE HIGH
#endif
#ifndef EXT_PWR_DETECT_VALUE
#define EXT_PWR_DETECT_VALUE HIGH
#endif
#endif
#ifdef EXT_CHRG_DETECT
#ifndef EXT_CHRG_DETECT_MODE
#define EXT_CHRG_DETECT_MODE INPUT
// If using internal pull resistors, we can infer EXT_CHRG_DETECT_VALUE
#elif EXT_CHRG_DETECT_MODE == INPUT_PULLUP
#define EXT_CHRG_DETECT_VALUE LOW
#elif EXT_CHRG_DETECT_MODE == INPUT_PULLDOWN
#define EXT_CHRG_DETECT_VALUE HIGH
#endif
#ifndef EXT_CHRG_DETECT_VALUE
#define EXT_CHRG_DETECT_VALUE HIGH
#endif
#endif
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR #if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
#if __has_include(<Adafruit_INA219.h>) #if __has_include(<Adafruit_INA219.h>)
INA219Sensor ina219Sensor; INA219Sensor ina219Sensor;
@@ -160,7 +128,7 @@ MAX17048Sensor max17048Sensor;
NullSensor max17048Sensor; NullSensor max17048Sensor;
#endif #endif
#endif #endif
#endif #endif // !MESHTASTIC_EXCLUDE_I2C
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && HAS_RAKPROT #if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && HAS_RAKPROT
RAK9154Sensor rak9154Sensor; RAK9154Sensor rak9154Sensor;
@@ -178,46 +146,12 @@ XPowersPPM *PPM = NULL;
#ifdef HAS_PMU #ifdef HAS_PMU
XPowersLibInterface *PMU = NULL; XPowersLibInterface *PMU = NULL;
#else
// Copy of the base class defined in axp20x.h.
// I'd rather not include axp20x.h as it brings Wire dependency.
class HasBatteryLevel
{
public:
/**
* Battery state of charge, from 0 to 100 or -1 for unknown
*/
virtual int getBatteryPercent() { return -1; }
/**
* The raw voltage of the battery or NAN if unknown
*/
virtual uint16_t getBattVoltage() { return 0; }
/**
* return true if there is a battery installed in this unit
*/
virtual bool isBatteryConnect() { return false; }
virtual bool isVbusIn() { return false; }
virtual bool isCharging() { return false; }
};
#endif #endif
bool pmu_irq = false;
Power *power; Power *power;
using namespace meshtastic; using namespace meshtastic;
// NRF52 has AREF_VOLTAGE defined in architecture.h but
// make sure it's included. If something is wrong with NRF52
// definition - compilation will fail on missing definition
#if !defined(AREF_VOLTAGE) && !defined(ARCH_NRF52)
#define AREF_VOLTAGE 3.3
#endif
/** /**
* If this board has a battery level sensor, set this to a valid implementation * If this board has a battery level sensor, set this to a valid implementation
*/ */
@@ -260,7 +194,7 @@ static void battery_adcDisable()
#endif #endif
} }
#endif #endif // BATTERY_PIN
/** /**
* A simple battery level sensor that assumes the battery voltage is attached * A simple battery level sensor that assumes the battery voltage is attached
@@ -319,7 +253,7 @@ class AnalogBatteryLevel : public HasBatteryLevel
} }
/** /**
* The raw voltage of the batteryin millivolts or NAN if unknown * The raw voltage of the battery in millivolts or NAN if unknown
*/ */
virtual uint16_t getBattVoltage() override virtual uint16_t getBattVoltage() override
{ {
@@ -336,16 +270,6 @@ class AnalogBatteryLevel : public HasBatteryLevel
} }
#endif #endif
#ifndef ADC_MULTIPLIER
#define ADC_MULTIPLIER 2.0
#endif
#ifndef BATTERY_SENSE_SAMPLES
#define BATTERY_SENSE_SAMPLES \
15 // Set the number of samples, it has an effect of increasing sensitivity in
// complex electromagnetic environment.
#endif
#ifdef BATTERY_PIN #ifdef BATTERY_PIN
// Override variant or default ADC_MULTIPLIER if we have the override pref // Override variant or default ADC_MULTIPLIER if we have the override pref
float operativeAdcMultiplier = float operativeAdcMultiplier =
@@ -1000,11 +924,8 @@ int32_t Power::runOnce()
powerFSM.trigger(EVENT_POWER_CONNECTED); powerFSM.trigger(EVENT_POWER_CONNECTED);
} }
#ifdef T_WATCH_S3 #ifdef PMU_POWER_BUTTON_IS_CANCEL
/* // cancel action also turns the screen on and off.
In the T-Watch S3 this code fragment reacts to the short press of the button by switching the
display on and off
*/
if (PMU->isPekeyShortPressIrq()) { if (PMU->isPekeyShortPressIrq()) {
LOG_INFO("Input: Corona Button Click"); LOG_INFO("Input: Corona Button Click");
InputEvent event = {.inputEvent = (input_broker_event)INPUT_BROKER_CANCEL, .kbchar = 0, .touchX = 0, .touchY = 0}; InputEvent event = {.inputEvent = (input_broker_event)INPUT_BROKER_CANCEL, .kbchar = 0, .touchX = 0, .touchY = 0};
@@ -1027,13 +948,6 @@ int32_t Power::runOnce()
LOG_DEBUG("Battery removed"); LOG_DEBUG("Battery removed");
} }
*/ */
#ifndef T_WATCH_S3 // FIXME - why is this triggering on the T-Watch S3?
if (PMU->isPekeyLongPressIrq()) {
LOG_DEBUG("PEK long button press");
if (screen)
screen->setOn(false);
}
#endif
PMU->clearIrqStatus(); PMU->clearIrqStatus();
} }
@@ -1102,8 +1016,8 @@ void Power::attachPowerInterrupts()
if (PMU) { if (PMU) {
attachInterrupt( attachInterrupt(
PMU_IRQ, PMU_IRQ,
[] { []() {
pmu_irq = true; power->pmu_irq = true;
power->setIntervalFromNow(0); power->setIntervalFromNow(0);
runASAP = true; runASAP = true;
}, },
@@ -1405,19 +1319,16 @@ bool Power::axpChipInit()
uint64_t pmuIrqMask = 0; uint64_t pmuIrqMask = 0;
if (PMU->getChipModel() == XPOWERS_AXP192) { if (PMU->getChipModel() == XPOWERS_AXP192) {
pmuIrqMask = XPOWERS_AXP192_VBUS_INSERT_IRQ | XPOWERS_AXP192_BAT_INSERT_IRQ | XPOWERS_AXP192_PKEY_SHORT_IRQ; pmuIrqMask = XPOWERS_AXP192_VBUS_INSERT_IRQ | XPOWERS_AXP192_VBUS_REMOVE_IRQ | XPOWERS_AXP192_PKEY_SHORT_IRQ;
} else if (PMU->getChipModel() == XPOWERS_AXP2101) { } else if (PMU->getChipModel() == XPOWERS_AXP2101) {
pmuIrqMask = XPOWERS_AXP2101_VBUS_INSERT_IRQ | XPOWERS_AXP2101_BAT_INSERT_IRQ | XPOWERS_AXP2101_PKEY_SHORT_IRQ; pmuIrqMask = XPOWERS_AXP2101_VBUS_INSERT_IRQ | XPOWERS_AXP2101_VBUS_REMOVE_IRQ | XPOWERS_AXP2101_PKEY_SHORT_IRQ;
} }
pinMode(PMU_IRQ, INPUT); pinMode(PMU_IRQ, INPUT);
// we do not look for AXPXXX_CHARGING_FINISHED_IRQ & AXPXXX_CHARGING_IRQ // We wake on IRQ, so only enable the IRQs that we care about.
// because it occurs repeatedly while there is no battery also it could cause // we want USB plug and unplug to update the screen and LED status,
// inadvertent waking from light sleep just because the battery filled we // and short press on the power button to trigger the "cancel" action in the UI (which also turns the screen on and off).
// don't look for AXPXXX_BATT_REMOVED_IRQ because it occurs repeatedly while
// no battery installed we don't look at AXPXXX_VBUS_REMOVED_IRQ because we
// don't have anything hooked to vbus
PMU->enableIRQ(pmuIrqMask); PMU->enableIRQ(pmuIrqMask);
PMU->clearIrqStatus(); PMU->clearIrqStatus();
@@ -1725,7 +1636,7 @@ bool Power::lipoChargerInit()
return true; return true;
} }
#else #else // HAS_PPM
/** /**
* The Lipo battery level sensor is unavailable - default to AnalogBatteryLevel * The Lipo battery level sensor is unavailable - default to AnalogBatteryLevel
*/ */
@@ -1733,7 +1644,7 @@ bool Power::lipoChargerInit()
{ {
return false; return false;
} }
#endif #endif // HAS_PPM
#ifdef HELTEC_MESH_SOLAR #ifdef HELTEC_MESH_SOLAR
#include "meshSolarApp.h" #include "meshSolarApp.h"
@@ -1795,7 +1706,7 @@ bool Power::meshSolarInit()
return true; return true;
} }
#else #else // HELTEC_MESH_SOLAR
/** /**
* The meshSolar battery level sensor is unavailable - default to * The meshSolar battery level sensor is unavailable - default to
* AnalogBatteryLevel * AnalogBatteryLevel
@@ -1804,7 +1715,7 @@ bool Power::meshSolarInit()
{ {
return false; return false;
} }
#endif #endif // HELTEC_MESH_SOLAR
#ifdef HAS_SERIAL_BATTERY_LEVEL #ifdef HAS_SERIAL_BATTERY_LEVEL
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
@@ -1812,7 +1723,7 @@ bool Power::meshSolarInit()
/** /**
* SerialBatteryLevel class for pulling battery information from a secondary MCU over serial. * SerialBatteryLevel class for pulling battery information from a secondary MCU over serial.
*/ */
class SerialBatteryLevel : public HasBatteryLevel class SerialBatteryLevel : public AnalogBatteryLevel
{ {
public: public:
@@ -1889,16 +1800,6 @@ class SerialBatteryLevel : public HasBatteryLevel
return false; return false;
} }
virtual bool isCharging() override
{
#ifdef EXT_CHRG_DETECT
return digitalRead(EXT_CHRG_DETECT) == EXT_CHRG_DETECT_VALUE;
#endif
// by default, we check the battery voltage only
return isVbusIn();
}
private: private:
SoftwareSerial BatterySerial = SoftwareSerial(SERIAL_BATTERY_RX, SERIAL_BATTERY_TX); SoftwareSerial BatterySerial = SoftwareSerial(SERIAL_BATTERY_RX, SERIAL_BATTERY_TX);
uint8_t Data[6] = {0}; uint8_t Data[6] = {0};
@@ -1928,7 +1829,7 @@ bool Power::serialBatteryInit()
return true; return true;
} }
#else #else // HAS_SERIAL_BATTERY_LEVEL
/** /**
* If this device has no serial battery level sensor, don't try to use it. * If this device has no serial battery level sensor, don't try to use it.
*/ */
@@ -1936,4 +1837,4 @@ bool Power::serialBatteryInit()
{ {
return false; return false;
} }
#endif #endif // HAS_SERIAL_BATTERY_LEVEL
+75
View File
@@ -28,6 +28,54 @@
#define NUM_CELLS 1 #define NUM_CELLS 1
#endif #endif
// Set the number of samples, it has an effect of increasing sensitivity in complex electromagnetic environment.
#ifndef BATTERY_SENSE_SAMPLES
#define BATTERY_SENSE_SAMPLES 15
#endif
#ifndef ADC_MULTIPLIER
#define ADC_MULTIPLIER 2.0
#endif
#ifdef EXT_PWR_DETECT
#ifndef EXT_PWR_DETECT_MODE
#define EXT_PWR_DETECT_MODE INPUT
// If using internal pull resistors, we can infer EXT_PWR_DETECT_VALUE
#elif EXT_PWR_DETECT_MODE == INPUT_PULLUP
#define EXT_PWR_DETECT_VALUE LOW
#elif EXT_PWR_DETECT_MODE == INPUT_PULLDOWN
#define EXT_PWR_DETECT_VALUE HIGH
#endif
#ifndef EXT_PWR_DETECT_VALUE
#define EXT_PWR_DETECT_VALUE HIGH
#endif
#endif
#ifdef EXT_CHRG_DETECT
#ifndef EXT_CHRG_DETECT_MODE
#define EXT_CHRG_DETECT_MODE INPUT
// If using internal pull resistors, we can infer EXT_CHRG_DETECT_VALUE
#elif EXT_CHRG_DETECT_MODE == INPUT_PULLUP
#define EXT_CHRG_DETECT_VALUE LOW
#elif EXT_CHRG_DETECT_MODE == INPUT_PULLDOWN
#define EXT_CHRG_DETECT_VALUE HIGH
#endif
#ifndef EXT_CHRG_DETECT_VALUE
#define EXT_CHRG_DETECT_VALUE HIGH
#endif
#endif
#ifndef DELAY_FOREVER
#define DELAY_FOREVER portMAX_DELAY
#endif
// NRF52 has AREF_VOLTAGE defined in architecture.h but
// make sure it's included. If something is wrong with NRF52
// definition - compilation will fail on missing definition
#if !defined(AREF_VOLTAGE) && !defined(ARCH_NRF52)
#define AREF_VOLTAGE 3.3
#endif
#ifdef BAT_MEASURE_ADC_UNIT #ifdef BAT_MEASURE_ADC_UNIT
extern RTC_NOINIT_ATTR uint64_t RTC_reg_b; extern RTC_NOINIT_ATTR uint64_t RTC_reg_b;
#include "soc/sens_reg.h" // needed for adc pin reset #include "soc/sens_reg.h" // needed for adc pin reset
@@ -86,6 +134,31 @@ extern RAK9154Sensor rak9154Sensor;
extern XPowersLibInterface *PMU; extern XPowersLibInterface *PMU;
#endif #endif
#ifndef HAS_PMU
// Copy of the base class defined in axp20x.h, to prevent an automatic wire.h dependency
class HasBatteryLevel
{
public:
/**
* Battery state of charge, from 0 to 100 or -1 for unknown
*/
virtual int getBatteryPercent() { return -1; }
/**
* The raw voltage of the battery or NAN if unknown
*/
virtual uint16_t getBattVoltage() { return 0; }
/**
* return true if there is a battery installed in this unit
*/
virtual bool isBatteryConnect() { return false; }
virtual bool isVbusIn() { return false; }
virtual bool isCharging() { return false; }
};
#endif
class Power : public concurrency::OSThread class Power : public concurrency::OSThread
{ {
@@ -110,6 +183,8 @@ class Power : public concurrency::OSThread
void attachPowerInterrupts(); void attachPowerInterrupts();
void detachPowerInterrupts(); void detachPowerInterrupts();
volatile bool pmu_irq = false;
protected: protected:
meshtastic::PowerStatus *statusHandler; meshtastic::PowerStatus *statusHandler;
+1
View File
@@ -42,6 +42,7 @@
#define DAC_I2S_MCLK -1 #define DAC_I2S_MCLK -1
#define HAS_AXP2101 #define HAS_AXP2101
#define PMU_POWER_BUTTON_IS_CANCEL // maps a short click of the power button to a cancel action (turning off the screen)
// PCF8563 RTC Module // PCF8563 RTC Module
#define PCF8563_RTC 0x51 #define PCF8563_RTC 0x51
+3 -3
View File
@@ -46,9 +46,9 @@
#define LR11X0_DIO_AS_RF_SWITCH #define LR11X0_DIO_AS_RF_SWITCH
#endif #endif
// Leave undefined to disable our PMU IRQ handler. DO NOT ENABLE THIS because the pmuirq can cause sperious interrupts // Voiding warrenties, we're gonna try the IRQ
// and waking from light sleep #define PMU_IRQ 40
// #define PMU_IRQ 40 #define PMU_POWER_BUTTON_IS_CANCEL // maps a short click of the power button to a cancel action (turning off the screen)
#define HAS_AXP2101 #define HAS_AXP2101
// PCF8563 RTC Module // PCF8563 RTC Module