Skip to content

Commit

Permalink
sensor: bq274xx: add few defines for magic numbers
Browse files Browse the repository at this point in the history
Add a couple of define for the data memory block size and config flag
bit.

Signed-off-by: Fabio Baltieri <[email protected]>
  • Loading branch information
fabiobaltieri authored and MaureenHelm committed Jul 21, 2023
1 parent 9bc43ac commit 35bb28a
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions drivers/sensor/bq274xx/bq274xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ LOG_MODULE_REGISTER(bq274xx, CONFIG_SENSOR_LOG_LEVEL);
/* Time it takes device to initialize before doing any configuration */
#define INIT_TIME 100U

/* Data memory size */
#define BQ27XXX_DM_SZ 32

/* Config update mode flag */
#define BQ27XXX_FLAG_CFGUP BIT(4)

static int bq274xx_cmd_reg_read(const struct device *dev, uint8_t reg_addr,
int16_t *val)
{
const struct bq274xx_config *config = dev->config;
uint8_t i2c_data[2];
int ret;

ret = i2c_burst_read_dt(&config->i2c, reg_addr, i2c_data, 2);
ret = i2c_burst_read_dt(&config->i2c, reg_addr, i2c_data, sizeof(i2c_data));
if (ret < 0) {
LOG_ERR("Unable to read register");
return -EIO;
Expand All @@ -58,7 +64,7 @@ static int bq274xx_ctrl_reg_write(const struct device *dev, uint16_t subcommand)
subcommand >> 8,
};

ret = i2c_write_dt(&config->i2c, tx_buf, 3);
ret = i2c_write_dt(&config->i2c, tx_buf, sizeof(tx_buf));
if (ret < 0) {
LOG_ERR("Failed to write into control register");
return -EIO;
Expand Down Expand Up @@ -132,7 +138,7 @@ static int bq274xx_gauge_configure(const struct device *dev)
uint16_t flags, designenergy_mwh, taperrate;
uint8_t designcap_msb, designcap_lsb, designenergy_msb, designenergy_lsb,
terminatevolt_msb, terminatevolt_lsb, taperrate_msb, taperrate_lsb;
uint8_t block[32];
uint8_t block[BQ27XXX_DM_SZ];

designenergy_mwh = (uint16_t)3.7 * config->design_capacity;
taperrate = (uint16_t)config->design_capacity / (0.1 * config->taper_current);
Expand Down Expand Up @@ -165,11 +171,11 @@ static int bq274xx_gauge_configure(const struct device *dev)
return -EIO;
}

if (!(flags & 0x0010)) {
if (!(flags & BQ27XXX_FLAG_CFGUP)) {
k_msleep(BQ274XX_SUBCLASS_DELAY * 10);
}

} while (!(flags & 0x0010));
} while (!(flags & BQ27XXX_FLAG_CFGUP));

ret = bq274xx_cmd_reg_write(dev, BQ274XX_EXT_DATA_CONTROL, 0x00);
if (ret < 0) {
Expand All @@ -191,14 +197,14 @@ static int bq274xx_gauge_configure(const struct device *dev)
return -EIO;
}

ret = bq274xx_read_data_block(dev, 0x00, block, 32);
ret = bq274xx_read_data_block(dev, 0, block, sizeof(block));
if (ret < 0) {
LOG_ERR("Unable to read block data");
return -EIO;
}

tmp_checksum = 0;
for (uint8_t i = 0; i < 32; i++) {
for (uint8_t i = 0; i < ARRAY_SIZE(block); i++) {
tmp_checksum += block[i];
}
tmp_checksum = 255 - tmp_checksum;
Expand Down Expand Up @@ -274,14 +280,14 @@ static int bq274xx_gauge_configure(const struct device *dev)
return -EIO;
}

ret = bq274xx_read_data_block(dev, 0x00, block, 32);
ret = bq274xx_read_data_block(dev, 0, block, sizeof(block));
if (ret < 0) {
LOG_ERR("Unable to read block data");
return -EIO;
}

checksum_new = 0;
for (uint8_t i = 0; i < 32; i++) {
for (uint8_t i = 0; i < ARRAY_SIZE(block); i++) {
checksum_new += block[i];
}
checksum_new = 255 - checksum_new;
Expand Down Expand Up @@ -319,10 +325,10 @@ static int bq274xx_gauge_configure(const struct device *dev)
return -EIO;
}

if (flags & 0x0010) {
if (flags & BQ27XXX_FLAG_CFGUP) {
k_msleep(BQ274XX_SUBCLASS_DELAY * 10);
}
} while (flags & 0x0010);
} while (flags & BQ27XXX_FLAG_CFGUP);

/* Seal the gauge */
ret = bq274xx_ctrl_reg_write(dev, BQ274XX_CTRL_SEALED);
Expand Down

0 comments on commit 35bb28a

Please sign in to comment.