api-hal-power.c 7.5 KB

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