bq27220.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #define BQ27220_ERROR 0x0
  5. #define BQ27220_SUCCESS 0x1
  6. typedef struct {
  7. // Low byte, Low bit first
  8. bool DSG : 1; // The device is in DISCHARGE
  9. bool SYSDWN : 1; // System down bit indicating the system should shut down
  10. bool TDA : 1; // Terminate Discharge Alarm
  11. bool BATTPRES : 1; // Battery Present detected
  12. bool AUTH_GD : 1; // Detect inserted battery
  13. bool OCVGD : 1; // Good OCV measurement taken
  14. bool TCA : 1; // Terminate Charge Alarm
  15. bool RSVD : 1; // Reserved
  16. // High byte, Low bit first
  17. bool CHGINH : 1; // Charge inhibit
  18. bool FC : 1; // Full-charged is detected
  19. bool OTD : 1; // Overtemperature in discharge condition is detected
  20. bool OTC : 1; // Overtemperature in charge condition is detected
  21. bool SLEEP : 1; // Device is operating in SLEEP mode when set
  22. bool OCVFAIL : 1; // Status bit indicating that the OCV reading failed due to current
  23. bool OCVCOMP : 1; // An OCV measurement update is complete
  24. bool FD : 1; // Full-discharge is detected
  25. } BatteryStatus;
  26. typedef struct {
  27. // Low byte, Low bit first
  28. bool CALMD : 1;
  29. bool SEC0 : 1;
  30. bool SEC1 : 1;
  31. bool EDV2 : 1;
  32. bool VDQ : 1;
  33. bool INITCOMP : 1;
  34. bool SMTH : 1;
  35. bool BTPINT : 1;
  36. // High byte, Low bit first
  37. uint8_t RSVD1 : 2;
  38. bool CFGUPDATE : 1;
  39. uint8_t RSVD0 : 5;
  40. } OperationStatus;
  41. typedef struct {
  42. // Low byte, Low bit first
  43. bool CCT : 1;
  44. bool CSYNC : 1;
  45. bool RSVD0 : 1;
  46. bool EDV_CMP : 1;
  47. bool SC : 1;
  48. bool FIXED_EDV0 : 1;
  49. uint8_t RSVD1 : 2;
  50. // High byte, Low bit first
  51. bool FCC_LIM : 1;
  52. bool RSVD2 : 1;
  53. bool FC_FOR_VDQ : 1;
  54. bool IGNORE_SD : 1;
  55. bool SME0 : 1;
  56. uint8_t RSVD3 : 3;
  57. } GaugingConfig;
  58. typedef struct {
  59. union {
  60. GaugingConfig gauge_conf;
  61. uint16_t gauge_conf_raw;
  62. } cedv_conf;
  63. uint16_t full_charge_cap;
  64. uint16_t design_cap;
  65. uint16_t EDV0;
  66. uint16_t EDV1;
  67. uint16_t EDV2;
  68. uint16_t EMF;
  69. uint16_t C0;
  70. uint16_t R0;
  71. uint16_t T0;
  72. uint16_t R1;
  73. uint8_t TC;
  74. uint8_t C1;
  75. uint16_t DOD0;
  76. uint16_t DOD10;
  77. uint16_t DOD20;
  78. uint16_t DOD30;
  79. uint16_t DOD40;
  80. uint16_t DOD50;
  81. uint16_t DOD60;
  82. uint16_t DOD70;
  83. uint16_t DOD80;
  84. uint16_t DOD90;
  85. uint16_t DOD100;
  86. } ParamCEDV;
  87. /** Initialize Driver
  88. * @return true on success, false otherwise
  89. */
  90. bool bq27220_init(const ParamCEDV* cedv);
  91. /** Get battery voltage in mV or error */
  92. uint16_t bq27220_get_voltage();
  93. /** Get current in mA or error*/
  94. int16_t bq27220_get_current();
  95. /** Get battery status */
  96. uint8_t bq27220_get_battery_status(BatteryStatus* battery_status);
  97. /** Get operation status */
  98. uint8_t bq27220_get_operation_status(OperationStatus* operation_status);
  99. /** Get temperature in units of 0.1°K */
  100. uint16_t bq27220_get_temperature();
  101. /** Get compensated full charge capacity in in mAh */
  102. uint16_t bq27220_get_full_charge_capacity();
  103. /** Get design capacity in mAh */
  104. uint16_t bq27220_get_design_capacity();
  105. /** Get remaining capacity in in mAh */
  106. uint16_t bq27220_get_remaining_capacity();
  107. /** Get predicted remaining battery capacity in percents */
  108. uint16_t bq27220_get_state_of_charge();
  109. /** Get ratio of full charge capacity over design capacity in percents */
  110. uint16_t bq27220_get_state_of_health();
  111. void bq27220_change_design_capacity(uint16_t capacity);