api-hal-power.c 7.1 KB

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