Skip to content

Commit

Permalink
sensor: bq274xx: use two bytes write for settings
Browse files Browse the repository at this point in the history
Use a combination of sys_cpu_to_be16 and i2c_burst_write_dt for setting
16 bits registers. Get rid of a bunch of temporary variables, custom
conversions and few bus writes.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
  • Loading branch information
fabiobaltieri authored and MaureenHelm committed Jul 21, 2023
1 parent ec98bf7 commit bb0135b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 63 deletions.
83 changes: 22 additions & 61 deletions drivers/sensor/bq274xx/bq274xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static int bq274xx_cmd_reg_read(const struct device *dev, uint8_t reg_addr,
return -EIO;
}

*val = (i2c_data[1] << 8) | i2c_data[0];
*val = sys_get_le16(i2c_data);

return 0;
}
Expand Down Expand Up @@ -136,9 +136,7 @@ static int bq274xx_gauge_configure(const struct device *dev)
const struct bq274xx_regs *regs = data->regs;
int ret;
uint8_t tmp_checksum, checksum_old, checksum_new;
uint16_t flags, designenergy_mwh, taperrate;
uint8_t designcap_msb, designcap_lsb, designenergy_msb, designenergy_lsb,
terminatevolt_msb, terminatevolt_lsb, taperrate_msb, taperrate_lsb;
uint16_t flags, designenergy_mwh, taperrate, reg_val;
uint8_t block[BQ27XXX_DM_SZ];
uint8_t try;

Expand Down Expand Up @@ -223,76 +221,39 @@ static int bq274xx_gauge_configure(const struct device *dev)
return -EIO;
}

designcap_msb = config->design_capacity >> 8;
designcap_lsb = config->design_capacity & 0x00FF;
designenergy_msb = designenergy_mwh >> 8;
designenergy_lsb = designenergy_mwh & 0x00FF;
terminatevolt_msb = config->terminate_voltage >> 8;
terminatevolt_lsb = config->terminate_voltage & 0x00FF;
taperrate_msb = taperrate >> 8;
taperrate_lsb = taperrate & 0x00FF;

ret = i2c_reg_write_byte_dt(&config->i2c,
BQ274XX_EXT_BLKDAT_HIGH(regs->dm_design_capacity),
designcap_msb);
if (ret < 0) {
LOG_ERR("Failed to write designCAP MSB");
return -EIO;
}

ret = i2c_reg_write_byte_dt(&config->i2c,
BQ274XX_EXT_BLKDAT_LOW(regs->dm_design_capacity),
designcap_lsb);
if (ret < 0) {
LOG_ERR("Failed to write designCAP LSB");
return -EIO;
}

ret = i2c_reg_write_byte_dt(&config->i2c,
BQ274XX_EXT_BLKDAT_HIGH(regs->dm_design_energy),
designenergy_msb);
if (ret < 0) {
LOG_ERR("Failed to write designEnergy MSB");
return -EIO;
}

ret = i2c_reg_write_byte_dt(&config->i2c,
BQ274XX_EXT_BLKDAT_LOW(regs->dm_design_energy),
designenergy_lsb);
if (ret < 0) {
LOG_ERR("Failed to write designEnergy LSB");
return -EIO;
}

ret = i2c_reg_write_byte_dt(&config->i2c,
BQ274XX_EXT_BLKDAT_HIGH(regs->dm_terminate_voltage),
terminatevolt_msb);
reg_val = sys_cpu_to_be16(config->design_capacity);
ret = i2c_burst_write_dt(&config->i2c,
BQ274XX_EXT_BLKDAT(regs->dm_design_capacity),
(uint8_t *)&reg_val, sizeof(reg_val));
if (ret < 0) {
LOG_ERR("Failed to write terminateVolt MSB");
LOG_ERR("Failed to write design capacity");
return -EIO;
}

ret = i2c_reg_write_byte_dt(&config->i2c,
BQ274XX_EXT_BLKDAT_LOW(regs->dm_terminate_voltage),
terminatevolt_lsb);
reg_val = sys_cpu_to_be16(designenergy_mwh);
ret = i2c_burst_write_dt(&config->i2c,
BQ274XX_EXT_BLKDAT(regs->dm_design_energy),
(uint8_t *)&reg_val, sizeof(reg_val));
if (ret < 0) {
LOG_ERR("Failed to write terminateVolt LSB");
LOG_ERR("Failed to write design energy");
return -EIO;
}

ret = i2c_reg_write_byte_dt(&config->i2c,
BQ274XX_EXT_BLKDAT_HIGH(regs->dm_taper_rate),
taperrate_msb);
reg_val = sys_cpu_to_be16(config->terminate_voltage);
ret = i2c_burst_write_dt(&config->i2c,
BQ274XX_EXT_BLKDAT(regs->dm_terminate_voltage),
(uint8_t *)&reg_val, sizeof(reg_val));
if (ret < 0) {
LOG_ERR("Failed to write taperRate MSB");
LOG_ERR("Failed to write terminate voltage");
return -EIO;
}

ret = i2c_reg_write_byte_dt(&config->i2c,
BQ274XX_EXT_BLKDAT_LOW(regs->dm_taper_rate),
taperrate_lsb);
reg_val = sys_cpu_to_be16(taperrate);
ret = i2c_burst_write_dt(&config->i2c,
BQ274XX_EXT_BLKDAT(regs->dm_taper_rate),
(uint8_t *)&reg_val, sizeof(reg_val));
if (ret < 0) {
LOG_ERR("Failed to write taperRate LSB");
LOG_ERR("Failed to write taper rate");
return -EIO;
}

Expand Down
3 changes: 1 addition & 2 deletions drivers/sensor/bq274xx/bq274xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@
#define BQ274XX_EXT_BLKDAT_END 0x5F /* BlockData_end() */
#define BQ274XX_EXT_CHECKSUM 0x60 /* BlockDataCheckSum() */
#define BQ274XX_EXT_DATA_CONTROL 0x61 /* BlockDataControl() */
#define BQ274XX_EXT_BLKDAT_HIGH(off) (BQ274XX_EXT_BLKDAT_START + off)
#define BQ274XX_EXT_BLKDAT_LOW(off) (BQ274XX_EXT_BLKDAT_START + off + 1)
#define BQ274XX_EXT_BLKDAT(off) (BQ274XX_EXT_BLKDAT_START + off)

/* Hold the register offset for a device variant. */
struct bq274xx_regs {
Expand Down

0 comments on commit bb0135b

Please sign in to comment.