forked from markqvist/RNode_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Power.h
452 lines (386 loc) · 14.7 KB
/
Power.h
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
#if BOARD_MODEL == BOARD_TBEAM
#include <XPowersLib.h>
XPowersLibInterface* PMU = NULL;
#ifndef PMU_WIRE_PORT
#define PMU_WIRE_PORT Wire
#endif
#define BAT_V_MIN 3.15
#define BAT_V_MAX 4.14
void disablePeripherals() {
if (PMU) {
// GNSS RTC PowerVDD
PMU->enablePowerOutput(XPOWERS_VBACKUP);
// LoRa VDD
PMU->disablePowerOutput(XPOWERS_ALDO2);
// GNSS VDD
PMU->disablePowerOutput(XPOWERS_ALDO3);
}
}
bool pmuInterrupt;
void setPmuFlag()
{
pmuInterrupt = true;
}
#elif BOARD_MODEL == BOARD_RNODE_NG_21 || BOARD_MODEL == BOARD_LORA32_V2_1
#define BAT_C_SAMPLES 7
#define BAT_D_SAMPLES 2
#define BAT_V_MIN 3.15
#define BAT_V_MAX 4.3
#define BAT_V_CHG 4.48
#define BAT_V_FLOAT 4.33
#define BAT_SAMPLES 5
const uint8_t pin_vbat = 35;
float bat_p_samples[BAT_SAMPLES];
float bat_v_samples[BAT_SAMPLES];
uint8_t bat_samples_count = 0;
int bat_discharging_samples = 0;
int bat_charging_samples = 0;
int bat_charged_samples = 0;
bool bat_voltage_dropping = false;
float bat_delay_v = 0;
#elif BOARD_MODEL == BOARD_RAK4631
#include "nrfx_power.h"
#define BAT_C_SAMPLES 7
#define BAT_D_SAMPLES 2
#define BAT_V_MIN 2.75
#define BAT_V_MAX 4.2
#define BAT_V_FLOAT 4.22
#define BAT_SAMPLES 5
#define VBAT_MV_PER_LSB (0.73242188F) // 3.0V ADC range and 12 - bit ADC resolution = 3000mV / 4096
#define VBAT_DIVIDER_COMP (1.73) // Compensation factor for the VBAT divider
#define VBAT_MV_PER_LSB_FIN (VBAT_DIVIDER_COMP * VBAT_MV_PER_LSB)
#define PIN_VBAT WB_A0
float bat_p_samples[BAT_SAMPLES];
float bat_v_samples[BAT_SAMPLES];
uint8_t bat_samples_count = 0;
int bat_discharging_samples = 0;
int bat_charging_samples = 0;
int bat_charged_samples = 0;
bool bat_voltage_dropping = false;
float bat_delay_v = 0;
#endif
uint32_t last_pmu_update = 0;
uint8_t pmu_target_pps = 1;
int pmu_update_interval = 1000/pmu_target_pps;
uint8_t pmu_rc = 0;
#define PMU_R_INTERVAL 5
void kiss_indicate_battery();
void measure_battery() {
#if BOARD_MODEL == BOARD_RNODE_NG_21 || BOARD_MODEL == BOARD_LORA32_V2_1
battery_installed = true;
battery_indeterminate = true;
bat_v_samples[bat_samples_count%BAT_SAMPLES] = (float)(analogRead(pin_vbat)) / 4095*2*3.3*1.1;
bat_p_samples[bat_samples_count%BAT_SAMPLES] = ((battery_voltage-BAT_V_MIN) / (BAT_V_MAX-BAT_V_MIN))*100.0;
bat_samples_count++;
if (!battery_ready && bat_samples_count >= BAT_SAMPLES) {
battery_ready = true;
}
if (battery_ready) {
battery_percent = 0;
for (uint8_t bi = 0; bi < BAT_SAMPLES; bi++) {
battery_percent += bat_p_samples[bi];
}
battery_percent = battery_percent/BAT_SAMPLES;
battery_voltage = 0;
for (uint8_t bi = 0; bi < BAT_SAMPLES; bi++) {
battery_voltage += bat_v_samples[bi];
}
battery_voltage = battery_voltage/BAT_SAMPLES;
if (bat_delay_v == 0) bat_delay_v = battery_voltage;
if (battery_percent > 100.0) battery_percent = 100.0;
if (battery_percent < 0.0) battery_percent = 0.0;
if (bat_samples_count%BAT_SAMPLES == 0) {
if (battery_voltage < bat_delay_v && battery_voltage < BAT_V_FLOAT) {
bat_voltage_dropping = true;
} else {
bat_voltage_dropping = false;
}
bat_samples_count = 0;
}
if (bat_voltage_dropping && battery_voltage < BAT_V_FLOAT) {
battery_state = BATTERY_STATE_DISCHARGING;
} else {
#if BOARD_MODEL == BOARD_RNODE_NG_21
battery_state = BATTERY_STATE_CHARGING;
#else
battery_state = BATTERY_STATE_DISCHARGING;
#endif
}
// if (bt_state == BT_STATE_CONNECTED) {
// SerialBT.printf("Bus voltage %.3fv. Unfiltered %.3fv.", battery_voltage, bat_v_samples[BAT_SAMPLES-1]);
// if (bat_voltage_dropping) {
// SerialBT.printf(" Voltage is dropping. Percentage %.1f%%.\n", battery_percent);
// } else {
// SerialBT.print(" Voltage is not dropping.\n");
// }
// }
}
#elif BOARD_MODEL == BOARD_TBEAM
if (PMU) {
float discharge_current = 0;
float charge_current = 0;
float ext_voltage = 0;
float ext_current = 0;
if (PMU->getChipModel() == XPOWERS_AXP192) {
discharge_current = ((XPowersAXP192*)PMU)->getBattDischargeCurrent();
charge_current = ((XPowersAXP192*)PMU)->getBatteryChargeCurrent();
battery_voltage = PMU->getBattVoltage()/1000.0;
// battery_percent = PMU->getBattPercentage()*1.0;
battery_installed = PMU->isBatteryConnect();
external_power = PMU->isVbusIn();
ext_voltage = PMU->getVbusVoltage()/1000.0;
ext_current = ((XPowersAXP192*)PMU)->getVbusCurrent();
}
else if (PMU->getChipModel() == XPOWERS_AXP2101) {
battery_voltage = PMU->getBattVoltage()/1000.0;
// battery_percent = PMU->getBattPercentage()*1.0;
battery_installed = PMU->isBatteryConnect();
external_power = PMU->isVbusIn();
ext_voltage = PMU->getVbusVoltage()/1000.0;
}
if (battery_installed) {
if (PMU->isCharging()) {
battery_state = BATTERY_STATE_CHARGING;
battery_percent = ((battery_voltage-BAT_V_MIN) / (BAT_V_MAX-BAT_V_MIN))*100.0;
} else {
if (PMU->isDischarge()) {
battery_state = BATTERY_STATE_DISCHARGING;
battery_percent = ((battery_voltage-BAT_V_MIN) / (BAT_V_MAX-BAT_V_MIN))*100.0;
} else {
battery_state = BATTERY_STATE_CHARGED;
battery_percent = 100.0;
}
}
} else {
battery_state = BATTERY_STATE_DISCHARGING;
battery_percent = 0.0;
battery_voltage = 0.0;
}
if (battery_percent > 100.0) battery_percent = 100.0;
if (battery_percent < 0.0) battery_percent = 0.0;
float charge_watts = battery_voltage*(charge_current/1000.0);
float discharge_watts = battery_voltage*(discharge_current/1000.0);
float ext_watts = ext_voltage*(ext_current/1000.0);
battery_ready = true;
// if (bt_state == BT_STATE_CONNECTED) {
// if (battery_installed) {
// if (external_power) {
// SerialBT.printf("External power connected, drawing %.2fw, %.1fmA at %.1fV\n", ext_watts, ext_current, ext_voltage);
// } else {
// SerialBT.println("Running on battery");
// }
// SerialBT.printf("Battery percentage %.1f%%\n", battery_percent);
// SerialBT.printf("Battery voltage %.2fv\n", battery_voltage);
// // SerialBT.printf("Temperature %.1f%\n", auxillary_temperature);
// if (battery_state == BATTERY_STATE_CHARGING) {
// SerialBT.printf("Charging with %.2fw, %.1fmA at %.1fV\n", charge_watts, charge_current, battery_voltage);
// } else if (battery_state == BATTERY_STATE_DISCHARGING) {
// SerialBT.printf("Discharging at %.2fw, %.1fmA at %.1fV\n", discharge_watts, discharge_current, battery_voltage);
// } else if (battery_state == BATTERY_STATE_CHARGED) {
// SerialBT.printf("Battery charged\n");
// }
// } else {
// SerialBT.println("No battery installed");
// }
// SerialBT.println("");
// }
}
else {
battery_ready = false;
}
#elif BOARD_MODEL == BOARD_RAK4631
battery_installed = true;
battery_indeterminate = false;
bat_v_samples[bat_samples_count%BAT_SAMPLES] = (float)(analogRead(PIN_VBAT)) * VBAT_MV_PER_LSB_FIN;
if (bat_v_samples[bat_samples_count%BAT_SAMPLES] < 3300) {
bat_p_samples[bat_samples_count%BAT_SAMPLES] = 0;
}
else if (bat_v_samples[bat_samples_count%BAT_SAMPLES] < 3600)
{
bat_v_samples[bat_samples_count%BAT_SAMPLES] -= 3300;
bat_p_samples[bat_samples_count%BAT_SAMPLES] = bat_v_samples[bat_samples_count%BAT_SAMPLES] / 30;
} else {
bat_v_samples[bat_samples_count%BAT_SAMPLES] -= 3600;
}
bat_p_samples[bat_samples_count%BAT_SAMPLES] = 10 + (bat_v_samples[bat_samples_count%BAT_SAMPLES] * 0.15F);
bat_samples_count++;
if (!battery_ready && bat_samples_count >= BAT_SAMPLES) {
battery_ready = true;
}
battery_percent = 0;
for (uint8_t bi = 0; bi < BAT_SAMPLES; bi++) {
battery_percent += bat_p_samples[bi];
}
battery_percent = battery_percent/BAT_SAMPLES;
battery_voltage = 0;
for (uint8_t bi = 0; bi < BAT_SAMPLES; bi++) {
battery_voltage += bat_v_samples[bi];
}
battery_voltage = battery_voltage/BAT_SAMPLES;
if (bat_delay_v == 0) bat_delay_v = battery_voltage;
if (battery_percent > 100.0) battery_percent = 100.0;
if (battery_percent < 0.0) battery_percent = 0.0;
if (bat_samples_count%BAT_SAMPLES == 0) {
if (battery_voltage < bat_delay_v && battery_voltage < BAT_V_FLOAT) {
bat_voltage_dropping = true;
} else {
bat_voltage_dropping = false;
}
bat_samples_count = 0;
}
nrfx_power_usb_state_t usbstate = nrfx_power_usbstatus_get();
if (usbstate == NRFX_POWER_USB_STATE_CONNECTED || usbstate == NRFX_POWER_USB_STATE_READY) {
// charging
battery_state = BATTERY_STATE_CHARGING;
} else {
battery_state = BATTERY_STATE_DISCHARGING;
}
if (battery_percent >= 98) {
battery_state = BATTERY_STATE_CHARGED;
}
#if HAS_BLE
if ((bt_state == BT_STATE_ON) || bt_state == BT_STATE_CONNECTED) {
if (battery_state != BATTERY_STATE_CHARGING) {
blebas.write(battery_percent);
} else {
blebas.write(100);
}
}
#endif
#endif
if (battery_ready) {
pmu_rc++;
if (pmu_rc%PMU_R_INTERVAL == 0) {
kiss_indicate_battery();
}
}
}
void update_pmu() {
if (millis()-last_pmu_update >= pmu_update_interval) {
measure_battery();
last_pmu_update = millis();
}
}
bool init_pmu() {
#if BOARD_MODEL == BOARD_RNODE_NG_21 || BOARD_MODEL == BOARD_LORA32_V2_1
pinMode(pin_vbat, INPUT);
return true;
#elif BOARD_MODEL == BOARD_TBEAM
Wire.begin(I2C_SDA, I2C_SCL);
if (!PMU) {
PMU = new XPowersAXP2101(PMU_WIRE_PORT);
if (!PMU->init()) {
Serial.println("Warning: Failed to find AXP2101 power management");
delete PMU;
PMU = NULL;
} else {
Serial.println("AXP2101 PMU init succeeded, using AXP2101 PMU");
}
}
if (!PMU) {
PMU = new XPowersAXP192(PMU_WIRE_PORT);
if (!PMU->init()) {
Serial.println("Warning: Failed to find AXP192 power management");
delete PMU;
PMU = NULL;
} else {
Serial.println("AXP192 PMU init succeeded, using AXP192 PMU");
}
}
if (!PMU) {
return false;
}
// Configure charging indicator
PMU->setChargingLedMode(XPOWERS_CHG_LED_OFF);
pinMode(PMU_IRQ, INPUT_PULLUP);
attachInterrupt(PMU_IRQ, setPmuFlag, FALLING);
if (PMU->getChipModel() == XPOWERS_AXP192) {
// Turn off unused power sources to save power
PMU->disablePowerOutput(XPOWERS_DCDC1);
PMU->disablePowerOutput(XPOWERS_DCDC2);
PMU->disablePowerOutput(XPOWERS_LDO2);
PMU->disablePowerOutput(XPOWERS_LDO3);
// Set the power of LoRa and GPS module to 3.3V
// LoRa
PMU->setPowerChannelVoltage(XPOWERS_LDO2, 3300);
// GPS
PMU->setPowerChannelVoltage(XPOWERS_LDO3, 3300);
// OLED
PMU->setPowerChannelVoltage(XPOWERS_DCDC1, 3300);
// Turn on LoRa
PMU->enablePowerOutput(XPOWERS_LDO2);
// Turn on GPS
//PMU->enablePowerOutput(XPOWERS_LDO3);
// protected oled power source
PMU->setProtectedChannel(XPOWERS_DCDC1);
// protected esp32 power source
PMU->setProtectedChannel(XPOWERS_DCDC3);
// enable oled power
PMU->enablePowerOutput(XPOWERS_DCDC1);
PMU->disableIRQ(XPOWERS_AXP192_ALL_IRQ);
PMU->enableIRQ(XPOWERS_AXP192_VBUS_REMOVE_IRQ |
XPOWERS_AXP192_VBUS_INSERT_IRQ |
XPOWERS_AXP192_BAT_CHG_DONE_IRQ |
XPOWERS_AXP192_BAT_CHG_START_IRQ |
XPOWERS_AXP192_BAT_REMOVE_IRQ |
XPOWERS_AXP192_BAT_INSERT_IRQ |
XPOWERS_AXP192_PKEY_SHORT_IRQ
);
}
else if (PMU->getChipModel() == XPOWERS_AXP2101) {
// Turn off unused power sources to save power
PMU->disablePowerOutput(XPOWERS_DCDC2);
PMU->disablePowerOutput(XPOWERS_DCDC3);
PMU->disablePowerOutput(XPOWERS_DCDC4);
PMU->disablePowerOutput(XPOWERS_DCDC5);
PMU->disablePowerOutput(XPOWERS_ALDO1);
PMU->disablePowerOutput(XPOWERS_ALDO2);
PMU->disablePowerOutput(XPOWERS_ALDO3);
PMU->disablePowerOutput(XPOWERS_ALDO4);
PMU->disablePowerOutput(XPOWERS_BLDO1);
PMU->disablePowerOutput(XPOWERS_BLDO2);
PMU->disablePowerOutput(XPOWERS_DLDO1);
PMU->disablePowerOutput(XPOWERS_DLDO2);
PMU->disablePowerOutput(XPOWERS_VBACKUP);
// Set the power of LoRa and GPS module to 3.3V
// LoRa
PMU->setPowerChannelVoltage(XPOWERS_ALDO2, 3300);
// GPS
PMU->setPowerChannelVoltage(XPOWERS_ALDO3, 3300);
PMU->setPowerChannelVoltage(XPOWERS_VBACKUP, 3300);
// ESP32 VDD
// ! No need to set, automatically open , Don't close it
// PMU->setPowerChannelVoltage(XPOWERS_DCDC1, 3300);
// PMU->setProtectedChannel(XPOWERS_DCDC1);
PMU->setProtectedChannel(XPOWERS_DCDC1);
// LoRa VDD
PMU->enablePowerOutput(XPOWERS_ALDO2);
// GNSS VDD
//PMU->enablePowerOutput(XPOWERS_ALDO3);
// GNSS RTC PowerVDD
//PMU->enablePowerOutput(XPOWERS_VBACKUP);
}
PMU->enableSystemVoltageMeasure();
PMU->enableVbusVoltageMeasure();
PMU->enableBattVoltageMeasure();
// It is necessary to disable the detection function of the TS pin on the board
// without the battery temperature detection function, otherwise it will cause abnormal charging
PMU->disableTSPinMeasure();
// Set the time of pressing the button to turn off
PMU->setPowerKeyPressOffTime(XPOWERS_POWEROFF_4S);
return true;
#elif BOARD_MODEL == BOARD_RAK4631 || BOARD_MODEL == BOARD_FREENODE
// board doesn't have PMU but we can measure batt voltage
// prep ADC for reading battery level
analogReference(AR_INTERNAL_3_0);
// Set the resolution to 12-bit (0..4095)
analogReadResolution(12);
// Let the ADC settle
delay(1);
// Get a single ADC sample and throw it away
float raw = analogRead(PIN_VBAT);
return true;
#else
return false;
#endif
}