furi-hal-power.c 7.8 KB

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