Skip to content

Commit

Permalink
Update XPowersLib
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Jun 1, 2024
1 parent 6786ce7 commit 07ca739
Show file tree
Hide file tree
Showing 10 changed files with 566 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/XPowersLib/.piopm
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type": "library", "name": "XPowersLib", "version": "0.2.3", "spec": {"owner": "lewisxhe", "id": 14299, "name": "XPowersLib", "requirements": null, "uri": null}}
{"type": "library", "name": "XPowersLib", "version": "0.2.4", "spec": {"owner": "lewisxhe", "id": 14299, "name": "XPowersLib", "requirements": null, "uri": null}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
MIT License
Copyright (c) 2024 lewis he
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/*
! WARN:
Please do not run the example without knowing the external load voltage of the PMU,
it may burn your external load, please check the voltage setting before running the example,
if there is any loss, please bear it by yourself
*/
#ifndef XPOWERS_NO_ERROR
#error "Running this example is known to not damage the device! Please go and uncomment this!"
#endif
// Defined using AXP2102
#define XPOWERS_CHIP_AXP2101

#include <Wire.h>
#include <Arduino.h>
#include "XPowersLib.h"

#ifndef CONFIG_PMU_SDA
#define CONFIG_PMU_SDA 21
#endif

#ifndef CONFIG_PMU_SCL
#define CONFIG_PMU_SCL 22
#endif

#ifndef CONFIG_PMU_IRQ
#define CONFIG_PMU_IRQ 35
#endif

XPowersPMU PMU;

bool pmu_flag = false;
uint8_t curIndex = 4;

void setFlag(void)
{
pmu_flag = true;
}


void printChargerConstantCurr()
{
const uint16_t currTable[] = {
0, 0, 0, 0, 100, 125, 150, 175, 200, 300, 400, 500, 600, 700, 800, 900, 1000
};
uint8_t val = PMU.getChargerConstantCurr();
Serial.printf("Setting Charge Target Current - VAL:%u CURRENT:%u\n", val, currTable[val]);
}

void setup()
{

Serial.begin(115200);

//Start while waiting for Serial monitoring
while (!Serial);

delay(3000);

Serial.println();

bool res = PMU.begin(Wire, AXP2101_SLAVE_ADDRESS, CONFIG_PMU_SDA, CONFIG_PMU_SCL);
if (!res) {
Serial.println("Failed to initialize power.....");
while (1) {
delay(5000);
}
}

// Set PMU interrupt
pinMode(CONFIG_PMU_IRQ, INPUT);
attachInterrupt(CONFIG_PMU_IRQ, setFlag, FALLING);

// Disable all interrupts
PMU.disableIRQ(XPOWERS_AXP2101_ALL_IRQ);

// Enable the required interrupt function
PMU.enableIRQ(
XPOWERS_AXP2101_BAT_INSERT_IRQ | XPOWERS_AXP2101_BAT_REMOVE_IRQ | //BATTERY
XPOWERS_AXP2101_VBUS_INSERT_IRQ | XPOWERS_AXP2101_VBUS_REMOVE_IRQ | //VBUS
XPOWERS_AXP2101_PKEY_SHORT_IRQ | XPOWERS_AXP2101_PKEY_LONG_IRQ | //POWER KEY
XPOWERS_AXP2101_BAT_CHG_DONE_IRQ | XPOWERS_AXP2101_BAT_CHG_START_IRQ //CHARGE
);
// Clear all interrupt flags
PMU.clearIrqStatus();

// Set the minimum common working voltage of the PMU VBUS input,
// below this value will turn off the PMU
PMU.setVbusVoltageLimit(XPOWERS_AXP2101_VBUS_VOL_LIM_4V36);
// Set the maximum current of the PMU VBUS input,
// higher than this value will turn off the PMU
PMU.setVbusCurrentLimit(XPOWERS_AXP2101_VBUS_CUR_LIM_1500MA);
// Set VSY off voltage as 2600mV , Adjustment range 2600mV ~ 3300mV
PMU.setSysPowerDownVoltage(2600);
// Set the precharge charging current
PMU.setPrechargeCurr(XPOWERS_AXP2101_PRECHARGE_50MA);
// Set constant current charge current limit
PMU.setChargerConstantCurr(XPOWERS_AXP2101_CHG_CUR_200MA);
// Set stop charging termination current
PMU.setChargerTerminationCurr(XPOWERS_AXP2101_CHG_ITERM_25MA);
// Set charge cut-off voltage
PMU.setChargeTargetVoltage(XPOWERS_AXP2101_CHG_VOL_4V1);

/*
The default setting is CHGLED is automatically controlled by the PMU.
- XPOWERS_CHG_LED_OFF,
- XPOWERS_CHG_LED_BLINK_1HZ,
- XPOWERS_CHG_LED_BLINK_4HZ,
- XPOWERS_CHG_LED_ON,
- XPOWERS_CHG_LED_CTRL_CHG,
* */
PMU.setChargingLedMode(XPOWERS_CHG_LED_OFF);
// Set cell battery voltage to 3.3V
PMU.setButtonBatteryChargeVoltage(3300);
// Enable cell battery charge function
PMU.enableButtonBatteryCharge();
// Print default setting current
printChargerConstantCurr();
}

void loop()
{
if (pmu_flag) {
pmu_flag = false;
// Get PMU Interrupt Status Register
uint32_t status = PMU.getIrqStatus();
if (PMU.isPekeyShortPressIrq()) {

// Set constant current charge current limit
// For setting a current greater than 500mA, the VBUS power supply must be sufficient. If the input is lower than 5V, the charging current will be below 500mA.
if (!PMU.setChargerConstantCurr(curIndex)) {
Serial.println("Setting Charger Constant Current Failed!");
}
printChargerConstantCurr();
Serial.println("===========================");
curIndex++;
curIndex %= (XPOWERS_AXP2101_CHG_CUR_1000MA + 1);
if (curIndex == 0) {
curIndex = 3;
}

// Toggle CHG led
PMU.setChargingLedMode(PMU.getChargingLedMode() != XPOWERS_CHG_LED_OFF ? XPOWERS_CHG_LED_OFF : XPOWERS_CHG_LED_ON);
}
// Clear PMU Interrupt Status Register
PMU.clearIrqStatus();
}

delay(200);
}

78 changes: 74 additions & 4 deletions lib/XPowersLib/examples/SY6970_Example/SY6970_Example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @license MIT
* @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd
* @date 2023-08-05
* @note SY6970 If the power supply is a separate USB power supply, VSYS can only provide a maximum load current of 500mA.
* If it is connected to a battery, the discharge current depends on the maximum discharge current of the battery.
*
*/
#include <XPowersLib.h>
Expand All @@ -27,6 +29,7 @@ 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()
{
Expand All @@ -43,17 +46,43 @@ void setup()
}
}

// Set the minimum operating voltage. Below this voltage, the PMU will protect
PMU.setSysPowerDownVoltage(3300);

// Set input current limit, default is 500mA
PMU.setInputCurrentLimit(3250);

Serial.printf("getInputCurrentLimit: %d mA\n",PMU.getInputCurrentLimit());

// Disable current limit pin
PMU.disableCurrentLimitPin();

// Set the charging target voltage, Range:3840 ~ 4608mV ,step:16 mV
PMU.setChargeTargetVoltage(3856);
PMU.setChargeTargetVoltage(4208);

// Set the precharge current , Range: 64mA ~ 1024mA ,step:64mA
PMU.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
PMU.setChargerConstantCurr(320);
PMU.setChargerConstantCurr(832);

// Get the set charging current
PMU.getChargerConstantCurr();
Serial.printf("getChargerConstantCurr: %d mA\n",PMU.getChargerConstantCurr());


// To obtain voltage data, the ADC must be enabled first
PMU.enableADCMeasure();

// Turn on charging function
// If there is no battery connected, do not turn on the charging function
PMU.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.
// PMU.disableCharge();


// The OTG function needs to enable OTG, and set the OTG control pin to HIGH
Expand All @@ -64,13 +93,54 @@ void setup()
// 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 PMU interrupt status
PMU.getIrqStatus();


if (PMU.isWatchdogFault()) {
Serial.println("Watchdog Fault");
}
if (PMU.isBoostFault()) {
Serial.println("Boost Fault");
}
if (PMU.isChargeFault()) {
Serial.println("Charge Fault");
}
if (PMU.isBatteryFault()) {
Serial.println("Batter Fault");
}
if (PMU.isNTCFault()) {
Serial.print("NTC Fault:");
Serial.print(PMU.getNTCStatusString());

Serial.print(" Percentage:");
Serial.print(PMU.getNTCPercentage()); Serial.println("%");
}
// The battery may be disconnected or damaged.
if (PMU.isVsysLowVoltageWarning()) {
Serial.println("In VSYSMIN regulation (BAT<VSYSMIN)");
}

}

// SY6970 When VBUS is input, the battery voltage detection will not take effect
if (millis() > cycleInterval) {

Expand All @@ -89,7 +159,7 @@ void loop()
Serial.print(PMU.getChargeStatusString()); Serial.print("\t");

Serial.print(PMU.getChargeTargetVoltage()); Serial.print("\t");
Serial.print(PMU.getChargerConstantCurr()); Serial.print("\t");
Serial.print(PMU.getChargeCurrent()); Serial.print("\t");
Serial.print(PMU.getPrechargeCurr()); Serial.print("\t");
Serial.print(PMU.getNTCStatus()); Serial.print("\t");
Serial.print(PMU.getNTCStatusString()); Serial.print("\t");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @file SY6970_Shutdown_Example.ino
* @author Lewis He ([email protected])
* @license MIT
* @copyright Copyright (c) 2024 Shenzhen Xin Yuan Electronic Technology Co., Ltd
* @date 2024-05-23
*
*/
#include <XPowersLib.h>

PowersSY6970 PMU;


#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 = PMU.init(Wire, i2c_sda, i2c_scl, SY6970_SLAVE_ADDRESS);
if (result == false) {
while (1) {
Serial.println("PMU is not online...");
delay(50);
}
}

}


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 PMU/QON button
// 2. Connect to USB
PMU.shutdown();
countdown = 10000;
}
cycleInterval = millis() + 1000;
}
}





Loading

0 comments on commit 07ca739

Please sign in to comment.