diff --git a/drivers/sensor/ina23x/ina230.c b/drivers/sensor/ina23x/ina230.c index feb31359e93..70b6ff157c8 100644 --- a/drivers/sensor/ina23x/ina230.c +++ b/drivers/sensor/ina23x/ina230.c @@ -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: diff --git a/drivers/sensor/ina23x/ina230.h b/drivers/sensor/ina23x/ina230.h index 1f45ffa12e2..407c0dd3983 100644 --- a/drivers/sensor/ina23x/ina230.h +++ b/drivers/sensor/ina23x/ina230.h @@ -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 diff --git a/drivers/sensor/ina23x/ina23x_common.h b/drivers/sensor/ina23x/ina23x_common.h index 60a288d58fc..7f525d6a900 100644 --- a/drivers/sensor/ina23x/ina23x_common.h +++ b/drivers/sensor/ina23x/ina23x_common.h @@ -12,11 +12,6 @@ #include #include -/** - * @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 */ diff --git a/tests/drivers/sensor/ina230/src/ina230_test.c b/tests/drivers/sensor/ina230/src/ina230_test.c index b10b17bece3..9ddda5db71c 100644 --- a/tests/drivers/sensor/ina230/src/ina230_test.c +++ b/tests/drivers/sensor/ina230/src/ina230_test.c @@ -89,6 +89,39 @@ static void test_shunt_cal(struct ina230_fixture *fixture) "Expected %d, got %d", shunt_register_expected, shunt_register_actual); } +static void test_current(struct ina230_fixture *fixture) +{ + /* 16-bit signed value for current register */ + const int16_t current_reg_vectors[] = { + 32767, + 1000, + 100, + 1, + 0, + -1, + -100, + -1000, + -32768, + }; + + for (int idx = 0; idx < ARRAY_SIZE(current_reg_vectors); idx++) { + struct sensor_value sensor_val; + int16_t current_register = current_reg_vectors[idx]; + double current_expected_A = fixture->current_lsb_uA * 1e-6 * current_register; + + /* set current reading */ + ina230_mock_set_register(fixture->mock->data, INA230_REG_CURRENT, current_register); + + /* Verify sensor value is correct */ + zassert_ok(sensor_sample_fetch(fixture->dev)); + zassert_ok(sensor_channel_get(fixture->dev, SENSOR_CHAN_CURRENT, &sensor_val)); + double current_actual_A = sensor_value_to_double(&sensor_val); + + zexpect_within(current_expected_A, current_actual_A, fixture->current_lsb_uA*1e-6, + "Expected %.6f A, got %.6f A", current_expected_A, current_actual_A); + } +} + static void test_bus_voltage(struct ina230_fixture *fixture) { zassert_not_null(fixture->mock); @@ -174,6 +207,7 @@ static struct ina230_fixture fixtures[] = { #define INA230_TESTS(inst) \ ZTEST(ina230_##inst, test_datasheet_example) { test_datasheet_example(&fixtures[inst]); } \ ZTEST(ina230_##inst, test_shunt_cal) { test_shunt_cal(&fixtures[inst]); } \ + ZTEST(ina230_##inst, test_current) { test_current(&fixtures[inst]); } \ ZTEST(ina230_##inst, test_bus_voltage) { test_bus_voltage(&fixtures[inst]); } \ ZTEST(ina230_##inst, test_power) { test_power(&fixtures[inst]); } \ ZTEST_SUITE(ina230_##inst, NULL, NULL, NULL, NULL, NULL);