furi_hal_power.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. #include <furi_hal_power.h>
  2. #include <furi_hal_clock.h>
  3. #include <furi_hal_bt.h>
  4. #include <furi_hal_vibro.h>
  5. #include <furi_hal_resources.h>
  6. #include <furi_hal_uart.h>
  7. #include <furi_hal_rtc.h>
  8. #include <furi_hal_debug.h>
  9. #include <stm32wbxx_ll_rcc.h>
  10. #include <stm32wbxx_ll_pwr.h>
  11. #include <stm32wbxx_ll_hsem.h>
  12. #include <stm32wbxx_ll_cortex.h>
  13. #include <stm32wbxx_ll_gpio.h>
  14. #include <hw_conf.h>
  15. #include <bq27220.h>
  16. #include <bq25896.h>
  17. #include <furi.h>
  18. #define TAG "FuriHalPower"
  19. #ifndef FURI_HAL_POWER_DEBUG_WFI_GPIO
  20. #define FURI_HAL_POWER_DEBUG_WFI_GPIO (&gpio_ext_pb2)
  21. #endif
  22. #ifndef FURI_HAL_POWER_DEBUG_STOP_GPIO
  23. #define FURI_HAL_POWER_DEBUG_STOP_GPIO (&gpio_ext_pc3)
  24. #endif
  25. #ifndef FURI_HAL_POWER_STOP_MODE
  26. #define FURI_HAL_POWER_STOP_MODE (LL_PWR_MODE_STOP2)
  27. #endif
  28. typedef struct {
  29. volatile uint8_t insomnia;
  30. volatile uint8_t suppress_charge;
  31. uint8_t gauge_initialized;
  32. uint8_t charger_initialized;
  33. } FuriHalPower;
  34. static volatile FuriHalPower furi_hal_power = {
  35. .insomnia = 0,
  36. .suppress_charge = 0,
  37. };
  38. const ParamCEDV cedv = {
  39. .cedv_conf.gauge_conf =
  40. {
  41. .CCT = 1,
  42. .CSYNC = 0,
  43. .EDV_CMP = 0,
  44. .SC = 1,
  45. .FIXED_EDV0 = 1,
  46. .FCC_LIM = 1,
  47. .FC_FOR_VDQ = 1,
  48. .IGNORE_SD = 1,
  49. .SME0 = 0,
  50. },
  51. .full_charge_cap = 2101,
  52. .design_cap = 2101,
  53. .EDV0 = 3300,
  54. .EDV1 = 3321,
  55. .EDV2 = 3355,
  56. .EMF = 3679,
  57. .C0 = 430,
  58. .C1 = 0,
  59. .R1 = 408,
  60. .R0 = 334,
  61. .T0 = 4626,
  62. .TC = 11,
  63. .DOD0 = 4044,
  64. .DOD10 = 3905,
  65. .DOD20 = 3807,
  66. .DOD30 = 3718,
  67. .DOD40 = 3642,
  68. .DOD50 = 3585,
  69. .DOD60 = 3546,
  70. .DOD70 = 3514,
  71. .DOD80 = 3477,
  72. .DOD90 = 3411,
  73. .DOD100 = 3299,
  74. };
  75. void furi_hal_power_init() {
  76. #ifdef FURI_HAL_POWER_DEBUG
  77. furi_hal_gpio_init_simple(FURI_HAL_POWER_DEBUG_WFI_GPIO, GpioModeOutputPushPull);
  78. furi_hal_gpio_init_simple(FURI_HAL_POWER_DEBUG_STOP_GPIO, GpioModeOutputPushPull);
  79. furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_WFI_GPIO, 0);
  80. furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_STOP_GPIO, 0);
  81. #endif
  82. LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
  83. LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN);
  84. LL_PWR_SetPowerMode(FURI_HAL_POWER_STOP_MODE);
  85. LL_C2_PWR_SetPowerMode(FURI_HAL_POWER_STOP_MODE);
  86. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  87. bq27220_init(&furi_hal_i2c_handle_power, &cedv);
  88. bq25896_init(&furi_hal_i2c_handle_power);
  89. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  90. FURI_LOG_I(TAG, "Init OK");
  91. }
  92. bool furi_hal_power_gauge_is_ok() {
  93. bool ret = true;
  94. BatteryStatus battery_status;
  95. OperationStatus operation_status;
  96. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  97. if(bq27220_get_battery_status(&furi_hal_i2c_handle_power, &battery_status) == BQ27220_ERROR ||
  98. bq27220_get_operation_status(&furi_hal_i2c_handle_power, &operation_status) ==
  99. BQ27220_ERROR) {
  100. ret = false;
  101. } else {
  102. ret &= battery_status.BATTPRES;
  103. ret &= operation_status.INITCOMP;
  104. ret &= (cedv.design_cap == bq27220_get_design_capacity(&furi_hal_i2c_handle_power));
  105. }
  106. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  107. return ret;
  108. }
  109. uint16_t furi_hal_power_insomnia_level() {
  110. return furi_hal_power.insomnia;
  111. }
  112. void furi_hal_power_insomnia_enter() {
  113. FURI_CRITICAL_ENTER();
  114. furi_assert(furi_hal_power.insomnia < UINT8_MAX);
  115. furi_hal_power.insomnia++;
  116. FURI_CRITICAL_EXIT();
  117. }
  118. void furi_hal_power_insomnia_exit() {
  119. FURI_CRITICAL_ENTER();
  120. furi_assert(furi_hal_power.insomnia > 0);
  121. furi_hal_power.insomnia--;
  122. FURI_CRITICAL_EXIT();
  123. }
  124. bool furi_hal_power_sleep_available() {
  125. return furi_hal_power.insomnia == 0;
  126. }
  127. static inline bool furi_hal_power_deep_sleep_available() {
  128. return furi_hal_bt_is_alive() && !furi_hal_rtc_is_flag_set(FuriHalRtcFlagLegacySleep) &&
  129. !furi_hal_debug_is_gdb_session_active();
  130. }
  131. static inline void furi_hal_power_light_sleep() {
  132. __WFI();
  133. }
  134. static inline void furi_hal_power_suspend_aux_periphs() {
  135. // Disable USART
  136. furi_hal_uart_suspend(FuriHalUartIdUSART1);
  137. furi_hal_uart_suspend(FuriHalUartIdLPUART1);
  138. }
  139. static inline void furi_hal_power_resume_aux_periphs() {
  140. // Re-enable USART
  141. furi_hal_uart_resume(FuriHalUartIdUSART1);
  142. furi_hal_uart_resume(FuriHalUartIdLPUART1);
  143. }
  144. static inline void furi_hal_power_deep_sleep() {
  145. furi_hal_power_suspend_aux_periphs();
  146. while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
  147. ;
  148. if(!LL_HSEM_1StepLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID)) {
  149. if(LL_PWR_IsActiveFlag_C2DS() || LL_PWR_IsActiveFlag_C2SB()) {
  150. // Release ENTRY_STOP_MODE semaphore
  151. LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
  152. // The switch on HSI before entering Stop Mode is required
  153. furi_hal_clock_switch_to_hsi();
  154. }
  155. } else {
  156. /**
  157. * The switch on HSI before entering Stop Mode is required
  158. */
  159. furi_hal_clock_switch_to_hsi();
  160. }
  161. /* Release RCC semaphore */
  162. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  163. // Prepare deep sleep
  164. LL_LPM_EnableDeepSleep();
  165. #if defined(__CC_ARM)
  166. // Force store operations
  167. __force_stores();
  168. #endif
  169. __WFI();
  170. LL_LPM_EnableSleep();
  171. /* Release ENTRY_STOP_MODE semaphore */
  172. LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
  173. while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
  174. ;
  175. if(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {
  176. furi_hal_clock_switch_to_pll();
  177. }
  178. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  179. furi_hal_power_resume_aux_periphs();
  180. furi_hal_rtc_sync_shadow();
  181. }
  182. void furi_hal_power_sleep() {
  183. if(furi_hal_power_deep_sleep_available()) {
  184. #ifdef FURI_HAL_POWER_DEBUG
  185. furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_STOP_GPIO, 1);
  186. #endif
  187. furi_hal_power_deep_sleep();
  188. #ifdef FURI_HAL_POWER_DEBUG
  189. furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_STOP_GPIO, 0);
  190. #endif
  191. } else {
  192. #ifdef FURI_HAL_POWER_DEBUG
  193. furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_WFI_GPIO, 1);
  194. #endif
  195. furi_hal_power_light_sleep();
  196. #ifdef FURI_HAL_POWER_DEBUG
  197. furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_WFI_GPIO, 0);
  198. #endif
  199. }
  200. }
  201. uint8_t furi_hal_power_get_pct() {
  202. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  203. uint8_t ret = bq27220_get_state_of_charge(&furi_hal_i2c_handle_power);
  204. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  205. return ret;
  206. }
  207. uint8_t furi_hal_power_get_bat_health_pct() {
  208. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  209. uint8_t ret = bq27220_get_state_of_health(&furi_hal_i2c_handle_power);
  210. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  211. return ret;
  212. }
  213. bool furi_hal_power_is_charging() {
  214. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  215. bool ret = bq25896_is_charging(&furi_hal_i2c_handle_power);
  216. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  217. return ret;
  218. }
  219. bool furi_hal_power_is_charging_done() {
  220. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  221. bool ret = bq25896_is_charging_done(&furi_hal_i2c_handle_power);
  222. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  223. return ret;
  224. }
  225. void furi_hal_power_shutdown() {
  226. furi_hal_power_insomnia_enter();
  227. furi_hal_bt_reinit();
  228. while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
  229. ;
  230. if(!LL_HSEM_1StepLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID)) {
  231. if(LL_PWR_IsActiveFlag_C2DS() || LL_PWR_IsActiveFlag_C2SB()) {
  232. // Release ENTRY_STOP_MODE semaphore
  233. LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
  234. }
  235. }
  236. // Prepare Wakeup pin
  237. LL_PWR_SetWakeUpPinPolarityLow(LL_PWR_WAKEUP_PIN2);
  238. LL_PWR_EnableWakeUpPin(LL_PWR_WAKEUP_PIN2);
  239. LL_C2_PWR_EnableWakeUpPin(LL_PWR_WAKEUP_PIN2);
  240. /* Release RCC semaphore */
  241. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  242. LL_PWR_DisableBootC2();
  243. LL_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  244. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  245. LL_LPM_EnableDeepSleep();
  246. __WFI();
  247. furi_crash("Insomniac core2");
  248. }
  249. void furi_hal_power_off() {
  250. // Crutch: shutting down with ext 3V3 off is causing LSE to stop
  251. furi_hal_power_enable_external_3_3v();
  252. furi_hal_vibro_on(true);
  253. furi_delay_us(50000);
  254. // Send poweroff to charger
  255. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  256. bq25896_poweroff(&furi_hal_i2c_handle_power);
  257. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  258. furi_hal_vibro_on(false);
  259. }
  260. void furi_hal_power_reset() {
  261. NVIC_SystemReset();
  262. }
  263. void furi_hal_power_enable_otg() {
  264. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  265. bq25896_enable_otg(&furi_hal_i2c_handle_power);
  266. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  267. }
  268. void furi_hal_power_disable_otg() {
  269. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  270. bq25896_disable_otg(&furi_hal_i2c_handle_power);
  271. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  272. }
  273. bool furi_hal_power_is_otg_enabled() {
  274. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  275. bool ret = bq25896_is_otg_enabled(&furi_hal_i2c_handle_power);
  276. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  277. return ret;
  278. }
  279. float furi_hal_power_get_battery_charge_voltage_limit() {
  280. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  281. float ret = (float)bq25896_get_vreg_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
  282. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  283. return ret;
  284. }
  285. void furi_hal_power_set_battery_charge_voltage_limit(float voltage) {
  286. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  287. // Adding 0.0005 is necessary because 4.016f is 4.015999794000, which gets truncated
  288. bq25896_set_vreg_voltage(&furi_hal_i2c_handle_power, (uint16_t)(voltage * 1000.0f + 0.0005f));
  289. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  290. }
  291. void furi_hal_power_check_otg_status() {
  292. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  293. if(bq25896_check_otg_fault(&furi_hal_i2c_handle_power))
  294. bq25896_disable_otg(&furi_hal_i2c_handle_power);
  295. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  296. }
  297. uint32_t furi_hal_power_get_battery_remaining_capacity() {
  298. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  299. uint32_t ret = bq27220_get_remaining_capacity(&furi_hal_i2c_handle_power);
  300. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  301. return ret;
  302. }
  303. uint32_t furi_hal_power_get_battery_full_capacity() {
  304. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  305. uint32_t ret = bq27220_get_full_charge_capacity(&furi_hal_i2c_handle_power);
  306. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  307. return ret;
  308. }
  309. uint32_t furi_hal_power_get_battery_design_capacity() {
  310. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  311. uint32_t ret = bq27220_get_design_capacity(&furi_hal_i2c_handle_power);
  312. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  313. return ret;
  314. }
  315. float furi_hal_power_get_battery_voltage(FuriHalPowerIC ic) {
  316. float ret = 0.0f;
  317. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  318. if(ic == FuriHalPowerICCharger) {
  319. ret = (float)bq25896_get_vbat_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
  320. } else if(ic == FuriHalPowerICFuelGauge) {
  321. ret = (float)bq27220_get_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
  322. }
  323. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  324. return ret;
  325. }
  326. float furi_hal_power_get_battery_current(FuriHalPowerIC ic) {
  327. float ret = 0.0f;
  328. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  329. if(ic == FuriHalPowerICCharger) {
  330. ret = (float)bq25896_get_vbat_current(&furi_hal_i2c_handle_power) / 1000.0f;
  331. } else if(ic == FuriHalPowerICFuelGauge) {
  332. ret = (float)bq27220_get_current(&furi_hal_i2c_handle_power) / 1000.0f;
  333. }
  334. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  335. return ret;
  336. }
  337. static float furi_hal_power_get_battery_temperature_internal(FuriHalPowerIC ic) {
  338. float ret = 0.0f;
  339. if(ic == FuriHalPowerICCharger) {
  340. // Linear approximation, +/- 5 C
  341. ret = (71.0f - (float)bq25896_get_ntc_mpct(&furi_hal_i2c_handle_power) / 1000) / 0.6f;
  342. } else if(ic == FuriHalPowerICFuelGauge) {
  343. ret = ((float)bq27220_get_temperature(&furi_hal_i2c_handle_power) - 2731.0f) / 10.0f;
  344. }
  345. return ret;
  346. }
  347. float furi_hal_power_get_battery_temperature(FuriHalPowerIC ic) {
  348. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  349. float ret = furi_hal_power_get_battery_temperature_internal(ic);
  350. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  351. return ret;
  352. }
  353. float furi_hal_power_get_usb_voltage() {
  354. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  355. float ret = (float)bq25896_get_vbus_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
  356. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  357. return ret;
  358. }
  359. void furi_hal_power_enable_external_3_3v() {
  360. furi_hal_gpio_write(&gpio_periph_power, 1);
  361. }
  362. void furi_hal_power_disable_external_3_3v() {
  363. furi_hal_gpio_write(&gpio_periph_power, 0);
  364. }
  365. void furi_hal_power_suppress_charge_enter() {
  366. vTaskSuspendAll();
  367. bool disable_charging = furi_hal_power.suppress_charge == 0;
  368. furi_hal_power.suppress_charge++;
  369. xTaskResumeAll();
  370. if(disable_charging) {
  371. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  372. bq25896_disable_charging(&furi_hal_i2c_handle_power);
  373. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  374. }
  375. }
  376. void furi_hal_power_suppress_charge_exit() {
  377. vTaskSuspendAll();
  378. furi_hal_power.suppress_charge--;
  379. bool enable_charging = furi_hal_power.suppress_charge == 0;
  380. xTaskResumeAll();
  381. if(enable_charging) {
  382. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  383. bq25896_enable_charging(&furi_hal_i2c_handle_power);
  384. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  385. }
  386. }
  387. void furi_hal_power_info_get(PropertyValueCallback out, char sep, void* context) {
  388. furi_assert(out);
  389. FuriString* value = furi_string_alloc();
  390. FuriString* key = furi_string_alloc();
  391. PropertyValueContext property_context = {
  392. .key = key, .value = value, .out = out, .sep = sep, .last = false, .context = context};
  393. if(sep == '.') {
  394. property_value_out(&property_context, NULL, 2, "format", "major", "2");
  395. property_value_out(&property_context, NULL, 2, "format", "minor", "1");
  396. } else {
  397. property_value_out(&property_context, NULL, 3, "power", "info", "major", "2");
  398. property_value_out(&property_context, NULL, 3, "power", "info", "minor", "1");
  399. }
  400. uint8_t charge = furi_hal_power_get_pct();
  401. property_value_out(&property_context, "%u", 2, "charge", "level", charge);
  402. const char* charge_state;
  403. if(furi_hal_power_is_charging()) {
  404. if((charge < 100) && (!furi_hal_power_is_charging_done())) {
  405. charge_state = "charging";
  406. } else {
  407. charge_state = "charged";
  408. }
  409. } else {
  410. charge_state = "discharging";
  411. }
  412. property_value_out(&property_context, NULL, 2, "charge", "state", charge_state);
  413. uint16_t charge_voltage_limit =
  414. (uint16_t)(furi_hal_power_get_battery_charge_voltage_limit() * 1000.f);
  415. property_value_out(
  416. &property_context, "%u", 3, "charge", "voltage", "limit", charge_voltage_limit);
  417. uint16_t voltage =
  418. (uint16_t)(furi_hal_power_get_battery_voltage(FuriHalPowerICFuelGauge) * 1000.f);
  419. property_value_out(&property_context, "%u", 2, "battery", "voltage", voltage);
  420. int16_t current =
  421. (int16_t)(furi_hal_power_get_battery_current(FuriHalPowerICFuelGauge) * 1000.f);
  422. property_value_out(&property_context, "%d", 2, "battery", "current", current);
  423. int16_t temperature = (int16_t)furi_hal_power_get_battery_temperature(FuriHalPowerICFuelGauge);
  424. property_value_out(&property_context, "%d", 2, "battery", "temp", temperature);
  425. property_value_out(
  426. &property_context, "%u", 2, "battery", "health", furi_hal_power_get_bat_health_pct());
  427. property_value_out(
  428. &property_context,
  429. "%lu",
  430. 2,
  431. "capacity",
  432. "remain",
  433. furi_hal_power_get_battery_remaining_capacity());
  434. property_value_out(
  435. &property_context,
  436. "%lu",
  437. 2,
  438. "capacity",
  439. "full",
  440. furi_hal_power_get_battery_full_capacity());
  441. property_context.last = true;
  442. property_value_out(
  443. &property_context,
  444. "%lu",
  445. 2,
  446. "capacity",
  447. "design",
  448. furi_hal_power_get_battery_design_capacity());
  449. furi_string_free(key);
  450. furi_string_free(value);
  451. }
  452. void furi_hal_power_debug_get(PropertyValueCallback out, void* context) {
  453. furi_assert(out);
  454. FuriString* value = furi_string_alloc();
  455. FuriString* key = furi_string_alloc();
  456. PropertyValueContext property_context = {
  457. .key = key, .value = value, .out = out, .sep = '.', .last = false, .context = context};
  458. BatteryStatus battery_status;
  459. OperationStatus operation_status;
  460. furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
  461. // Power Debug version
  462. property_value_out(&property_context, NULL, 2, "format", "major", "1");
  463. property_value_out(&property_context, NULL, 2, "format", "minor", "0");
  464. property_value_out(
  465. &property_context,
  466. "%d",
  467. 2,
  468. "charger",
  469. "vbus",
  470. bq25896_get_vbus_voltage(&furi_hal_i2c_handle_power));
  471. property_value_out(
  472. &property_context,
  473. "%d",
  474. 2,
  475. "charger",
  476. "vsys",
  477. bq25896_get_vsys_voltage(&furi_hal_i2c_handle_power));
  478. property_value_out(
  479. &property_context,
  480. "%d",
  481. 2,
  482. "charger",
  483. "vbat",
  484. bq25896_get_vbat_voltage(&furi_hal_i2c_handle_power));
  485. property_value_out(
  486. &property_context,
  487. "%d",
  488. 2,
  489. "charger",
  490. "vreg",
  491. bq25896_get_vreg_voltage(&furi_hal_i2c_handle_power));
  492. property_value_out(
  493. &property_context,
  494. "%d",
  495. 2,
  496. "charger",
  497. "current",
  498. bq25896_get_vbat_current(&furi_hal_i2c_handle_power));
  499. const uint32_t ntc_mpct = bq25896_get_ntc_mpct(&furi_hal_i2c_handle_power);
  500. if(bq27220_get_battery_status(&furi_hal_i2c_handle_power, &battery_status) != BQ27220_ERROR &&
  501. bq27220_get_operation_status(&furi_hal_i2c_handle_power, &operation_status) !=
  502. BQ27220_ERROR) {
  503. property_value_out(&property_context, "%lu", 2, "charger", "ntc", ntc_mpct);
  504. property_value_out(&property_context, "%d", 2, "gauge", "calmd", operation_status.CALMD);
  505. property_value_out(&property_context, "%d", 2, "gauge", "sec", operation_status.SEC);
  506. property_value_out(&property_context, "%d", 2, "gauge", "edv2", operation_status.EDV2);
  507. property_value_out(&property_context, "%d", 2, "gauge", "vdq", operation_status.VDQ);
  508. property_value_out(
  509. &property_context, "%d", 2, "gauge", "initcomp", operation_status.INITCOMP);
  510. property_value_out(&property_context, "%d", 2, "gauge", "smth", operation_status.SMTH);
  511. property_value_out(&property_context, "%d", 2, "gauge", "btpint", operation_status.BTPINT);
  512. property_value_out(
  513. &property_context, "%d", 2, "gauge", "cfgupdate", operation_status.CFGUPDATE);
  514. // Battery status register, part 1
  515. property_value_out(&property_context, "%d", 2, "gauge", "chginh", battery_status.CHGINH);
  516. property_value_out(&property_context, "%d", 2, "gauge", "fc", battery_status.FC);
  517. property_value_out(&property_context, "%d", 2, "gauge", "otd", battery_status.OTD);
  518. property_value_out(&property_context, "%d", 2, "gauge", "otc", battery_status.OTC);
  519. property_value_out(&property_context, "%d", 2, "gauge", "sleep", battery_status.SLEEP);
  520. property_value_out(&property_context, "%d", 2, "gauge", "ocvfail", battery_status.OCVFAIL);
  521. property_value_out(&property_context, "%d", 2, "gauge", "ocvcomp", battery_status.OCVCOMP);
  522. property_value_out(&property_context, "%d", 2, "gauge", "fd", battery_status.FD);
  523. // Battery status register, part 2
  524. property_value_out(&property_context, "%d", 2, "gauge", "dsg", battery_status.DSG);
  525. property_value_out(&property_context, "%d", 2, "gauge", "sysdwn", battery_status.SYSDWN);
  526. property_value_out(&property_context, "%d", 2, "gauge", "tda", battery_status.TDA);
  527. property_value_out(
  528. &property_context, "%d", 2, "gauge", "battpres", battery_status.BATTPRES);
  529. property_value_out(&property_context, "%d", 2, "gauge", "authgd", battery_status.AUTH_GD);
  530. property_value_out(&property_context, "%d", 2, "gauge", "ocvgd", battery_status.OCVGD);
  531. property_value_out(&property_context, "%d", 2, "gauge", "tca", battery_status.TCA);
  532. property_value_out(&property_context, "%d", 2, "gauge", "rsvd", battery_status.RSVD);
  533. // Voltage and current info
  534. property_value_out(
  535. &property_context,
  536. "%d",
  537. 3,
  538. "gauge",
  539. "capacity",
  540. "full",
  541. bq27220_get_full_charge_capacity(&furi_hal_i2c_handle_power));
  542. property_value_out(
  543. &property_context,
  544. "%d",
  545. 3,
  546. "gauge",
  547. "capacity",
  548. "design",
  549. bq27220_get_design_capacity(&furi_hal_i2c_handle_power));
  550. property_value_out(
  551. &property_context,
  552. "%d",
  553. 3,
  554. "gauge",
  555. "capacity",
  556. "remain",
  557. bq27220_get_remaining_capacity(&furi_hal_i2c_handle_power));
  558. property_value_out(
  559. &property_context,
  560. "%d",
  561. 3,
  562. "gauge",
  563. "state",
  564. "charge",
  565. bq27220_get_state_of_charge(&furi_hal_i2c_handle_power));
  566. property_value_out(
  567. &property_context,
  568. "%d",
  569. 3,
  570. "gauge",
  571. "state",
  572. "health",
  573. bq27220_get_state_of_health(&furi_hal_i2c_handle_power));
  574. property_value_out(
  575. &property_context,
  576. "%d",
  577. 2,
  578. "gauge",
  579. "voltage",
  580. bq27220_get_voltage(&furi_hal_i2c_handle_power));
  581. property_value_out(
  582. &property_context,
  583. "%d",
  584. 2,
  585. "gauge",
  586. "current",
  587. bq27220_get_current(&furi_hal_i2c_handle_power));
  588. property_context.last = true;
  589. const int battery_temp =
  590. (int)furi_hal_power_get_battery_temperature_internal(FuriHalPowerICFuelGauge);
  591. property_value_out(&property_context, "%d", 2, "gauge", "temperature", battery_temp);
  592. } else {
  593. property_context.last = true;
  594. property_value_out(&property_context, "%lu", 2, "charger", "ntc", ntc_mpct);
  595. }
  596. furi_string_free(key);
  597. furi_string_free(value);
  598. furi_hal_i2c_release(&furi_hal_i2c_handle_power);
  599. }