api-hal-power.c 6.0 KB

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