bq27220.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. _Static_assert(sizeof(BatteryStatus) == 2, "Incorrect structure size");
  28. typedef struct {
  29. // Low byte, Low bit first
  30. bool CALMD : 1; /**< Calibration mode enabled */
  31. uint8_t SEC : 2; /**< Current security access */
  32. bool EDV2 : 1; /**< EDV2 threshold exceeded */
  33. bool VDQ : 1; /**< Indicates if Current discharge cycle is NOT qualified or qualified for an FCC updated */
  34. bool INITCOMP : 1; /**< gauge initialization is complete */
  35. bool SMTH : 1; /**< RemainingCapacity is scaled by smooth engine */
  36. bool BTPINT : 1; /**< BTP threshold has been crossed */
  37. // High byte, Low bit first
  38. uint8_t RSVD1 : 2;
  39. bool CFGUPDATE : 1; /**< Gauge is in CONFIG UPDATE mode */
  40. uint8_t RSVD0 : 5;
  41. } OperationStatus;
  42. _Static_assert(sizeof(OperationStatus) == 2, "Incorrect structure size");
  43. typedef struct {
  44. // Low byte, Low bit first
  45. bool CCT : 1;
  46. bool CSYNC : 1;
  47. bool RSVD0 : 1;
  48. bool EDV_CMP : 1;
  49. bool SC : 1;
  50. bool FIXED_EDV0 : 1;
  51. uint8_t RSVD1 : 2;
  52. // High byte, Low bit first
  53. bool FCC_LIM : 1;
  54. bool RSVD2 : 1;
  55. bool FC_FOR_VDQ : 1;
  56. bool IGNORE_SD : 1;
  57. bool SME0 : 1;
  58. uint8_t RSVD3 : 3;
  59. } GaugingConfig;
  60. _Static_assert(sizeof(GaugingConfig) == 2, "Incorrect structure size");
  61. typedef struct {
  62. union {
  63. GaugingConfig gauge_conf;
  64. uint16_t gauge_conf_raw;
  65. } cedv_conf;
  66. uint16_t full_charge_cap;
  67. uint16_t design_cap;
  68. uint16_t EDV0;
  69. uint16_t EDV1;
  70. uint16_t EDV2;
  71. uint16_t EMF;
  72. uint16_t C0;
  73. uint16_t R0;
  74. uint16_t T0;
  75. uint16_t R1;
  76. uint8_t TC;
  77. uint8_t C1;
  78. uint16_t DOD0;
  79. uint16_t DOD10;
  80. uint16_t DOD20;
  81. uint16_t DOD30;
  82. uint16_t DOD40;
  83. uint16_t DOD50;
  84. uint16_t DOD60;
  85. uint16_t DOD70;
  86. uint16_t DOD80;
  87. uint16_t DOD90;
  88. uint16_t DOD100;
  89. } ParamCEDV;
  90. /** Initialize Driver
  91. * @return true on success, false otherwise
  92. */
  93. bool bq27220_init(FuriHalI2cBusHandle* handle, const ParamCEDV* cedv);
  94. /** Get battery voltage in mV or error */
  95. uint16_t bq27220_get_voltage(FuriHalI2cBusHandle* handle);
  96. /** Get current in mA or error*/
  97. int16_t bq27220_get_current(FuriHalI2cBusHandle* handle);
  98. /** Get battery status */
  99. uint8_t bq27220_get_battery_status(FuriHalI2cBusHandle* handle, BatteryStatus* battery_status);
  100. /** Get operation status */
  101. uint8_t
  102. bq27220_get_operation_status(FuriHalI2cBusHandle* handle, OperationStatus* operation_status);
  103. /** Get temperature in units of 0.1°K */
  104. uint16_t bq27220_get_temperature(FuriHalI2cBusHandle* handle);
  105. /** Get compensated full charge capacity in in mAh */
  106. uint16_t bq27220_get_full_charge_capacity(FuriHalI2cBusHandle* handle);
  107. /** Get design capacity in mAh */
  108. uint16_t bq27220_get_design_capacity(FuriHalI2cBusHandle* handle);
  109. /** Get remaining capacity in in mAh */
  110. uint16_t bq27220_get_remaining_capacity(FuriHalI2cBusHandle* handle);
  111. /** Get predicted remaining battery capacity in percents */
  112. uint16_t bq27220_get_state_of_charge(FuriHalI2cBusHandle* handle);
  113. /** Get ratio of full charge capacity over design capacity in percents */
  114. uint16_t bq27220_get_state_of_health(FuriHalI2cBusHandle* handle);
  115. void bq27220_change_design_capacity(FuriHalI2cBusHandle* handle, uint16_t capacity);