furi_hal_power.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  243. bq25896_poweroff(&furi_hal_i2c_handle_power);
  244. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  245. }
  246. void furi_hal_power_reset() {
  247. NVIC_SystemReset();
  248. }
  249. void furi_hal_power_enable_otg() {
  250. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  251. bq25896_enable_otg(&furi_hal_i2c_handle_power);
  252. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  253. }
  254. void furi_hal_power_disable_otg() {
  255. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  256. bq25896_disable_otg(&furi_hal_i2c_handle_power);
  257. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  258. }
  259. bool furi_hal_power_is_otg_enabled() {
  260. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  261. bool ret = bq25896_is_otg_enabled(&furi_hal_i2c_handle_power);
  262. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  263. return ret;
  264. }
  265. void furi_hal_power_check_otg_status() {
  266. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  267. if(bq25896_check_otg_fault(&furi_hal_i2c_handle_power))
  268. bq25896_disable_otg(&furi_hal_i2c_handle_power);
  269. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  270. }
  271. uint32_t furi_hal_power_get_battery_remaining_capacity() {
  272. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  273. uint32_t ret = bq27220_get_remaining_capacity(&furi_hal_i2c_handle_power);
  274. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  275. return ret;
  276. }
  277. uint32_t furi_hal_power_get_battery_full_capacity() {
  278. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  279. uint32_t ret = bq27220_get_full_charge_capacity(&furi_hal_i2c_handle_power);
  280. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  281. return ret;
  282. }
  283. uint32_t furi_hal_power_get_battery_design_capacity() {
  284. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  285. uint32_t ret = bq27220_get_design_capacity(&furi_hal_i2c_handle_power);
  286. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  287. return ret;
  288. }
  289. float furi_hal_power_get_battery_voltage(FuriHalPowerIC ic) {
  290. float ret = 0.0f;
  291. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  292. if(ic == FuriHalPowerICCharger) {
  293. ret = (float)bq25896_get_vbat_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
  294. } else if(ic == FuriHalPowerICFuelGauge) {
  295. ret = (float)bq27220_get_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
  296. }
  297. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  298. return ret;
  299. }
  300. float furi_hal_power_get_battery_current(FuriHalPowerIC ic) {
  301. float ret = 0.0f;
  302. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  303. if(ic == FuriHalPowerICCharger) {
  304. ret = (float)bq25896_get_vbat_current(&furi_hal_i2c_handle_power) / 1000.0f;
  305. } else if(ic == FuriHalPowerICFuelGauge) {
  306. ret = (float)bq27220_get_current(&furi_hal_i2c_handle_power) / 1000.0f;
  307. }
  308. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  309. return ret;
  310. }
  311. static float furi_hal_power_get_battery_temperature_internal(FuriHalPowerIC ic) {
  312. float ret = 0.0f;
  313. if(ic == FuriHalPowerICCharger) {
  314. // Linear approximation, +/- 5 C
  315. ret = (71.0f - (float)bq25896_get_ntc_mpct(&furi_hal_i2c_handle_power) / 1000) / 0.6f;
  316. } else if(ic == FuriHalPowerICFuelGauge) {
  317. ret = ((float)bq27220_get_temperature(&furi_hal_i2c_handle_power) - 2731.0f) / 10.0f;
  318. }
  319. return ret;
  320. }
  321. float furi_hal_power_get_battery_temperature(FuriHalPowerIC ic) {
  322. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  323. float ret = furi_hal_power_get_battery_temperature_internal(ic);
  324. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  325. return ret;
  326. }
  327. float furi_hal_power_get_usb_voltage() {
  328. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  329. float ret = (float)bq25896_get_vbus_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
  330. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  331. return ret;
  332. }
  333. void furi_hal_power_dump_state() {
  334. BatteryStatus battery_status;
  335. OperationStatus operation_status;
  336. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  337. if(bq27220_get_battery_status(&furi_hal_i2c_handle_power, &battery_status) == BQ27220_ERROR ||
  338. bq27220_get_operation_status(&furi_hal_i2c_handle_power, &operation_status) ==
  339. BQ27220_ERROR) {
  340. printf("Failed to get bq27220 status. Communication error.\r\n");
  341. } else {
  342. // Operation status register
  343. printf(
  344. "bq27220: CALMD: %d, SEC: %d, EDV2: %d, VDQ: %d, INITCOMP: %d, SMTH: %d, BTPINT: %d, CFGUPDATE: %d\r\n",
  345. operation_status.CALMD,
  346. operation_status.SEC,
  347. operation_status.EDV2,
  348. operation_status.VDQ,
  349. operation_status.INITCOMP,
  350. operation_status.SMTH,
  351. operation_status.BTPINT,
  352. operation_status.CFGUPDATE);
  353. // Battery status register, part 1
  354. printf(
  355. "bq27220: CHGINH: %d, FC: %d, OTD: %d, OTC: %d, SLEEP: %d, OCVFAIL: %d, OCVCOMP: %d, FD: %d\r\n",
  356. battery_status.CHGINH,
  357. battery_status.FC,
  358. battery_status.OTD,
  359. battery_status.OTC,
  360. battery_status.SLEEP,
  361. battery_status.OCVFAIL,
  362. battery_status.OCVCOMP,
  363. battery_status.FD);
  364. // Battery status register, part 2
  365. printf(
  366. "bq27220: DSG: %d, SYSDWN: %d, TDA: %d, BATTPRES: %d, AUTH_GD: %d, OCVGD: %d, TCA: %d, RSVD: %d\r\n",
  367. battery_status.DSG,
  368. battery_status.SYSDWN,
  369. battery_status.TDA,
  370. battery_status.BATTPRES,
  371. battery_status.AUTH_GD,
  372. battery_status.OCVGD,
  373. battery_status.TCA,
  374. battery_status.RSVD);
  375. // Voltage and current info
  376. printf(
  377. "bq27220: Full capacity: %dmAh, Design capacity: %dmAh, Remaining capacity: %dmAh, State of Charge: %d%%, State of health: %d%%\r\n",
  378. bq27220_get_full_charge_capacity(&furi_hal_i2c_handle_power),
  379. bq27220_get_design_capacity(&furi_hal_i2c_handle_power),
  380. bq27220_get_remaining_capacity(&furi_hal_i2c_handle_power),
  381. bq27220_get_state_of_charge(&furi_hal_i2c_handle_power),
  382. bq27220_get_state_of_health(&furi_hal_i2c_handle_power));
  383. printf(
  384. "bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n",
  385. bq27220_get_voltage(&furi_hal_i2c_handle_power),
  386. bq27220_get_current(&furi_hal_i2c_handle_power),
  387. (int)furi_hal_power_get_battery_temperature_internal(FuriHalPowerICFuelGauge));
  388. }
  389. printf(
  390. "bq25896: VBUS: %d, VSYS: %d, VBAT: %d, Current: %d, NTC: %ldm%%\r\n",
  391. bq25896_get_vbus_voltage(&furi_hal_i2c_handle_power),
  392. bq25896_get_vsys_voltage(&furi_hal_i2c_handle_power),
  393. bq25896_get_vbat_voltage(&furi_hal_i2c_handle_power),
  394. bq25896_get_vbat_current(&furi_hal_i2c_handle_power),
  395. bq25896_get_ntc_mpct(&furi_hal_i2c_handle_power));
  396. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  397. }
  398. void furi_hal_power_enable_external_3_3v() {
  399. LL_GPIO_SetOutputPin(PERIPH_POWER_GPIO_Port, PERIPH_POWER_Pin);
  400. }
  401. void furi_hal_power_disable_external_3_3v() {
  402. LL_GPIO_ResetOutputPin(PERIPH_POWER_GPIO_Port, PERIPH_POWER_Pin);
  403. }
  404. void furi_hal_power_suppress_charge_enter() {
  405. vTaskSuspendAll();
  406. bool disable_charging = furi_hal_power.suppress_charge == 0;
  407. furi_hal_power.suppress_charge++;
  408. xTaskResumeAll();
  409. if(disable_charging) {
  410. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  411. bq25896_disable_charging(&furi_hal_i2c_handle_power);
  412. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  413. }
  414. }
  415. void furi_hal_power_suppress_charge_exit() {
  416. vTaskSuspendAll();
  417. furi_hal_power.suppress_charge--;
  418. bool enable_charging = furi_hal_power.suppress_charge == 0;
  419. xTaskResumeAll();
  420. if(enable_charging) {
  421. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  422. bq25896_enable_charging(&furi_hal_i2c_handle_power);
  423. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  424. }
  425. }
  426. void furi_hal_power_info_get(FuriHalPowerInfoCallback out, void* context) {
  427. furi_assert(out);
  428. string_t value;
  429. string_init(value);
  430. // Power Info version
  431. out("power_info_major", "1", false, context);
  432. out("power_info_minor", "0", false, context);
  433. uint8_t charge = furi_hal_power_get_pct();
  434. string_printf(value, "%u", charge);
  435. out("charge_level", string_get_cstr(value), false, context);
  436. if(furi_hal_power_is_charging()) {
  437. if(charge < 100) {
  438. string_printf(value, "charging");
  439. } else {
  440. string_printf(value, "charged");
  441. }
  442. } else {
  443. string_printf(value, "discharging");
  444. }
  445. out("charge_state", string_get_cstr(value), false, context);
  446. uint16_t voltage =
  447. (uint16_t)(furi_hal_power_get_battery_voltage(FuriHalPowerICFuelGauge) * 1000.f);
  448. string_printf(value, "%u", voltage);
  449. out("battery_voltage", string_get_cstr(value), false, context);
  450. int16_t current =
  451. (int16_t)(furi_hal_power_get_battery_current(FuriHalPowerICFuelGauge) * 1000.f);
  452. string_printf(value, "%d", current);
  453. out("battery_current", string_get_cstr(value), false, context);
  454. int16_t temperature = (int16_t)furi_hal_power_get_battery_temperature(FuriHalPowerICFuelGauge);
  455. string_printf(value, "%d", temperature);
  456. out("gauge_temp", string_get_cstr(value), false, context);
  457. string_printf(value, "%u", furi_hal_power_get_bat_health_pct());
  458. out("battery_health", string_get_cstr(value), false, context);
  459. string_printf(value, "%u", furi_hal_power_get_battery_remaining_capacity());
  460. out("capacity_remain", string_get_cstr(value), false, context);
  461. string_printf(value, "%u", furi_hal_power_get_battery_full_capacity());
  462. out("capacity_full", string_get_cstr(value), false, context);
  463. string_printf(value, "%u", furi_hal_power_get_battery_design_capacity());
  464. out("capacity_design", string_get_cstr(value), true, context);
  465. string_clear(value);
  466. }