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>
This commit is contained in:
parent
21f4fef78b
commit
7efe988a54
6 changed files with 310 additions and 0 deletions
9
tests/drivers/led/led_api/CMakeLists.txt
Normal file
9
tests/drivers/led/led_api/CMakeLists.txt
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.13.1)
|
||||||
|
|
||||||
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
|
project(led_api)
|
||||||
|
|
||||||
|
FILE(GLOB app_sources src/*.c)
|
||||||
|
target_sources(app PRIVATE ${app_sources})
|
4
tests/drivers/led/led_api/prj.conf
Normal file
4
tests/drivers/led/led_api/prj.conf
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
CONFIG_LED=y
|
||||||
|
CONFIG_ZTEST=y
|
||||||
|
CONFIG_TEST_USERSPACE=y
|
||||||
|
CONFIG_LOG=y
|
24
tests/drivers/led/led_api/src/main.c
Normal file
24
tests/drivers/led/led_api/src/main.c
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Seagate Technology LLC
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <zephyr.h>
|
||||||
|
#include <ztest.h>
|
||||||
|
|
||||||
|
#include "test_led_api.h"
|
||||||
|
|
||||||
|
void test_main(void)
|
||||||
|
{
|
||||||
|
k_object_access_grant(get_led_controller(), k_current_get());
|
||||||
|
|
||||||
|
ztest_test_suite(led_user_test,
|
||||||
|
ztest_user_unit_test(test_led_setup),
|
||||||
|
ztest_user_unit_test(test_led_get_info),
|
||||||
|
ztest_user_unit_test(test_led_on),
|
||||||
|
ztest_user_unit_test(test_led_off),
|
||||||
|
ztest_user_unit_test(test_led_set_color),
|
||||||
|
ztest_user_unit_test(test_led_set_brightness));
|
||||||
|
ztest_run_test_suite(led_user_test);
|
||||||
|
}
|
249
tests/drivers/led/led_api/src/test_led_api.c
Normal file
249
tests/drivers/led/led_api/src/test_led_api.c
Normal file
|
@ -0,0 +1,249 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Seagate Technology LLC
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <zephyr.h>
|
||||||
|
#include <ztest.h>
|
||||||
|
#include <drivers/led.h>
|
||||||
|
|
||||||
|
#define BRIGHTNESS_MAX 100
|
||||||
|
#define TEST_MAX_COLORS 8
|
||||||
|
#define COLOR_FULL 0xff
|
||||||
|
|
||||||
|
#if DT_NODE_HAS_STATUS(DT_ALIAS(led_controller_0), okay)
|
||||||
|
#define LED_CTRL_NODE_ID DT_ALIAS(led_controller_0)
|
||||||
|
#define LED_CTRL_DEV_NAME DT_LABEL(LED_CTRL_NODE_ID)
|
||||||
|
#else
|
||||||
|
#error "LED controller device not found"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define _COLOR_MAPPING(led_node_id) \
|
||||||
|
const uint8_t test_color_mapping_##led_node_id[] = \
|
||||||
|
DT_PROP(led_node_id, color_mapping)
|
||||||
|
|
||||||
|
#define COLOR_MAPPING(led_node_id) \
|
||||||
|
IF_ENABLED(DT_NODE_HAS_PROP(led_node_id, color_mapping), \
|
||||||
|
(_COLOR_MAPPING(led_node_id);))
|
||||||
|
|
||||||
|
#define LED_INFO_COLOR(led_node_id) \
|
||||||
|
{ \
|
||||||
|
.label = DT_LABEL(led_node_id), \
|
||||||
|
.index = DT_PROP_OR(led_node_id, index, 0), \
|
||||||
|
.num_colors = \
|
||||||
|
DT_PROP_LEN(led_node_id, color_mapping), \
|
||||||
|
.color_mapping = test_color_mapping_##led_node_id, \
|
||||||
|
},
|
||||||
|
|
||||||
|
#define LED_INFO_NO_COLOR(led_node_id) \
|
||||||
|
{ \
|
||||||
|
.label = DT_LABEL(led_node_id), \
|
||||||
|
.index = DT_PROP_OR(led_node_id, index, 0), \
|
||||||
|
.num_colors = 0, \
|
||||||
|
.color_mapping = NULL, \
|
||||||
|
},
|
||||||
|
|
||||||
|
#define LED_INFO(led_node_id) \
|
||||||
|
COND_CODE_1(DT_NODE_HAS_PROP(led_node_id, color_mapping), \
|
||||||
|
(LED_INFO_COLOR(led_node_id)), \
|
||||||
|
(LED_INFO_NO_COLOR(led_node_id)))
|
||||||
|
|
||||||
|
#define LED_CONTROLLER_INFO(node_id) \
|
||||||
|
\
|
||||||
|
DT_FOREACH_CHILD(node_id, COLOR_MAPPING) \
|
||||||
|
\
|
||||||
|
const struct led_info test_led_info[] = { \
|
||||||
|
DT_FOREACH_CHILD(node_id, LED_INFO) \
|
||||||
|
}; \
|
||||||
|
\
|
||||||
|
static ZTEST_DMEM int num_leds = ARRAY_SIZE(test_led_info)
|
||||||
|
|
||||||
|
LED_CONTROLLER_INFO(LED_CTRL_NODE_ID);
|
||||||
|
|
||||||
|
static ZTEST_BMEM struct device *led_ctrl;
|
||||||
|
|
||||||
|
struct device *get_led_controller(void)
|
||||||
|
{
|
||||||
|
return device_get_binding(LED_CTRL_DEV_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_led_setup(void)
|
||||||
|
{
|
||||||
|
led_ctrl = get_led_controller();
|
||||||
|
zassert_not_null(led_ctrl,
|
||||||
|
"LED controller " LED_CTRL_DEV_NAME " not found");
|
||||||
|
|
||||||
|
zassert_not_equal(num_leds, 0,
|
||||||
|
"No LEDs subnodes found in DT for controller "
|
||||||
|
LED_CTRL_DEV_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_led_get_info(void)
|
||||||
|
{
|
||||||
|
uint8_t led;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (!led_ctrl || !num_leds)
|
||||||
|
ztest_test_skip();
|
||||||
|
|
||||||
|
for (led = 0; led < num_leds; led++) {
|
||||||
|
const struct led_info *info;
|
||||||
|
uint8_t col;
|
||||||
|
|
||||||
|
ret = led_get_info(led_ctrl, led, &info);
|
||||||
|
if (ret == -ENOTSUP) {
|
||||||
|
TC_PRINT("led_get_info() syscall is not supported.\n");
|
||||||
|
ztest_test_skip();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
zassert_equal(ret, 0, "LED %d - led_get_info() error (ret=%d)",
|
||||||
|
led, ret);
|
||||||
|
|
||||||
|
zassert_true(!strcmp(info->label, test_led_info[led].label),
|
||||||
|
"LED %d - label: %s instead of %s",
|
||||||
|
led, info->label, test_led_info[led].label);
|
||||||
|
|
||||||
|
zassert_equal(info->index, test_led_info[led].index,
|
||||||
|
"LED %d - index: %d instead of %d", led,
|
||||||
|
info->index, test_led_info[led].index);
|
||||||
|
|
||||||
|
zassert_equal(info->num_colors, test_led_info[led].num_colors,
|
||||||
|
"LED %d - num_colors: %d instead of %d", led,
|
||||||
|
info->num_colors, test_led_info[led].num_colors);
|
||||||
|
|
||||||
|
TC_PRINT("LED %d - label: %s, index: %d, num_colors: %d",
|
||||||
|
led, info->label, info->index, info->num_colors);
|
||||||
|
|
||||||
|
if (!info->num_colors)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
TC_PRINT(" color_mapping: ");
|
||||||
|
|
||||||
|
for (col = 0; col < info->num_colors; col++) {
|
||||||
|
zassert_equal(info->color_mapping[col],
|
||||||
|
test_led_info[led].color_mapping[col],
|
||||||
|
"LED %d - color_mapping[%d]=%d instead of %d",
|
||||||
|
led, col, info->color_mapping[col],
|
||||||
|
test_led_info[led].color_mapping[col]);
|
||||||
|
TC_PRINT("%d", info->color_mapping[col]);
|
||||||
|
}
|
||||||
|
TC_PRINT("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_led_on(void)
|
||||||
|
{
|
||||||
|
uint8_t led;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (!led_ctrl || !num_leds)
|
||||||
|
ztest_test_skip();
|
||||||
|
|
||||||
|
for (led = 0; led < num_leds; led++) {
|
||||||
|
ret = led_on(led_ctrl, led);
|
||||||
|
zassert_equal(ret, 0, "LED %d - failed to turn on", led);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_led_off(void)
|
||||||
|
{
|
||||||
|
uint8_t led;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (!led_ctrl || !num_leds)
|
||||||
|
ztest_test_skip();
|
||||||
|
|
||||||
|
for (led = 0; led < num_leds; led++) {
|
||||||
|
ret = led_off(led_ctrl, led);
|
||||||
|
zassert_equal(ret, 0, "LED %d - failed to turn off", led);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_led_set_color(void)
|
||||||
|
{
|
||||||
|
uint8_t led;
|
||||||
|
uint8_t colors[TEST_MAX_COLORS + 1];
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (!led_ctrl || !num_leds)
|
||||||
|
ztest_test_skip();
|
||||||
|
|
||||||
|
for (led = 0; led < num_leds; led++) {
|
||||||
|
uint8_t num_colors = test_led_info[led].num_colors;
|
||||||
|
uint8_t col;
|
||||||
|
|
||||||
|
if (num_colors > TEST_MAX_COLORS) {
|
||||||
|
TC_PRINT("LED %d - skip set_color test, num_colors: %d"
|
||||||
|
" (test limit is %d)",
|
||||||
|
led, num_colors, TEST_MAX_COLORS);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(colors, 0, sizeof(colors));
|
||||||
|
|
||||||
|
/* Try to set more colors than supported. */
|
||||||
|
ret = led_set_color(led_ctrl, led, num_colors + 1, colors);
|
||||||
|
zassert_not_equal(ret, 0, "LED %d - setting %d"
|
||||||
|
" colors should fail (%d supported)",
|
||||||
|
led, num_colors + 1, num_colors);
|
||||||
|
|
||||||
|
if (!num_colors) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Try to set less colors than supported. */
|
||||||
|
ret = led_set_color(led_ctrl, led, num_colors - 1, colors);
|
||||||
|
zassert_not_equal(ret, 0, "LED %d - setting %d"
|
||||||
|
" colors should fail (%d supported)",
|
||||||
|
led, num_colors - 1, num_colors);
|
||||||
|
|
||||||
|
/* Ensure the LED is on to get a visual feedback. */
|
||||||
|
led_set_brightness(led_ctrl, led, BRIGHTNESS_MAX / 2);
|
||||||
|
|
||||||
|
/* Set each color gradually to its maximum level. */
|
||||||
|
for (col = 0; col < num_colors; col++) {
|
||||||
|
uint16_t level;
|
||||||
|
|
||||||
|
memset(colors, 0, sizeof(colors));
|
||||||
|
|
||||||
|
for (level = 0; level <= COLOR_FULL; level++) {
|
||||||
|
colors[col] = level;
|
||||||
|
|
||||||
|
ret = led_set_color(led_ctrl, led,
|
||||||
|
num_colors, colors);
|
||||||
|
zassert_equal(ret, 0,
|
||||||
|
"LED %d - failed to set color[%d] to %d",
|
||||||
|
led, level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_led_set_brightness(void)
|
||||||
|
{
|
||||||
|
uint8_t led;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (!led_ctrl || !num_leds)
|
||||||
|
ztest_test_skip();
|
||||||
|
|
||||||
|
for (led = 0; led < num_leds; led++) {
|
||||||
|
uint16_t level;
|
||||||
|
|
||||||
|
for (level = 0; level <= BRIGHTNESS_MAX; level++) {
|
||||||
|
ret = led_set_brightness(led_ctrl, led, level);
|
||||||
|
zassert_equal(ret, 0,
|
||||||
|
"LED %d - failed to set brightness to %d",
|
||||||
|
led, level);
|
||||||
|
}
|
||||||
|
for (level = BRIGHTNESS_MAX + 1; level <= 255; level++) {
|
||||||
|
ret = led_set_brightness(led_ctrl, led, level);
|
||||||
|
zassert_not_equal(ret, 0,
|
||||||
|
"LED %d - setting brightness to %d"
|
||||||
|
" should fail (maximum: %d)",
|
||||||
|
led, level, BRIGHTNESS_MAX);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
tests/drivers/led/led_api/src/test_led_api.h
Normal file
19
tests/drivers/led/led_api/src/test_led_api.h
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Seagate Technology LLC
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TEST_LED_API_H_
|
||||||
|
#define TEST_LED_API_H_
|
||||||
|
|
||||||
|
struct device *get_led_controller(void);
|
||||||
|
|
||||||
|
void test_led_setup(void);
|
||||||
|
void test_led_get_info(void);
|
||||||
|
void test_led_on(void);
|
||||||
|
void test_led_off(void);
|
||||||
|
void test_led_set_color(void);
|
||||||
|
void test_led_set_brightness(void);
|
||||||
|
|
||||||
|
#endif
|
5
tests/drivers/led/led_api/testcase.yaml
Normal file
5
tests/drivers/led/led_api/testcase.yaml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
tests:
|
||||||
|
drivers.i2c:
|
||||||
|
tags: drivers led
|
||||||
|
depends_on: led
|
||||||
|
filter: dt_alias_exists("led-controller-0")
|
Loading…
Add table
Add a link
Reference in a new issue