bq27220.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "bq27220.h"
  2. #include "bq27220_reg.h"
  3. #include <furi-hal-i2c.h>
  4. #include <furi-hal-delay.h>
  5. #include <stdbool.h>
  6. #define TAG "Gauge"
  7. uint16_t bq27220_read_word(uint8_t address) {
  8. uint8_t buffer[2] = {address};
  9. uint16_t ret;
  10. with_furi_hal_i2c(
  11. uint16_t, &ret, () {
  12. if(furi_hal_i2c_trx(
  13. POWER_I2C, BQ27220_ADDRESS, buffer, 1, buffer, 2, BQ27220_I2C_TIMEOUT)) {
  14. return *(uint16_t*)buffer;
  15. } else {
  16. return 0;
  17. }
  18. });
  19. return ret;
  20. }
  21. bool bq27220_control(uint16_t control) {
  22. bool ret;
  23. with_furi_hal_i2c(
  24. bool, &ret, () {
  25. uint8_t buffer[3];
  26. buffer[0] = CommandControl;
  27. buffer[1] = control & 0xFF;
  28. buffer[2] = (control >> 8) & 0xFF;
  29. return furi_hal_i2c_tx(POWER_I2C, BQ27220_ADDRESS, buffer, 3, BQ27220_I2C_TIMEOUT);
  30. });
  31. return ret;
  32. }
  33. uint8_t bq27220_get_checksum(uint8_t* data, uint16_t len) {
  34. uint8_t ret = 0;
  35. for(uint16_t i = 0; i < len; i++) {
  36. ret += data[i];
  37. }
  38. return 0xFF - ret;
  39. }
  40. bool bq27220_set_parameter_u16(uint16_t address, uint16_t value) {
  41. bool ret;
  42. uint8_t buffer[5];
  43. with_furi_hal_i2c(
  44. bool, &ret, () {
  45. buffer[0] = CommandSelectSubclass;
  46. buffer[1] = address & 0xFF;
  47. buffer[2] = (address >> 8) & 0xFF;
  48. buffer[3] = (value >> 8) & 0xFF;
  49. buffer[4] = value & 0xFF;
  50. return furi_hal_i2c_tx(POWER_I2C, BQ27220_ADDRESS, buffer, 5, BQ27220_I2C_TIMEOUT);
  51. });
  52. delay_us(10000);
  53. uint8_t checksum = bq27220_get_checksum(&buffer[1], 4);
  54. with_furi_hal_i2c(
  55. bool, &ret, () {
  56. buffer[0] = CommandMACDataSum;
  57. buffer[1] = checksum;
  58. buffer[2] = 6;
  59. return furi_hal_i2c_tx(POWER_I2C, BQ27220_ADDRESS, buffer, 3, BQ27220_I2C_TIMEOUT);
  60. });
  61. delay_us(10000);
  62. return ret;
  63. }
  64. bool bq27220_init(const ParamCEDV* cedv) {
  65. uint32_t timeout = 100;
  66. uint16_t design_cap = bq27220_get_design_capacity();
  67. if(cedv->design_cap == design_cap) {
  68. FURI_LOG_I(TAG, "Skip battery profile update");
  69. return true;
  70. }
  71. FURI_LOG_I(TAG, "Start updating battery profile");
  72. OperationStatus status = {};
  73. if(!bq27220_control(Control_ENTER_CFG_UPDATE)) {
  74. FURI_LOG_E(TAG, "Can't configure update");
  75. return false;
  76. };
  77. while((status.CFGUPDATE != 1) && (timeout-- > 0)) {
  78. bq27220_get_operation_status(&status);
  79. }
  80. bq27220_set_parameter_u16(AddressGaugingConfig, cedv->cedv_conf.gauge_conf_raw);
  81. bq27220_set_parameter_u16(AddressFullChargeCapacity, cedv->full_charge_cap);
  82. bq27220_set_parameter_u16(AddressDesignCapacity, cedv->design_cap);
  83. bq27220_set_parameter_u16(AddressEMF, cedv->EMF);
  84. bq27220_set_parameter_u16(AddressC0, cedv->C0);
  85. bq27220_set_parameter_u16(AddressR0, cedv->R0);
  86. bq27220_set_parameter_u16(AddressT0, cedv->T0);
  87. bq27220_set_parameter_u16(AddressR1, cedv->R1);
  88. bq27220_set_parameter_u16(AddressTC, (cedv->TC) << 8 | cedv->C1);
  89. bq27220_set_parameter_u16(AddressStartDOD0, cedv->DOD0);
  90. bq27220_set_parameter_u16(AddressStartDOD10, cedv->DOD10);
  91. bq27220_set_parameter_u16(AddressStartDOD20, cedv->DOD20);
  92. bq27220_set_parameter_u16(AddressStartDOD30, cedv->DOD30);
  93. bq27220_set_parameter_u16(AddressStartDOD40, cedv->DOD40);
  94. bq27220_set_parameter_u16(AddressStartDOD50, cedv->DOD40);
  95. bq27220_set_parameter_u16(AddressStartDOD60, cedv->DOD60);
  96. bq27220_set_parameter_u16(AddressStartDOD70, cedv->DOD70);
  97. bq27220_set_parameter_u16(AddressStartDOD80, cedv->DOD80);
  98. bq27220_set_parameter_u16(AddressStartDOD90, cedv->DOD90);
  99. bq27220_set_parameter_u16(AddressStartDOD100, cedv->DOD100);
  100. bq27220_set_parameter_u16(AddressEDV0, cedv->EDV0);
  101. bq27220_set_parameter_u16(AddressEDV1, cedv->EDV1);
  102. bq27220_set_parameter_u16(AddressEDV2, cedv->EDV2);
  103. bq27220_control(Control_EXIT_CFG_UPDATE);
  104. delay_us(10000);
  105. design_cap = bq27220_get_design_capacity();
  106. if(cedv->design_cap == design_cap) {
  107. FURI_LOG_I(TAG, "Battery profile update success");
  108. return true;
  109. } else {
  110. FURI_LOG_E(TAG, "Battery profile update failed");
  111. return false;
  112. }
  113. }
  114. uint16_t bq27220_get_voltage() {
  115. return bq27220_read_word(CommandVoltage);
  116. }
  117. int16_t bq27220_get_current() {
  118. return bq27220_read_word(CommandCurrent);
  119. }
  120. uint8_t bq27220_get_battery_status(BatteryStatus* battery_status) {
  121. uint16_t data = bq27220_read_word(CommandBatteryStatus);
  122. if(data == BQ27220_ERROR) {
  123. return BQ27220_ERROR;
  124. } else {
  125. *(uint16_t*)battery_status = data;
  126. return BQ27220_SUCCESS;
  127. }
  128. }
  129. uint8_t bq27220_get_operation_status(OperationStatus* operation_status) {
  130. uint16_t data = bq27220_read_word(CommandOperationStatus);
  131. if(data == BQ27220_ERROR) {
  132. return BQ27220_ERROR;
  133. } else {
  134. *(uint16_t*)operation_status = data;
  135. return BQ27220_SUCCESS;
  136. }
  137. }
  138. uint16_t bq27220_get_temperature() {
  139. return bq27220_read_word(CommandTemperature);
  140. }
  141. uint16_t bq27220_get_full_charge_capacity() {
  142. return bq27220_read_word(CommandFullChargeCapacity);
  143. }
  144. uint16_t bq27220_get_design_capacity() {
  145. return bq27220_read_word(CommandDesignCapacity);
  146. }
  147. uint16_t bq27220_get_remaining_capacity() {
  148. return bq27220_read_word(CommandRemainingCapacity);
  149. }
  150. uint16_t bq27220_get_state_of_charge() {
  151. return bq27220_read_word(CommandStateOfCharge);
  152. }
  153. uint16_t bq27220_get_state_of_health() {
  154. return bq27220_read_word(CommandStateOfHealth);
  155. }