From d16bccca2e73d49a7280ffd70ec019dc26a1b8af Mon Sep 17 00:00:00 2001 From: lewis he Date: Wed, 20 May 2020 11:01:27 +0800 Subject: [PATCH] Update AXP202 Library ,Add ChargeCurrent example --- .../AXP20x_ChargeCurrent.ino | 37 +++++ src/TTGO.h | 2 + src/drive/axp/axp20x.cpp | 131 +++++++++++++++++- src/drive/axp/axp20x.h | 30 +++- 4 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 examples/Basic unit/AXP20x_ChargeCurrent/AXP20x_ChargeCurrent.ino diff --git a/examples/Basic unit/AXP20x_ChargeCurrent/AXP20x_ChargeCurrent.ino b/examples/Basic unit/AXP20x_ChargeCurrent/AXP20x_ChargeCurrent.ino new file mode 100644 index 0000000..239bef1 --- /dev/null +++ b/examples/Basic unit/AXP20x_ChargeCurrent/AXP20x_ChargeCurrent.ino @@ -0,0 +1,37 @@ + +#include + +TTGOClass *ttgo; +TFT_eSPI *tft; +void setup() +{ + Serial.begin(115200); + ttgo = TTGOClass::getWatch(); + ttgo->begin(); + ttgo->openBL(); + tft = ttgo->eTFT; + + tft->fillScreen(TFT_BLACK); + tft->drawString("T-Watch AXP202", 0, 50, 2); + tft->setTextFont(2); + tft->setTextColor(TFT_WHITE, TFT_BLACK); + + + int cur = ttgo->power->getChargeControlCur(); + tft->setCursor(0, 100); + tft->print("Default Current: "); tft->print(cur); tft->println(" mA"); + + tft->setCursor(0, 130); + //axp202 allows maximum charging current of 1800mA, minimum 300mA + tft->print("Set charge control current 500 mA"); + ttgo->power->setChargeControlCur(500); + + tft->setCursor(0, 160); + cur = ttgo->power->getChargeControlCur(); + tft->print("Setting Current: "); tft->print(cur); tft->println(" mA"); + +} + +void loop() +{ +} diff --git a/src/TTGO.h b/src/TTGO.h index aa9d04e..30f0689 100644 --- a/src/TTGO.h +++ b/src/TTGO.h @@ -51,6 +51,8 @@ public: power->setChgLEDMode(AXP20X_LED_OFF); // Turn off external enable power->setPowerOutPut(AXP202_EXTEN, false); + //axp202 allows maximum charging current of 1800mA, minimum 300mA + power->setChargeControlCur(300); } if (touchScreen) { Wire1.begin(I2C_SDA, I2C_SCL); diff --git a/src/drive/axp/axp20x.cpp b/src/drive/axp/axp20x.cpp index 215241f..0adf4fa 100644 --- a/src/drive/axp/axp20x.cpp +++ b/src/drive/axp/axp20x.cpp @@ -59,6 +59,8 @@ const uint8_t AXP20X_Class::targetVolParams[] = { 0b01100000 }; + + // Power Output Control register uint8_t AXP20X_Class::_outputReg; @@ -362,7 +364,7 @@ uint32_t AXP20X_Class::getBattChargeCoulomb() uint8_t buffer[4]; if (!_init) return AXP_NOT_INIT; - _readByte(0xB1, 4, buffer); + _readByte(0xB0, 4, buffer); return (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3]; } @@ -385,6 +387,77 @@ float AXP20X_Class::getCoulombData() return result; } + +//------------------------------------------------------- +// New Coulomb functions by MrFlexi +//------------------------------------------------------- + +uint8_t AXP20X_Class::getCoulombRegister() +{ + uint8_t buffer; + if (!_init) + return AXP_NOT_INIT; + _readByte(AXP202_COULOMB_CTL, 1, &buffer); + return buffer; +} + + +int AXP20X_Class::setCoulombRegister(uint8_t val) +{ + if (!_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; + _writeByte(AXP202_COULOMB_CTL, 1, &val); + return AXP_PASS; +} + +int AXP20X_Class::DisableCoulombcounter(void) +{ + + if (!_init) + return AXP_NOT_INIT; + uint8_t val = 0x00; + _writeByte(AXP202_COULOMB_CTL, 1, &val); + return AXP_PASS; +} + +int AXP20X_Class::StopCoulombcounter(void) +{ + + if (!_init) + return AXP_NOT_INIT; + uint8_t val = 0xB8; + _writeByte(AXP202_COULOMB_CTL, 1, &val); + return AXP_PASS; +} + + +int AXP20X_Class::ClearCoulombcounter(void) +{ + + if (!_init) + return AXP_NOT_INIT; + uint8_t val = 0xA0; + _writeByte(AXP202_COULOMB_CTL, 1, &val); + return AXP_PASS; +} + +//------------------------------------------------------- +// END +//------------------------------------------------------- + + + uint8_t AXP20X_Class::getAdcSamplingRate() { //axp192 same axp202 aregister address 0x84 @@ -1053,6 +1126,8 @@ int AXP20X_Class::getBattPercentage() { if (!_init) return AXP_NOT_INIT; + if (_chip_id != AXP202_CHIP_ID) + return AXP_NOT_SUPPORT; uint8_t val; if (!isBatteryConnect()) return 0; @@ -1669,3 +1744,57 @@ int AXP20X_Class::gpioRead(axp_gpio_t gpio) } return AXP_NOT_SUPPORT; } + + + +int AXP20X_Class::getChargeControlCur() +{ + int cur; + uint8_t val; + if (!_init) + return AXP_NOT_INIT; + switch (_chip_id) { + case AXP202_CHIP_ID: + _readByte(AXP202_CHARGE1, 1, &val); + val &= 0x0F; + cur = val * 100 + 300; + if (cur > 1800 || cur < 300)return 0; + return cur; + case AXP192_CHIP_ID: + case AXP173_CHIP_ID: + _readByte(AXP202_CHARGE1, 1, &val); + return val & 0x0F; + default: + break; + } + return AXP_NOT_SUPPORT; +} + +int AXP20X_Class::setChargeControlCur(uint16_t mA) +{ + uint8_t val; + if (!_init) + return AXP_NOT_INIT; + switch (_chip_id) { + case AXP202_CHIP_ID: + _readByte(AXP202_CHARGE1, 1, &val); + val &= 0b11110000; + mA -= 300; + val |= (mA / 100); + _writeByte(AXP202_CHARGE1, 1, &val); + return AXP_PASS; + case AXP192_CHIP_ID: + case AXP173_CHIP_ID: + _readByte(AXP202_CHARGE1, 1, &val); + val &= 0b11110000; + if(mA > AXP1XX_CHARGE_CUR_1320MA) + mA = AXP1XX_CHARGE_CUR_1320MA; + val |= mA; + _writeByte(AXP202_CHARGE1, 1, &val); + return AXP_PASS; + default: + break; + } + return AXP_NOT_SUPPORT; +} + diff --git a/src/drive/axp/axp20x.h b/src/drive/axp/axp20x.h index 6e5243e..f2ca85a 100644 --- a/src/drive/axp/axp20x.h +++ b/src/drive/axp/axp20x.h @@ -542,7 +542,24 @@ typedef enum { AXP192_GPIO_3V3, } axp192_gpio_voltage_t; - +typedef enum { + AXP1XX_CHARGE_CUR_100MA, + AXP1XX_CHARGE_CUR_190MA, + AXP1XX_CHARGE_CUR_280MA, + AXP1XX_CHARGE_CUR_360MA, + AXP1XX_CHARGE_CUR_450MA, + AXP1XX_CHARGE_CUR_550MA, + AXP1XX_CHARGE_CUR_630MA, + AXP1XX_CHARGE_CUR_700MA, + AXP1XX_CHARGE_CUR_780MA, + AXP1XX_CHARGE_CUR_880MA, + AXP1XX_CHARGE_CUR_960MA, + AXP1XX_CHARGE_CUR_1000MA, + AXP1XX_CHARGE_CUR_1080MA, + AXP1XX_CHARGE_CUR_1160MA, + AXP1XX_CHARGE_CUR_1240MA, + AXP1XX_CHARGE_CUR_1320MA, +} axp1xx_charge_current_t; typedef uint8_t (*axp_com_fptr_t)(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint8_t len); @@ -709,6 +726,12 @@ public: int setAdcSamplingRate(axp_adc_sampling_rate_t rate); uint8_t getAdcSamplingRate(); float getCoulombData(); + uint8_t getCoulombRegister(); + int setCoulombRegister(uint8_t val); + int EnableCoulombcounter(void); + int DisableCoulombcounter(void); + int StopCoulombcounter(void); + int ClearCoulombcounter(void); int setGPIOMode(axp_gpio_t gpio, axp_gpio_mode_t mode); @@ -718,6 +741,11 @@ public: int gpioWrite(axp_gpio_t gpio, uint8_t vol); int gpioRead(axp_gpio_t gpio); + // When the chip is axp192 / 173, the allowed values are 0 ~ 15, corresponding to the axp1xx_charge_current_t enumeration + // When the chip is axp202 allows maximum charging current of 1800mA, minimum 300mA + int getChargeControlCur(); + int setChargeControlCur(uint16_t mA); + private: uint16_t _getRegistH8L5(uint8_t regh8, uint8_t regl5) {