From a2422790e6f7bf3a2188f0ecd171dbb71a055da9 Mon Sep 17 00:00:00 2001 From: Ton Huisman Date: Sun, 30 Oct 2022 22:32:37 +0100 Subject: [PATCH] [Lib] Pull AXP192 library local, and implement missing features, exclude from ESP8266 builds --- lib/I2C_AXP192/LICENSE | 121 +++++++ lib/I2C_AXP192/README.md | 71 ++++ lib/I2C_AXP192/examples/AXP192/AXP192.ino | 54 +++ lib/I2C_AXP192/keywords.txt | 45 +++ lib/I2C_AXP192/library.properties | 10 + lib/I2C_AXP192/src/I2C_AXP192.cpp | 388 ++++++++++++++++++++++ lib/I2C_AXP192/src/I2C_AXP192.h | 85 +++++ platformio_core_defs.ini | 1 + platformio_esp32_envs.ini | 2 +- platformio_esp82xx_base.ini | 1 + platformio_esp82xx_envs.ini | 3 + 11 files changed, 780 insertions(+), 1 deletion(-) create mode 100644 lib/I2C_AXP192/LICENSE create mode 100644 lib/I2C_AXP192/README.md create mode 100644 lib/I2C_AXP192/examples/AXP192/AXP192.ino create mode 100644 lib/I2C_AXP192/keywords.txt create mode 100644 lib/I2C_AXP192/library.properties create mode 100644 lib/I2C_AXP192/src/I2C_AXP192.cpp create mode 100644 lib/I2C_AXP192/src/I2C_AXP192.h diff --git a/lib/I2C_AXP192/LICENSE b/lib/I2C_AXP192/LICENSE new file mode 100644 index 000000000..0e259d42c --- /dev/null +++ b/lib/I2C_AXP192/LICENSE @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/lib/I2C_AXP192/README.md b/lib/I2C_AXP192/README.md new file mode 100644 index 000000000..37aa2bcf0 --- /dev/null +++ b/lib/I2C_AXP192/README.md @@ -0,0 +1,71 @@ +# I2C AXP192 Power Management Library + +This is a library of AXP192, the Power Management for I2C connectivity. + +## Support Devices + +- [M5Stack M5Stick](https://docs.m5stack.com/#/en/core/m5stickc) +- [M5Stack M5Stick Plus](https://docs.m5stack.com/#/en/core/m5stickc_plus) + +## On-board devices + +- [LilyGO-T-Beam](https://github.com/Xinyuan-LilyGO/LilyGO-T-Beam) +- [LilyGO T-PCIE](https://github.com/Xinyuan-LilyGO/LilyGo-T-PCIE) + +## Usage +```c +#include "I2C_AXP192.h" + +I2C_AXP192 axp192(I2C_AXP192_DEFAULT_ADDRESS, Wire1); + +const int btnPin = 37; + +void setup() { + Serial.begin(115200); + delay(1000); + + Wire1.begin(21, 22); + + I2C_AXP192_InitDef initDef = { + .EXTEN = true, + .BACKUP = true, + .DCDC1 = 3300, + .DCDC2 = 0, + .DCDC3 = 0, + .LDO2 = 3000, + .LDO3 = 3000, + .GPIO0 = 2800, + .GPIO1 = -1, + .GPIO2 = -1, + .GPIO3 = -1, + .GPIO4 = -1, + }; + axp192.begin(initDef); + + pinMode(btnPin, INPUT); +} + +void loop() { + Serial.printf("getBatteryVoltage() : %7.2f\n", axp192.getBatteryVoltage()); + Serial.printf("getBatteryDischargeCurrent() : %7.2f\n", axp192.getBatteryDischargeCurrent()); + Serial.printf("getBatteryChargeCurrent() : %7.2f\n", axp192.getBatteryChargeCurrent()); + Serial.printf("getAcinVolatge() : %7.2f\n", axp192.getAcinVolatge()); + Serial.printf("getAcinCurrent() : %7.2f\n", axp192.getAcinCurrent()); + Serial.printf("getVbusVoltage() : %7.2f\n", axp192.getVbusVoltage()); + Serial.printf("getVbusCurrent() : %7.2f\n", axp192.getVbusCurrent()); + Serial.printf("getInternalTemperature() : %7.2f\n", axp192.getInternalTemperature()); + Serial.printf("getApsVoltage() : %7.2f\n", axp192.getApsVoltage()); + Serial.printf("getPekPress() : %4d\n" , axp192.getPekPress()); + + Serial.println(); + + if (digitalRead(btnPin) == 0) { + Serial.printf("powerOff()\n"); + Serial.flush(); + delay(1000); + axp192.powerOff(); + } + + delay(1000); +} +``` diff --git a/lib/I2C_AXP192/examples/AXP192/AXP192.ino b/lib/I2C_AXP192/examples/AXP192/AXP192.ino new file mode 100644 index 000000000..8124e54be --- /dev/null +++ b/lib/I2C_AXP192/examples/AXP192/AXP192.ino @@ -0,0 +1,54 @@ +#include "I2C_AXP192.h" + +I2C_AXP192 axp192(I2C_AXP192_DEFAULT_ADDRESS, Wire1); + +const int btnPin = 37; + +void setup() { + Serial.begin(115200); + delay(1000); + + Wire1.begin(21, 22); + + I2C_AXP192_InitDef initDef = { + .EXTEN = true, + .BACKUP = true, + .DCDC1 = 3300, + .DCDC2 = 0, + .DCDC3 = 0, + .LDO2 = 3000, + .LDO3 = 3000, + .GPIO0 = 2800, + .GPIO1 = -1, + .GPIO2 = -1, + .GPIO3 = -1, + .GPIO4 = -1, + }; + axp192.begin(initDef); + + pinMode(btnPin, INPUT); +} + +void loop() { + Serial.printf("getBatteryVoltage() : %7.2f\n", axp192.getBatteryVoltage()); + Serial.printf("getBatteryDischargeCurrent() : %7.2f\n", axp192.getBatteryDischargeCurrent()); + Serial.printf("getBatteryChargeCurrent() : %7.2f\n", axp192.getBatteryChargeCurrent()); + Serial.printf("getAcinVolatge() : %7.2f\n", axp192.getAcinVolatge()); + Serial.printf("getAcinCurrent() : %7.2f\n", axp192.getAcinCurrent()); + Serial.printf("getVbusVoltage() : %7.2f\n", axp192.getVbusVoltage()); + Serial.printf("getVbusCurrent() : %7.2f\n", axp192.getVbusCurrent()); + Serial.printf("getInternalTemperature() : %7.2f\n", axp192.getInternalTemperature()); + Serial.printf("getApsVoltage() : %7.2f\n", axp192.getApsVoltage()); + Serial.printf("getPekPress() : %4d\n" , axp192.getPekPress()); + + Serial.println(); + + if (digitalRead(btnPin) == 0) { + Serial.printf("powerOff()\n"); + Serial.flush(); + delay(1000); + axp192.powerOff(); + } + + delay(1000); +} diff --git a/lib/I2C_AXP192/keywords.txt b/lib/I2C_AXP192/keywords.txt new file mode 100644 index 000000000..cb9df0b97 --- /dev/null +++ b/lib/I2C_AXP192/keywords.txt @@ -0,0 +1,45 @@ +####################################### +# Syntax Coloring Map +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +I2C_AXP192 KEYWORD1 +I2C_AXP192_InitDef KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +setDCDC1 KEYWORD2 +setDCDC2 KEYWORD2 +setDCDC3 KEYWORD2 +setLDO2 KEYWORD2 +setLDO3 KEYWORD2 +setGPIO0 KEYWORD2 +setGPIO1 KEYWORD2 +setGPIO2 KEYWORD2 +setGPIO3 KEYWORD2 +setGPIO4 KEYWORD2 +setEXTEN KEYWORD2 +setBACKUP KEYWORD2 + +getBatteryVoltage KEYWORD2 +getBatteryDischargeCurrent KEYWORD2 +getBatteryChargeCurrent KEYWORD2 +getAcinVolatge KEYWORD2 +getAcinCurrent KEYWORD2 +getVbusVoltage KEYWORD2 +getVbusCurrent KEYWORD2 +getInternalTemperature KEYWORD2 +getApsVoltage KEYWORD2 +powerOff KEYWORD2 +getPekPress KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/lib/I2C_AXP192/library.properties b/lib/I2C_AXP192/library.properties new file mode 100644 index 000000000..9786c6d01 --- /dev/null +++ b/lib/I2C_AXP192/library.properties @@ -0,0 +1,10 @@ +name=I2C AXP192 Power management +version=1.0.4 +author=TANAKA Masayuki +maintainer=TANAKA Masayuki +sentence=This is a library of AXP192, the Power Management for I2C connectivity. Additions for GPIO1..4 by tonhuisman. +paragraph=This is a library of AXP192, the Power Management for I2C connectivity. Additions for GPIO1..4 by tonhuisman. +category=Device Control +url=https://github.com/tanakamasayuki/I2C_AXP192 +architectures=* +includes=I2C_AXP192.h diff --git a/lib/I2C_AXP192/src/I2C_AXP192.cpp b/lib/I2C_AXP192/src/I2C_AXP192.cpp new file mode 100644 index 000000000..faac296d1 --- /dev/null +++ b/lib/I2C_AXP192/src/I2C_AXP192.cpp @@ -0,0 +1,388 @@ +#include "I2C_AXP192.h" + +I2C_AXP192::I2C_AXP192(uint8_t deviceAddress, TwoWire& i2cPort) { + _deviceAddress = deviceAddress; + _i2cPort = &i2cPort; +} + +void I2C_AXP192::begin(I2C_AXP192_InitDef initDef) { + ESP_LOGD("AXP192", "Begin"); + + setEXTEN(initDef.EXTEN); + setBACKUP(initDef.BACKUP); + + setDCDC1(initDef.DCDC1); + setDCDC2(initDef.DCDC2); + setDCDC3(initDef.DCDC3); + + setLDO2(initDef.LDO2); + setLDO3(initDef.LDO3); + + writeByte(0x90, 0b00000000); // GPIO0 open drain output + writeByte(0x92, 0b00000000); // GPIO1 open drain output + writeByte(0x93, 0b00000000); // GPIO2 open drain output + writeByte(0x95, 0b10000101); // GPIO3/4 open drain output + + setLDOIO(initDef.LDOIO); + setGPIO0(initDef.GPIO0); + setGPIO1(initDef.GPIO1); + setGPIO2(initDef.GPIO2); + setGPIO3(initDef.GPIO3); + setGPIO4(initDef.GPIO4); + + writeByte(0x84, 0b11110010); // ADC 200Hz + writeByte(0x82, 0b11111111); // ADC All Enable + writeByte(0x33, 0b11000000); // Charge 4.2V, 100mA + writeByte(0x36, 0b00001100); // PEK 128ms, PW OFF 4S + writeByte(0x30, 0b10000000); // VBUS Open + writeByte(0x39, 0b11111100); // Temp Protection + writeByte(0x31, 0b00000100); // Power Off 3.0V +} + +uint8_t I2C_AXP192::readByte(uint8_t address) { + _i2cPort->beginTransmission(_deviceAddress); + _i2cPort->write(address); + _i2cPort->endTransmission(); + _i2cPort->requestFrom(_deviceAddress, 1); + uint8_t val = _i2cPort->read(); + + ESP_LOGD("AXP192", "readByte(%02X) = %02X", address, val); + return val; +} + +void I2C_AXP192::writeByte(uint8_t address, uint8_t data) { + _i2cPort->beginTransmission(_deviceAddress); + _i2cPort->write(address); + _i2cPort->write(data); + _i2cPort->endTransmission(); + ESP_LOGD("AXP192", "writeByte(%02X) = %02X", address, data); +} + +void I2C_AXP192::bitOn(uint8_t address, uint8_t bit) { + uint8_t add = address; + uint8_t val = readByte(add) | bit; + + writeByte(add, val); +} + +void I2C_AXP192::bitOff(uint8_t address, uint8_t bit) { + uint8_t add = address; + uint8_t val = readByte(add) & ~bit; + + writeByte(add, val); +} + +// Borrowed from Core2 code +uint8_t I2C_AXP192::calcVoltageData(uint16_t value, + uint16_t maxv, + uint16_t minv, + uint16_t step) { + uint8_t data = 0; + + if (value > maxv) { value = maxv; } + + if (value > minv) { data = (value - minv) / step; } + return data; +} + +/// @param number 0=DCDC1 / 1=DCDC2 / 2=DCDC3 (Borrowed from Core2 code) +void I2C_AXP192::setDCVoltage(uint8_t number, + uint16_t voltage) { + if (voltage < 0) { return; } + uint8_t addr; + uint8_t vdata; + + if (number > 2) { return; } + + switch (number) { + case 0: + addr = 0x26; + vdata = calcVoltageData(voltage, 3500, 700, 25) & 0x7F; + break; + case 1: + addr = 0x25; + vdata = calcVoltageData(voltage, 2275, 700, 25) & 0x3F; + break; + case 2: + addr = 0x27; + vdata = calcVoltageData(voltage, 3500, 700, 25) & 0x7F; + break; + } + writeByte(addr, (readByte(addr) & 0x80) | vdata); +} + +void I2C_AXP192::setLDOVoltage(uint8_t number, + uint16_t voltage) { + if (voltage < 0) { return; } + uint8_t vdata = calcVoltageData(voltage, 3300, 1800, 100) & 0x0F; + + switch (number) { + // uint8_t reg, data; + case 2: + writeByte(0x28, (readByte(0x28) & 0x0F) | (vdata << 4)); + break; + case 3: + writeByte(0x28, (readByte(0x28) & 0xF0) | vdata); + break; + } +} + +void I2C_AXP192::setDCDC1(int16_t voltage) { + if (voltage < 0) { return; } + uint8_t add = 0x12; + uint8_t val; + + if ((voltage < 700) || (3500 < voltage)) { + // Disable + bitOff(add, (1 << 0)); + return; + } else { + // Enable + bitOn(add, (1 << 0)); + } + + // Set + setDCVoltage(0, voltage); +} + +void I2C_AXP192::setDCDC2(int16_t voltage) { + if (voltage < 0) { return; } + uint8_t add = 0x12; + uint8_t val; + + if ((voltage < 700) || (2750 < voltage)) { + // Disable + bitOff(add, (1 << 4)); + return; + } else { + // Enable + bitOn(add, (1 << 4)); + } + + // Set + setDCVoltage(1, voltage); +} + +void I2C_AXP192::setDCDC3(int16_t voltage) { + if (voltage < 0) { return; } + uint8_t add = 0x12; + uint8_t val; + + if ((voltage < 700) || (3500 < voltage)) { + // Disable + bitOff(add, (1 << 1)); + return; + } else { + // Enable + bitOn(add, (1 << 1)); + } + + // Set + setDCVoltage(2, voltage); +} + +void I2C_AXP192::setLDO2(int16_t voltage) { + if (voltage < 0) { return; } + uint8_t add = 0x12; + uint8_t val; + + if ((voltage < 1800) || (3300 < voltage)) { + // Disable + bitOff(add, (1 << 2)); + return; + } else { + // Enable + bitOn(add, (1 << 2)); + } + + // Set + setLDOVoltage(2, voltage); +} + +void I2C_AXP192::setLDO3(int16_t voltage) { + if (voltage < 0) { return; } + uint8_t add = 0x12; + uint8_t val; + + if ((voltage < 1800) || (3300 < voltage)) { + // Disable + bitOff(add, (1 << 3)); + return; + } else { + // Enable + bitOn(add, (1 << 3)); + } + + // Set + setLDOVoltage(3, voltage); +} + +void I2C_AXP192::setEXTEN(bool enable) { + uint8_t add = 0x12; + + if (enable) { + // Enable + bitOn(add, (1 << 6)); + } else { + // Disable + bitOff(add, (1 << 6)); + } +} + +void I2C_AXP192::setBACKUP(bool enable) { + uint8_t add = 0x35; + + if (enable) { + // Enable + bitOn(add, (1 << 7)); + } else { + // Disable + bitOff(add, (1 << 7)); + } +} + +void I2C_AXP192::setLDOIO(int16_t voltage) { + if (voltage < 0) { return; } + uint8_t add = 0x91; + + if ((voltage >= 1800) || (3300 >= voltage)) { + // Set voltage + uint8_t val = (calcVoltageData(voltage, 3300, 1800, 100) & 0x0F) << 4; + writeByte(add, val); + } +} + +void I2C_AXP192::setGPIO0(int state) { + if (state < 0) { return; } + const uint8_t add = 0x90; + + if (state == 0) { + bitOff(add, 1u); + } else { + bitOn(add, 1u); + } +} + +void I2C_AXP192::setGPIO1(int state) { + if (state < 0) { return; } + const uint8_t add = 0x94; + + if (state == 0) { + bitOff(add, 2u); + } else { + bitOn(add, 2u); + } +} + +void I2C_AXP192::setGPIO2(int state) { + if (state < 0) { return; } + const uint8_t add = 0x94; + + if (state == 0) { + bitOff(add, 4u); + } else { + bitOn(add, 4u); + } +} + +void I2C_AXP192::setGPIO3(int state) { + if (state < 0) { return; } + const uint8_t add = 0x96; + + if (state == 0) { + bitOff(add, 1u); + } else { + bitOn(add, 1u); + } +} + +void I2C_AXP192::setGPIO4(int state) { + if (state < 0) { return; } + const uint8_t add = 0x96; + + if (state == 0) { + bitOff(add, 2u); + } else { + bitOn(add, 2u); + } +} + +float I2C_AXP192::getBatteryVoltage() { + uint16_t val = readByte(0x78) << 4; + + val |= readByte(0x79); + return val * 1.1; +} + +float I2C_AXP192::getBatteryDischargeCurrent() { + uint16_t val = readByte(0x7c) << 5; + + val |= readByte(0x7d); + return val * 0.5; +} + +float I2C_AXP192::getBatteryChargeCurrent() { + uint16_t val = readByte(0x7a) << 5; + + val |= readByte(0x7b); + return val * 0.5; +} + +float I2C_AXP192::getBatteryPower() { + uint32_t val = (readByte(0x70) << 16) | (readByte(0x71) << 8) | readByte(0x72); + + return 1.1 * 0.5 * val / 1000.0; +} + +float I2C_AXP192::getAcinVolatge() { + uint16_t val = readByte(0x56) << 4; + + val |= readByte(0x57); + return val * 1.7; +} + +float I2C_AXP192::getAcinCurrent() { + uint16_t val = readByte(0x58) << 4; + + val |= readByte(0x59); + return val * 0.625; +} + +float I2C_AXP192::getVbusVoltage() { + uint16_t val = readByte(0x5a) << 4; + + val |= readByte(0x5b); + return val * 1.7; +} + +float I2C_AXP192::getVbusCurrent() { + uint16_t val = readByte(0x5c) << 4; + + val |= readByte(0x5d); + return val * 0.375; +} + +float I2C_AXP192::getInternalTemperature() { + uint16_t val = readByte(0x5e) << 4; + + val |= readByte(0x5f); + return -144.7 + val * 0.1; +} + +float I2C_AXP192::getApsVoltage() { + uint16_t val = readByte(0x7e) << 4; + + val |= readByte(0x7f); + return val * 1.4; +} + +void I2C_AXP192::powerOff() { + bitOn(0x32, (1 << 7)); +} + +uint8_t I2C_AXP192::getPekPress() { + uint8_t val = readByte(0x46); + + writeByte(0x46, 0x03); + return val; +} diff --git a/lib/I2C_AXP192/src/I2C_AXP192.h b/lib/I2C_AXP192/src/I2C_AXP192.h new file mode 100644 index 000000000..9e791158f --- /dev/null +++ b/lib/I2C_AXP192/src/I2C_AXP192.h @@ -0,0 +1,85 @@ +#ifndef __I2C_AXP192_H__ +#define __I2C_AXP192_H__ + +#include +#include + +#define I2C_AXP192_DEFAULT_ADDRESS 0x34 + +typedef struct { + bool EXTEN; + bool BACKUP; + int16_t DCDC1; + int16_t DCDC2; + int16_t DCDC3; + int16_t LDO2; + int16_t LDO3; + int16_t LDOIO; + int GPIO0; + int GPIO1; + int GPIO2; + int GPIO3; + int GPIO4; +} I2C_AXP192_InitDef; + +class I2C_AXP192 { +public: + + I2C_AXP192(uint8_t deviceAddress = I2C_AXP192_DEFAULT_ADDRESS, + TwoWire& i2cPort = Wire); + void begin(I2C_AXP192_InitDef initDef); + + void setDCDC1(int16_t voltage); + void setDCDC2(int16_t voltage); + void setDCDC3(int16_t voltage); + + void setLDO2(int16_t voltage); + void setLDO3(int16_t voltage); + + void setLDOIO(int16_t voltage); + void setGPIO0(int voltage); + void setGPIO1(int voltage); + void setGPIO2(int voltage); + void setGPIO3(int voltage); + void setGPIO4(int voltage); + + void setEXTEN(bool enable); + void setBACKUP(bool enable); + + float getBatteryVoltage(); + float getBatteryDischargeCurrent(); + float getBatteryChargeCurrent(); + float getBatteryPower(); + float getAcinVolatge(); + float getAcinCurrent(); + float getVbusVoltage(); + float getVbusCurrent(); + float getInternalTemperature(); + float getApsVoltage(); + + void powerOff(); + uint8_t getPekPress(); + +private: + + uint8_t readByte(uint8_t address); + void writeByte(uint8_t address, + uint8_t data); + void bitOn(uint8_t address, + uint8_t bit); + void bitOff(uint8_t address, + uint8_t bit); + void setDCVoltage(uint8_t number, + uint16_t voltage); + uint8_t calcVoltageData(uint16_t value, + uint16_t maxv, + uint16_t minv, + uint16_t step); + void setLDOVoltage(uint8_t number, + uint16_t voltage); + + TwoWire *_i2cPort; + int _deviceAddress; +}; + +#endif // ifndef __I2C_AXP192_H__ diff --git a/platformio_core_defs.ini b/platformio_core_defs.ini index a4d3f01d1..3aa5d7ba2 100644 --- a/platformio_core_defs.ini +++ b/platformio_core_defs.ini @@ -49,6 +49,7 @@ lib_ignore = ESP32_ping HeatpumpIR TinyWireM LittleFS(esp8266) + I2C AXP192 Power management [esp82xx_2_5_x] build_flags = -DNDEBUG diff --git a/platformio_esp32_envs.ini b/platformio_esp32_envs.ini index e5848379e..5a22527df 100644 --- a/platformio_esp32_envs.ini +++ b/platformio_esp32_envs.ini @@ -49,8 +49,8 @@ lib_deps = VL53L0X @ 1.3.0 RAK12019_LTR390_UV_Light Adafruit NeoMatrix - tanakamasayuki/I2C AXP192 Power management @ ^1.0.4 IthoCC1101 + I2C AXP192 Power management Adafruit PCD8544 Nokia 5110 LCD library extra_scripts = post:tools/pio/post_esp32.py ${extra_scripts_default.extra_scripts} diff --git a/platformio_esp82xx_base.ini b/platformio_esp82xx_base.ini index a6a4a4d0e..05a3d01c1 100644 --- a/platformio_esp82xx_base.ini +++ b/platformio_esp82xx_base.ini @@ -108,6 +108,7 @@ lib_ignore = ${esp82xx_defaults.lib_ignore} SD SDFS LittleFS(esp8266) + I2C AXP192 Power management monitor_filters = esp8266_exception_decoder extra_scripts = pre:tools/pio/pre_default_check.py ${extra_scripts_esp8266.extra_scripts} diff --git a/platformio_esp82xx_envs.ini b/platformio_esp82xx_envs.ini index e80390bf5..e2e27ebfb 100644 --- a/platformio_esp82xx_envs.ini +++ b/platformio_esp82xx_envs.ini @@ -18,6 +18,7 @@ lib_ignore = ESP32_ping HeatpumpIR IRremoteESP8266 ServoESP32 + I2C AXP192 Power management extra_scripts = pre:tools/pio/pre_custom_esp82xx.py ${extra_scripts_esp8266.extra_scripts} @@ -53,6 +54,7 @@ lib_ignore = ESP32_ping Adafruit Motor Shield V2 Library Adafruit_ST77xx Adafruit NeoMatrix + I2C AXP192 Power management extra_scripts = ${esp8266_custom_common.extra_scripts} pre:tools/pio/ir_build_check.py @@ -120,6 +122,7 @@ lib_ignore = ESP32_ping Adafruit Motor Shield V2 Library Adafruit_ST77xx Adafruit NeoMatrix + I2C AXP192 Power management extra_scripts = ${esp8266_custom_common.extra_scripts} pre:tools/pio/ir_build_check.py