M5Unified
AXP2101_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 "AXP2101_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 /*
15  DCDC1 : 1.5-3.4V, 2000mA
16  DCDC2 : 0.5-1.2V,1.22-1.54V, 2000mA
17  DCDC3 : 0.5-1.2V,1.22-1.54V, 1.6-3.4V, 2000mA
18  DCDC4 : 0.5-1.2V, 1.22-1.84V, 1500mA
19  DCDC5 : 1.2V , 1.4-3.7V, 1000mA
20 
21  RTCLDO1/2 : 1.8V/2.5V/3V/3.3V, 30mA
22 
23  ALDO1~4 : 0.5-3.5V, 100mV/step 300mA
24 */
26  {
27  std::uint8_t val;
28  _init = readRegister(0x03, &val, 1);
29  if (_init)
30  {
31  _init = (val == 0x4A);
32 #if defined (ESP_LOGV)
33  ESP_LOGV("AXP2101", "reg03h:%02x : init:%d", val, _init);
34 #endif
35  }
36  return _init;
37  }
38 
39  // 0=ALDO1 ~ 3=ALDO4 / 4=BLDO1 / 5=BLDO2
40  void AXP2101_Class::_set_LDO(std::uint8_t num, int voltage)
41  {
42  if (num > 5) return;
43  std::uint8_t reg_volt = num + 0x92;
44  voltage -= 500;
46  std::uint_fast8_t val = (voltage < 0) ? 0 : std::min(voltage / 100, 0x1E);
47  writeRegister8(reg_volt, val);
48 
49  std::uint_fast8_t reg90bit = 1 << num;
50  if (voltage < 0)
51  {
52  bitOff(0x90, reg90bit);
53  }
54  else
55  {
56  bitOn(0x90, reg90bit);
57  }
58  }
59 
60  void AXP2101_Class::_set_DLDO(std::uint8_t num, int voltage)
61  {
62  if (num > 1) return;
63 
64  std::uint8_t reg_volt = num + 0x99;
65  voltage -= 500;
67  std::uint_fast8_t val = (voltage < 0) ? 0 : std::min(voltage / (num ? 50 : 100), num ? 0x13 : 0x1C);
68  writeRegister8(reg_volt, val);
69 
70  uint8_t reg = 0x90 + num;
71  uint8_t bit = num ? 0x01 : 0x80;
72  if (voltage < 0)
73  {
74  bitOff(reg, bit);
75  }
76  else
77  {
78  bitOn(reg, bit);
79  }
80  }
81 
82  bool AXP2101_Class::_get_LDOEn(std::uint8_t num)
83  {
84  bool res = false;
85  if (num <= 5) {
86  std::uint_fast8_t reg90bit = 1 << num;
87  res = readRegister8(0x90) & reg90bit;
88  }
89  return res;
90  }
91 
93  {
94 #if defined (ESP_LOGE)
95  ESP_LOGE("AXP2101","setReg0x20Bit0 : %d", flg);
96 #endif
100  if (flg)
101  {
102  bitOn(0x20, 1);
103  }
104  else
105  {
106  bitOff(0x20, 1);
107  }
109  bitOff(AXP2101_EFREQ_CTRL, 0x04);
111  }
112 
114  {
115  std::uint8_t val = 0;
116  if (readRegister(0x18, &val, 1))
117  {
118  writeRegister8(0x18, (val & 0xFD) + (enable << 1));
119  }
120  }
121 
122  void AXP2101_Class::setChargeCurrent(std::uint16_t max_mA)
123  {
124  max_mA /= 5;
125  if (max_mA > 1000/5) { max_mA = 1000/5; }
126  static constexpr std::uint8_t table[] = { 125 / 5, 150 / 5, 175 / 5, 200 / 5, 300 / 5, 400 / 5, 500 / 5, 600 / 5, 700 / 5, 800 / 5, 900 / 5, 1000 / 5, 255 };
127 
128  size_t i = 0;
129  while (table[i] <= max_mA) { ++i; }
130  i += 4;
131  writeRegister8(0x62, i);
132  }
133 
134  void AXP2101_Class::setChargeVoltage(std::uint16_t max_mV)
135  {
136  max_mV = (max_mV / 10) - 400;
137  if (max_mV > 460 - 400) { max_mV = 460 - 400; }
138  static constexpr std::uint8_t table[] =
139  { 410 - 400
140  , 420 - 400
141  , 435 - 400
142  , 440 - 400
143  , 460 - 400
144  , 255
145  };
146  size_t i = 0;
147  while (table[i] <= max_mV) { ++i; }
148 
149  if (++i >= 0b110) { i = 0; }
150  writeRegister8(0x64, i);
151  }
152 
154  {
155  std::int8_t res = readRegister8(0xA4);
156  return res;
157  }
158 
161  {
162  uint32_t val = (readRegister8(0x01) >> 5) & 0b11;
163  // 0b01:charge / 0b10:dischage / 0b00:stanby
164  return (val == 1) ? 1 : ((val == 2) ? -1 : 0);
165  }
166 
168  {
169  return (readRegister8(0x01) & 0b01100000) == 0b00100000;
170  }
171 
173  {
174  bitOn(0x10, 0x01);
175  }
176 
177  void AXP2101_Class::setAdcState(bool enable)
178  {
179  }
180 
181  void AXP2101_Class::setAdcRate( std::uint8_t rate )
182  {
183  }
184 
185  void AXP2101_Class::setBACKUP(bool enable)
186  {
187 /*
188  static constexpr std::uint8_t add = 0x35;
189  static constexpr std::uint8_t bit = 1 << 7;
190  if (enable)
191  { // Enable
192  bitOn(add, bit);
193  }
194  else
195  { // Disable
196  bitOff(add, bit);
197  }
198 //*/
199  }
200 
202  {
203 return false;
204  }
206  { // VBUS good indication
207  return readRegister8(0x00) & 0x20;
208  }
209 
211  { // Battery present state
212  return readRegister8(0x00) & 0x04;
213  }
214 
215  std::uint8_t AXP2101_Class::getPekPress(void)
216  {
217  std::uint8_t val = readRegister8(0x49) & 0x0C;
218  if (val) { writeRegister8(0x49, val); }
219  return val >> 2;
220  }
221 
223  {
224 return 0;
225  }
226 
228  {
229 return 0;
230  }
231 
233  {
234 return 0;
235  }
236 
238  {
239 return 0;
240  }
241 
243  {
244 return 0;
245  }
246 
248  {
249 return 0;
250  }
251 
253  {
254  return readRegister14(0x34) / 1000.0f;
255  }
256 
258  {
259 return 0;
260  }
261 
263  {
264 return 0;
265  }
266 
268  {
269 return 0;
270  }
271 
272 
273  std::size_t AXP2101_Class::readRegister12(std::uint8_t addr)
274  {
275  std::uint8_t buf[2] = {0};
276  readRegister(addr, buf, 2);
277  return (buf[0] & 0x0F) << 8 | buf[1];
278  }
279  std::size_t AXP2101_Class::readRegister14(std::uint8_t addr)
280  {
281  std::uint8_t buf[2] = {0};
282  readRegister(addr, buf, 2);
283  return (buf[0] & 0x3F) << 8 | buf[1];
284  }
285  std::size_t AXP2101_Class::readRegister16(std::uint8_t addr)
286  {
287  std::uint8_t buf[2] = {0};
288  readRegister(addr, buf, 2);
289  return buf[0] << 8 | buf[1];
290  }
291 
292 }
float getACINVoltage(void)
float getInternalTemperature(void)
int getChargeStatus(void)
static constexpr uint8_t AXP2101_TWI_ADDR_EXT
float getBatteryVoltage(void)
float getVBUSVoltage(void)
std::uint8_t getPekPress(void)
float getACINCurrent(void)
float getVBUSCurrent(void)
bool isCharging(void)
Get whether the battery is currently charging or not.
void setBatteryCharge(bool enable)
float getBatteryPower(void)
static constexpr uint8_t AXP2101_EFREQ_CTRL
bool getBatState(void)
std::int8_t getBatteryLevel(void)
void setChargeCurrent(std::uint16_t max_mA)
float getAPSVoltage(void)
void setAdcRate(std::uint8_t rate)
float getBatteryChargeCurrent(void)
void setChargeVoltage(std::uint16_t max_mV)
void setReg0x20Bit0(bool)
static constexpr uint8_t AXP2101_EFUS_OP_CFG
void setBACKUP(bool enable)
float getBatteryDischargeCurrent(void)
void setAdcState(bool enable)
bool bitOn(std::uint8_t reg, std::uint8_t bit) const
Definition: I2C_Class.hpp:182
bool bitOff(std::uint8_t reg, std::uint8_t bit) const
Definition: I2C_Class.hpp:187
bool readRegister(std::uint8_t reg, std::uint8_t *result, std::size_t length) const
Definition: I2C_Class.hpp:177
std::uint8_t readRegister8(std::uint8_t reg) const
Definition: I2C_Class.hpp:165
bool writeRegister8(std::uint8_t reg, std::uint8_t data) const
Definition: I2C_Class.hpp:160
Definition: M5Unified.cpp:48