7static const char *
const TAG =
"lc709203f.sensor";
10static const uint8_t LC709203F_I2C_ADDR_DEFAULT = 0x0B;
13static const uint8_t LC709203F_BEFORE_RSOC = 0x04;
14static const uint8_t LC709203F_THERMISTOR_B = 0x06;
15static const uint8_t LC709203F_INITIAL_RSOC = 0x07;
16static const uint8_t LC709203F_CELL_TEMPERATURE = 0x08;
17static const uint8_t LC709203F_CELL_VOLTAGE = 0x09;
18static const uint8_t LC709203F_CURRENT_DIRECTION = 0x0A;
19static const uint8_t LC709203F_APA = 0x0B;
20static const uint8_t LC709203F_APT = 0x0C;
21static const uint8_t LC709203F_RSOC = 0x0D;
22static const uint8_t LC709203F_ITE = 0x0F;
23static const uint8_t LC709203F_IC_VERSION = 0x11;
24static const uint8_t LC709203F_CHANGE_OF_THE_PARAMETER = 0x12;
25static const uint8_t LC709203F_ALARM_LOW_RSOC = 0x13;
26static const uint8_t LC709203F_ALARM_LOW_CELL_VOLTAGE = 0x14;
27static const uint8_t LC709203F_IC_POWER_MODE = 0x15;
28static const uint8_t LC709203F_STATUS_BIT = 0x16;
29static const uint8_t LC709203F_NUMBER_OF_THE_PARAMETER = 0x1A;
31static const uint8_t LC709203F_POWER_MODE_ON = 0x0001;
32static const uint8_t LC709203F_POWER_MODE_SLEEP = 0x0002;
36static const uint8_t LC709203F_I2C_RETRY_COUNT = 10;
52 ESP_LOGCONFIG(TAG,
"Running setup");
60 if (this->set_register_(LC709203F_IC_POWER_MODE, LC709203F_POWER_MODE_ON) !=
i2c::NO_ERROR) {
82 if (this->get_register_(LC709203F_CELL_VOLTAGE, &buffer) ==
i2c::NO_ERROR) {
89 if (this->get_register_(LC709203F_ITE, &buffer) ==
i2c::NO_ERROR) {
99 if (this->get_register_(LC709203F_CELL_TEMPERATURE, &buffer) ==
i2c::NO_ERROR) {
109 if (this->set_register_(LC709203F_IC_POWER_MODE, LC709203F_POWER_MODE_ON) !=
i2c::NO_ERROR) {
126 if (this->set_register_(LC709203F_INITIAL_RSOC, 0xAA55) ==
i2c::NO_ERROR) {
133 if (this->set_register_(LC709203F_STATUS_BIT, 0x0001) ==
i2c::NO_ERROR) {
138 }
else if (this->set_register_(LC709203F_STATUS_BIT, 0x0000) ==
i2c::NO_ERROR) {
150 ESP_LOGCONFIG(TAG,
"LC709203F:");
151 LOG_I2C_DEVICE(
this);
153 LOG_UPDATE_INTERVAL(
this);
155 " Pack Size: %d mAH\n"
161 ESP_LOGCONFIG(TAG,
" Pack Rated Voltage: 3.%sV", this->
pack_voltage_ == 0x0000 ?
"8" :
"7");
168 ESP_LOGCONFIG(TAG,
" B_Constant: %d", this->
b_constant_);
170 ESP_LOGCONFIG(TAG,
" No Temperature Sensor");
174uint8_t Lc709203f::get_register_(uint8_t register_to_read, uint16_t *register_value) {
176 uint8_t read_buffer[6];
178 read_buffer[0] = (this->
address_) << 1;
179 read_buffer[1] = register_to_read;
180 read_buffer[2] = ((this->
address_) << 1) | 0x01;
182 for (uint8_t i = 0; i <= LC709203F_I2C_RETRY_COUNT; i++) {
189 return_code = this->
read_register(register_to_read, &read_buffer[3], 3,
false);
193 str_sprintf(
"Error code %d when reading from register 0x%02X", return_code, register_to_read).c_str());
194 }
else if (this->crc8_(read_buffer, 5) != read_buffer[5]) {
198 *register_value = ((uint16_t) read_buffer[4] << 8) | (uint16_t) read_buffer[3];
207 *register_value = 0x0000;
215uint8_t Lc709203f::set_register_(uint8_t register_to_set, uint16_t value_to_set) {
217 uint8_t write_buffer[5];
221 write_buffer[0] = (this->
address_) << 1;
222 write_buffer[1] = register_to_set;
223 write_buffer[2] = value_to_set & 0xFF;
224 write_buffer[3] = (value_to_set >> 8) & 0xFF;
225 write_buffer[4] = this->crc8_(write_buffer, 4);
227 for (uint8_t i = 0; i <= LC709203F_I2C_RETRY_COUNT; i++) {
230 return_code = this->
write(&write_buffer[1], 4,
true);
235 str_sprintf(
"Error code %d when writing to register 0x%02X", return_code, register_to_set).c_str());
244uint8_t Lc709203f::crc8_(uint8_t *byte_buffer, uint8_t length_of_crc) {
246 const uint8_t polynomial(0x07);
248 for (uint8_t j = length_of_crc; j; --j) {
249 crc ^= *byte_buffer++;
251 for (uint8_t i = 8; i; --i) {
252 crc = (crc & 0x80) ? (crc << 1) ^ polynomial : (crc << 1);
259 static const uint16_t PACK_SIZE_ARRAY[6] = {100, 200, 500, 1000, 2000, 3000};
260 static const uint16_t APA_ARRAY[6] = {0x08, 0x0B, 0x10, 0x19, 0x2D, 0x36};
268 for (uint8_t i = 0; i < 6; i++) {
269 if (PACK_SIZE_ARRAY[i] == pack_size) {
271 this->
apa_ = APA_ARRAY[i];
273 }
else if ((i > 0) && (PACK_SIZE_ARRAY[i] > pack_size) && (PACK_SIZE_ARRAY[i - 1] < pack_size)) {
278 slope =
static_cast<float>(APA_ARRAY[i] - APA_ARRAY[i - 1]) /
279 static_cast<float>(PACK_SIZE_ARRAY[i] - PACK_SIZE_ARRAY[i - 1]);
282 intercept =
static_cast<float>(APA_ARRAY[i]) - slope *
static_cast<float>(PACK_SIZE_ARRAY[i]);
284 this->
apa_ =
static_cast<uint8_t
>(slope * pack_size + intercept);