M5Unified
AXP192_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 "AXP192_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 : 0.7-3.5V, 25mV/step 1200mA
16  DCDC2 : 0.7-2.275V,25mV/step 1600mA
17  DCDC3 : 0.7-3.5V, 25mV/step 700mA
18 
19  LDOio0: 1.8-3.3V, 100mV/step 50mA
20  LDO1 : 30mA always on
21  LDO2 : 1.8-3.3V, 100mV/step 200mA
22  LDO3 : 1.8-3.3V, 100mV/step 200mA
23 */
25  {
26  std::uint8_t val;
27  _init = readRegister(0x03, &val, 1);
28  if (_init)
29  {
30  _init = (val == 0x03);
31 #if defined (ESP_LOGV)
32  ESP_LOGV("AXP192", "reg03h:%02x : init:%d", val, _init);
33 #endif
34  }
35  return _init;
36  }
37 
39  void AXP192_Class::_set_DCDC(std::uint8_t num, int voltage)
40  {
41  static constexpr uint8_t reg12bit_tbl[] = { 0x01, 0x10, 0x02 };
42  static constexpr uint8_t volt_reg_tbl[] = { 0x26, 0x23, 0x27 };
43  static constexpr uint8_t volt_max_tbl[] = { 0x7F, 0x3F, 0x7F };
44 
45  voltage -= 700;
46  std::uint_fast8_t val = (voltage < 0) ? 0 : std::min(voltage / 25, (int)volt_max_tbl[num]);
47  writeRegister8(volt_reg_tbl[num], val);
48  if (voltage < 0)
49  {
50  bitOff(0x12, reg12bit_tbl[num]);
51  }
52  else
53  {
54  bitOn(0x12, reg12bit_tbl[num]);
55  }
56  }
57 
59  void AXP192_Class::_set_LDO(std::uint8_t num, int voltage)
60  {
61  if (num > 3 || num == 1) return;
62  std::uint8_t reg_volt = (num == 0) ? 0x91 : 0x28;
63  voltage -= 1800;
65  std::uint_fast8_t val = (voltage < 0) ? 0 : std::min(voltage / 100, 0x0F);
66  std::uint_fast8_t now = readRegister8(reg_volt);
67  if (num == 3)
68  {
69  now = (now & 0xF0) + val;
70  }
71  else
72  {
73  now = (now & 0x0F) | (val << 4);
74  }
75  writeRegister8(reg_volt, now);
76 
77  if (num)
78  { // LDO2 , LDO3
79  std::uint_fast8_t reg12bit = 1 << num;
80  if (voltage < 0)
81  {
82  bitOff(0x12, reg12bit);
83  }
84  else
85  {
86  bitOn(0x12, reg12bit);
87  }
88  }
89  else
90  { // LDOio0
91  writeRegister8(0x90, (voltage < 0) ? 0x07 : 0x02 );
92  }
93  }
94 
96  void AXP192_Class::_set_LDO2_LDO3(std::uint8_t num, int voltage)
97  {
98  voltage -= 1800;
99  std::uint_fast8_t val = (voltage < 0) ? 0 : std::min(voltage / 100, 0x0F);
100  std::uint_fast8_t now = readRegister8(0x28);
101  if (num == 1)
102  {
103  now = (now & 0xF0) + val;
104  }
105  else
106  {
107  now = (now & 0x0F) | (val << 4);
108  }
109  writeRegister8(0x28, now);
110 
111  std::uint_fast8_t reg12bit = 1 << (num + 2);
112  if (voltage < 0)
113  {
114  bitOff(0x12, reg12bit);
115  }
116  else
117  {
118  bitOn(0x12, reg12bit);
119  }
120  }
121 
123  void AXP192_Class::_set_GPIO0_2(std::uint8_t num, bool state)
124  {
125  static constexpr uint8_t reg[] = { 0x90, 0x92, 0x93 };
126  writeRegister8(reg[num], state ? 0x06 : 0x05); // floating or LOW
127  }
128 
130  void AXP192_Class::_set_GPIO3_4(std::uint8_t num, bool state)
131  {
132  uint32_t bit = num ? 2 : 1;
133  if (state)
134  {
135  bitOn(0x96, bit);
136  }
137  else
138  {
139  bitOff(0x96, bit);
140  }
141  uint_fast8_t mask = num ? ~0x0C : ~0x03;
142  uint_fast8_t reg0x95 = readRegister8(0x95) & mask;
143  writeRegister8(0x95, reg0x95 | (num ? 0x84 : 0x81)); // set GPIO mode
144  }
145 
147  {
148  std::uint8_t val = 0;
149  if (readRegister(0x33, &val, 1))
150  {
151  writeRegister8(0x33, (val & 0x7F) + (enable ? 0x80 : 0x00));
152  }
153  }
154 
155  void AXP192_Class::setChargeCurrent(std::uint16_t max_mA)
156  {
157  max_mA /= 10;
158  if (max_mA > 132) { max_mA = 132; }
159  static constexpr std::uint8_t table[] = { 19, 28, 36, 45, 55, 63, 70, 78, 88, 96, 100, 108, 116, 124, 132, 255 };
160 
161  size_t i = 0;
162  while (table[i] <= max_mA) { ++i; }
163 
164  std::uint8_t val = 0;
165  if (readRegister(0x33, &val, 1))
166  {
167  writeRegister8(0x33, (val & 0xF0) + i);
168  }
169  }
170 
171  void AXP192_Class::setChargeVoltage(std::uint16_t max_mV)
172  {
173  max_mV = (max_mV / 10) - 410;
174  if (max_mV > 436 - 410) { max_mV = 436 - 410; }
175  static constexpr std::uint8_t table[] =
176  { 415 - 410
177  , 420 - 410
178  , 436 - 410
179  , 255
180  };
181  size_t i = 0;
182  while (table[i] <= max_mV) { ++i; }
183 
184  std::uint8_t val = 0;
185  if (readRegister(0x33, &val, 1))
186  {
187  writeRegister8(0x33, (val & 0x9F) + (i << 5));
188  }
189  }
190 
192  {
193  std::uint8_t buf[4];
194  if (!readRegister(0x78, buf, 4)) { return -1; }
195 
196  std::uint_fast16_t voltage = (buf[0] << 4) + buf[1];
197  std::uint_fast16_t current = (buf[2] << 5) + buf[3];
198 
199  std::int_fast16_t res = (voltage > 3150) ? (( voltage - 3075 ) * 0.16f )
200  : (voltage > 2690) ? (( voltage - 2690 ) * 0.027f )
201  : 0;
202  if (current > 16) { res -= 16; }
203 
204  return (res < 100) ? res : 100;
205  }
206 
208  {
209  return readRegister8(0x00) & 0x04;
210  }
211 
213  {
214  bitOn(0x32, 0x80);
215  }
216 
217  void AXP192_Class::setAdcState(bool enable)
218  {
219  writeRegister8(0x82, enable ? 0xff : 0x00);
220  }
221 
222  void AXP192_Class::setAdcRate( std::uint8_t rate )
223  {
224  std::uint_fast8_t buf = readRegister8(0x84);
225  writeRegister8(0x84, (buf & ~(0xc0)) | (rate & 0xc0));
226  }
227 
228  void AXP192_Class::setEXTEN(bool enable)
229  {
230  static constexpr std::uint8_t add = 0x12;
231  static constexpr std::uint8_t bit = 1 << 6;
232  if (enable)
233  {
234  bitOn(add, bit);
235  }
236  else
237  {
238  bitOff(add, bit);
239  }
240  }
241 
243  {
244  return readRegister8(0x12) & (1 << 6);
245  }
246 
247  void AXP192_Class::setBACKUP(bool enable)
248  {
249  static constexpr std::uint8_t add = 0x35;
250  static constexpr std::uint8_t bit = 1 << 7;
251  if (enable)
252  { // Enable
253  bitOn(add, bit);
254  }
255  else
256  { // Disable
257  bitOff(add, bit);
258  }
259  }
260 
262  {
263  return readRegister8(0x00) & 0x80;
264  }
266  {
267  return readRegister8(0x00) & 0x20;
268  }
269 
271  {
272  return readRegister8(0x01) & 0x20;
273  }
274 
275  std::uint8_t AXP192_Class::getPekPress(void)
276  {
277  std::uint8_t val = readRegister8(0x46) & 0x03;
278  if (val) { writeRegister8(0x46, val); }
279  return val;
280  }
281 
283  {
284  return readRegister12(0x56) * (1.7f / 1000.0f);
285  }
286 
288  {
289  return readRegister12(0x58) * 0.625f;
290  }
291 
293  {
294  return readRegister12(0x5a) * (1.7f / 1000.0f);
295  }
296 
298  {
299  return readRegister12(0x5c) * 0.375f;
300  }
301 
303  {
304  return readRegister12(0x5e) * 0.1f -144.7f;
305  }
306 
308  {
309  return readRegister24(0x70) * (1.1f * 0.5f / 1000.0f);
310  }
311 
313  {
314  return readRegister12(0x78) * (1.1f / 1000.0f);
315  }
316 
318  {
319  return readRegister13(0x7a) * 0.5f;
320  }
321 
323  {
324  return readRegister13(0x7c) * 0.5f;
325  }
326 
328  {
329  return readRegister12(0x7e) * (1.4f / 1000.0f);
330  }
331 
332 
333  std::size_t AXP192_Class::readRegister12(std::uint8_t addr)
334  {
335  std::uint8_t buf[2] = {0};
336  readRegister(addr, buf, 2);
337  return buf[0] << 4 | buf[1];
338  }
339  std::size_t AXP192_Class::readRegister13(std::uint8_t addr)
340  {
341  std::uint8_t buf[2] = {0};
342  readRegister(addr, buf, 2);
343  return buf[0] << 5 | buf[1];
344  }
345  std::size_t AXP192_Class::readRegister16(std::uint8_t addr)
346  {
347  std::uint8_t buf[2] = {0};
348  readRegister(addr, buf, 2);
349  return buf[0] << 8 | buf[1];
350  }
351  std::size_t AXP192_Class::readRegister24(std::uint8_t addr)
352  {
353  std::uint8_t buf[3] = {0};
354  readRegister(addr, buf, 3);
355  return buf[0] << 16 | buf[1] << 8 | buf[2];
356  }
357  std::size_t AXP192_Class::readRegister32(std::uint8_t addr)
358  {
359  std::uint8_t buf[4] = {0};
360  readRegister(addr, buf, 4);
361  return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
362  }
363 }
float getBatteryVoltage(void)
bool begin(void)
float getACINCurrent(void)
float getInternalTemperature(void)
std::int8_t getBatteryLevel(void)
void setBACKUP(bool enable)
std::uint8_t getPekPress(void)
float getVBUSVoltage(void)
float getVBUSCurrent(void)
float getAPSVoltage(void)
bool isCharging(void)
Get whether the battery is currently charging or not.
void setChargeCurrent(std::uint16_t max_mA)
float getACINVoltage(void)
void setBatteryCharge(bool enable)
float getBatteryPower(void)
void setChargeVoltage(std::uint16_t max_mV)
float getBatteryChargeCurrent(void)
void setAdcState(bool enable)
bool getBatState(void)
void setEXTEN(bool enable)
float getBatteryDischargeCurrent(void)
void setAdcRate(std::uint8_t rate)
bool getEXTEN(void)
void powerOff(void)
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