furi_hal_power.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. #include <furi_hal_power.h>
  2. #include <furi_hal_clock.h>
  3. #include <furi_hal_bt.h>
  4. #include <furi_hal_resources.h>
  5. #include <furi_hal_uart.h>
  6. #include <stm32wbxx_ll_rcc.h>
  7. #include <stm32wbxx_ll_pwr.h>
  8. #include <stm32wbxx_ll_hsem.h>
  9. #include <stm32wbxx_ll_cortex.h>
  10. #include <stm32wbxx_ll_gpio.h>
  11. #include <hw_conf.h>
  12. #include <bq27220.h>
  13. #include <bq25896.h>
  14. #include <furi.h>
  15. #define TAG "FuriHalPower"
  16. #ifdef FURI_HAL_POWER_DEEP_SLEEP_ENABLED
  17. #define FURI_HAL_POWER_DEEP_INSOMNIA 0
  18. #else
  19. #define FURI_HAL_POWER_DEEP_INSOMNIA 1
  20. #endif
  21. typedef struct {
  22. volatile uint8_t insomnia;
  23. volatile uint8_t deep_insomnia;
  24. volatile uint8_t suppress_charge;
  25. uint8_t gauge_initialized;
  26. uint8_t charger_initialized;
  27. } FuriHalPower;
  28. static volatile FuriHalPower furi_hal_power = {
  29. .insomnia = 0,
  30. .deep_insomnia = FURI_HAL_POWER_DEEP_INSOMNIA,
  31. .suppress_charge = 0,
  32. };
  33. const ParamCEDV cedv = {
  34. .cedv_conf.gauge_conf =
  35. {
  36. .CCT = 1,
  37. .CSYNC = 0,
  38. .EDV_CMP = 0,
  39. .SC = 1,
  40. .FIXED_EDV0 = 1,
  41. .FCC_LIM = 1,
  42. .FC_FOR_VDQ = 1,
  43. .IGNORE_SD = 1,
  44. .SME0 = 0,
  45. },
  46. .full_charge_cap = 2101,
  47. .design_cap = 2101,
  48. .EDV0 = 3300,
  49. .EDV1 = 3321,
  50. .EDV2 = 3355,
  51. .EMF = 3679,
  52. .C0 = 430,
  53. .C1 = 0,
  54. .R1 = 408,
  55. .R0 = 334,
  56. .T0 = 4626,
  57. .TC = 11,
  58. .DOD0 = 4044,
  59. .DOD10 = 3905,
  60. .DOD20 = 3807,
  61. .DOD30 = 3718,
  62. .DOD40 = 3642,
  63. .DOD50 = 3585,
  64. .DOD60 = 3546,
  65. .DOD70 = 3514,
  66. .DOD80 = 3477,
  67. .DOD90 = 3411,
  68. .DOD100 = 3299,
  69. };
  70. void furi_hal_power_init() {
  71. LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
  72. LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN);
  73. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  74. bq27220_init(&furi_hal_i2c_handle_power, &cedv);
  75. bq25896_init(&furi_hal_i2c_handle_power);
  76. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  77. #ifdef FURI_HAL_OS_DEBUG
  78. furi_hal_gpio_init_simple(&gpio_ext_pb2, GpioModeOutputPushPull);
  79. furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeOutputPushPull);
  80. #endif
  81. FURI_LOG_I(TAG, "Init OK");
  82. }
  83. bool furi_hal_power_gauge_is_ok() {
  84. bool ret = true;
  85. BatteryStatus battery_status;
  86. OperationStatus operation_status;
  87. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  88. if(bq27220_get_battery_status(&furi_hal_i2c_handle_power, &battery_status) == BQ27220_ERROR ||
  89. bq27220_get_operation_status(&furi_hal_i2c_handle_power, &operation_status) ==
  90. BQ27220_ERROR) {
  91. ret = false;
  92. } else {
  93. ret &= battery_status.BATTPRES;
  94. ret &= operation_status.INITCOMP;
  95. ret &= (cedv.design_cap == bq27220_get_design_capacity(&furi_hal_i2c_handle_power));
  96. }
  97. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  98. return ret;
  99. }
  100. uint16_t furi_hal_power_insomnia_level() {
  101. return furi_hal_power.insomnia;
  102. }
  103. void furi_hal_power_insomnia_enter() {
  104. FURI_CRITICAL_ENTER();
  105. furi_assert(furi_hal_power.insomnia < UINT8_MAX);
  106. furi_hal_power.insomnia++;
  107. FURI_CRITICAL_EXIT();
  108. }
  109. void furi_hal_power_insomnia_exit() {
  110. FURI_CRITICAL_ENTER();
  111. furi_assert(furi_hal_power.insomnia > 0);
  112. furi_hal_power.insomnia--;
  113. FURI_CRITICAL_EXIT();
  114. }
  115. bool furi_hal_power_sleep_available() {
  116. return furi_hal_power.insomnia == 0;
  117. }
  118. bool furi_hal_power_deep_sleep_available() {
  119. return furi_hal_bt_is_alive() && furi_hal_power.deep_insomnia == 0;
  120. }
  121. void furi_hal_power_light_sleep() {
  122. __WFI();
  123. }
  124. static inline void furi_hal_power_suspend_aux_periphs() {
  125. // Disable USART
  126. furi_hal_uart_suspend(FuriHalUartIdUSART1);
  127. furi_hal_uart_suspend(FuriHalUartIdLPUART1);
  128. // TODO: Disable USB
  129. }
  130. static inline void furi_hal_power_resume_aux_periphs() {
  131. // Re-enable USART
  132. furi_hal_uart_resume(FuriHalUartIdUSART1);
  133. furi_hal_uart_resume(FuriHalUartIdLPUART1);
  134. // TODO: Re-enable USB
  135. }
  136. void furi_hal_power_deep_sleep() {
  137. furi_hal_power_suspend_aux_periphs();
  138. while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
  139. ;
  140. if(!LL_HSEM_1StepLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID)) {
  141. if(LL_PWR_IsActiveFlag_C2DS() || LL_PWR_IsActiveFlag_C2SB()) {
  142. // Release ENTRY_STOP_MODE semaphore
  143. LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
  144. // The switch on HSI before entering Stop Mode is required
  145. furi_hal_clock_switch_to_hsi();
  146. }
  147. } else {
  148. /**
  149. * The switch on HSI before entering Stop Mode is required
  150. */
  151. furi_hal_clock_switch_to_hsi();
  152. }
  153. /* Release RCC semaphore */
  154. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  155. // Prepare deep sleep
  156. LL_PWR_SetPowerMode(LL_PWR_MODE_STOP2);
  157. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_STOP2);
  158. LL_LPM_EnableDeepSleep();
  159. #if defined(__CC_ARM)
  160. // Force store operations
  161. __force_stores();
  162. #endif
  163. __WFI();
  164. LL_LPM_EnableSleep();
  165. // Make sure that values differ to prevent disaster on wfi
  166. LL_PWR_SetPowerMode(LL_PWR_MODE_STOP0);
  167. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  168. LL_PWR_ClearFlag_C1STOP_C1STB();
  169. LL_PWR_ClearFlag_C2STOP_C2STB();
  170. /* Release ENTRY_STOP_MODE semaphore */
  171. LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
  172. while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
  173. ;
  174. if(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {
  175. furi_hal_clock_switch_to_pll();
  176. }
  177. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  178. furi_hal_power_resume_aux_periphs();
  179. }
  180. void furi_hal_power_sleep() {
  181. if(furi_hal_power_deep_sleep_available()) {
  182. #ifdef FURI_HAL_OS_DEBUG
  183. furi_hal_gpio_write(&gpio_ext_pc3, 1);
  184. #endif
  185. furi_hal_power_deep_sleep();
  186. #ifdef FURI_HAL_OS_DEBUG
  187. furi_hal_gpio_write(&gpio_ext_pc3, 0);
  188. #endif
  189. } else {
  190. #ifdef FURI_HAL_OS_DEBUG
  191. furi_hal_gpio_write(&gpio_ext_pb2, 1);
  192. #endif
  193. furi_hal_power_light_sleep();
  194. #ifdef FURI_HAL_OS_DEBUG
  195. furi_hal_gpio_write(&gpio_ext_pb2, 0);
  196. #endif
  197. }
  198. }
  199. uint8_t furi_hal_power_get_pct() {
  200. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  201. uint8_t ret = bq27220_get_state_of_charge(&furi_hal_i2c_handle_power);
  202. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  203. return ret;
  204. }
  205. uint8_t furi_hal_power_get_bat_health_pct() {
  206. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  207. uint8_t ret = bq27220_get_state_of_health(&furi_hal_i2c_handle_power);
  208. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  209. return ret;
  210. }
  211. bool furi_hal_power_is_charging() {
  212. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  213. bool ret = bq25896_is_charging(&furi_hal_i2c_handle_power);
  214. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  215. return ret;
  216. }
  217. void furi_hal_power_shutdown() {
  218. furi_hal_power_insomnia_enter();
  219. furi_hal_bt_reinit();
  220. while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
  221. ;
  222. if(!LL_HSEM_1StepLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID)) {
  223. if(LL_PWR_IsActiveFlag_C2DS() || LL_PWR_IsActiveFlag_C2SB()) {
  224. // Release ENTRY_STOP_MODE semaphore
  225. LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
  226. }
  227. }
  228. // Prepare Wakeup pin
  229. LL_PWR_SetWakeUpPinPolarityLow(LL_PWR_WAKEUP_PIN2);
  230. LL_PWR_EnableWakeUpPin(LL_PWR_WAKEUP_PIN2);
  231. LL_C2_PWR_EnableWakeUpPin(LL_PWR_WAKEUP_PIN2);
  232. /* Release RCC semaphore */
  233. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  234. LL_PWR_DisableBootC2();
  235. LL_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  236. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  237. LL_LPM_EnableDeepSleep();
  238. __WFI();
  239. furi_crash("Insomniac core2");
  240. }
  241. void furi_hal_power_off() {
  242. // Crutch: shutting down with ext 3V3 off is causing LSE to stop
  243. furi_hal_power_enable_external_3_3v();
  244. furi_delay_us(1000);
  245. // Send poweroff to charger
  246. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  247. bq25896_poweroff(&furi_hal_i2c_handle_power);
  248. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  249. }
  250. void furi_hal_power_reset() {
  251. NVIC_SystemReset();
  252. }
  253. void furi_hal_power_enable_otg() {
  254. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  255. bq25896_enable_otg(&furi_hal_i2c_handle_power);
  256. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  257. }
  258. void furi_hal_power_disable_otg() {
  259. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  260. bq25896_disable_otg(&furi_hal_i2c_handle_power);
  261. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  262. }
  263. bool furi_hal_power_is_otg_enabled() {
  264. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  265. bool ret = bq25896_is_otg_enabled(&furi_hal_i2c_handle_power);
  266. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  267. return ret;
  268. }
  269. void furi_hal_power_check_otg_status() {
  270. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  271. if(bq25896_check_otg_fault(&furi_hal_i2c_handle_power))
  272. bq25896_disable_otg(&furi_hal_i2c_handle_power);
  273. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  274. }
  275. uint32_t furi_hal_power_get_battery_remaining_capacity() {
  276. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  277. uint32_t ret = bq27220_get_remaining_capacity(&furi_hal_i2c_handle_power);
  278. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  279. return ret;
  280. }
  281. uint32_t furi_hal_power_get_battery_full_capacity() {
  282. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  283. uint32_t ret = bq27220_get_full_charge_capacity(&furi_hal_i2c_handle_power);
  284. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  285. return ret;
  286. }
  287. uint32_t furi_hal_power_get_battery_design_capacity() {
  288. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  289. uint32_t ret = bq27220_get_design_capacity(&furi_hal_i2c_handle_power);
  290. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  291. return ret;
  292. }
  293. float furi_hal_power_get_battery_voltage(FuriHalPowerIC ic) {
  294. float ret = 0.0f;
  295. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  296. if(ic == FuriHalPowerICCharger) {
  297. ret = (float)bq25896_get_vbat_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
  298. } else if(ic == FuriHalPowerICFuelGauge) {
  299. ret = (float)bq27220_get_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
  300. }
  301. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  302. return ret;
  303. }
  304. float furi_hal_power_get_battery_current(FuriHalPowerIC ic) {
  305. float ret = 0.0f;
  306. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  307. if(ic == FuriHalPowerICCharger) {
  308. ret = (float)bq25896_get_vbat_current(&furi_hal_i2c_handle_power) / 1000.0f;
  309. } else if(ic == FuriHalPowerICFuelGauge) {
  310. ret = (float)bq27220_get_current(&furi_hal_i2c_handle_power) / 1000.0f;
  311. }
  312. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  313. return ret;
  314. }
  315. static float furi_hal_power_get_battery_temperature_internal(FuriHalPowerIC ic) {
  316. float ret = 0.0f;
  317. if(ic == FuriHalPowerICCharger) {
  318. // Linear approximation, +/- 5 C
  319. ret = (71.0f - (float)bq25896_get_ntc_mpct(&furi_hal_i2c_handle_power) / 1000) / 0.6f;
  320. } else if(ic == FuriHalPowerICFuelGauge) {
  321. ret = ((float)bq27220_get_temperature(&furi_hal_i2c_handle_power) - 2731.0f) / 10.0f;
  322. }
  323. return ret;
  324. }
  325. float furi_hal_power_get_battery_temperature(FuriHalPowerIC ic) {
  326. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  327. float ret = furi_hal_power_get_battery_temperature_internal(ic);
  328. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  329. return ret;
  330. }
  331. float furi_hal_power_get_usb_voltage() {
  332. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  333. float ret = (float)bq25896_get_vbus_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
  334. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  335. return ret;
  336. }
  337. void furi_hal_power_dump_state() {
  338. BatteryStatus battery_status;
  339. OperationStatus operation_status;
  340. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  341. if(bq27220_get_battery_status(&furi_hal_i2c_handle_power, &battery_status) == BQ27220_ERROR ||
  342. bq27220_get_operation_status(&furi_hal_i2c_handle_power, &operation_status) ==
  343. BQ27220_ERROR) {
  344. printf("Failed to get bq27220 status. Communication error.\r\n");
  345. } else {
  346. // Operation status register
  347. printf(
  348. "bq27220: CALMD: %d, SEC: %d, EDV2: %d, VDQ: %d, INITCOMP: %d, SMTH: %d, BTPINT: %d, CFGUPDATE: %d\r\n",
  349. operation_status.CALMD,
  350. operation_status.SEC,
  351. operation_status.EDV2,
  352. operation_status.VDQ,
  353. operation_status.INITCOMP,
  354. operation_status.SMTH,
  355. operation_status.BTPINT,
  356. operation_status.CFGUPDATE);
  357. // Battery status register, part 1
  358. printf(
  359. "bq27220: CHGINH: %d, FC: %d, OTD: %d, OTC: %d, SLEEP: %d, OCVFAIL: %d, OCVCOMP: %d, FD: %d\r\n",
  360. battery_status.CHGINH,
  361. battery_status.FC,
  362. battery_status.OTD,
  363. battery_status.OTC,
  364. battery_status.SLEEP,
  365. battery_status.OCVFAIL,
  366. battery_status.OCVCOMP,
  367. battery_status.FD);
  368. // Battery status register, part 2
  369. printf(
  370. "bq27220: DSG: %d, SYSDWN: %d, TDA: %d, BATTPRES: %d, AUTH_GD: %d, OCVGD: %d, TCA: %d, RSVD: %d\r\n",
  371. battery_status.DSG,
  372. battery_status.SYSDWN,
  373. battery_status.TDA,
  374. battery_status.BATTPRES,
  375. battery_status.AUTH_GD,
  376. battery_status.OCVGD,
  377. battery_status.TCA,
  378. battery_status.RSVD);
  379. // Voltage and current info
  380. printf(
  381. "bq27220: Full capacity: %dmAh, Design capacity: %dmAh, Remaining capacity: %dmAh, State of Charge: %d%%, State of health: %d%%\r\n",
  382. bq27220_get_full_charge_capacity(&furi_hal_i2c_handle_power),
  383. bq27220_get_design_capacity(&furi_hal_i2c_handle_power),
  384. bq27220_get_remaining_capacity(&furi_hal_i2c_handle_power),
  385. bq27220_get_state_of_charge(&furi_hal_i2c_handle_power),
  386. bq27220_get_state_of_health(&furi_hal_i2c_handle_power));
  387. printf(
  388. "bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n",
  389. bq27220_get_voltage(&furi_hal_i2c_handle_power),
  390. bq27220_get_current(&furi_hal_i2c_handle_power),
  391. (int)furi_hal_power_get_battery_temperature_internal(FuriHalPowerICFuelGauge));
  392. }
  393. printf(
  394. "bq25896: VBUS: %d, VSYS: %d, VBAT: %d, Current: %d, NTC: %ldm%%\r\n",
  395. bq25896_get_vbus_voltage(&furi_hal_i2c_handle_power),
  396. bq25896_get_vsys_voltage(&furi_hal_i2c_handle_power),
  397. bq25896_get_vbat_voltage(&furi_hal_i2c_handle_power),
  398. bq25896_get_vbat_current(&furi_hal_i2c_handle_power),
  399. bq25896_get_ntc_mpct(&furi_hal_i2c_handle_power));
  400. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  401. }
  402. void furi_hal_power_enable_external_3_3v() {
  403. furi_hal_gpio_write(&periph_power, 1);
  404. }
  405. void furi_hal_power_disable_external_3_3v() {
  406. furi_hal_gpio_write(&periph_power, 0);
  407. }
  408. void furi_hal_power_suppress_charge_enter() {
  409. vTaskSuspendAll();
  410. bool disable_charging = furi_hal_power.suppress_charge == 0;
  411. furi_hal_power.suppress_charge++;
  412. xTaskResumeAll();
  413. if(disable_charging) {
  414. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  415. bq25896_disable_charging(&furi_hal_i2c_handle_power);
  416. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  417. }
  418. }
  419. void furi_hal_power_suppress_charge_exit() {
  420. vTaskSuspendAll();
  421. furi_hal_power.suppress_charge--;
  422. bool enable_charging = furi_hal_power.suppress_charge == 0;
  423. xTaskResumeAll();
  424. if(enable_charging) {
  425. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  426. bq25896_enable_charging(&furi_hal_i2c_handle_power);
  427. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  428. }
  429. }
  430. void furi_hal_power_info_get(FuriHalPowerInfoCallback out, void* context) {
  431. furi_assert(out);
  432. string_t value;
  433. string_init(value);
  434. // Power Info version
  435. out("power_info_major", "1", false, context);
  436. out("power_info_minor", "0", false, context);
  437. uint8_t charge = furi_hal_power_get_pct();
  438. string_printf(value, "%u", charge);
  439. out("charge_level", string_get_cstr(value), false, context);
  440. if(furi_hal_power_is_charging()) {
  441. if(charge < 100) {
  442. string_printf(value, "charging");
  443. } else {
  444. string_printf(value, "charged");
  445. }
  446. } else {
  447. string_printf(value, "discharging");
  448. }
  449. out("charge_state", string_get_cstr(value), false, context);
  450. uint16_t voltage =
  451. (uint16_t)(furi_hal_power_get_battery_voltage(FuriHalPowerICFuelGauge) * 1000.f);
  452. string_printf(value, "%u", voltage);
  453. out("battery_voltage", string_get_cstr(value), false, context);
  454. int16_t current =
  455. (int16_t)(furi_hal_power_get_battery_current(FuriHalPowerICFuelGauge) * 1000.f);
  456. string_printf(value, "%d", current);
  457. out("battery_current", string_get_cstr(value), false, context);
  458. int16_t temperature = (int16_t)furi_hal_power_get_battery_temperature(FuriHalPowerICFuelGauge);
  459. string_printf(value, "%d", temperature);
  460. out("gauge_temp", string_get_cstr(value), false, context);
  461. string_printf(value, "%u", furi_hal_power_get_bat_health_pct());
  462. out("battery_health", string_get_cstr(value), false, context);
  463. string_printf(value, "%u", furi_hal_power_get_battery_remaining_capacity());
  464. out("capacity_remain", string_get_cstr(value), false, context);
  465. string_printf(value, "%u", furi_hal_power_get_battery_full_capacity());
  466. out("capacity_full", string_get_cstr(value), false, context);
  467. string_printf(value, "%u", furi_hal_power_get_battery_design_capacity());
  468. out("capacity_design", string_get_cstr(value), true, context);
  469. string_clear(value);
  470. }