Skip to content

Commit

Permalink
Add Power Manage Function
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyu-Zhao committed Aug 24, 2023
1 parent e1553fa commit fdbb037
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
53 changes: 38 additions & 15 deletions src/AXP2101.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,38 +295,61 @@ void AXP2101::coreS3_AW9523_init() {
write1Byte(0x58, 0x02, 0b00000100);
}

void AXP2101::setBoostEn(bool state) {
uint8_t value = read8Bit(AW9523_ADDR, 0x03);
void AXP2101::setBusOutEn(bool state) { // BUS OUT EN
uint8_t value = read8Bit(AW9523_ADDR, 0x02);
if (state == true)
value |= 0b10000000;
value |= 0b00000010;
else
value &= ~0b10000000;
write1Byte(AW9523_ADDR, 0x03, value);
value &= ~0b00000010;
write1Byte(AW9523_ADDR, 0x02, value);
}

void AXP2101::setBusOutEn(bool state) {
void AXP2101::setUsbOtgEn(bool state) { // USB OTG EN
uint8_t value = read8Bit(AW9523_ADDR, 0x02);
if (state == true)
value |= 0b00000010;
value |= 0b00100000;
else
value &= ~0b00000010;
value &= ~0b00100000;
write1Byte(AW9523_ADDR, 0x02, value);
}

void AXP2101::setBoostEn(bool state) { // Boost
uint8_t value = read8Bit(AW9523_ADDR, 0x03);
if (state == true)
value |= 0b10000000;
else
value &= ~0b10000000;
write1Byte(AW9523_ADDR, 0x03, value);
}

void AXP2101::setBoostBusOutEn(bool state) {
setBoostEn(state);
// delay is required to prevent reverse current flow from VBUS to BUS_OUT
if (state == false) delay(250);
setBusOutEn(state);
}

void AXP2101::setUsbOtgEn(bool state) {
uint8_t value = read8Bit(AW9523_ADDR, 0x02);
if (state == true)
value |= 0b00100000;
else
value &= ~0b00100000;
write1Byte(AW9523_ADDR, 0x02, value);
void AXP2101::powerModeSet(power_mode mode) {
switch (mode) {
case POWER_MODE_USB_IN_BUS_IN:
setUsbOtgEn(false); // USB_OTG_EN=0
setBoostBusOutEn(false); // BUS_OUT_EN,Boost=0
break;
case POWER_MODE_USB_IN_BUS_OUT:
setUsbOtgEn(false); // USB_OTG_EN=0
setBoostBusOutEn(true); // BUS_OUT_EN,Boost=1
break;
case POWER_MODE_USB_OUT_BUS_IN:
setUsbOtgEn(true); // USB_OTG_EN=1
setBoostBusOutEn(false); // BUS_OUT_EN,Boost=0
break;
case POWER_MODE_USB_OUT_BUS_OUT:
setUsbOtgEn(true); // USB_OTG_EN=1
setBoostBusOutEn(true); // BUS_OUT_EN,Boost=1
break;
default:
break;
}
}

/**
Expand Down
20 changes: 14 additions & 6 deletions src/AXP2101.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
#include "utility/I2C_PORT.h"

/* AXP173 device address */
#define AXP2101_ADDR 0x34
#define AW9523_ADDR 0x58
#define AXP173_ADDR_READ 0x69
#define AXP173_ADDR_WRITE 0x68
#define AXP2101_ADDR 0x34
#define AW9523_ADDR 0x58

enum power_mode {
POWER_MODE_USB_IN_BUS_IN = 0,
POWER_MODE_USB_IN_BUS_OUT,
POWER_MODE_USB_OUT_BUS_IN,
POWER_MODE_USB_OUT_BUS_OUT,
};

class AXP2101 : public I2C_PORT {
private:
Expand Down Expand Up @@ -52,6 +57,7 @@ class AXP2101 : public I2C_PORT {
COULOMETER_PAUSE,
COULOMETER_ENABLE,
};

/* Init */
bool begin(TwoWire* wire = &Wire1);
/* Power input state */
Expand Down Expand Up @@ -97,10 +103,12 @@ class AXP2101 : public I2C_PORT {
void coreS3_init();
void coreS3_AW9523_init();

void setBoostEn(bool state);
// Power Manage
void setBusOutEn(bool state);
void setBoostBusOutEn(bool state);
void setUsbOtgEn(bool state);
void setBoostEn(bool state);
void setBoostBusOutEn(bool state);
void powerModeSet(power_mode mode);

void coreS3_VBUS_boost(bool state);
bool isSDCardExist();
Expand Down

0 comments on commit fdbb037

Please sign in to comment.