Skip to content

Commit

Permalink
max17055: Fix current conversion from MAX17055 unit to milliamps
Browse files Browse the repository at this point in the history
`SENSOR_CHAN_GAUGE_AVG_CURRENT` is currently treated as a capacity
by the MAX17055 driver, however the unit conversion is different
for current and must be calculated separately.

Add a separate method to convert a current reading to milliamps
from 1.5625 uV/R_SENSE units, instead of the 5uVH/R_SENSE conversion
that was previously used.

Tested by comparing value read and converted from MAX17055 with
value from an external power profiling kit.

Signed-off-by: Hayden Ball <hayden@playerdata.co.uk>
  • Loading branch information
ball-hayden authored and nashif committed Jan 30, 2021
1 parent ea2ab69 commit 893857b
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions drivers/sensor/max17055/max17055.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ static int max17055_reg_read(struct max17055_data *priv, int reg_addr,
return 0;
}

/**
* @brief Convert current in MAX17055 units to milliamps
*
* @param rsense_mohms Value of Rsense in milliohms
* @param val Value to convert (taken from a MAX17055 register)
* @return corresponding value in milliamps
*/
static int current_to_ma(unsigned int rsense_mohms, int16_t val)
{
return (val * 1.5625) / rsense_mohms;
}

/**
* @brief Convert capacity in MAX17055 units to milliamps
*
Expand Down Expand Up @@ -91,11 +103,11 @@ static int max17055_channel_get(const struct device *dev,
valp->val2 = tmp % 1000000;
break;
case SENSOR_CHAN_GAUGE_AVG_CURRENT: {
int cap_ma;
int current_ma;

cap_ma = capacity_to_ma(config->rsense_mohms,
priv->avg_current);
set_millis(valp, cap_ma);
current_ma = current_to_ma(config->rsense_mohms,
priv->avg_current);
set_millis(valp, current_ma);
break;
}
case SENSOR_CHAN_GAUGE_STATE_OF_CHARGE:
Expand Down

0 comments on commit 893857b

Please sign in to comment.