Skip to content

Commit

Permalink
bq769x2: Dynamic config & averaging for temperature sensors
Browse files Browse the repository at this point in the history
Configure and read temperature values from all temperature sensors specified in temperature_registers. Calculate and update the average, minimum, and maximum temperature values for the battery cells.
  • Loading branch information
pasrom authored and martinjaeger committed Oct 30, 2023
1 parent f110d83 commit 55c2ea2
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions app/src/bq769x2/bms_bq769x2.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

LOG_MODULE_REGISTER(bq769x0, CONFIG_LOG_DEFAULT_LEVEL);

static const uint8_t temperature_registers[] = { BQ769X2_CMD_TEMP_TS1, BQ769X2_CMD_TEMP_TS3 };
BUILD_ASSERT(ARRAY_SIZE(temperature_registers) >= BOARD_NUM_THERMISTORS_MAX,
"Temperature array size is smaller than configured temperature sensors!");

static void bms_update_balancing(Bms *bms);

static int detect_num_cells(Bms *bms)
Expand Down Expand Up @@ -112,6 +116,19 @@ int bms_init_hardware(Bms *bms)
return err;
}

// configure all temperature sensors specified in temperature_registers
for (size_t i = 0; i < BOARD_NUM_THERMISTORS_MAX; i++) {
// skip BQ769X2_CMD_TEMP_TS1, because standard config is already set to 0x7
if (temperature_registers[i] == BQ769X2_CMD_TEMP_TS1) {
continue;
}

err = bq769x2_datamem_write_u1(temperature_registers[i], 0x7);
if (err) {
return err;
}
}

// set resolution for CC2 current to 10 mA and stack/pack voltage to 10 mV
err = bq769x2_datamem_write_u1(BQ769X2_SET_CONF_DA, 0x06);
if (err) {
Expand Down Expand Up @@ -442,13 +459,25 @@ static int bms_apply_temp_limits(Bms *bms)

void bms_read_temperatures(Bms *bms)
{
int num_temps = 0;
float sum_temps = 0;
int16_t temp = 0; // unit: 0.1 K

/* by default, only TS1 is configured as cell temperature sensor */
bq769x2_direct_read_i2(BQ769X2_CMD_TEMP_TS1, &temp);
bms->status.bat_temp_avg = (temp * 0.1F) - 273.15F;
bms->status.bat_temp_min = bms->status.bat_temp_avg;
bms->status.bat_temp_max = bms->status.bat_temp_avg;
for (size_t i = 0; i < BOARD_NUM_THERMISTORS_MAX; i++) {
bq769x2_direct_read_i2(temperature_registers[i], &temp);
bms->status.bat_temps[i] = (temp * 0.1F) - 273.15F;

if (bms->status.bat_temps[i] < bms->status.bat_temp_min) {
bms->status.bat_temp_min = bms->status.bat_temps[i];
}
if (bms->status.bat_temps[i] > bms->status.bat_temp_max) {
bms->status.bat_temp_max = bms->status.bat_temps[i];
}
num_temps++;
sum_temps += bms->status.bat_temps[i];
}

bms->status.bat_temp_avg = sum_temps / num_temps;

/* MOSFET temperature sensor connected to DCHG pin */
bq769x2_direct_read_i2(BQ769X2_CMD_TEMP_DCHG, &temp);
Expand Down

0 comments on commit 55c2ea2

Please sign in to comment.