-
Notifications
You must be signed in to change notification settings - Fork 222
/
AXP192.cpp
611 lines (524 loc) · 15.7 KB
/
AXP192.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#include "AXP192.h"
AXP192::AXP192() {
}
void AXP192::begin(bool disableLDO2, bool disableLDO3, bool disableRTC,
bool disableDCDC1, bool disableDCDC3, bool disableLDO0) {
Wire1.begin(21, 22);
Wire1.setClock(400000);
// Set LDO2 & LDO3(TFT_LED & TFT) 3.0V
Write1Byte(0x28, 0xcc);
// Set ADC sample rate to 200hz
Write1Byte(0x84, 0b11110010);
// Set ADC to All Enable
Write1Byte(0x82, 0xff);
// Bat charge voltage to 4.2, Current 100MA
Write1Byte(0x33, 0xc0);
// Depending on configuration enable LDO2, LDO3, DCDC1, DCDC3.
byte buf = (Read8bit(0x12) & 0xef) | 0x4D;
if (disableLDO3) buf &= ~(1 << 3);
if (disableLDO2) buf &= ~(1 << 2);
if (disableDCDC3) buf &= ~(1 << 1);
if (disableDCDC1) buf &= ~(1 << 0);
Write1Byte(0x12, buf);
// 128ms power on, 4s power off
Write1Byte(0x36, 0x0C);
if (!disableLDO0) {
// Set MIC voltage to 2.8V
Write1Byte(0x91, 0xA0);
// Set GPIO0 to LDO
Write1Byte(0x90, 0x02);
} else {
Write1Byte(0x90, 0x07); // GPIO0 floating
}
// Disable vbus hold limit
Write1Byte(0x30, 0x80);
// Set temperature protection
Write1Byte(0x39, 0xfc);
// Enable RTC BAT charge
Write1Byte(0x35, 0xa2 & (disableRTC ? 0x7F : 0xFF));
// Enable bat detection
Write1Byte(0x32, 0x46);
// Set Power off voltage 3.0v
Write1Byte(0x31, (Read8bit(0x31) & 0xf8) | (1 << 2));
}
void AXP192::Write1Byte(uint8_t Addr, uint8_t Data) {
Wire1.beginTransmission(0x34);
Wire1.write(Addr);
Wire1.write(Data);
Wire1.endTransmission();
}
uint8_t AXP192::Read8bit(uint8_t Addr) {
Wire1.beginTransmission(0x34);
Wire1.write(Addr);
Wire1.endTransmission();
Wire1.requestFrom(0x34, 1);
return Wire1.read();
}
uint16_t AXP192::Read12Bit(uint8_t Addr) {
uint16_t Data = 0;
uint8_t buf[2];
ReadBuff(Addr, 2, buf);
Data = ((buf[0] << 4) + buf[1]);
return Data;
}
uint16_t AXP192::Read13Bit(uint8_t Addr) {
uint16_t Data = 0;
uint8_t buf[2];
ReadBuff(Addr, 2, buf);
Data = ((buf[0] << 5) + buf[1]);
return Data;
}
uint16_t AXP192::Read16bit(uint8_t Addr) {
uint16_t ReData = 0;
Wire1.beginTransmission(0x34);
Wire1.write(Addr);
Wire1.endTransmission();
Wire1.requestFrom(0x34, 2);
for (int i = 0; i < 2; i++) {
ReData <<= 8;
ReData |= Wire1.read();
}
return ReData;
}
uint32_t AXP192::Read24bit(uint8_t Addr) {
uint32_t ReData = 0;
Wire1.beginTransmission(0x34);
Wire1.write(Addr);
Wire1.endTransmission();
Wire1.requestFrom(0x34, 3);
for (int i = 0; i < 3; i++) {
ReData <<= 8;
ReData |= Wire1.read();
}
return ReData;
}
uint32_t AXP192::Read32bit(uint8_t Addr) {
uint32_t ReData = 0;
Wire1.beginTransmission(0x34);
Wire1.write(Addr);
Wire1.endTransmission();
Wire1.requestFrom(0x34, 4);
for (int i = 0; i < 4; i++) {
ReData <<= 8;
ReData |= Wire1.read();
}
return ReData;
}
void AXP192::ReadBuff(uint8_t Addr, uint8_t Size, uint8_t *Buff) {
Wire1.beginTransmission(0x34);
Wire1.write(Addr);
Wire1.endTransmission();
Wire1.requestFrom(0x34, (int)Size);
for (int i = 0; i < Size; i++) {
*(Buff + i) = Wire1.read();
}
}
void AXP192::ScreenBreath(int brightness) {
if (brightness > 100 || brightness < 0) return;
int vol = map(brightness, 0, 100, 2500, 3200);
// Serial.printf("brightness:%d\n", brightness);
// Serial.printf("vol:%d\n", vol);
vol = (vol < 1800) ? 0 : (vol - 1800) / 100;
// Serial.printf("vol:%d\n", vol);
// Serial.printf("vol:%x\n\n", (uint16_t)vol);
uint8_t buf = Read8bit(0x28);
// Serial.printf("buf:%hhu\n", buf);
// Serial.printf("buf:%d\n", buf);
// Serial.printf("buf:%x\n", buf);
// Serial.printf("result:%x\n---\n", ((buf & 0x0f) | ((uint16_t)vol << 4)));
Write1Byte(0x28, ((buf & 0x0f) | ((uint16_t)vol << 4)));
}
void AXP192::ScreenSwitch(bool state) {
if (state == false) {
uint8_t buf = Read8bit(0x28);
Serial.printf("buf:%d\n", buf);
Write1Byte(0x28, ((buf & 0x0f)));
Serial.printf("buf:%d\n", Read8bit(0x28));
} else if (state == true) {
ScreenBreath(80);
}
}
// Return True = Battery Exist
bool AXP192::GetBatState() {
if (Read8bit(0x01) | 0x20)
return true;
else
return false;
}
// Input Power Status
uint8_t AXP192::GetInputPowerStatus() {
return Read8bit(0x00);
}
// Battery Charging Status
uint8_t AXP192::GetBatteryChargingStatus() {
return Read8bit(0x01);
}
//---------coulombcounter_from_here---------
// enable: void EnableCoulombcounter(void);
// disable: void DisableCOulombcounter(void);
// stop: void StopCoulombcounter(void);
// clear: void ClearCoulombcounter(void);
// get charge data: uint32_t GetCoulombchargeData(void);
// get discharge data: uint32_t GetCoulombdischargeData(void);
// get coulomb val affter calculation: float GetCoulombData(void);
//------------------------------------------
void AXP192::EnableCoulombcounter(void) {
Write1Byte(0xB8, 0x80);
}
void AXP192::DisableCoulombcounter(void) {
Write1Byte(0xB8, 0x00);
}
void AXP192::StopCoulombcounter(void) {
Write1Byte(0xB8, 0xC0);
}
void AXP192::ClearCoulombcounter(void) {
Write1Byte(0xB8, Read8bit(0xB8) | 0x20); // Only set the Clear Flag
}
uint32_t AXP192::GetCoulombchargeData(void) {
return Read32bit(0xB0);
}
uint32_t AXP192::GetCoulombdischargeData(void) {
return Read32bit(0xB4);
}
float AXP192::GetCoulombData(void) {
uint32_t coin = GetCoulombchargeData();
uint32_t coout = GetCoulombdischargeData();
uint32_t valueDifferent = 0;
bool bIsNegative = false;
if (coin > coout) { // Expected, in always more then out
valueDifferent = coin - coout;
} else { // Warning: Out is more than In, the battery is not started at 0%
// just Flip the output sign later
bIsNegative = true;
valueDifferent = coout - coin;
}
// c = 65536 * current_LSB * (coin - coout) / 3600 / ADC rate
// Adc rate can be read from 84H, change this variable if you change the ADC
// reate
float ccc = (65536 * 0.5 * valueDifferent) / 3600.0 /
200.0; // Note the ADC has defaulted to be 200 Hz
if (bIsNegative) ccc = 0.0 - ccc; // Flip it back to negative
return ccc;
}
//----------coulomb_end_at_here----------
uint16_t AXP192::GetVbatData(void) {
uint16_t vbat = 0;
uint8_t buf[2];
ReadBuff(0x78, 2, buf);
vbat = ((buf[0] << 4) + buf[1]); // V
return vbat;
}
uint16_t AXP192::GetVinData(void) {
uint16_t vin = 0;
uint8_t buf[2];
ReadBuff(0x56, 2, buf);
vin = ((buf[0] << 4) + buf[1]); // V
return vin;
}
uint16_t AXP192::GetIinData(void) {
uint16_t iin = 0;
uint8_t buf[2];
ReadBuff(0x58, 2, buf);
iin = ((buf[0] << 4) + buf[1]);
return iin;
}
uint16_t AXP192::GetVusbinData(void) {
uint16_t vin = 0;
uint8_t buf[2];
ReadBuff(0x5a, 2, buf);
vin = ((buf[0] << 4) + buf[1]); // V
return vin;
}
uint16_t AXP192::GetIusbinData(void) {
uint16_t iin = 0;
uint8_t buf[2];
ReadBuff(0x5C, 2, buf);
iin = ((buf[0] << 4) + buf[1]);
return iin;
}
uint16_t AXP192::GetIchargeData(void) {
uint16_t icharge = 0;
uint8_t buf[2];
ReadBuff(0x7A, 2, buf);
icharge = (buf[0] << 5) + buf[1];
return icharge;
}
uint16_t AXP192::GetIdischargeData(void) {
uint16_t idischarge = 0;
uint8_t buf[2];
ReadBuff(0x7C, 2, buf);
idischarge = (buf[0] << 5) + buf[1];
return idischarge;
}
uint16_t AXP192::GetTempData(void) {
uint16_t temp = 0;
uint8_t buf[2];
ReadBuff(0x5e, 2, buf);
temp = ((buf[0] << 4) + buf[1]);
return temp;
}
uint32_t AXP192::GetPowerbatData(void) {
uint32_t power = 0;
uint8_t buf[3];
ReadBuff(0x70, 2, buf);
power = (buf[0] << 16) + (buf[1] << 8) + buf[2];
return power;
}
uint16_t AXP192::GetVapsData(void) {
uint16_t vaps = 0;
uint8_t buf[2];
ReadBuff(0x7e, 2, buf);
vaps = ((buf[0] << 4) + buf[1]);
return vaps;
}
void AXP192::SetSleep(void) {
Write1Byte(0x31,
Read8bit(0x31) | (1 << 3)); // Turn on short press to wake up
Write1Byte(0x90, Read8bit(0x90) | 0x07); // GPIO0 floating
Write1Byte(0x82, 0x00); // Disable ADCs
Write1Byte(0x12, Read8bit(0x12) & 0xA1); // Disable all outputs but DCDC1
}
void AXP192::WakeUpDisplayAfterLightSleep(void) {
// LDO2 is LCD Backlight
// LDO3 is LCD Power
// Enable Ext, LDO3, LDO2, DCDC1
Write1Byte(0x12, Read8bit(0x12) | 0x4D);
}
uint8_t AXP192::GetWarningLeve(void) {
Wire1.beginTransmission(0x34);
Wire1.write(0x47);
Wire1.endTransmission();
Wire1.requestFrom(0x34, 1);
uint8_t buf = Wire1.read();
return (buf & 0x01);
}
// -- sleep
void AXP192::DeepSleep(uint64_t time_in_us) {
SetSleep();
esp_sleep_enable_ext0_wakeup((gpio_num_t)37, LOW);
if (time_in_us > 0) {
esp_sleep_enable_timer_wakeup(time_in_us);
} else {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
}
(time_in_us == 0) ? esp_deep_sleep_start() : esp_deep_sleep(time_in_us);
}
void AXP192::LightSleep(uint64_t time_in_us) {
SetSleep();
if (time_in_us > 0) {
esp_sleep_enable_timer_wakeup(time_in_us);
} else {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
}
esp_light_sleep_start();
WakeUpDisplayAfterLightSleep();
}
// Return 0 = not press, 0x01 = long press(1.5s), 0x02 = short press
uint8_t AXP192::GetBtnPress() {
uint8_t state = Read8bit(0x46); // IRQ 3 status.
if (state) {
Write1Byte(0x46, 0x03); // Write 1 back to clear IRQ
}
return state;
}
// Low Volt Level 1, when APS Volt Output < 3.4496 V
// Low Volt Level 2, when APS Volt Output < 3.3992 V, then this flag is SET
// (0x01) Flag will reset once battery volt is charged above Low Volt Level 1
// Note: now AXP192 have the Shutdown Voltage of 3.0V (B100) Def in REG 31H
uint8_t AXP192::GetWarningLevel(void) {
return Read8bit(0x47) & 0x01;
}
float AXP192::GetBatVoltage() {
float ADCLSB = 1.1 / 1000.0;
uint16_t ReData = Read12Bit(0x78);
return ReData * ADCLSB;
}
float AXP192::GetBatCurrent() {
float ADCLSB = 0.5;
uint16_t CurrentIn = Read13Bit(0x7A);
uint16_t CurrentOut = Read13Bit(0x7C);
return (CurrentIn - CurrentOut) * ADCLSB;
}
float AXP192::GetVinVoltage() {
float ADCLSB = 1.7 / 1000.0;
uint16_t ReData = Read12Bit(0x56);
return ReData * ADCLSB;
}
float AXP192::GetVinCurrent() {
float ADCLSB = 0.625;
uint16_t ReData = Read12Bit(0x58);
return ReData * ADCLSB;
}
float AXP192::GetVBusVoltage() {
float ADCLSB = 1.7 / 1000.0;
uint16_t ReData = Read12Bit(0x5A);
return ReData * ADCLSB;
}
float AXP192::GetVBusCurrent() {
float ADCLSB = 0.375;
uint16_t ReData = Read12Bit(0x5C);
return ReData * ADCLSB;
}
float AXP192::GetTempInAXP192() {
float ADCLSB = 0.1;
const float OFFSET_DEG_C = -144.7;
uint16_t ReData = Read12Bit(0x5E);
return OFFSET_DEG_C + ReData * ADCLSB;
}
float AXP192::GetBatPower() {
float VoltageLSB = 1.1;
float CurrentLCS = 0.5;
uint32_t ReData = Read24bit(0x70);
return VoltageLSB * CurrentLCS * ReData / 1000.0;
}
float AXP192::GetBatChargeCurrent() {
float ADCLSB = 0.5;
uint16_t ReData = Read13Bit(0x7A);
return ReData * ADCLSB;
}
float AXP192::GetAPSVoltage() {
float ADCLSB = 1.4 / 1000.0;
uint16_t ReData = Read12Bit(0x7E);
return ReData * ADCLSB;
}
float AXP192::GetBatCoulombInput() {
uint32_t ReData = Read32bit(0xB0);
return ReData * 65536 * 0.5 / 3600 / 25.0;
}
float AXP192::GetBatCoulombOut() {
uint32_t ReData = Read32bit(0xB4);
return ReData * 65536 * 0.5 / 3600 / 25.0;
}
void AXP192::SetCoulombClear() {
Write1Byte(0xB8, 0x20);
}
// Can turn LCD Backlight OFF for power saving
void AXP192::SetLDO2(bool State) {
uint8_t buf = Read8bit(0x12);
if (State == true) {
buf = (1 << 2) | buf;
} else {
buf = ~(1 << 2) & buf;
}
Write1Byte(0x12, buf);
}
void AXP192::SetLDO3(bool State) {
uint8_t buf = Read8bit(0x12);
if (State == true) {
buf = (1 << 3) | buf;
} else {
buf = ~(1 << 3) & buf;
}
Write1Byte(0x12, buf);
}
void AXP192::SetGPIO0(bool State) {
uint8_t buf = Read8bit(0x90);
if (State == true) {
buf &= ~(0x07); // clear last 3 bits
buf |= 0x02; // set as LDO
} else {
buf |= 0x07; // set as floating
}
Write1Byte(0x90, buf);
}
// Default is VOLTAGE_4200MV
void AXP192::SetChargeVoltage(uint8_t voltage) {
uint8_t buf = Read8bit(0x33);
buf = (buf & ~(0x60)) | (voltage & 0x60);
Write1Byte(0x33, buf);
}
// Not recommend to set charge current > 100mA, since Battery is only 80mAh.
// more then 1C charge-rate may shorten battery life-span.
void AXP192::SetChargeCurrent(uint8_t current) {
uint8_t buf = Read8bit(0x33);
buf = (buf & 0xf0) | (current & 0x07);
Write1Byte(0x33, buf);
}
// Set power off voltage
void AXP192::SetVOff(uint8_t voltage) {
Write1Byte(0x31, (Read8bit(0x31) & 0xf8) | voltage);
}
// Cut all power, except for LDO1 (RTC)
void AXP192::PowerOff() {
Write1Byte(0x32, Read8bit(0x32) | 0x80); // MSB for Power Off
}
void AXP192::SetAdcState(bool state) {
Write1Byte(0x82, state ? 0xff : 0x00); // Enable / Disable all ADCs
}
void AXP192::DisableAllIRQ() {
Write1Byte(0x40, 0x00);
Write1Byte(0x41, 0x00);
Write1Byte(0x42, 0x00);
Write1Byte(0x43, 0x00);
Write1Byte(0x4a, 0x00);
}
void AXP192::EnablePressIRQ(bool short_press, bool long_press) {
uint8_t value = Read8bit(0x42);
value &= 0xfc;
value |= short_press ? (0x01 << 1) : 0x00;
value |= long_press ? (0x01 << 0) : 0x00;
Write1Byte(0x42, value);
}
void AXP192::ClearAllIRQ() {
Write1Byte(0x44, 0xff);
Write1Byte(0x45, 0xff);
Write1Byte(0x46, 0xff);
Write1Byte(0x47, 0xff);
Write1Byte(0x4D, 0xff);
}
void AXP192::GetPressIRQ(bool *short_press, bool *long_press) {
uint8_t status = 0x00;
status = Read8bit(0x46);
*short_press = (status & (0x01 << 1)) ? true : false;
*long_press = (status & (0x01 << 0)) ? true : false;
}
void AXP192::ClearPressIRQ(bool short_press, bool long_press) {
uint8_t value = 0x00;
value |= short_press ? (0x01 << 1) : 0x00;
value |= long_press ? (0x01 << 0) : 0x00;
Write1Byte(0x46, value);
}
void AXP192::SetAdcRate(uint8_t rate) {
uint8_t buf = Read8bit(0x84);
buf = (buf & ~(0xc0)) | (rate & 0xc0);
Write1Byte(0x84, buf);
}
// AXP192 have a 6 byte storage, when the power is still valid, the data will
// not be lost
void AXP192::Read6BytesStorage(uint8_t *bufPtr) {
// Address from 0x06 - 0x0B
Wire1.beginTransmission(0x34);
Wire1.write(0x06);
Wire1.endTransmission();
Wire1.requestFrom(0x34, 6);
for (int i = 0; i < 6; ++i) {
bufPtr[i] = Wire1.read();
}
}
// AXP192 have a 6 byte storage, when the power is still valid, the data will
// not be lost
void AXP192::Write6BytesStorage(uint8_t *bufPtr) {
// Address from 0x06 - 0x0B
Wire1.beginTransmission(0x34);
Wire1.write(0x06);
Wire1.write(bufPtr[0]);
Wire1.write(0x07);
Wire1.write(bufPtr[1]);
Wire1.write(0x08);
Wire1.write(bufPtr[2]);
Wire1.write(0x09);
Wire1.write(bufPtr[3]);
Wire1.write(0x0A);
Wire1.write(bufPtr[4]);
Wire1.write(0x0B);
Wire1.write(bufPtr[5]);
Wire1.endTransmission();
}
void AXP192::SetPeripherialsPower(uint8_t state) {
if (!state)
Write1Byte(0x10, Read8bit(0x10) & 0XFB);
else if (state)
Write1Byte(0x10, Read8bit(0x10) | 0X04);
// uint8_t data;
// Set EXTEN to enable 5v boost
}