api-hal-power.c 5.5 KB

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