Skip to content

Commit a17d86b

Browse files
committed
tests: drivers: sensors: ALS
Added a new sensor test for ALS. Signed-off-by: Rick Talbott <[email protected]>
1 parent 3b4c77b commit a17d86b

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) 2023 T-Mobile
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_I2C=y
5+
CONFIG_SENSOR=y
6+
CONFIG_TSL2540=y
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Copyright (c) 2023 T-Mobile
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
/ {
6+
aliases {
7+
als-0 = &tsl2540_i2c;
8+
};
9+
};
10+
11+
&i2c0 {
12+
status = "okay";
13+
tsl2540_i2c: tsl2540@39 {
14+
status = "okay";
15+
compatible = "ams,tsl2540";
16+
reg = <0x39 0x4>;
17+
};
18+
};

tests/drivers/sensor/als/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_ZTEST_NEW_API=y

tests/drivers/sensor/als/src/main.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2032 T-Mobile
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @defgroup driver_sensor_subsys_tests sensor_subsys
9+
* @ingroup all_tests
10+
* @{
11+
* @}
12+
*/
13+
14+
#include <zephyr/ztest.h>
15+
#include <zephyr/drivers/sensor.h>
16+
17+
struct sensor_als_fixture {
18+
const struct device *als_i2c;
19+
};
20+
21+
static enum sensor_channel channel[] = {
22+
SENSOR_CHAN_LIGHT,
23+
SENSOR_CHAN_IR,
24+
};
25+
26+
static void test_sensor_als_basic(const struct device *dev)
27+
{
28+
zassert_equal(sensor_sample_fetch(dev), 0, "fail to fetch sample");
29+
30+
for (int i = 0; i < ARRAY_SIZE(channel); i++) {
31+
struct sensor_value val;
32+
33+
zassert_ok(sensor_channel_get(dev, channel[i], &val),
34+
"fail to get channel");
35+
}
36+
}
37+
38+
/* Run all of our tests on an als device with the given label */
39+
static void run_tests_on_als(const struct device *als)
40+
{
41+
zassert_true(device_is_ready(als), "als device is not ready");
42+
43+
PRINT("Running tests on '%s'\n", als->name);
44+
k_object_access_grant(als, k_current_get());
45+
}
46+
47+
48+
ZTEST_USER_F(sensor_als, test_sensor_als_basic_i2c)
49+
{
50+
if (fixture->als_i2c == NULL) {
51+
ztest_test_skip();
52+
}
53+
54+
run_tests_on_als(fixture->als_i2c);
55+
test_sensor_als_basic(fixture->als_i2c);
56+
}
57+
58+
static void *sensor_als_setup(void)
59+
{
60+
static struct sensor_als_fixture fixture = {
61+
.als_i2c = DEVICE_DT_GET_OR_NULL(DT_ALIAS(als_0))
62+
};
63+
64+
return &fixture;
65+
}
66+
67+
ZTEST_SUITE(sensor_als, NULL, sensor_als_setup, NULL, NULL, NULL);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests:
2+
drivers.sensor.als:
3+
tags:
4+
- drivers
5+
- sensor
6+
- subsys
7+
integration_platforms:
8+
- tmo_dev_edge

0 commit comments

Comments
 (0)