Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
Added setSleep method , sleep mode example
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis he committed Jun 8, 2020
1 parent 5d98fe2 commit c8d7cbe
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 23 deletions.
115 changes: 115 additions & 0 deletions examples/axp_sleep_mode/axp_sleep_mode.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include <Wire.h>
#include <axp20x.h>

AXP20X_Class axp;

const uint8_t i2c_sda = 21;
const uint8_t i2c_scl = 22;
const uint8_t irq_pin = 35;

bool pmu_irq = false;


void sleepPMU()
{
int ret;
// PEK or GPIO edge wake-up function enable setting in Sleep mode
do {
// In order to ensure that it is set correctly,
// the loop waits for it to return the correct return value
Serial.println("Set PMU in sleep mode");
ret = axp.setSleep();
delay(500);
} while (ret != AXP_PASS) ;

// Turn off all power channels, only use PEK or AXP GPIO to wake up

// After setting AXP202/AXP192 to sleep,
// it will start to record the status of the power channel that was turned off after setting,
// it will restore the previously set state after PEK button or GPIO wake up


// Turn off all AXP192 power channels
ret = axp.setPowerOutPut(AXP192_LDO2, AXP202_OFF);
Serial.printf("Set Power AXP192_LDO2:%s\n", ret == AXP_PASS ? "OK" : "FAIL");

ret = axp.setPowerOutPut(AXP192_LDO3, AXP202_OFF);
Serial.printf("Set Power AXP192_LDO3:%s\n", ret == AXP_PASS ? "OK" : "FAIL");

ret = axp.setPowerOutPut(AXP192_DCDC1, AXP202_OFF);
Serial.printf("Set Power AXP192_DCDC1:%s\n", ret == AXP_PASS ? "OK" : "FAIL");

ret = axp.setPowerOutPut(AXP192_DCDC2, AXP202_OFF);
Serial.printf("Set Power AXP192_DCDC2:%s\n", ret == AXP_PASS ? "OK" : "FAIL");

ret = axp.setPowerOutPut(AXP192_EXTEN, AXP202_OFF);
Serial.printf("Set Power AXP192_EXTEN:%s\n", ret == AXP_PASS ? "OK" : "FAIL");

Serial.flush();
delay(1000);

// Tbeam v1.0 uses DC3 as the MCU power channel, turning it off as the last
ret = axp.setPowerOutPut(AXP192_DCDC3, AXP202_OFF);
Serial.printf("Set Power AXP192_DCDC3:%s\n", ret == AXP_PASS ? "OK" : "FAIL");

// Turn off all AXP202 power channels
// axp.setPowerOutPut(AXP202_LDO2, AXP202_OFF);
// axp.setPowerOutPut(AXP202_LDO3, AXP202_OFF);
// axp.setPowerOutPut(AXP202_LDO4, AXP202_OFF);
// axp.setPowerOutPut(AXP202_DCDC2, AXP202_OFF);
// axp.setPowerOutPut(AXP202_DCDC3, AXP202_OFF);
// axp.setPowerOutPut(AXP202_EXTEN, AXP202_OFF);


// If you set the power supply to sleep mode and you turn off the power supply of the MCU,
// you will not be able to use the wake-up mode provided by the MCU.
// If you do not turn off the power of the MCU, you can continue to use it
}

void setup()
{
Serial.begin(115200);

delay(3000);

Wire.begin(i2c_sda, i2c_scl);


// Test with AXP192 ,TBeam v1.0 board
int ret = axp.begin(Wire, AXP192_SLAVE_ADDRESS);
if (ret == AXP_FAIL) {
Serial.println("AXP Power begin failed");
while (1);
}

// Register the PMU interrupt pin, it will be triggered on the falling edge
pinMode(irq_pin, INPUT);
attachInterrupt(irq_pin, [] {
pmu_irq = true;
}, FALLING);

// Before using IRQ, remember to clear the IRQ status register
axp.clearIRQ();

// Turn on the key to press the interrupt function
axp.enableIRQ(AXP202_PEK_SHORTPRESS_IRQ, true);


Serial.println("Wait for the PEK button to be pressed");
}

void loop()
{
if ( pmu_irq) {
pmu_irq = false;
axp.readIRQ();
// When the PEK button is pressed briefly, the PMU is set to sleep
if (axp.isPEKShortPressIRQ()) {
// Clear all interrupt status before going to sleep
axp.clearIRQ();
// Set PMU to sleep
sleepPMU();
}
}
}

60 changes: 37 additions & 23 deletions src/axp20x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,55 +405,55 @@ uint8_t AXP20X_Class::getCoulombRegister()
int AXP20X_Class::setCoulombRegister(uint8_t val)
{
if (!_init)
return AXP_NOT_INIT;
return AXP_NOT_INIT;
_writeByte(AXP202_COULOMB_CTL, 1, &val);
return AXP_PASS;
}


int AXP20X_Class::EnableCoulombcounter(void)
{
if (!_init)
return AXP_NOT_INIT;
uint8_t val = 0x80;

if (!_init)
return AXP_NOT_INIT;
uint8_t val = 0x80;
_writeByte(AXP202_COULOMB_CTL, 1, &val);
return AXP_PASS;
return AXP_PASS;
}

int AXP20X_Class::DisableCoulombcounter(void)
{
if (!_init)
return AXP_NOT_INIT;
uint8_t val = 0x00;

if (!_init)
return AXP_NOT_INIT;
uint8_t val = 0x00;
_writeByte(AXP202_COULOMB_CTL, 1, &val);
return AXP_PASS;
return AXP_PASS;
}

int AXP20X_Class::StopCoulombcounter(void)
{
if (!_init)
return AXP_NOT_INIT;
uint8_t val = 0xB8;

if (!_init)
return AXP_NOT_INIT;
uint8_t val = 0xB8;
_writeByte(AXP202_COULOMB_CTL, 1, &val);
return AXP_PASS;
return AXP_PASS;
}


int AXP20X_Class::ClearCoulombcounter(void)
{
if (!_init)
return AXP_NOT_INIT;
uint8_t val = 0xA0;

if (!_init)
return AXP_NOT_INIT;
uint8_t val = 0xA0;
_writeByte(AXP202_COULOMB_CTL, 1, &val);
return AXP_PASS;
return AXP_PASS;
}

//-------------------------------------------------------
// END
// END
//-------------------------------------------------------


Expand Down Expand Up @@ -1787,7 +1787,7 @@ int AXP20X_Class::setChargeControlCur(uint16_t mA)
case AXP173_CHIP_ID:
_readByte(AXP202_CHARGE1, 1, &val);
val &= 0b11110000;
if(mA > AXP1XX_CHARGE_CUR_1320MA)
if (mA > AXP1XX_CHARGE_CUR_1320MA)
mA = AXP1XX_CHARGE_CUR_1320MA;
val |= mA;
_writeByte(AXP202_CHARGE1, 1, &val);
Expand All @@ -1798,3 +1798,17 @@ int AXP20X_Class::setChargeControlCur(uint16_t mA)
return AXP_NOT_SUPPORT;
}

int AXP20X_Class::setSleep()
{
int ret;
uint8_t val = 0;
ret = _readByte(AXP202_VOFF_SET, 1, &val);
if (ret != 0)return AXP_FAIL;
val |= _BV(3);
ret = _writeByte(AXP202_VOFF_SET, 1, &val);
if (ret != 0)return AXP_FAIL;
ret = _readByte(AXP202_VOFF_SET, 1, &val);
if (ret != 0)return AXP_FAIL;

return val == 0X0B ? AXP_PASS : AXP_FAIL;
}
3 changes: 3 additions & 0 deletions src/axp20x.h
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,9 @@ class AXP20X_Class
int getChargeControlCur();
int setChargeControlCur(uint16_t mA);


int setSleep();

private:
uint16_t _getRegistH8L5(uint8_t regh8, uint8_t regl5)
{
Expand Down

0 comments on commit c8d7cbe

Please sign in to comment.