M5Unified
IP5306_Class.cpp
Go to the documentation of this file.
1 // Copyright (c) M5Stack. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 
4 #include "IP5306_Class.hpp"
5 
6 #if __has_include(<esp_log.h>)
7 #include <esp_log.h>
8 #endif
9 
10 #include <algorithm>
11 
12 namespace m5
13 {
14  static constexpr std::uint8_t REG_SYS_CTL0 = 0x00;
15  static constexpr std::uint8_t REG_SYS_CTL1 = 0x01;
16  static constexpr std::uint8_t REG_SYS_CTL2 = 0x02;
17  static constexpr std::uint8_t REG_READ0 = 0x70;
18  static constexpr std::uint8_t REG_READ1 = 0x71;
19  static constexpr std::uint8_t REG_READ2 = 0x72;
20  static constexpr std::uint8_t REG_READ3 = 0x77;
21  static constexpr std::uint8_t REG_READ4 = 0x78;
22  static constexpr std::uint8_t REG_CHG_CTL0 = 0x20;
23  static constexpr std::uint8_t REG_CHG_CTL1 = 0x21;
24  static constexpr std::uint8_t REG_CHG_CTL2 = 0x22;
25  static constexpr std::uint8_t REG_CHG_CTL3 = 0x23;
26  static constexpr std::uint8_t REG_CHG_DIG_CTL0 = 0x24;
27 
28  static constexpr std::uint8_t BOOST_OUT_BIT = 0x02;
29 
31  {
32  std::uint8_t val = 0;
33  writeRegister(0x06, &val, 1); // reg06h WLED flashlight disabled
34  val = 2;
35  _init = writeRegister(0x06, &val, 1);
36  if (_init)
37  {
38 #if defined (ESP_LOGV)
39  ESP_LOGV("IP5306", "found");
40 #endif
41  }
42  return _init;
43  }
44 
46  {
47  std::uint8_t data;
48  if (readRegister(REG_READ4, &data, 1)) {
49  switch (data >> 4) {
50  case 0x00: return 100;
51  case 0x08: return 75;
52  case 0x0C: return 50;
53  case 0x0E: return 25;
54  default: return 0;
55  }
56  }
57  return -1;
58  }
59 
61  {
62  static constexpr std::uint8_t CHARGE_OUT_BIT = 0x10;
63 
64  std::uint8_t val = 0;
65  if (readRegister(REG_SYS_CTL0, &val, 1))
66  {
67  writeRegister8(REG_SYS_CTL0, enable ? (val | CHARGE_OUT_BIT) : (val & (~CHARGE_OUT_BIT)));
68  }
69  }
70 
71  void IP5306_Class::setChargeCurrent(std::uint16_t max_mA)
72  {
73  max_mA = (max_mA > 50) ? (max_mA - 50) / 100 : 0;
74  if (max_mA > 31) { max_mA = 31; }
75 
76  std::uint8_t val = 0;
77  if (readRegister(REG_CHG_DIG_CTL0, &val, 1))
78  {
79  writeRegister8(REG_CHG_DIG_CTL0, (val & 0xE0) + max_mA);
80  }
81  }
82 
83  void IP5306_Class::setChargeVoltage(std::uint16_t max_mV)
84  {
85  max_mV = (max_mV / 10);
86  max_mV = (max_mV > 410) ? max_mV - 410 : 0;
87  if (max_mV > 436 - 410) { max_mV = 436 - 410; }
88  static constexpr std::uint8_t table[] =
89  { 430 - 410
90  , 435 - 410
91  , 440 - 410
92  , 255
93  };
94  size_t i = 0;
95  while (table[i] <= max_mV) { ++i; }
96 
97  static constexpr std::uint8_t regdata[4] =
98  { 0x02 // 4.2v + boost 28mV
99  , 0x05 // 4.3v + boost 14mV
100  , 0x09 // 4.35v + boost 14mV
101  , 0x0D // 4.4v + boost 14mV
102  };
103  writeRegister8(REG_CHG_CTL2, regdata[i]);
104  }
105 
107  {
108  std::uint8_t val = 0;
109  return (readRegister(0x71, &val, 1)) && (val % 0x0C);
110  }
111 
113  std::uint8_t data;
114  if (readRegister(REG_SYS_CTL0, &data, 1) == true)
115  {
116  data = en ? (data | BOOST_OUT_BIT) : (data & (~BOOST_OUT_BIT));
117  return writeRegister(REG_SYS_CTL0, &data, 1);
118  }
119  return false;
120  }
121 }
bool readRegister(std::uint8_t reg, std::uint8_t *result, std::size_t length) const
Definition: I2C_Class.hpp:177
bool writeRegister8(std::uint8_t reg, std::uint8_t data) const
Definition: I2C_Class.hpp:160
bool writeRegister(std::uint8_t reg, const std::uint8_t *data, std::size_t length) const
Definition: I2C_Class.hpp:172
std::int8_t getBatteryLevel(void)
void setChargeVoltage(std::uint16_t max_mV)
void setBatteryCharge(bool enable)
void setChargeCurrent(std::uint16_t max_mA)
bool isCharging(void)
Get whether the battery is currently charging or not.
bool setPowerBoostKeepOn(bool en)
Set whether or not to continue supplying power even at low loads.
bool begin(void)
Definition: M5Unified.cpp:48