api-hal-power.c 6.2 KB

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