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
sy24561_setup(void)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
ZTEST_USER_F(sy24561,test_get_some_props_failed_returns_bad_status)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
ZTEST_USER_F(sy24561,test_get_props__returns_ok)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 zassert_equal(props[0].voltage, 3199000);
67 zassert_equal(props[1].relative_state_of_charge, 74);
68 zassert_equal(props[2].fg_status, 0);
69 zassert_equal(props[3].current_direction, 0);
70 }
71
ZTEST_USER_F(sy24561,test_out_of_range_temperature_are_cropped)72 ZTEST_USER_F(sy24561, test_out_of_range_temperature_are_cropped)
73 {
74 union fuel_gauge_prop_val val;
75 int ret = 0;
76
77 val.temperature = 0;
78 ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_TEMPERATURE,
79 val); /* A warning is triggered but it should pass */
80 zassert_equal(ret, 0, "Setting too low temperature has good status");
81
82 val.temperature = 0xffff;
83 ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_TEMPERATURE,
84 val); /* A warning is triggered but it should pass */
85 zassert_equal(ret, 0, "Setting too high temperature has good status");
86 }
87
ZTEST_USER_F(sy24561,test_out_of_range_alarm_threshold_are_cropped)88 ZTEST_USER_F(sy24561, test_out_of_range_alarm_threshold_are_cropped)
89 {
90 union fuel_gauge_prop_val val;
91 int ret = 0;
92
93 val.state_of_charge_alarm = 0u;
94 ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_STATE_OF_CHARGE_ALARM,
95 val); /* A warning is triggered but it should pass */
96 zassert_equal(ret, 0, "Setting too low alarm threshold has good status");
97
98 val.state_of_charge_alarm = 0xffu;
99 ret = fuel_gauge_set_prop(fixture->dev, FUEL_GAUGE_STATE_OF_CHARGE_ALARM,
100 val); /* A warning is triggered but it should pass */
101 zassert_equal(ret, 0, "Setting too high alarm threshold has good status");
102 }
103
104 ZTEST_SUITE(sy24561, NULL, sy24561_setup, NULL, NULL, NULL);
105