api-hal-power.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include <api-hal-power.h>
  2. #include <api-hal-clock.h>
  3. #include <api-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. volatile uint32_t api_hal_power_insomnia = 1;
  14. void HAL_RCC_CSSCallback(void) {
  15. // TODO: notify user about issue with HSE
  16. NVIC_SystemReset();
  17. }
  18. void api_hal_power_init() {
  19. LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN);
  20. bq27220_init();
  21. bq25896_init();
  22. }
  23. uint16_t api_hal_power_insomnia_level() {
  24. return api_hal_power_insomnia;
  25. }
  26. void api_hal_power_insomnia_enter() {
  27. api_hal_power_insomnia++;
  28. }
  29. void api_hal_power_insomnia_exit() {
  30. api_hal_power_insomnia--;
  31. }
  32. bool api_hal_power_deep_available() {
  33. return api_hal_bt_is_alive() && api_hal_power_insomnia == 0;
  34. }
  35. void api_hal_power_light_sleep() {
  36. __WFI();
  37. }
  38. void api_hal_power_deep_sleep() {
  39. while( LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID));
  40. if (!LL_HSEM_1StepLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID)) {
  41. if(LL_PWR_IsActiveFlag_C2DS()) {
  42. // Release ENTRY_STOP_MODE semaphore
  43. LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
  44. // The switch on HSI before entering Stop Mode is required
  45. api_hal_clock_switch_to_hsi();
  46. }
  47. } else {
  48. /**
  49. * The switch on HSI before entering Stop Mode is required
  50. */
  51. api_hal_clock_switch_to_hsi();
  52. }
  53. /* Release RCC semaphore */
  54. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  55. // Prepare deep sleep
  56. LL_PWR_SetPowerMode(LL_PWR_MODE_STOP1);
  57. LL_LPM_EnableDeepSleep();
  58. #if defined ( __CC_ARM)
  59. // Force store operations
  60. __force_stores();
  61. #endif
  62. __WFI();
  63. /* Release ENTRY_STOP_MODE semaphore */
  64. LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
  65. while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID));
  66. if(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {
  67. api_hal_clock_switch_to_pll();
  68. }
  69. LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
  70. }
  71. void api_hal_power_sleep() {
  72. if(api_hal_power_deep_available()) {
  73. api_hal_power_deep_sleep();
  74. } else {
  75. api_hal_power_light_sleep();
  76. }
  77. }
  78. uint8_t api_hal_power_get_pct() {
  79. return bq27220_get_state_of_charge();
  80. }
  81. uint8_t api_hal_power_get_bat_health_pct() {
  82. return bq27220_get_state_of_health();
  83. }
  84. bool api_hal_power_is_charging() {
  85. return bq25896_is_charging();
  86. }
  87. void api_hal_power_off() {
  88. bq25896_poweroff();
  89. }
  90. void api_hal_power_enable_otg() {
  91. bq25896_enable_otg();
  92. }
  93. void api_hal_power_disable_otg() {
  94. bq25896_disable_otg();
  95. }
  96. uint32_t api_hal_power_get_battery_remaining_capacity() {
  97. return bq27220_get_remaining_capacity();
  98. }
  99. uint32_t api_hal_power_get_battery_full_capacity() {
  100. return bq27220_get_full_charge_capacity();
  101. }
  102. float api_hal_power_get_battery_voltage(ApiHalPowerIC ic) {
  103. if (ic == ApiHalPowerICCharger) {
  104. return (float)bq25896_get_vbat_voltage() / 1000.0f;
  105. } else if (ic == ApiHalPowerICFuelGauge) {
  106. return (float)bq27220_get_voltage() / 1000.0f;
  107. } else {
  108. return 0.0f;
  109. }
  110. }
  111. float api_hal_power_get_battery_current(ApiHalPowerIC ic) {
  112. if (ic == ApiHalPowerICCharger) {
  113. return (float)bq25896_get_vbat_current() / 1000.0f;
  114. } else if (ic == ApiHalPowerICFuelGauge) {
  115. return (float)bq27220_get_current() / 1000.0f;
  116. } else {
  117. return 0.0f;
  118. }
  119. }
  120. float api_hal_power_get_battery_temperature(ApiHalPowerIC ic) {
  121. if (ic == ApiHalPowerICCharger) {
  122. // Linear approximation, +/- 5 C
  123. return (71.0f - (float)bq25896_get_ntc_mpct()/1000) / 0.6f;
  124. } else if (ic == ApiHalPowerICFuelGauge) {
  125. return ((float)bq27220_get_temperature() - 2731.0f) / 10.0f;
  126. } else {
  127. return 0.0f;
  128. }
  129. }
  130. float api_hal_power_get_usb_voltage(){
  131. return (float)bq25896_get_vbus_voltage() / 1000.0f;;
  132. }
  133. void api_hal_power_dump_state(string_t buffer) {
  134. BatteryStatus battery_status;
  135. OperationStatus operation_status;
  136. if (bq27220_get_battery_status(&battery_status) == BQ27220_ERROR
  137. || bq27220_get_operation_status(&operation_status) == BQ27220_ERROR) {
  138. string_cat_printf(buffer, "Failed to get bq27220 status. Communication error.\r\n");
  139. } else {
  140. string_cat_printf(buffer,
  141. "bq27220: CALMD: %d, SEC0: %d, SEC1: %d, EDV2: %d, VDQ: %d, INITCOMP: %d, SMTH: %d, BTPINT: %d, CFGUPDATE: %d\r\n",
  142. operation_status.CALMD, operation_status.SEC0, operation_status.SEC1,
  143. operation_status.EDV2, operation_status.VDQ, operation_status.INITCOMP,
  144. operation_status.SMTH, operation_status.BTPINT, operation_status.CFGUPDATE
  145. );
  146. // Battery status register, part 1
  147. string_cat_printf(buffer,
  148. "bq27220: CHGINH: %d, FC: %d, OTD: %d, OTC: %d, SLEEP: %d, OCVFAIL: %d, OCVCOMP: %d, FD: %d\r\n",
  149. battery_status.CHGINH, battery_status.FC, battery_status.OTD,
  150. battery_status.OTC, battery_status.SLEEP, battery_status.OCVFAIL,
  151. battery_status.OCVCOMP, battery_status.FD
  152. );
  153. // Battery status register, part 2
  154. string_cat_printf(buffer,
  155. "bq27220: DSG: %d, SYSDWN: %d, TDA: %d, BATTPRES: %d, AUTH_GD: %d, OCVGD: %d, TCA: %d, RSVD: %d\r\n",
  156. battery_status.DSG, battery_status.SYSDWN, battery_status.TDA,
  157. battery_status.BATTPRES, battery_status.AUTH_GD, battery_status.OCVGD,
  158. battery_status.TCA, battery_status.RSVD
  159. );
  160. // Voltage and current info
  161. string_cat_printf(buffer,
  162. "bq27220: Full capacity: %dmAh, Remaining capacity: %dmAh, State of Charge: %d%%, State of health: %d%%\r\n",
  163. bq27220_get_full_charge_capacity(), bq27220_get_remaining_capacity(),
  164. bq27220_get_state_of_charge(), bq27220_get_state_of_health()
  165. );
  166. string_cat_printf(buffer,
  167. "bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n",
  168. bq27220_get_voltage(), bq27220_get_current(), (int)api_hal_power_get_battery_temperature(ApiHalPowerICFuelGauge)
  169. );
  170. }
  171. string_cat_printf(buffer,
  172. "bq25896: VBUS: %d, VSYS: %d, VBAT: %d, Current: %d, NTC: %dm%%\r\n",
  173. bq25896_get_vbus_voltage(), bq25896_get_vsys_voltage(),
  174. bq25896_get_vbat_voltage(), bq25896_get_vbat_current(),
  175. bq25896_get_ntc_mpct()
  176. );
  177. }
  178. void api_hal_power_enable_external_3_3v(){
  179. LL_GPIO_SetOutputPin(PERIPH_POWER_GPIO_Port, PERIPH_POWER_Pin);
  180. }
  181. void api_hal_power_disable_external_3_3v(){
  182. LL_GPIO_ResetOutputPin(PERIPH_POWER_GPIO_Port, PERIPH_POWER_Pin);
  183. }