Skip to content

Commit

Permalink
sensor: bq274xx: support fetching all channels
Browse files Browse the repository at this point in the history
Change bq274xx_sample_fetch to support SENSOR_CHAN_ALL. This makes it
possible to get the sensor data using the sensor shell, besides being
generally convenient.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
  • Loading branch information
fabiobaltieri committed Jul 9, 2023
1 parent b96381e commit 687c3bb
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions drivers/sensor/bq274xx/bq274xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,107 +448,106 @@ static int bq274xx_sample_fetch(const struct device *dev, enum sensor_channel ch
}
}

switch (chan) {
case SENSOR_CHAN_GAUGE_VOLTAGE:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_VOLTAGE) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_VOLTAGE,
&data->voltage);
if (ret < 0) {
LOG_ERR("Failed to read voltage");
return -EIO;
}
break;
}

case SENSOR_CHAN_GAUGE_AVG_CURRENT:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_AVG_CURRENT) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_AVG_CURRENT,
&data->avg_current);
if (ret < 0) {
LOG_ERR("Failed to read average current ");
return -EIO;
}
break;
}

case SENSOR_CHAN_GAUGE_TEMP:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_TEMP) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_INT_TEMP,
&data->internal_temperature);
if (ret < 0) {
LOG_ERR("Failed to read internal temperature");
return -EIO;
}
break;
}

case SENSOR_CHAN_GAUGE_STDBY_CURRENT:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_STDBY_CURRENT) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_STDBY_CURRENT,
&data->stdby_current);
if (ret < 0) {
LOG_ERR("Failed to read standby current");
return -EIO;
}
break;
}

case SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_MAX_CURRENT,
&data->max_load_current);
if (ret < 0) {
LOG_ERR("Failed to read maximum current");
return -EIO;
}
break;
}

case SENSOR_CHAN_GAUGE_STATE_OF_CHARGE:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_STATE_OF_CHARGE) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_SOC,
&data->state_of_charge);
if (ret < 0) {
LOG_ERR("Failed to read state of charge");
return -EIO;
}
break;
}

case SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_FULL_CAPACITY,
&data->full_charge_capacity);
if (ret < 0) {
LOG_ERR("Failed to read full charge capacity");
return -EIO;
}
break;
}

case SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_REM_CAPACITY,
&data->remaining_charge_capacity);
if (ret < 0) {
LOG_ERR("Failed to read remaining charge capacity");
return -EIO;
}
break;
}

case SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_NOM_CAPACITY,
&data->nom_avail_capacity);
if (ret < 0) {
LOG_ERR("Failed to read nominal available capacity");
return -EIO;
}
break;
}

case SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_AVAIL_CAPACITY,
&data->full_avail_capacity);
if (ret < 0) {
LOG_ERR("Failed to read full available capacity");
return -EIO;
}
break;
}

case SENSOR_CHAN_GAUGE_AVG_POWER:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_AVG_POWER) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_AVG_POWER,
&data->avg_power);
if (ret < 0) {
LOG_ERR("Failed to read battery average power");
return -EIO;
}
break;
}

case SENSOR_CHAN_GAUGE_STATE_OF_HEALTH:
if (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_GAUGE_STATE_OF_HEALTH) {
ret = bq274xx_cmd_reg_read(dev, BQ274XX_CMD_SOH,
&data->state_of_health);

Expand All @@ -558,10 +557,6 @@ static int bq274xx_sample_fetch(const struct device *dev, enum sensor_channel ch
LOG_ERR("Failed to read state of health");
return -EIO;
}
break;

default:
return -ENOTSUP;
}

return 0;
Expand Down

0 comments on commit 687c3bb

Please sign in to comment.