From a62b62dda84c76a58fde627331e5561f1de03de3 Mon Sep 17 00:00:00 2001 From: Philipp Steiner Date: Thu, 24 Apr 2025 14:09:27 +0200 Subject: [PATCH] tests: fuelgauge: Add Onsemi LC709203F driver Add test for the Onsemi LC709203F fuel gauge Signed-off-by: Philipp Steiner --- .../fuel_gauge/lc709203f/CMakeLists.txt | 7 + .../lc709203f/boards/native_sim.conf | 3 + .../lc709203f/boards/native_sim.overlay | 14 ++ tests/drivers/fuel_gauge/lc709203f/prj.conf | 6 + .../fuel_gauge/lc709203f/src/test_lc709203f.c | 195 ++++++++++++++++++ .../fuel_gauge/lc709203f/testcase.yaml | 7 + 6 files changed, 232 insertions(+) create mode 100644 tests/drivers/fuel_gauge/lc709203f/CMakeLists.txt create mode 100644 tests/drivers/fuel_gauge/lc709203f/boards/native_sim.conf create mode 100644 tests/drivers/fuel_gauge/lc709203f/boards/native_sim.overlay create mode 100644 tests/drivers/fuel_gauge/lc709203f/prj.conf create mode 100644 tests/drivers/fuel_gauge/lc709203f/src/test_lc709203f.c create mode 100644 tests/drivers/fuel_gauge/lc709203f/testcase.yaml diff --git a/tests/drivers/fuel_gauge/lc709203f/CMakeLists.txt b/tests/drivers/fuel_gauge/lc709203f/CMakeLists.txt new file mode 100644 index 00000000000..84b71bcfcbc --- /dev/null +++ b/tests/drivers/fuel_gauge/lc709203f/CMakeLists.txt @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(device) + +target_sources(app PRIVATE src/test_lc709203f.c) diff --git a/tests/drivers/fuel_gauge/lc709203f/boards/native_sim.conf b/tests/drivers/fuel_gauge/lc709203f/boards/native_sim.conf new file mode 100644 index 00000000000..022a71dd0f0 --- /dev/null +++ b/tests/drivers/fuel_gauge/lc709203f/boards/native_sim.conf @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: Apache-2.0 + +CONFIG_EMUL=y diff --git a/tests/drivers/fuel_gauge/lc709203f/boards/native_sim.overlay b/tests/drivers/fuel_gauge/lc709203f/boards/native_sim.overlay new file mode 100644 index 00000000000..7df1b59a5f4 --- /dev/null +++ b/tests/drivers/fuel_gauge/lc709203f/boards/native_sim.overlay @@ -0,0 +1,14 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + */ + +&i2c0 { + lc709203f: lc709203f@0b { + compatible = "onnn,lc709203f"; + status = "okay"; + reg = <0x0b>; + apa = "500mAh"; + battery-profile = <0x01>; + thermistor; + }; +}; diff --git a/tests/drivers/fuel_gauge/lc709203f/prj.conf b/tests/drivers/fuel_gauge/lc709203f/prj.conf new file mode 100644 index 00000000000..ce11abb435a --- /dev/null +++ b/tests/drivers/fuel_gauge/lc709203f/prj.conf @@ -0,0 +1,6 @@ +CONFIG_ZTEST=y +CONFIG_I2C=y +CONFIG_TEST_USERSPACE=y +CONFIG_LOG=y + +CONFIG_FUEL_GAUGE=y diff --git a/tests/drivers/fuel_gauge/lc709203f/src/test_lc709203f.c b/tests/drivers/fuel_gauge/lc709203f/src/test_lc709203f.c new file mode 100644 index 00000000000..d67c4cdfcee --- /dev/null +++ b/tests/drivers/fuel_gauge/lc709203f/src/test_lc709203f.c @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2025 Philipp Steiner + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +struct lc709203f_fixture { + const struct device *dev; + const struct fuel_gauge_driver_api *api; +}; + +static void *lc709203f_setup(void) +{ + static ZTEST_DMEM struct lc709203f_fixture fixture; + + fixture.dev = DEVICE_DT_GET_ANY(onnn_lc709203f); + k_object_access_all_grant(fixture.dev); + + zassert_true(device_is_ready(fixture.dev), "Fuel Gauge not found"); + + return &fixture; +} + +ZTEST_USER_F(lc709203f, test_get_some_props_failed__returns_bad_status) +{ + fuel_gauge_prop_t props[] = { + /* First invalid property */ + FUEL_GAUGE_PROP_MAX, + /* Second invalid property */ + FUEL_GAUGE_PROP_MAX, + /* Valid property */ + FUEL_GAUGE_VOLTAGE, + }; + union fuel_gauge_prop_val vals[ARRAY_SIZE(props)]; + + int ret = fuel_gauge_get_props(fixture->dev, props, vals, ARRAY_SIZE(props)); + + zassert_equal(ret, -ENOTSUP, "Getting bad property has a good status."); +} + +ZTEST_USER_F(lc709203f, test_set_all_props_failed__returns_err) +{ + fuel_gauge_prop_t prop_types[] = { + /* Invalid property */ + FUEL_GAUGE_PROP_MAX, + }; + union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)] = {0}; + + int ret = fuel_gauge_set_props(fixture->dev, prop_types, props, ARRAY_SIZE(props)); + + zassert_equal(ret, -ENOTSUP); +} + +ZTEST_USER_F(lc709203f, test_set_some_props_failed__returns_err) +{ + fuel_gauge_prop_t prop_types[] = { + /* First invalid property */ + FUEL_GAUGE_PROP_MAX, + /* Second invalid property */ + FUEL_GAUGE_PROP_MAX, + /* Valid property */ + FUEL_GAUGE_STATE_OF_CHARGE_ALARM, + /* Set Manufacturer's Access to arbitrary word */ + + }; + + union fuel_gauge_prop_val props[] = { + /* First invalid property */ + {0}, + /* Second invalid property */ + {0}, + /* Valid property */ + /* Sets state of charge threshold to generate Alarm signal*/ + {.state_of_charge_alarm = 10}, + }; + + int ret = fuel_gauge_set_props(fixture->dev, prop_types, props, ARRAY_SIZE(props)); + + zassert_equal(ret, -ENOTSUP); +} + +ZTEST_USER_F(lc709203f, test_set_prop_can_be_get) +{ + uint16_t sbs_mode = 0x0002; + uint16_t current_direction = 0x0001; + uint8_t state_of_charge_alarm = 20; + uint32_t low_voltage_alarm = 3000 * 1000; + + fuel_gauge_prop_t prop_types[] = { + FUEL_GAUGE_SBS_MODE, + FUEL_GAUGE_CURRENT_DIRECTION, + FUEL_GAUGE_STATE_OF_CHARGE_ALARM, + FUEL_GAUGE_LOW_VOLTAGE_ALARM, + }; + + union fuel_gauge_prop_val set_props[] = { + { + .sbs_mode = sbs_mode, + }, + { + .current_direction = current_direction, + }, + { + .state_of_charge_alarm = state_of_charge_alarm, + }, + { + .low_voltage_alarm = low_voltage_alarm, + }, + }; + + union fuel_gauge_prop_val get_props[ARRAY_SIZE(prop_types)]; + + zassert_ok( + fuel_gauge_set_props(fixture->dev, prop_types, set_props, ARRAY_SIZE(set_props))); + + zassert_ok( + fuel_gauge_get_props(fixture->dev, prop_types, get_props, ARRAY_SIZE(get_props))); + + zassert_equal(get_props[0].sbs_mode, sbs_mode); + zassert_equal(get_props[1].current_direction, current_direction); + zassert_equal(get_props[2].state_of_charge_alarm, state_of_charge_alarm); + zassert_equal(get_props[3].low_voltage_alarm, low_voltage_alarm); +} + +ZTEST_USER_F(lc709203f, test_get_props__returns_ok) +{ + fuel_gauge_prop_t props[] = { + FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE, + FUEL_GAUGE_TEMPERATURE, + FUEL_GAUGE_VOLTAGE, + FUEL_GAUGE_SBS_MODE, + FUEL_GAUGE_DESIGN_CAPACITY, + FUEL_GAUGE_CURRENT_DIRECTION, + FUEL_GAUGE_STATE_OF_CHARGE_ALARM, + FUEL_GAUGE_LOW_VOLTAGE_ALARM, + }; + union fuel_gauge_prop_val vals[ARRAY_SIZE(props)]; + + int ret = fuel_gauge_get_props(fixture->dev, props, vals, ARRAY_SIZE(props)); + +#if CONFIG_EMUL + zassert_equal(vals[0].relative_state_of_charge, 50); + zassert_equal(vals[1].temperature, 0x0BA6); + zassert_equal(vals[2].voltage, 3700 * 1000); + zassert_equal(vals[3].sbs_mode, 0x0001); + zassert_equal(vals[4].design_cap, 500); + zassert_true(((vals[5].current_direction == 0x0000) || + (vals[5].current_direction == 0x0001) || + (vals[5].current_direction == 0xFFFF))); + zassert_equal(vals[6].state_of_charge_alarm, 0x0008); + zassert_equal(vals[7].low_voltage_alarm, 0x0000); +#else + zassert_between_inclusive(vals[0].relative_state_of_charge, 0, 100); + zassert_between_inclusive(vals[1].temperature, 0x09E4, 0x0D04); + zassert_between_inclusive(vals[2].voltage, 0, 0xFFFF * 1000); + zassert_between_inclusive(vals[3].sbs_mode, 0x0001, 0x0002); + zassert_true(((vals[4].design_cap == 100) || (vals[4].design_cap == 200) || + (vals[4].design_cap == 500) || (vals[4].design_cap == 1000) || + (vals[4].design_cap == 3000))); + zassert_true(((vals[5].current_direction == 0x0000) || + (vals[5].current_direction == 0x0001) || + (vals[5].current_direction == 0xFFFF))); + zassert_between_inclusive(vals[6].state_of_charge_alarm, 0, 100); + zassert_between_inclusive(vals[7].low_voltage_alarm, 0, 0xFFFF * 1000); +#endif + + zassert_equal(ret, 0, "Getting bad property has a good status."); +} + +ZTEST_USER_F(lc709203f, test_set_get_single_prop) +{ + uint8_t test_value = 5; + + union fuel_gauge_prop_val state_of_charge_alarm_set = { + .state_of_charge_alarm = test_value, + }; + union fuel_gauge_prop_val state_of_charge_alarm_get; + + zassert_ok(fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_STATE_OF_CHARGE_ALARM, + state_of_charge_alarm_set)); + zassert_ok(fuel_gauge_get_prop(fixture->dev, FUEL_GAUGE_STATE_OF_CHARGE_ALARM, + &state_of_charge_alarm_get)); + zassert_equal(state_of_charge_alarm_get.state_of_charge_alarm, test_value); +} + +ZTEST_SUITE(lc709203f, NULL, lc709203f_setup, NULL, NULL, NULL); diff --git a/tests/drivers/fuel_gauge/lc709203f/testcase.yaml b/tests/drivers/fuel_gauge/lc709203f/testcase.yaml new file mode 100644 index 00000000000..317307c492b --- /dev/null +++ b/tests/drivers/fuel_gauge/lc709203f/testcase.yaml @@ -0,0 +1,7 @@ +tests: + drivers.fuel_gauge.lc709203f: + tags: + - fuel_gauge + filter: dt_compat_enabled("onnn,lc709203f") + platform_allow: + - native_sim