Skip to content

Commit ccb2684

Browse files
committed
test: driver/fuel_gauge/sy24561: add test
Add test for fuel gauge SY24561 Signed-off-by: Franck Duriez <franck.lucien.duriez@gmail.com>
1 parent dd37668 commit ccb2684

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2023 Alvaro Garcia Gomez <maxpowel@gmail.com>
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_set_invalid_temperature_returns_bad_status)
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, val);
75+
zassert_equal(ret, -EINVAL, "Setting too low temperature has good status");
76+
77+
val.temperature = 0xffff;
78+
ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_TEMPERATURE, val);
79+
zassert_equal(ret, -EINVAL, "Setting too high temperature has good status");
80+
}
81+
82+
ZTEST_USER_F(sy24561, test_set_invalid_alarm_threshold_returns_bad_status)
83+
{
84+
union fuel_gauge_prop_val val;
85+
int ret = 0;
86+
87+
val.sbs_remaining_capacity_alarm = 0;
88+
ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_SBS_REMAINING_CAPACITY_ALARM, val);
89+
zassert_equal(ret, -EINVAL, "Setting too low alarm threshold has good status");
90+
91+
val.sbs_remaining_capacity_alarm = 0xffff;
92+
ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_SBS_REMAINING_CAPACITY_ALARM, val);
93+
zassert_equal(ret, -EINVAL, "Setting too high alarm threshold has good status");
94+
}
95+
96+
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)