furi-hal-power.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include <furi-hal-power.h>
  2. #include <furi-hal-clock.h>
  3. #include <furi-hal-bt.h>
  4. #include <stm32wbxx_ll_rcc.h>
  5. #include <stm32wbxx_ll_pwr.h>
  6. #include <stm32wbxx_ll_hsem.h>
  7. #include <stm32wbxx_ll_cortex.h>
  8. #include <stm32wbxx_ll_gpio.h>
  9. #include <main.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. } FuriHalPower;
  20. static volatile FuriHalPower furi_hal_power = {
  21. .insomnia = 0,
  22. .deep_insomnia = 1,
  23. .suppress_charge = 0,
  24. };
  25. const ParamCEDV cedv = {
  26. .cedv_conf.gauge_conf = {
  27. .CCT = 1,
  28. .CSYNC = 0,
  29. .EDV_CMP = 0,
  30. .SC = 1,
  31. .FIXED_EDV0 = 1,
  32. .FCC_LIM = 1,
  33. .FC_FOR_VDQ = 1,
  34. .IGNORE_SD = 1,
  35. .SME0 = 0,
  36. },
  37. .full_charge_cap = 2100,
  38. .design_cap = 2100,
  39. .EDV0 = 3300,
  40. .EDV1 = 3321,
  41. .EDV2 = 3355,
  42. .EMF = 3679,
  43. .C0 = 430,
  44. .C1 = 0,
  45. .R1 = 408,
  46. .R0 = 334,
  47. .T0 = 4626,
  48. .TC = 11,
  49. .DOD0 = 4044,
  50. .DOD10 = 3905,
  51. .DOD20 = 3807,
  52. .DOD30 = 3718,
  53. .DOD40 = 3642,
  54. .DOD50 = 3585,
  55. .DOD60 = 3546,
  56. .DOD70 = 3514,
  57. .DOD80 = 3477,
  58. .DOD90 = 3411,
  59. .DOD100 = 3299,
  60. };
  61. void HAL_RCC_CSSCallback(void) {
  62. // TODO: notify user about issue with HSE
  63. furi_hal_power_reset();
  64. }
  65. void furi_hal_power_init() {
  66. LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
  67. LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN);
  68. bq27220_init(&cedv);
  69. bq25896_init();
  70. FURI_LOG_I(TAG, "Init OK");
  71. }
  72. uint16_t furi_hal_power_insomnia_level() {
  73. return furi_hal_power.insomnia;
  74. }
  75. void furi_hal_power_insomnia_enter() {
  76. vTaskSuspendAll();
  77. furi_hal_power.insomnia++;
  78. xTaskResumeAll();
  79. }
  80. void furi_hal_power_insomnia_exit() {
  81. vTaskSuspendAll();
  82. furi_hal_power.insomnia--;
  83. xTaskResumeAll();
  84. }
  85. bool furi_hal_power_sleep_available() {
  86. return furi_hal_power.insomnia == 0;
  87. }
  88. bool furi_hal_power_deep_sleep_available() {
  89. return furi_hal_bt_is_alive() && furi_hal_power.deep_insomnia == 0;
  90. }
  91. void furi_hal_power_light_sleep() {
  92. __WFI();
  93. }
  94. void furi_hal_power_deep_sleep() {
  95. while( LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID));
  96. if (!LL_HSEM_1StepLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID)) {
  97. if(LL_PWR_IsActiveFlag_C2DS()) {
  98. // Release ENTRY_STOP_MODE semaphore
  99. LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
  100. // The switch on HSI before entering Stop Mode is required
  101. furi_hal_clock_switch_to_hsi();
  102. }
  103. } else {
  104. /**
  105. * The switch on HSI before entering Stop Mode is required
  106. */
  107. furi_hal_clock_switch_to_hsi();
  108. }
  109. /* Release RCC semaphore */
  110. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  111. // Prepare deep sleep
  112. LL_PWR_SetPowerMode(LL_PWR_MODE_STOP1);
  113. LL_LPM_EnableDeepSleep();
  114. #if defined ( __CC_ARM)
  115. // Force store operations
  116. __force_stores();
  117. #endif
  118. __WFI();
  119. /* Release ENTRY_STOP_MODE semaphore */
  120. LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
  121. while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID));
  122. if(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {
  123. furi_hal_clock_switch_to_pll();
  124. }
  125. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  126. }
  127. void furi_hal_power_sleep() {
  128. if(furi_hal_power_deep_sleep_available()) {
  129. furi_hal_power_deep_sleep();
  130. } else {
  131. furi_hal_power_light_sleep();
  132. }
  133. }
  134. uint8_t furi_hal_power_get_pct() {
  135. return bq27220_get_state_of_charge();
  136. }
  137. uint8_t furi_hal_power_get_bat_health_pct() {
  138. return bq27220_get_state_of_health();
  139. }
  140. bool furi_hal_power_is_charging() {
  141. return bq25896_is_charging();
  142. }
  143. void furi_hal_power_off() {
  144. bq25896_poweroff();
  145. }
  146. void furi_hal_power_reset() {
  147. NVIC_SystemReset();
  148. }
  149. void furi_hal_power_enable_otg() {
  150. bq25896_enable_otg();
  151. }
  152. void furi_hal_power_disable_otg() {
  153. bq25896_disable_otg();
  154. }
  155. bool furi_hal_power_is_otg_enabled() {
  156. return bq25896_is_otg_enabled();
  157. }
  158. uint32_t furi_hal_power_get_battery_remaining_capacity() {
  159. return bq27220_get_remaining_capacity();
  160. }
  161. uint32_t furi_hal_power_get_battery_full_capacity() {
  162. return bq27220_get_full_charge_capacity();
  163. }
  164. float furi_hal_power_get_battery_voltage(FuriHalPowerIC ic) {
  165. if (ic == FuriHalPowerICCharger) {
  166. return (float)bq25896_get_vbat_voltage() / 1000.0f;
  167. } else if (ic == FuriHalPowerICFuelGauge) {
  168. return (float)bq27220_get_voltage() / 1000.0f;
  169. } else {
  170. return 0.0f;
  171. }
  172. }
  173. float furi_hal_power_get_battery_current(FuriHalPowerIC ic) {
  174. if (ic == FuriHalPowerICCharger) {
  175. return (float)bq25896_get_vbat_current() / 1000.0f;
  176. } else if (ic == FuriHalPowerICFuelGauge) {
  177. return (float)bq27220_get_current() / 1000.0f;
  178. } else {
  179. return 0.0f;
  180. }
  181. }
  182. float furi_hal_power_get_battery_temperature(FuriHalPowerIC ic) {
  183. if (ic == FuriHalPowerICCharger) {
  184. // Linear approximation, +/- 5 C
  185. return (71.0f - (float)bq25896_get_ntc_mpct()/1000) / 0.6f;
  186. } else if (ic == FuriHalPowerICFuelGauge) {
  187. return ((float)bq27220_get_temperature() - 2731.0f) / 10.0f;
  188. } else {
  189. return 0.0f;
  190. }
  191. }
  192. float furi_hal_power_get_usb_voltage(){
  193. return (float)bq25896_get_vbus_voltage() / 1000.0f;
  194. }
  195. void furi_hal_power_dump_state() {
  196. BatteryStatus battery_status;
  197. OperationStatus operation_status;
  198. if (bq27220_get_battery_status(&battery_status) == BQ27220_ERROR
  199. || bq27220_get_operation_status(&operation_status) == BQ27220_ERROR) {
  200. printf("Failed to get bq27220 status. Communication error.\r\n");
  201. } else {
  202. printf(
  203. "bq27220: CALMD: %d, SEC0: %d, SEC1: %d, EDV2: %d, VDQ: %d, INITCOMP: %d, SMTH: %d, BTPINT: %d, CFGUPDATE: %d\r\n",
  204. operation_status.CALMD, operation_status.SEC0, operation_status.SEC1,
  205. operation_status.EDV2, operation_status.VDQ, operation_status.INITCOMP,
  206. operation_status.SMTH, operation_status.BTPINT, operation_status.CFGUPDATE
  207. );
  208. // Battery status register, part 1
  209. printf(
  210. "bq27220: CHGINH: %d, FC: %d, OTD: %d, OTC: %d, SLEEP: %d, OCVFAIL: %d, OCVCOMP: %d, FD: %d\r\n",
  211. battery_status.CHGINH, battery_status.FC, battery_status.OTD,
  212. battery_status.OTC, battery_status.SLEEP, battery_status.OCVFAIL,
  213. battery_status.OCVCOMP, battery_status.FD
  214. );
  215. // Battery status register, part 2
  216. printf(
  217. "bq27220: DSG: %d, SYSDWN: %d, TDA: %d, BATTPRES: %d, AUTH_GD: %d, OCVGD: %d, TCA: %d, RSVD: %d\r\n",
  218. battery_status.DSG, battery_status.SYSDWN, battery_status.TDA,
  219. battery_status.BATTPRES, battery_status.AUTH_GD, battery_status.OCVGD,
  220. battery_status.TCA, battery_status.RSVD
  221. );
  222. // Voltage and current info
  223. printf(
  224. "bq27220: Full capacity: %dmAh, Design capacity: %dmAh, Remaining capacity: %dmAh, State of Charge: %d%%, State of health: %d%%\r\n",
  225. bq27220_get_full_charge_capacity(), bq27220_get_design_capacity(), bq27220_get_remaining_capacity(),
  226. bq27220_get_state_of_charge(), bq27220_get_state_of_health()
  227. );
  228. printf(
  229. "bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n",
  230. bq27220_get_voltage(), bq27220_get_current(), (int)furi_hal_power_get_battery_temperature(FuriHalPowerICFuelGauge)
  231. );
  232. }
  233. printf(
  234. "bq25896: VBUS: %d, VSYS: %d, VBAT: %d, Current: %d, NTC: %ldm%%\r\n",
  235. bq25896_get_vbus_voltage(), bq25896_get_vsys_voltage(),
  236. bq25896_get_vbat_voltage(), bq25896_get_vbat_current(),
  237. bq25896_get_ntc_mpct()
  238. );
  239. }
  240. void furi_hal_power_enable_external_3_3v(){
  241. LL_GPIO_SetOutputPin(PERIPH_POWER_GPIO_Port, PERIPH_POWER_Pin);
  242. }
  243. void furi_hal_power_disable_external_3_3v(){
  244. LL_GPIO_ResetOutputPin(PERIPH_POWER_GPIO_Port, PERIPH_POWER_Pin);
  245. }
  246. void furi_hal_power_suppress_charge_enter() {
  247. vTaskSuspendAll();
  248. bool disable_charging = furi_hal_power.suppress_charge == 0;
  249. furi_hal_power.suppress_charge++;
  250. xTaskResumeAll();
  251. if (disable_charging) {
  252. bq25896_disable_charging();
  253. }
  254. }
  255. void furi_hal_power_suppress_charge_exit() {
  256. vTaskSuspendAll();
  257. furi_hal_power.suppress_charge--;
  258. bool enable_charging = furi_hal_power.suppress_charge == 0;
  259. xTaskResumeAll();
  260. if (enable_charging) {
  261. bq25896_enable_charging();
  262. }
  263. }