api-hal-power.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <api-hal-power.h>
  2. #include <bq27220.h>
  3. #include <bq25896.h>
  4. void api_hal_power_init() {
  5. bq27220_init();
  6. bq25896_init();
  7. }
  8. uint8_t api_hal_power_get_pct() {
  9. return bq27220_get_state_of_charge();
  10. }
  11. bool api_hal_power_is_charging() {
  12. return bq25896_is_charging();
  13. }
  14. void api_hal_power_off() {
  15. bq25896_poweroff();
  16. }
  17. void api_hal_power_enable_otg() {
  18. bq25896_enable_otg();
  19. }
  20. void api_hal_power_disable_otg() {
  21. bq25896_disable_otg();
  22. }
  23. float api_hal_power_get_battery_voltage() {
  24. return (float)bq27220_get_voltage() / 1000.0f;
  25. }
  26. float api_hal_power_get_battery_current() {
  27. return (float)bq27220_get_current() / 1000.0f;
  28. }
  29. void api_hal_power_dump_state(string_t buffer) {
  30. BatteryStatus battery_status;
  31. OperationStatus operation_status;
  32. if (bq27220_get_battery_status(&battery_status) == BQ27220_ERROR
  33. || bq27220_get_operation_status(&operation_status) == BQ27220_ERROR) {
  34. string_cat_printf(buffer, "Failed to get bq27220 status. Communication error.\r\n");
  35. } else {
  36. string_cat_printf(buffer,
  37. "bq27220: CALMD: %d, SEC0: %d, SEC1: %d, EDV2: %d, VDQ: %d, INITCOMP: %d, SMTH: %d, BTPINT: %d, CFGUPDATE: %d\r\n",
  38. operation_status.CALMD, operation_status.SEC0, operation_status.SEC1,
  39. operation_status.EDV2, operation_status.VDQ, operation_status.INITCOMP,
  40. operation_status.SMTH, operation_status.BTPINT, operation_status.CFGUPDATE
  41. );
  42. // Battery status register, part 1
  43. string_cat_printf(buffer,
  44. "bq27220: CHGINH: %d, FC: %d, OTD: %d, OTC: %d, SLEEP: %d, OCVFAIL: %d, OCVCOMP: %d, FD: %d\r\n",
  45. battery_status.CHGINH, battery_status.FC, battery_status.OTD,
  46. battery_status.OTC, battery_status.SLEEP, battery_status.OCVFAIL,
  47. battery_status.OCVCOMP, battery_status.FD
  48. );
  49. // Battery status register, part 2
  50. string_cat_printf(buffer,
  51. "bq27220: DSG: %d, SYSDWN: %d, TDA: %d, BATTPRES: %d, AUTH_GD: %d, OCVGD: %d, TCA: %d, RSVD: %d\r\n",
  52. battery_status.DSG, battery_status.SYSDWN, battery_status.TDA,
  53. battery_status.BATTPRES, battery_status.AUTH_GD, battery_status.OCVGD,
  54. battery_status.TCA, battery_status.RSVD
  55. );
  56. // Voltage and current info
  57. string_cat_printf(buffer,
  58. "bq27220: Full capacity: %dmAh, Remaining capacity: %dmAh, State of Charge: %d%%\r\n",
  59. bq27220_get_full_charge_capacity(), bq27220_get_remaining_capacity(),
  60. bq27220_get_state_of_charge()
  61. );
  62. string_cat_printf(buffer,
  63. "bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n",
  64. bq27220_get_voltage(), bq27220_get_current(), (bq27220_get_temperature() - 2731)/10
  65. );
  66. }
  67. string_cat_printf(buffer,
  68. "bq25896: VBUS: %d, VSYS: %d, VBAT: %d, Current: %d, NTC: %dm%%\r\n",
  69. bq25896_get_vbus_voltage(), bq25896_get_vsys_voltage(),
  70. bq25896_get_vbat_voltage(), bq25896_get_vbat_current(),
  71. bq25896_get_ntc_mpct()
  72. );
  73. }