api-hal-power.c 6.9 KB

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