api-hal-power.c 7.2 KB

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