Skip to content

Commit 06bcb5a

Browse files
franckduriezcfriedt
authored andcommitted
test: driver/fuel_gauge/sy24561: add test
Add test for fuel gauge SY24561 Signed-off-by: Franck Duriez <[email protected]>
1 parent 5f84be6 commit 06bcb5a

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(device)
6+
7+
target_sources(app PRIVATE src/test_sy24561.c)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
CONFIG_EMUL=y
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
&i2c0 {
6+
sy24651: sy24651@48 {
7+
compatible = "silergy,sy24561";
8+
reg = <0x48>;
9+
status = "okay";
10+
};
11+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_TEST_USERSPACE=y
3+
4+
CONFIG_LOG=y
5+
CONFIG_I2C=y
6+
CONFIG_I2C_LOG_LEVEL_DBG=y
7+
CONFIG_FUEL_GAUGE=y
8+
CONFIG_FUEL_GAUGE_LOG_LEVEL_DBG=y
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2023 Alvaro Garcia Gomez <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/device.h>
8+
#include <zephyr/drivers/fuel_gauge.h>
9+
#include <zephyr/drivers/i2c.h>
10+
#include <zephyr/logging/log.h>
11+
#include <zephyr/sys/byteorder.h>
12+
#include <zephyr/sys/util.h>
13+
#include <zephyr/ztest.h>
14+
#include <zephyr/ztest_assert.h>
15+
16+
struct sy24561_fixture {
17+
const struct device *dev;
18+
const struct fuel_gauge_driver_api *api;
19+
};
20+
21+
void emul_sy24561_set_crate_status(int value);
22+
23+
static void *sy24561_setup(void)
24+
{
25+
static ZTEST_DMEM struct sy24561_fixture fixture;
26+
27+
fixture.dev = DEVICE_DT_GET_ANY(silergy_sy24561);
28+
k_object_access_all_grant(fixture.dev);
29+
30+
zassert_true(device_is_ready(fixture.dev), "Fuel Gauge not found");
31+
32+
return &fixture;
33+
}
34+
35+
ZTEST_USER_F(sy24561, test_get_some_props_failed_returns_bad_status)
36+
{
37+
fuel_gauge_prop_t prop_types[] = {
38+
/* First invalid property */
39+
FUEL_GAUGE_PROP_MAX,
40+
/* Second invalid property */
41+
FUEL_GAUGE_PROP_MAX,
42+
/* Valid property */
43+
FUEL_GAUGE_VOLTAGE,
44+
};
45+
union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)];
46+
47+
int ret = fuel_gauge_get_props(fixture->dev, prop_types, props, ARRAY_SIZE(props));
48+
49+
zassert_equal(ret, -ENOTSUP, "Getting bad property has a good status.");
50+
}
51+
52+
ZTEST_USER_F(sy24561, test_get_props__returns_ok)
53+
{
54+
/* Validate what props are supported by the driver */
55+
56+
fuel_gauge_prop_t prop_types[] = {
57+
FUEL_GAUGE_VOLTAGE,
58+
FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE,
59+
FUEL_GAUGE_STATUS,
60+
FUEL_GAUGE_CURRENT_DIRECTION,
61+
};
62+
63+
union fuel_gauge_prop_val props[ARRAY_SIZE(prop_types)];
64+
65+
zassert_ok(fuel_gauge_get_props(fixture->dev, prop_types, props, ARRAY_SIZE(props)));
66+
}
67+
68+
ZTEST_USER_F(sy24561, test_out_of_range_temperature_are_cropped)
69+
{
70+
union fuel_gauge_prop_val val;
71+
int ret = 0;
72+
73+
val.temperature = 0;
74+
ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_TEMPERATURE,
75+
val); /* A warning is triggered but it should pass */
76+
zassert_equal(ret, 0, "Setting too low temperature has good status");
77+
78+
val.temperature = 0xffff;
79+
ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_TEMPERATURE,
80+
val); /* A warning is triggered but it should pass */
81+
zassert_equal(ret, 0, "Setting too high temperature has good status");
82+
}
83+
84+
ZTEST_USER_F(sy24561, test_out_of_range_alarm_threshold_are_cropped)
85+
{
86+
union fuel_gauge_prop_val val;
87+
int ret = 0;
88+
89+
val.state_of_charge_alarm = 0u;
90+
ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_STATE_OF_CHARGE_ALARM,
91+
val); /* A warning is triggered but it should pass */
92+
zassert_equal(ret, 0, "Setting too low alarm threshold has good status");
93+
94+
val.state_of_charge_alarm = 0xffu;
95+
ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_STATE_OF_CHARGE_ALARM,
96+
val); /* A warning is triggered but it should pass */
97+
zassert_equal(ret, 0, "Setting too high alarm threshold has good status");
98+
}
99+
100+
ZTEST_SUITE(sy24561, NULL, sy24561_setup, NULL, NULL, NULL);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests:
2+
drivers.fuel_gauge.sy24561:
3+
tags:
4+
- fuel_gauge
5+
filter: dt_compat_enabled("silergy,sy24561")
6+
platform_allow:
7+
- native_sim

0 commit comments

Comments
 (0)