8static const char *
const TAG =
"lc709203f.sensor";
11static const uint8_t LC709203F_I2C_ADDR_DEFAULT = 0x0B;
14static const uint8_t LC709203F_BEFORE_RSOC = 0x04;
15static const uint8_t LC709203F_THERMISTOR_B = 0x06;
16static const uint8_t LC709203F_INITIAL_RSOC = 0x07;
17static const uint8_t LC709203F_CELL_TEMPERATURE = 0x08;
18static const uint8_t LC709203F_CELL_VOLTAGE = 0x09;
19static const uint8_t LC709203F_CURRENT_DIRECTION = 0x0A;
20static const uint8_t LC709203F_APA = 0x0B;
21static const uint8_t LC709203F_APT = 0x0C;
22static const uint8_t LC709203F_RSOC = 0x0D;
23static const uint8_t LC709203F_ITE = 0x0F;
24static const uint8_t LC709203F_IC_VERSION = 0x11;
25static const uint8_t LC709203F_CHANGE_OF_THE_PARAMETER = 0x12;
26static const uint8_t LC709203F_ALARM_LOW_RSOC = 0x13;
27static const uint8_t LC709203F_ALARM_LOW_CELL_VOLTAGE = 0x14;
28static const uint8_t LC709203F_IC_POWER_MODE = 0x15;
29static const uint8_t LC709203F_STATUS_BIT = 0x16;
30static const uint8_t LC709203F_NUMBER_OF_THE_PARAMETER = 0x1A;
32static const uint8_t LC709203F_POWER_MODE_ON = 0x0001;
33static const uint8_t LC709203F_POWER_MODE_SLEEP = 0x0002;
37static const uint8_t LC709203F_I2C_RETRY_COUNT = 10;
59 if (this->set_register_(LC709203F_IC_POWER_MODE, LC709203F_POWER_MODE_ON) !=
i2c::NO_ERROR) {
81 if (this->get_register_(LC709203F_CELL_VOLTAGE, &buffer) ==
i2c::NO_ERROR) {
88 if (this->get_register_(LC709203F_ITE, &buffer) ==
i2c::NO_ERROR) {
98 if (this->get_register_(LC709203F_CELL_TEMPERATURE, &buffer) ==
i2c::NO_ERROR) {
108 if (this->set_register_(LC709203F_IC_POWER_MODE, LC709203F_POWER_MODE_ON) !=
i2c::NO_ERROR) {
125 if (this->set_register_(LC709203F_INITIAL_RSOC, 0xAA55) ==
i2c::NO_ERROR) {
132 if (this->set_register_(LC709203F_STATUS_BIT, 0x0001) ==
i2c::NO_ERROR) {
137 }
else if (this->set_register_(LC709203F_STATUS_BIT, 0x0000) ==
i2c::NO_ERROR) {
149 ESP_LOGCONFIG(TAG,
"LC709203F:");
150 LOG_I2C_DEVICE(
this);
152 LOG_UPDATE_INTERVAL(
this);
154 " Pack Size: %d mAH\n"
160 ESP_LOGCONFIG(TAG,
" Pack Rated Voltage: 3.%sV", this->
pack_voltage_ == 0x0000 ?
"8" :
"7");
167 ESP_LOGCONFIG(TAG,
" B_Constant: %d", this->
b_constant_);
169 ESP_LOGCONFIG(TAG,
" No Temperature Sensor");
173uint8_t Lc709203f::get_register_(uint8_t register_to_read, uint16_t *register_value) {
175 uint8_t read_buffer[6];
177 read_buffer[0] = (this->
address_) << 1;
178 read_buffer[1] = register_to_read;
179 read_buffer[2] = ((this->
address_) << 1) | 0x01;
181 for (uint8_t i = 0; i <= LC709203F_I2C_RETRY_COUNT; i++) {
188 return_code = this->
read_register(register_to_read, &read_buffer[3], 3);
192 str_sprintf(
"Error code %d when reading from register 0x%02X", return_code, register_to_read).c_str());
193 }
else if (
crc8(read_buffer, 5, 0x00, 0x07,
true) != read_buffer[5]) {
197 *register_value = ((uint16_t) read_buffer[4] << 8) | (uint16_t) read_buffer[3];
206 *register_value = 0x0000;
214uint8_t Lc709203f::set_register_(uint8_t register_to_set, uint16_t value_to_set) {
216 uint8_t write_buffer[5];
220 write_buffer[0] = (this->
address_) << 1;
221 write_buffer[1] = register_to_set;
222 write_buffer[2] = value_to_set & 0xFF;
223 write_buffer[3] = (value_to_set >> 8) & 0xFF;
224 write_buffer[4] =
crc8(write_buffer, 4, 0x00, 0x07,
true);
226 for (uint8_t i = 0; i <= LC709203F_I2C_RETRY_COUNT; i++) {
229 return_code = this->
write(&write_buffer[1], 4);
234 str_sprintf(
"Error code %d when writing to register 0x%02X", return_code, register_to_set).c_str());
244 static const uint16_t PACK_SIZE_ARRAY[6] = {100, 200, 500, 1000, 2000, 3000};
245 static const uint16_t APA_ARRAY[6] = {0x08, 0x0B, 0x10, 0x19, 0x2D, 0x36};
253 for (uint8_t i = 0; i < 6; i++) {
254 if (PACK_SIZE_ARRAY[i] == pack_size) {
256 this->
apa_ = APA_ARRAY[i];
258 }
else if ((i > 0) && (PACK_SIZE_ARRAY[i] > pack_size) && (PACK_SIZE_ARRAY[i - 1] < pack_size)) {
263 slope =
static_cast<float>(APA_ARRAY[i] - APA_ARRAY[i - 1]) /
264 static_cast<float>(PACK_SIZE_ARRAY[i] - PACK_SIZE_ARRAY[i - 1]);
267 intercept =
static_cast<float>(APA_ARRAY[i]) - slope *
static_cast<float>(PACK_SIZE_ARRAY[i]);
269 this->
apa_ =
static_cast<uint8_t
>(slope * pack_size + intercept);
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.