Skip to content

Commit 7efe988

Browse files
simonguinotcarlescufi
authored andcommitted
tests: introduce tests for LED drivers
This patch introduces some tests for the LED drivers. The following functions of the LED API are tested: - led_get_info - led_on - led_off - led_set_color - led_set_brightness Note that the led-controller-0 alias must be defined in the DTS of the board used to run this tests. Signed-off-by: Simon Guinot <simon.guinot@seagate.com>
1 parent 21f4fef commit 7efe988

File tree

6 files changed

+310
-0
lines changed

6 files changed

+310
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(led_api)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})

tests/drivers/led/led_api/prj.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_LED=y
2+
CONFIG_ZTEST=y
3+
CONFIG_TEST_USERSPACE=y
4+
CONFIG_LOG=y
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2020 Seagate Technology LLC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <ztest.h>
9+
10+
#include "test_led_api.h"
11+
12+
void test_main(void)
13+
{
14+
k_object_access_grant(get_led_controller(), k_current_get());
15+
16+
ztest_test_suite(led_user_test,
17+
ztest_user_unit_test(test_led_setup),
18+
ztest_user_unit_test(test_led_get_info),
19+
ztest_user_unit_test(test_led_on),
20+
ztest_user_unit_test(test_led_off),
21+
ztest_user_unit_test(test_led_set_color),
22+
ztest_user_unit_test(test_led_set_brightness));
23+
ztest_run_test_suite(led_user_test);
24+
}
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
/*
2+
* Copyright (c) 2020 Seagate Technology LLC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <ztest.h>
9+
#include <drivers/led.h>
10+
11+
#define BRIGHTNESS_MAX 100
12+
#define TEST_MAX_COLORS 8
13+
#define COLOR_FULL 0xff
14+
15+
#if DT_NODE_HAS_STATUS(DT_ALIAS(led_controller_0), okay)
16+
#define LED_CTRL_NODE_ID DT_ALIAS(led_controller_0)
17+
#define LED_CTRL_DEV_NAME DT_LABEL(LED_CTRL_NODE_ID)
18+
#else
19+
#error "LED controller device not found"
20+
#endif
21+
22+
#define _COLOR_MAPPING(led_node_id) \
23+
const uint8_t test_color_mapping_##led_node_id[] = \
24+
DT_PROP(led_node_id, color_mapping)
25+
26+
#define COLOR_MAPPING(led_node_id) \
27+
IF_ENABLED(DT_NODE_HAS_PROP(led_node_id, color_mapping), \
28+
(_COLOR_MAPPING(led_node_id);))
29+
30+
#define LED_INFO_COLOR(led_node_id) \
31+
{ \
32+
.label = DT_LABEL(led_node_id), \
33+
.index = DT_PROP_OR(led_node_id, index, 0), \
34+
.num_colors = \
35+
DT_PROP_LEN(led_node_id, color_mapping), \
36+
.color_mapping = test_color_mapping_##led_node_id, \
37+
},
38+
39+
#define LED_INFO_NO_COLOR(led_node_id) \
40+
{ \
41+
.label = DT_LABEL(led_node_id), \
42+
.index = DT_PROP_OR(led_node_id, index, 0), \
43+
.num_colors = 0, \
44+
.color_mapping = NULL, \
45+
},
46+
47+
#define LED_INFO(led_node_id) \
48+
COND_CODE_1(DT_NODE_HAS_PROP(led_node_id, color_mapping), \
49+
(LED_INFO_COLOR(led_node_id)), \
50+
(LED_INFO_NO_COLOR(led_node_id)))
51+
52+
#define LED_CONTROLLER_INFO(node_id) \
53+
\
54+
DT_FOREACH_CHILD(node_id, COLOR_MAPPING) \
55+
\
56+
const struct led_info test_led_info[] = { \
57+
DT_FOREACH_CHILD(node_id, LED_INFO) \
58+
}; \
59+
\
60+
static ZTEST_DMEM int num_leds = ARRAY_SIZE(test_led_info)
61+
62+
LED_CONTROLLER_INFO(LED_CTRL_NODE_ID);
63+
64+
static ZTEST_BMEM struct device *led_ctrl;
65+
66+
struct device *get_led_controller(void)
67+
{
68+
return device_get_binding(LED_CTRL_DEV_NAME);
69+
}
70+
71+
void test_led_setup(void)
72+
{
73+
led_ctrl = get_led_controller();
74+
zassert_not_null(led_ctrl,
75+
"LED controller " LED_CTRL_DEV_NAME " not found");
76+
77+
zassert_not_equal(num_leds, 0,
78+
"No LEDs subnodes found in DT for controller "
79+
LED_CTRL_DEV_NAME);
80+
}
81+
82+
void test_led_get_info(void)
83+
{
84+
uint8_t led;
85+
int ret;
86+
87+
if (!led_ctrl || !num_leds)
88+
ztest_test_skip();
89+
90+
for (led = 0; led < num_leds; led++) {
91+
const struct led_info *info;
92+
uint8_t col;
93+
94+
ret = led_get_info(led_ctrl, led, &info);
95+
if (ret == -ENOTSUP) {
96+
TC_PRINT("led_get_info() syscall is not supported.\n");
97+
ztest_test_skip();
98+
return;
99+
}
100+
101+
zassert_equal(ret, 0, "LED %d - led_get_info() error (ret=%d)",
102+
led, ret);
103+
104+
zassert_true(!strcmp(info->label, test_led_info[led].label),
105+
"LED %d - label: %s instead of %s",
106+
led, info->label, test_led_info[led].label);
107+
108+
zassert_equal(info->index, test_led_info[led].index,
109+
"LED %d - index: %d instead of %d", led,
110+
info->index, test_led_info[led].index);
111+
112+
zassert_equal(info->num_colors, test_led_info[led].num_colors,
113+
"LED %d - num_colors: %d instead of %d", led,
114+
info->num_colors, test_led_info[led].num_colors);
115+
116+
TC_PRINT("LED %d - label: %s, index: %d, num_colors: %d",
117+
led, info->label, info->index, info->num_colors);
118+
119+
if (!info->num_colors)
120+
continue;
121+
122+
TC_PRINT(" color_mapping: ");
123+
124+
for (col = 0; col < info->num_colors; col++) {
125+
zassert_equal(info->color_mapping[col],
126+
test_led_info[led].color_mapping[col],
127+
"LED %d - color_mapping[%d]=%d instead of %d",
128+
led, col, info->color_mapping[col],
129+
test_led_info[led].color_mapping[col]);
130+
TC_PRINT("%d", info->color_mapping[col]);
131+
}
132+
TC_PRINT("\n");
133+
}
134+
}
135+
136+
void test_led_on(void)
137+
{
138+
uint8_t led;
139+
int ret;
140+
141+
if (!led_ctrl || !num_leds)
142+
ztest_test_skip();
143+
144+
for (led = 0; led < num_leds; led++) {
145+
ret = led_on(led_ctrl, led);
146+
zassert_equal(ret, 0, "LED %d - failed to turn on", led);
147+
}
148+
}
149+
150+
void test_led_off(void)
151+
{
152+
uint8_t led;
153+
int ret;
154+
155+
if (!led_ctrl || !num_leds)
156+
ztest_test_skip();
157+
158+
for (led = 0; led < num_leds; led++) {
159+
ret = led_off(led_ctrl, led);
160+
zassert_equal(ret, 0, "LED %d - failed to turn off", led);
161+
}
162+
}
163+
164+
void test_led_set_color(void)
165+
{
166+
uint8_t led;
167+
uint8_t colors[TEST_MAX_COLORS + 1];
168+
int ret;
169+
170+
if (!led_ctrl || !num_leds)
171+
ztest_test_skip();
172+
173+
for (led = 0; led < num_leds; led++) {
174+
uint8_t num_colors = test_led_info[led].num_colors;
175+
uint8_t col;
176+
177+
if (num_colors > TEST_MAX_COLORS) {
178+
TC_PRINT("LED %d - skip set_color test, num_colors: %d"
179+
" (test limit is %d)",
180+
led, num_colors, TEST_MAX_COLORS);
181+
continue;
182+
}
183+
184+
memset(colors, 0, sizeof(colors));
185+
186+
/* Try to set more colors than supported. */
187+
ret = led_set_color(led_ctrl, led, num_colors + 1, colors);
188+
zassert_not_equal(ret, 0, "LED %d - setting %d"
189+
" colors should fail (%d supported)",
190+
led, num_colors + 1, num_colors);
191+
192+
if (!num_colors) {
193+
continue;
194+
}
195+
196+
/* Try to set less colors than supported. */
197+
ret = led_set_color(led_ctrl, led, num_colors - 1, colors);
198+
zassert_not_equal(ret, 0, "LED %d - setting %d"
199+
" colors should fail (%d supported)",
200+
led, num_colors - 1, num_colors);
201+
202+
/* Ensure the LED is on to get a visual feedback. */
203+
led_set_brightness(led_ctrl, led, BRIGHTNESS_MAX / 2);
204+
205+
/* Set each color gradually to its maximum level. */
206+
for (col = 0; col < num_colors; col++) {
207+
uint16_t level;
208+
209+
memset(colors, 0, sizeof(colors));
210+
211+
for (level = 0; level <= COLOR_FULL; level++) {
212+
colors[col] = level;
213+
214+
ret = led_set_color(led_ctrl, led,
215+
num_colors, colors);
216+
zassert_equal(ret, 0,
217+
"LED %d - failed to set color[%d] to %d",
218+
led, level);
219+
}
220+
}
221+
}
222+
}
223+
224+
void test_led_set_brightness(void)
225+
{
226+
uint8_t led;
227+
int ret;
228+
229+
if (!led_ctrl || !num_leds)
230+
ztest_test_skip();
231+
232+
for (led = 0; led < num_leds; led++) {
233+
uint16_t level;
234+
235+
for (level = 0; level <= BRIGHTNESS_MAX; level++) {
236+
ret = led_set_brightness(led_ctrl, led, level);
237+
zassert_equal(ret, 0,
238+
"LED %d - failed to set brightness to %d",
239+
led, level);
240+
}
241+
for (level = BRIGHTNESS_MAX + 1; level <= 255; level++) {
242+
ret = led_set_brightness(led_ctrl, led, level);
243+
zassert_not_equal(ret, 0,
244+
"LED %d - setting brightness to %d"
245+
" should fail (maximum: %d)",
246+
led, level, BRIGHTNESS_MAX);
247+
}
248+
}
249+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2020 Seagate Technology LLC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef TEST_LED_API_H_
8+
#define TEST_LED_API_H_
9+
10+
struct device *get_led_controller(void);
11+
12+
void test_led_setup(void);
13+
void test_led_get_info(void);
14+
void test_led_on(void);
15+
void test_led_off(void);
16+
void test_led_set_color(void);
17+
void test_led_set_brightness(void);
18+
19+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tests:
2+
drivers.i2c:
3+
tags: drivers led
4+
depends_on: led
5+
filter: dt_alias_exists("led-controller-0")

0 commit comments

Comments
 (0)