drivers: sensor: ina230: fix current sign issue

Fix sign handling for ina230 current calculation.

Signed-off-by: Eric Holmberg <eric.holmberg@northriversystems.co.nz>
This commit is contained in:
Eric Holmberg 2023-08-16 21:54:28 +12:00 committed by Maureen Helm
commit f5b78270bc
4 changed files with 40 additions and 20 deletions

View file

@ -29,8 +29,8 @@ static int ina230_channel_get(const struct device *dev, enum sensor_channel chan
{
struct ina230_data *data = dev->data;
const struct ina230_config *const config = dev->config;
uint32_t bus_uv, current_ua, power_uw;
int32_t sign;
uint32_t bus_uv, power_uw;
int32_t current_ua;
switch (chan) {
case SENSOR_CHAN_VOLTAGE:
@ -42,21 +42,12 @@ static int ina230_channel_get(const struct device *dev, enum sensor_channel chan
break;
case SENSOR_CHAN_CURRENT:
if (data->current & INA23X_CURRENT_SIGN_BIT) {
current_ua = ~data->current + 1U;
sign = -1;
} else {
current_ua = data->current;
sign = 1;
}
/* see datasheet "Programming" section for reference */
current_ua = current_ua * config->current_lsb;
current_ua = data->current * config->current_lsb;
/* convert to fractional amperes */
val->val1 = sign * (int32_t)(current_ua / 1000000U);
val->val2 = sign * (int32_t)(current_ua % 1000000U);
val->val1 = current_ua / 1000000L;
val->val2 = current_ua % 1000000L;
break;
case SENSOR_CHAN_POWER:

View file

@ -34,7 +34,7 @@
struct ina230_data {
const struct device *dev;
uint16_t current;
int16_t current;
uint16_t bus_voltage;
uint16_t power;
#ifdef CONFIG_INA230_TRIGGER

View file

@ -12,11 +12,6 @@
#include <zephyr/drivers/i2c.h>
#include <zephyr/sys/util_macro.h>
/**
* @brief Macro used to test if the current's sign bit is set
*/
#define INA23X_CURRENT_SIGN_BIT BIT(15)
/**
* @brief Macro used to check if the current's LSB is 1mA
*/