diff --git a/variants/arduino_nesso_n1/ArduinoNessoN1Board.h b/variants/arduino_nesso_n1/ArduinoNessoN1Board.h index 76f37b877..67fdce08f 100644 --- a/variants/arduino_nesso_n1/ArduinoNessoN1Board.h +++ b/variants/arduino_nesso_n1/ArduinoNessoN1Board.h @@ -32,6 +32,12 @@ public: } uint16_t getBattMilliVolts() override { +#ifdef MESH_DEBUG + MESH_DEBUG_PRINTLN("getBattMilliVolts(): isCharging(): %u", battery.isCharging()); + MESH_DEBUG_PRINTLN("getBattMilliVolts(): Current charge level %u %%", battery.getChargeLevel()); + MESH_DEBUG_PRINTLN("getBattMilliVolts(): Current voltage %f V", battery.getVoltage()); + MESH_DEBUG_PRINTLN("getBattMilliVolts(): Current voltage %u mV", battery.getMilliVoltage()); +#endif return battery.getMilliVoltage(); } diff --git a/variants/arduino_nesso_n1/expander.cpp b/variants/arduino_nesso_n1/expander.cpp index 80e53b4c4..0e281e759 100644 --- a/variants/arduino_nesso_n1/expander.cpp +++ b/variants/arduino_nesso_n1/expander.cpp @@ -141,6 +141,7 @@ void NessoBattery::enableCharge() { bool charge_enable_bit = readBitRegister(AW32001_I2C_CHIP_ADDR, AW32001_REG_PWR_CFG, 3); MESH_DEBUG_PRINTLN("NessoBattery::enableCharge(): Current charge setting (low is on): %u", charge_enable_bit); + MESH_DEBUG_PRINTLN("NessoBattery::enableCharge(): isCharging(): %u", NessoBattery::isCharging()); MESH_DEBUG_PRINTLN("NessoBattery::enableCharge(): Current charge level %u %%", NessoBattery::getChargeLevel()); MESH_DEBUG_PRINTLN("NessoBattery::enableCharge(): Current voltage %f V", NessoBattery::getVoltage()); MESH_DEBUG_PRINTLN("NessoBattery::enableCharge(): Current voltage %u mV", NessoBattery::getMilliVoltage()); @@ -154,6 +155,34 @@ void NessoBattery::enableCharge() { #endif } +NessoBattery::ChargeStatus NessoBattery::getChargeStatus(void) { + uint8_t reg_value = 0; + if (_power_mgmt_init) + { + reg_value = readRegister(AW32001_I2C_CHIP_ADDR, AW32001_REG_SYS_STA); + // Extract bits 4 and 3 for charge status + reg_value = (reg_value >> 3) & 0b00000011; // Get bits 4 and 3 + MESH_DEBUG_PRINTLN("NessoBattery::getChargeStatus(): bits 4 and 3 from register %#02x = %u", AW32001_REG_SYS_STA, reg_value); + switch (reg_value) + { + case 0b00: return CS_NOT_CHARGING; // Not charging + case 0b01: return CS_PRE_CHARGE; // Pre-charge + case 0b10: return CS_CHARGE; // Charging + case 0b11: return CS_CHARGE_DONE; // Charge done + default: return CS_UNKNOWN; // Unknown state + } + } + MESH_DEBUG_PRINTLN("NessoBattery::getChargeStatus(): failed, probably chip wasn't init"); + return CS_UNKNOWN; // Return unknown if read failed +} + +bool NessoBattery::isCharging(void) +{ + ChargeStatus status = getChargeStatus(); + MESH_DEBUG_PRINTLN("NessoBattery::isCharging(): ChargeStatus = %u; is? false0/true1 = %u", status, (status == CS_PRE_CHARGE || status == CS_CHARGE)); + return (status == CS_PRE_CHARGE || status == CS_CHARGE); +} + float NessoBattery::getVoltage() { // BQ27220 - address 0x55 if (!wireInitialized) { @@ -162,6 +191,7 @@ float NessoBattery::getVoltage() { } MESH_DEBUG_PRINTLN("NessoBattery::getVoltage()"); uint16_t voltage = (readRegister(0x55, 0x9) << 8) | readRegister(0x55, 0x8); + MESH_DEBUG_PRINTLN("NessoBattery::getVoltage(): %f", voltage / 1000.0f); return (float)voltage / 1000.0f; } @@ -173,6 +203,7 @@ uint16_t NessoBattery::getMilliVoltage() { } MESH_DEBUG_PRINTLN("NessoBattery::getMilliVoltage()"); uint16_t voltage = (readRegister(0x55, 0x9) << 8) | readRegister(0x55, 0x8); + MESH_DEBUG_PRINTLN("NessoBattery::getMilliVoltage(): %u", voltage); return voltage; } @@ -185,6 +216,7 @@ uint16_t NessoBattery::getChargeLevel() { MESH_DEBUG_PRINTLN("NessoBattery::getChargeLevel()"); uint16_t current_capacity = readRegister(0x55, 0x11) << 8 | readRegister(0x55, 0x10); uint16_t total_capacity = readRegister(0x55, 0x13) << 8 | readRegister(0x55, 0x12); + MESH_DEBUG_PRINTLN("NessoBattery::getChargeLevel(): curr = %u / total = %u; pct = %u %%", current_capacity, total_capacity, ((current_capacity * 100) / total_capacity)); return (current_capacity * 100) / total_capacity; } diff --git a/variants/arduino_nesso_n1/pins_arduino.h b/variants/arduino_nesso_n1/pins_arduino.h index 07390aadd..1f3a675f9 100644 --- a/variants/arduino_nesso_n1/pins_arduino.h +++ b/variants/arduino_nesso_n1/pins_arduino.h @@ -43,14 +43,14 @@ static const uint8_t LCD_RS = 16; // AW32001 registers -static constexpr const uint8_t AW32001_REG_PWR_CFG = 0x01; // Power Configuration -static constexpr const uint8_t AW32001_REG_CHR_CUR = 0x02; // Charging current -static constexpr const uint8_t AW32001_REG_CHR_VOL = 0x04; // Charge voltage -static constexpr const uint8_t AW32001_REG_CHR_TMR = 0x05; // Charge timer -static constexpr const uint8_t AW32001_REG_SYS_STA = 0x08; // System status -static constexpr const uint8_t AW32001_REG_CHIP_ID = 0x0A; // ChipID +static const uint8_t AW32001_REG_PWR_CFG = 0x01; // Power Configuration +static const uint8_t AW32001_REG_CHR_CUR = 0x02; // Charging current +static const uint8_t AW32001_REG_CHR_VOL = 0x04; // Charge voltage +static const uint8_t AW32001_REG_CHR_TMR = 0x05; // Charge timer +static const uint8_t AW32001_REG_SYS_STA = 0x08; // System status +static const uint8_t AW32001_REG_CHIP_ID = 0x0A; // ChipID -static constexpr const uint8_t AW32001_I2C_CHIP_ADDR = 0x49; // ChipID +static const uint8_t AW32001_I2C_CHIP_ADDR = 0x49; // ChipID @@ -67,12 +67,23 @@ class NessoBattery { private: bool _power_mgmt_init = false; public: + enum ChargeStatus + { + CS_UNKNOWN = -1, + CS_NOT_CHARGING = 0, + CS_PRE_CHARGE = 1, + CS_CHARGE = 2, + CS_CHARGE_DONE = 3 + }; + NessoBattery(){}; - void begin(){}; // setup and check power management chip + void begin(); // setup and check power management chip void enableCharge(); // enable charging via power management chip float getVoltage(); // get battery voltage in Volts uint16_t getMilliVoltage(); // get battery voltage in millivolts uint16_t getChargeLevel(); // get battery charge level in percents + ChargeStatus getChargeStatus(); + bool isCharging(); }; extern ExpanderPin LORA_LNA_ENABLE;