From 0ff4465ef40c6057fa5d61888d5312bb938a9e95 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Tue, 4 Jun 2024 16:36:49 +0800 Subject: [PATCH] Added BQ25896 examples --- examples/BQ25896_Example/BQ25896_Example.ino | 176 ++++++++++++++++++ .../BQ25896_Shutdown_Example.ino | 70 +++++++ platformio.ini | 10 +- 3 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 examples/BQ25896_Example/BQ25896_Example.ino create mode 100644 examples/BQ25896_Shutdown_Example/BQ25896_Shutdown_Example.ino diff --git a/examples/BQ25896_Example/BQ25896_Example.ino b/examples/BQ25896_Example/BQ25896_Example.ino new file mode 100644 index 0000000..0d70f0a --- /dev/null +++ b/examples/BQ25896_Example/BQ25896_Example.ino @@ -0,0 +1,176 @@ +/** + * @file BQ25896_Example.ino + * @author Lewis He (lewishe@outlook.com) + * @license MIT + * @copyright Copyright (c) 2024 Shenzhen Xin Yuan Electronic Technology Co., Ltd + * @date 2024-06-04 + * + */ +#include + +XPowersPPM PPM; + + +#ifndef CONFIG_PMU_SDA +#define CONFIG_PMU_SDA 0 +#endif + +#ifndef CONFIG_PMU_SCL +#define CONFIG_PMU_SCL 1 +#endif + +#ifndef CONFIG_PMU_IRQ +#define CONFIG_PMU_IRQ 28 +#endif + +const uint8_t i2c_sda = CONFIG_PMU_SDA; +const uint8_t i2c_scl = CONFIG_PMU_SCL; +const uint8_t pmu_irq_pin = CONFIG_PMU_IRQ; +uint32_t cycleInterval; +bool pmu_irq = false; + +void setup() +{ + Serial.begin(115200); + while (!Serial); + + + bool result = PPM.init(Wire, i2c_sda, i2c_scl, BQ25896_SLAVE_ADDRESS); + + if (result == false) { + while (1) { + Serial.println("PPM is not online..."); + delay(50); + } + } + + // Set the minimum operating voltage. Below this voltage, the PPM will protect + PPM.setSysPowerDownVoltage(3300); + + // Set input current limit, default is 500mA + PPM.setInputCurrentLimit(3250); + + Serial.printf("getInputCurrentLimit: %d mA\n",PPM.getInputCurrentLimit()); + + // Disable current limit pin + PPM.disableCurrentLimitPin(); + + // Set the charging target voltage, Range:3840 ~ 4608mV ,step:16 mV + PPM.setChargeTargetVoltage(4208); + + // Set the precharge current , Range: 64mA ~ 1024mA ,step:64mA + PPM.setPrechargeCurr(64); + + // The premise is that Limit Pin is disabled, or it will only follow the maximum charging current set by Limi tPin. + // Set the charging current , Range:0~5056mA ,step:64mA + PPM.setChargerConstantCurr(832); + + // Get the set charging current + PPM.getChargerConstantCurr(); + Serial.printf("getChargerConstantCurr: %d mA\n",PPM.getChargerConstantCurr()); + + + // To obtain voltage data, the ADC must be enabled first + PPM.enableADCMeasure(); + + // Turn on charging function + // If there is no battery connected, do not turn on the charging function + PPM.enableCharge(); + + // Turn off charging function + // If USB is used as the only power input, it is best to turn off the charging function, + // otherwise the VSYS power supply will have a sawtooth wave, affecting the discharge output capability. + // PPM.disableCharge(); + + + // The OTG function needs to enable OTG, and set the OTG control pin to HIGH + // After OTG is enabled, if an external power supply is plugged in, OTG will be turned off + + // PPM.enableOTG(); + // PPM.disableOTG(); + // pinMode(OTG_ENABLE_PIN, OUTPUT); + // digitalWrite(OTG_ENABLE_PIN, HIGH); + + + attachInterrupt(pmu_irq_pin, []() { + pmu_irq = true; + }, FALLING); + + delay(2000); +} + + +void loop() +{ + + if (pmu_irq) { + pmu_irq = false; + Serial.print("-> ["); + Serial.print(millis() / 1000); + Serial.print("] "); + + // Get PPM interrupt status + PPM.getIrqStatus(); + + + if (PPM.isWatchdogFault()) { + Serial.println("Watchdog Fault"); + } + if (PPM.isBoostFault()) { + Serial.println("Boost Fault"); + } + if (PPM.isChargeFault()) { + Serial.println("Charge Fault"); + } + if (PPM.isBatteryFault()) { + Serial.println("Batter Fault"); + } + if (PPM.isNTCFault()) { + Serial.print("NTC Fault:"); + Serial.print(PPM.getNTCStatusString()); + + Serial.print(" Percentage:"); + Serial.print(PPM.getNTCPercentage()); Serial.println("%"); + } + // The battery may be disconnected or damaged. + if (PPM.isVsysLowVoltageWarning()) { + Serial.println("In VSYSMIN regulation (BAT cycleInterval) { + + Serial.println("Sats VBUS VBAT SYS VbusStatus String ChargeStatus String TargetVoltage ChargeCurrent Precharge NTCStatus String"); + Serial.println(" (mV) (mV) (mV) (HEX) (HEX) (mV) (mA) (mA) (HEX) "); + Serial.println("--------------------------------------------------------------------------------------------------------------------------------"); + Serial.print(PPM.isVbusIn() ? "Connected" : "Disconnect"); Serial.print("\t"); + Serial.print(PPM.getVbusVoltage()); Serial.print("\t"); + Serial.print(PPM.getBattVoltage()); Serial.print("\t"); + Serial.print(PPM.getSystemVoltage()); Serial.print("\t"); + Serial.print("0x"); + Serial.print(PPM.getBusStatus(), HEX); Serial.print("\t"); + Serial.print(PPM.getBusStatusString()); Serial.print("\t"); + Serial.print("0x"); + Serial.print(PPM.chargeStatus(), HEX); Serial.print("\t"); + Serial.print(PPM.getChargeStatusString()); Serial.print("\t"); + + Serial.print(PPM.getChargeTargetVoltage()); Serial.print("\t"); + Serial.print(PPM.getChargeCurrent()); Serial.print("\t"); + Serial.print(PPM.getPrechargeCurr()); Serial.print("\t"); + Serial.print(PPM.getNTCStatus()); Serial.print("\t"); + Serial.print(PPM.getNTCStatusString()); Serial.print("\t"); + + + Serial.println(); + Serial.println(); + cycleInterval = millis() + 1000; + } + +} + + + + + diff --git a/examples/BQ25896_Shutdown_Example/BQ25896_Shutdown_Example.ino b/examples/BQ25896_Shutdown_Example/BQ25896_Shutdown_Example.ino new file mode 100644 index 0000000..5224a90 --- /dev/null +++ b/examples/BQ25896_Shutdown_Example/BQ25896_Shutdown_Example.ino @@ -0,0 +1,70 @@ +/** + * @file BQ25896_Shutdown_Example.ino + * @author Lewis He (lewishe@outlook.com) + * @license MIT + * @copyright Copyright (c) 2024 Shenzhen Xin Yuan Electronic Technology Co., Ltd + * @date 2024-06-04 + * + */ +#include + +XPowersPPM PPM; + + +#ifndef CONFIG_PMU_SDA +#define CONFIG_PMU_SDA 0 +#endif + +#ifndef CONFIG_PMU_SCL +#define CONFIG_PMU_SCL 1 +#endif + +#ifndef CONFIG_PMU_IRQ +#define CONFIG_PMU_IRQ 28 +#endif + +const uint8_t i2c_sda = CONFIG_PMU_SDA; +const uint8_t i2c_scl = CONFIG_PMU_SCL; +const uint8_t pmu_irq_pin = CONFIG_PMU_IRQ; +uint32_t cycleInterval; +uint32_t countdown = 10; + + +void setup() +{ + Serial.begin(115200); + while (!Serial); + + bool result = PPM.init(Wire, i2c_sda, i2c_scl, BQ25896_SLAVE_ADDRESS); + if (result == false) { + while (1) { + Serial.println("PPM is not online..."); + delay(1000); + } + } + +} + + +void loop() +{ + if (millis() > cycleInterval) { + Serial.printf("%d\n", countdown); + if (!(countdown--)) { + Serial.println("Shutdown ....."); + // The shutdown function can only be used when the battery is connected alone, + // and cannot be shut down when connected to USB. + // It can only be powered on in the following two ways: + // 1. Press the PPM/QON button + // 2. Connect to USB + PPM.shutdown(); + countdown = 10000; + } + cycleInterval = millis() + 1000; + } +} + + + + + diff --git a/platformio.ini b/platformio.ini index c19196c..4a6275d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,7 +12,7 @@ ; src_dir = examples/AXP192_Example ; src_dir = examples/AXP202_Example ; src_dir = examples/AXP2101_Charge_Example -src_dir = examples/AXP2101_Charge_Current_Setting +; src_dir = examples/AXP2101_Charge_Current_Setting ; src_dir = examples/AXP2101_Example ; src_dir = examples/AXP2101_InterruptExample ; src_dir = examples/AXP2101_Voltage_Example @@ -23,6 +23,9 @@ src_dir = examples/AXP2101_Charge_Current_Setting ; src_dir = examples/SY6970_Watchdog_Example ; src_dir = examples/SY6970_Shutdown_Example +; src_dir = examples/BQ25896_Example +src_dir = examples/BQ25896_Shutdown_Example + default_envs=esp32s3 ; default_envs=esp32dev ; default_envs=nucleo_f411re @@ -38,6 +41,11 @@ build_flags = ; -DCONFIG_PMU_SDA=1 ; -DCONFIG_PMU_SCL=2 + ; T-EPD47 S3 + -DCONFIG_PMU_SDA=6 + -DCONFIG_PMU_SCL=5 + -DCONFIG_PMU_IRQ=15 + ; -DCONFIG_PMU_SDA=3 ; -DCONFIG_PMU_SCL=2 ; -DCONFIG_PMU_IRQ=15