furi_hal_power.c 15 KB

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