bq27220.h 3.7 KB

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