diff --git a/variants/arduino_nesso_n1/ArduinoNessoN1Board.cpp b/variants/arduino_nesso_n1/ArduinoNessoN1Board.cpp index 988cca6dc..777befb32 100644 --- a/variants/arduino_nesso_n1/ArduinoNessoN1Board.cpp +++ b/variants/arduino_nesso_n1/ArduinoNessoN1Board.cpp @@ -15,6 +15,7 @@ void ArduinoNessoN1Board::begin() { digitalWrite(P_LORA_TX_LED, HIGH); #endif + battery.begin(); battery.enableCharge(); MESH_DEBUG_PRINTLN("ArduinoNessoN1.begin(): set Nesso N1 pin modes and default states..."); diff --git a/variants/arduino_nesso_n1/expander.cpp b/variants/arduino_nesso_n1/expander.cpp index 9de8486a8..80e53b4c4 100644 --- a/variants/arduino_nesso_n1/expander.cpp +++ b/variants/arduino_nesso_n1/expander.cpp @@ -94,20 +94,64 @@ int digitalRead(ExpanderPin pin) { return readBitRegister(pin.address, 0xF, pin.pin); } -void NessoBattery::enableCharge() { +void NessoBattery::begin() { // AW32001E - address 0x49 - // set CEB bit low (charge enable) + // Spec: https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/docs/products/core/LLM630%20Computer%20Kit/AW32001E.pdf if (!wireInitialized) { Wire.begin(SDA, SCL); wireInitialized = true; } + uint8_t val = 0; + val = readRegister(AW32001_I2C_CHIP_ADDR, AW32001_REG_CHIP_ID); + // coarsely check if chip is actually the right chip + auto res = (val == AW32001_I2C_CHIP_ADDR); + if (res) { + val = readRegister(AW32001_I2C_CHIP_ADDR, AW32001_REG_CHR_TMR); +#ifdef MESH_DEBUG + // Debug output the WatchDog Timer (wdt) state + MESH_DEBUG_PRINTLN("NessoBattery.begin(): CHR_TMR full register; bits 5,6 are for WDT = %#02x", val); +#endif + // disable WatchDog Timer (wdt) + // take existing register value AND with 00011111 + val = val & 0x1f; + writeRegister(AW32001_I2C_CHIP_ADDR, AW32001_REG_CHR_TMR, val); + } +#ifdef MESH_DEBUG + else { + MESH_DEBUG_PRINTLN("NessoBattery.begin(): Register of chip ADDR = %u != I2C of chip %u", AW32001_REG_CHIP_ID, AW32001_I2C_CHIP_ADDR); + } +#endif + + // store if chip is initiated by whether it passed above checks and had watchdog disabled + _power_mgmt_init = res; +} + +void NessoBattery::enableCharge() { + // AW32001E - address 0x49 + // set CEB (charge enable) bit (3) low (0) in AW32001_REG_PWR_CFG (0x01) MESH_DEBUG_PRINTLN("NessoBattery::enableCharge()"); - 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()); - writeBitRegister(0x49, 0x1, 3, false); + if (_power_mgmt_init) { + MESH_DEBUG_PRINTLN("NessoBattery::enableCharge(): _power_mgmt_init = true"); + if (!wireInitialized) { + Wire.begin(SDA, SCL); + wireInitialized = true; + } + + 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(): 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()); + + writeBitRegister(AW32001_I2C_CHIP_ADDR, AW32001_REG_PWR_CFG, 3, false); + } +#ifdef MESH_DEBUG + else { + MESH_DEBUG_PRINTLN("NessoBattery::enableCharge(): _power_mgmt_init is false, won't enable charge"); + } +#endif } float NessoBattery::getVoltage() { diff --git a/variants/arduino_nesso_n1/pins_arduino.h b/variants/arduino_nesso_n1/pins_arduino.h index c675613f5..07390aadd 100644 --- a/variants/arduino_nesso_n1/pins_arduino.h +++ b/variants/arduino_nesso_n1/pins_arduino.h @@ -41,6 +41,19 @@ static const uint8_t SYS_IRQ = 3; static const uint8_t LCD_CS = 17; 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 constexpr const uint8_t AW32001_I2C_CHIP_ADDR = 0x49; // ChipID + + + #if !defined(MAIN_ESP32_HAL_GPIO_H_) && defined(__cplusplus) /* address: 0x43/0x44 */ class ExpanderPin { @@ -51,9 +64,12 @@ public: }; class NessoBattery { +private: + bool _power_mgmt_init = false; public: NessoBattery(){}; - void enableCharge(); // enable charging + 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