-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathfuri_hal_power.c
488 lines (403 loc) · 15.1 KB
/
furi_hal_power.c
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
#include <furi_hal_power.h>
#include <furi_hal_clock.h>
#include <furi_hal_bt.h>
#include <furi_hal_resources.h>
#include <stm32wbxx_ll_rcc.h>
#include <stm32wbxx_ll_pwr.h>
#include <stm32wbxx_ll_hsem.h>
#include <stm32wbxx_ll_cortex.h>
#include <stm32wbxx_ll_gpio.h>
#include <hw_conf.h>
#include <bq27220.h>
#include <bq25896.h>
#include <furi.h>
#define TAG "FuriHalPower"
typedef struct {
volatile uint8_t insomnia;
volatile uint8_t deep_insomnia;
volatile uint8_t suppress_charge;
uint8_t gauge_initialized;
uint8_t charger_initialized;
} FuriHalPower;
static volatile FuriHalPower furi_hal_power = {
.insomnia = 0,
.deep_insomnia = 1,
.suppress_charge = 0,
};
const ParamCEDV cedv = {
.cedv_conf.gauge_conf =
{
.CCT = 1,
.CSYNC = 0,
.EDV_CMP = 0,
.SC = 1,
.FIXED_EDV0 = 1,
.FCC_LIM = 1,
.FC_FOR_VDQ = 1,
.IGNORE_SD = 1,
.SME0 = 0,
},
.full_charge_cap = 2101,
.design_cap = 2101,
.EDV0 = 3300,
.EDV1 = 3321,
.EDV2 = 3355,
.EMF = 3679,
.C0 = 430,
.C1 = 0,
.R1 = 408,
.R0 = 334,
.T0 = 4626,
.TC = 11,
.DOD0 = 4044,
.DOD10 = 3905,
.DOD20 = 3807,
.DOD30 = 3718,
.DOD40 = 3642,
.DOD50 = 3585,
.DOD60 = 3546,
.DOD70 = 3514,
.DOD80 = 3477,
.DOD90 = 3411,
.DOD100 = 3299,
};
void HAL_RCC_CSSCallback(void) {
// TODO: notify user about issue with HSE
furi_hal_power_reset();
}
void furi_hal_power_init() {
LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN);
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
bq27220_init(&furi_hal_i2c_handle_power, &cedv);
bq25896_init(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
FURI_LOG_I(TAG, "Init OK");
}
bool furi_hal_power_gauge_is_ok() {
bool ret = true;
BatteryStatus battery_status;
OperationStatus operation_status;
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
if(bq27220_get_battery_status(&furi_hal_i2c_handle_power, &battery_status) == BQ27220_ERROR ||
bq27220_get_operation_status(&furi_hal_i2c_handle_power, &operation_status) ==
BQ27220_ERROR) {
ret = false;
} else {
ret &= battery_status.BATTPRES;
ret &= operation_status.INITCOMP;
ret &= (cedv.design_cap == bq27220_get_design_capacity(&furi_hal_i2c_handle_power));
}
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
uint16_t furi_hal_power_insomnia_level() {
return furi_hal_power.insomnia;
}
void furi_hal_power_insomnia_enter() {
FURI_CRITICAL_ENTER();
furi_assert(furi_hal_power.insomnia < UINT8_MAX);
furi_hal_power.insomnia++;
FURI_CRITICAL_EXIT();
}
void furi_hal_power_insomnia_exit() {
FURI_CRITICAL_ENTER();
furi_assert(furi_hal_power.insomnia > 0);
furi_hal_power.insomnia--;
FURI_CRITICAL_EXIT();
}
bool furi_hal_power_sleep_available() {
return furi_hal_power.insomnia == 0;
}
bool furi_hal_power_deep_sleep_available() {
return furi_hal_bt_is_alive() && furi_hal_power.deep_insomnia == 0;
}
void furi_hal_power_light_sleep() {
__WFI();
}
void furi_hal_power_deep_sleep() {
while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
;
if(!LL_HSEM_1StepLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID)) {
if(LL_PWR_IsActiveFlag_C2DS()) {
// Release ENTRY_STOP_MODE semaphore
LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
// The switch on HSI before entering Stop Mode is required
furi_hal_clock_switch_to_hsi();
}
} else {
/**
* The switch on HSI before entering Stop Mode is required
*/
furi_hal_clock_switch_to_hsi();
}
/* Release RCC semaphore */
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
// Prepare deep sleep
LL_PWR_SetPowerMode(LL_PWR_MODE_STOP1);
LL_LPM_EnableDeepSleep();
#if defined(__CC_ARM)
// Force store operations
__force_stores();
#endif
__WFI();
/* Release ENTRY_STOP_MODE semaphore */
LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
;
if(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {
furi_hal_clock_switch_to_pll();
}
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
}
void furi_hal_power_sleep() {
if(furi_hal_power_deep_sleep_available()) {
furi_hal_power_deep_sleep();
} else {
furi_hal_power_light_sleep();
}
}
uint8_t furi_hal_power_get_pct() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
uint8_t ret = bq27220_get_state_of_charge(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
uint8_t furi_hal_power_get_bat_health_pct() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
uint8_t ret = bq27220_get_state_of_health(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
bool furi_hal_power_is_charging() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
bool ret = bq25896_is_charging(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
void furi_hal_power_off() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
bq25896_poweroff(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
}
void furi_hal_power_reset() {
NVIC_SystemReset();
}
void furi_hal_power_enable_otg() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
bq25896_enable_otg(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
}
void furi_hal_power_disable_otg() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
bq25896_disable_otg(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
}
bool furi_hal_power_is_otg_enabled() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
bool ret = bq25896_is_otg_enabled(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
void furi_hal_power_check_otg_status() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
if(bq25896_check_otg_fault(&furi_hal_i2c_handle_power))
bq25896_disable_otg(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
}
uint32_t furi_hal_power_get_battery_remaining_capacity() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
uint32_t ret = bq27220_get_remaining_capacity(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
uint32_t furi_hal_power_get_battery_full_capacity() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
uint32_t ret = bq27220_get_full_charge_capacity(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
uint32_t furi_hal_power_get_battery_design_capacity() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
uint32_t ret = bq27220_get_design_capacity(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
float furi_hal_power_get_battery_voltage(FuriHalPowerIC ic) {
float ret = 0.0f;
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
if(ic == FuriHalPowerICCharger) {
ret = (float)bq25896_get_vbat_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
} else if(ic == FuriHalPowerICFuelGauge) {
ret = (float)bq27220_get_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
}
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
float furi_hal_power_get_battery_current(FuriHalPowerIC ic) {
float ret = 0.0f;
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
if(ic == FuriHalPowerICCharger) {
ret = (float)bq25896_get_vbat_current(&furi_hal_i2c_handle_power) / 1000.0f;
} else if(ic == FuriHalPowerICFuelGauge) {
ret = (float)bq27220_get_current(&furi_hal_i2c_handle_power) / 1000.0f;
}
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
static float furi_hal_power_get_battery_temperature_internal(FuriHalPowerIC ic) {
float ret = 0.0f;
if(ic == FuriHalPowerICCharger) {
// Linear approximation, +/- 5 C
ret = (71.0f - (float)bq25896_get_ntc_mpct(&furi_hal_i2c_handle_power) / 1000) / 0.6f;
} else if(ic == FuriHalPowerICFuelGauge) {
ret = ((float)bq27220_get_temperature(&furi_hal_i2c_handle_power) - 2731.0f) / 10.0f;
}
return ret;
}
float furi_hal_power_get_battery_temperature(FuriHalPowerIC ic) {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
float ret = furi_hal_power_get_battery_temperature_internal(ic);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
float furi_hal_power_get_usb_voltage() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
float ret = (float)bq25896_get_vbus_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}
void furi_hal_power_dump_state() {
BatteryStatus battery_status;
OperationStatus operation_status;
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
if(bq27220_get_battery_status(&furi_hal_i2c_handle_power, &battery_status) == BQ27220_ERROR ||
bq27220_get_operation_status(&furi_hal_i2c_handle_power, &operation_status) ==
BQ27220_ERROR) {
printf("Failed to get bq27220 status. Communication error.\r\n");
} else {
// Operation status register
printf(
"bq27220: CALMD: %d, SEC: %d, EDV2: %d, VDQ: %d, INITCOMP: %d, SMTH: %d, BTPINT: %d, CFGUPDATE: %d\r\n",
operation_status.CALMD,
operation_status.SEC,
operation_status.EDV2,
operation_status.VDQ,
operation_status.INITCOMP,
operation_status.SMTH,
operation_status.BTPINT,
operation_status.CFGUPDATE);
// Battery status register, part 1
printf(
"bq27220: CHGINH: %d, FC: %d, OTD: %d, OTC: %d, SLEEP: %d, OCVFAIL: %d, OCVCOMP: %d, FD: %d\r\n",
battery_status.CHGINH,
battery_status.FC,
battery_status.OTD,
battery_status.OTC,
battery_status.SLEEP,
battery_status.OCVFAIL,
battery_status.OCVCOMP,
battery_status.FD);
// Battery status register, part 2
printf(
"bq27220: DSG: %d, SYSDWN: %d, TDA: %d, BATTPRES: %d, AUTH_GD: %d, OCVGD: %d, TCA: %d, RSVD: %d\r\n",
battery_status.DSG,
battery_status.SYSDWN,
battery_status.TDA,
battery_status.BATTPRES,
battery_status.AUTH_GD,
battery_status.OCVGD,
battery_status.TCA,
battery_status.RSVD);
// Voltage and current info
printf(
"bq27220: Full capacity: %dmAh, Design capacity: %dmAh, Remaining capacity: %dmAh, State of Charge: %d%%, State of health: %d%%\r\n",
bq27220_get_full_charge_capacity(&furi_hal_i2c_handle_power),
bq27220_get_design_capacity(&furi_hal_i2c_handle_power),
bq27220_get_remaining_capacity(&furi_hal_i2c_handle_power),
bq27220_get_state_of_charge(&furi_hal_i2c_handle_power),
bq27220_get_state_of_health(&furi_hal_i2c_handle_power));
printf(
"bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n",
bq27220_get_voltage(&furi_hal_i2c_handle_power),
bq27220_get_current(&furi_hal_i2c_handle_power),
(int)furi_hal_power_get_battery_temperature_internal(FuriHalPowerICFuelGauge));
}
printf(
"bq25896: VBUS: %d, VSYS: %d, VBAT: %d, Current: %d, NTC: %ldm%%\r\n",
bq25896_get_vbus_voltage(&furi_hal_i2c_handle_power),
bq25896_get_vsys_voltage(&furi_hal_i2c_handle_power),
bq25896_get_vbat_voltage(&furi_hal_i2c_handle_power),
bq25896_get_vbat_current(&furi_hal_i2c_handle_power),
bq25896_get_ntc_mpct(&furi_hal_i2c_handle_power));
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
}
void furi_hal_power_enable_external_3_3v() {
LL_GPIO_SetOutputPin(PERIPH_POWER_GPIO_Port, PERIPH_POWER_Pin);
}
void furi_hal_power_disable_external_3_3v() {
LL_GPIO_ResetOutputPin(PERIPH_POWER_GPIO_Port, PERIPH_POWER_Pin);
}
void furi_hal_power_suppress_charge_enter() {
vTaskSuspendAll();
bool disable_charging = furi_hal_power.suppress_charge == 0;
furi_hal_power.suppress_charge++;
xTaskResumeAll();
if(disable_charging) {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
bq25896_disable_charging(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
}
}
void furi_hal_power_suppress_charge_exit() {
vTaskSuspendAll();
furi_hal_power.suppress_charge--;
bool enable_charging = furi_hal_power.suppress_charge == 0;
xTaskResumeAll();
if(enable_charging) {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
bq25896_enable_charging(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
}
}
void furi_hal_power_info_get(FuriHalPowerInfoCallback out, void* context) {
furi_assert(out);
string_t value;
string_init(value);
// Power Info version
out("power_info_major", "1", false, context);
out("power_info_minor", "0", false, context);
uint8_t charge = furi_hal_power_get_pct();
string_printf(value, "%u", charge);
out("charge_level", string_get_cstr(value), false, context);
if(furi_hal_power_is_charging()) {
if(charge < 100) {
string_printf(value, "charging");
} else {
string_printf(value, "charged");
}
} else {
string_printf(value, "discharging");
}
out("charge_state", string_get_cstr(value), false, context);
uint16_t voltage =
(uint16_t)(furi_hal_power_get_battery_voltage(FuriHalPowerICFuelGauge) * 1000.f);
string_printf(value, "%u", voltage);
out("battery_voltage", string_get_cstr(value), false, context);
int16_t current =
(int16_t)(furi_hal_power_get_battery_current(FuriHalPowerICFuelGauge) * 1000.f);
string_printf(value, "%d", current);
out("battery_current", string_get_cstr(value), false, context);
int16_t temperature = (int16_t)furi_hal_power_get_battery_temperature(FuriHalPowerICFuelGauge);
string_printf(value, "%d", temperature);
out("gauge_temp", string_get_cstr(value), false, context);
string_printf(value, "%u", furi_hal_power_get_bat_health_pct());
out("battery_health", string_get_cstr(value), false, context);
string_printf(value, "%u", furi_hal_power_get_battery_remaining_capacity());
out("capacity_remain", string_get_cstr(value), false, context);
string_printf(value, "%u", furi_hal_power_get_battery_full_capacity());
out("capacity_full", string_get_cstr(value), false, context);
string_printf(value, "%u", furi_hal_power_get_battery_design_capacity());
out("capacity_design", string_get_cstr(value), true, context);
string_clear(value);
}