2021-03-02 14:45:47 +03:00
|
|
|
#include "bq27220.h"
|
|
|
|
#include "bq27220_reg.h"
|
2021-02-18 15:49:32 +03:00
|
|
|
|
2021-03-02 14:45:47 +03:00
|
|
|
#include <api-hal-i2c.h>
|
2020-11-11 09:31:35 +03:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
uint16_t bq27220_read_word(uint8_t address) {
|
2021-03-10 16:23:54 +03:00
|
|
|
uint8_t buffer[2] = {address};
|
2021-02-18 15:49:32 +03:00
|
|
|
uint16_t ret;
|
2021-03-10 16:23:54 +03:00
|
|
|
with_api_hal_i2c(
|
|
|
|
uint16_t, &ret, () {
|
|
|
|
if(api_hal_i2c_trx(
|
|
|
|
POWER_I2C, BQ27220_ADDRESS, buffer, 1, buffer, 2, BQ27220_I2C_TIMEOUT)) {
|
|
|
|
return *(uint16_t*)buffer;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
2021-02-18 15:49:32 +03:00
|
|
|
return ret;
|
2020-11-11 09:31:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool bq27220_control(uint16_t control) {
|
2021-02-18 15:49:32 +03:00
|
|
|
bool ret;
|
2021-03-10 16:23:54 +03:00
|
|
|
with_api_hal_i2c(
|
|
|
|
bool, &ret, () {
|
|
|
|
uint8_t buffer[3];
|
|
|
|
buffer[0] = CommandControl;
|
|
|
|
buffer[1] = (control >> 8) & 0xFF;
|
|
|
|
buffer[2] = control & 0xFF;
|
|
|
|
return api_hal_i2c_tx(POWER_I2C, BQ27220_ADDRESS, buffer, 3, BQ27220_I2C_TIMEOUT);
|
|
|
|
});
|
2021-02-18 15:49:32 +03:00
|
|
|
return ret;
|
2020-11-11 09:31:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void bq27220_init() {
|
|
|
|
bq27220_control(Control_ENTER_CFG_UPDATE);
|
|
|
|
bq27220_control(Control_SET_PROFILE_2);
|
|
|
|
bq27220_control(Control_EXIT_CFG_UPDATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t bq27220_get_voltage() {
|
|
|
|
return bq27220_read_word(CommandVoltage);
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t bq27220_get_current() {
|
2020-12-02 13:47:13 +03:00
|
|
|
return bq27220_read_word(CommandCurrent);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t bq27220_get_battery_status(BatteryStatus* battery_status) {
|
|
|
|
uint16_t data = bq27220_read_word(CommandBatteryStatus);
|
2021-03-10 16:23:54 +03:00
|
|
|
if(data == BQ27220_ERROR) {
|
2020-12-02 13:47:13 +03:00
|
|
|
return BQ27220_ERROR;
|
|
|
|
} else {
|
|
|
|
*(uint16_t*)battery_status = data;
|
|
|
|
return BQ27220_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t bq27220_get_operation_status(OperationStatus* operation_status) {
|
|
|
|
uint16_t data = bq27220_read_word(CommandOperationStatus);
|
2021-03-10 16:23:54 +03:00
|
|
|
if(data == BQ27220_ERROR) {
|
2020-12-02 13:47:13 +03:00
|
|
|
return BQ27220_ERROR;
|
|
|
|
} else {
|
|
|
|
*(uint16_t*)operation_status = data;
|
|
|
|
return BQ27220_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t bq27220_get_temperature() {
|
|
|
|
return bq27220_read_word(CommandTemperature);
|
2020-11-11 09:31:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t bq27220_get_full_charge_capacity() {
|
|
|
|
return bq27220_read_word(CommandFullChargeCapacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t bq27220_get_remaining_capacity() {
|
|
|
|
return bq27220_read_word(CommandRemainingCapacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t bq27220_get_state_of_charge() {
|
|
|
|
return bq27220_read_word(CommandStateOfCharge);
|
|
|
|
}
|
2021-03-02 19:07:26 +03:00
|
|
|
|
|
|
|
uint16_t bq27220_get_state_of_health() {
|
|
|
|
return bq27220_read_word(CommandStateOfHealth);
|
|
|
|
}
|