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:
parent
5a6610240e
commit
f5b78270bc
4 changed files with 40 additions and 20 deletions
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue