Skip to content

Commit 275c690

Browse files
nordic-baburlubos
authored andcommitted
[nrf fromtree] tests: drivers: adc: add adc_error_cases tests.
Tests are checking error codes returned from adc_read() and adc_channel_setup() used with invalid configurations. Signed-off-by: Bartlomiej Buczek <[email protected]> (cherry picked from commit da81490)
1 parent de67069 commit 275c690

File tree

7 files changed

+272
-0
lines changed

7 files changed

+272
-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.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(adc_error_cases)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
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+
* Copyright (c) 2024 Nordic Semiconductor ASA
5+
*/
6+
7+
/ {
8+
aliases {
9+
adc = &adc;
10+
};
11+
};
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+
* Copyright (c) 2024 Nordic Semiconductor ASA
5+
*/
6+
7+
/ {
8+
aliases {
9+
adc = &adc;
10+
};
11+
};
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+
* Copyright (c) 2024 Nordic Semiconductor ASA
5+
*/
6+
7+
/ {
8+
aliases {
9+
adc = &adc;
10+
};
11+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_ZTEST=y
2+
3+
CONFIG_ADC=y
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
* Copyright (c) 2024, Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/ztest.h>
10+
#include <zephyr/drivers/adc.h>
11+
12+
static const struct device *dev_adc = DEVICE_DT_GET(DT_ALIAS(adc));
13+
#define BUFFER_LEN 8
14+
static uint16_t m_sample_buffer[BUFFER_LEN];
15+
16+
static const struct adc_channel_cfg valid_channel_cfg = {
17+
.gain = ADC_GAIN_1,
18+
.channel_id = 0,
19+
.reference = ADC_REF_INTERNAL,
20+
.acquisition_time = ADC_ACQ_TIME_DEFAULT,
21+
.differential = false,
22+
#ifdef CONFIG_ADC_CONFIGURABLE_INPUTS
23+
.input_positive = 1,
24+
#endif
25+
};
26+
27+
static const struct adc_sequence valid_seq = {
28+
.buffer = m_sample_buffer,
29+
.buffer_size = BUFFER_LEN * sizeof(m_sample_buffer),
30+
.options = NULL,
31+
.resolution = 10,
32+
.oversampling = 0,
33+
.channels = 1,
34+
};
35+
36+
/**
37+
* @brief test adc_read() with invalid oversampling value
38+
*
39+
* function should return -EINVAL
40+
*/
41+
42+
ZTEST(adc_error_cases, test_adc_read_invalid_oversampling)
43+
{
44+
int ret;
45+
46+
adc_channel_setup(dev_adc, &valid_channel_cfg);
47+
48+
struct adc_sequence invalid_seq = valid_seq;
49+
/* Set oversampling to invalid value */
50+
invalid_seq.oversampling = 99;
51+
52+
ret = adc_read(dev_adc, &invalid_seq);
53+
54+
zassert_true(
55+
ret == -EINVAL,
56+
"adc_read() should return -EINVAL,"
57+
" got unexpected value of %d",
58+
ret
59+
);
60+
}
61+
62+
/**
63+
* @brief test adc_read() with invalid resolution value
64+
*
65+
* function should return -EINVAL
66+
*/
67+
68+
ZTEST(adc_error_cases, test_adc_read_invalid_resolution)
69+
{
70+
int ret;
71+
72+
adc_channel_setup(dev_adc, &valid_channel_cfg);
73+
74+
struct adc_sequence invalid_seq = valid_seq;
75+
/* Set resolution to invalid value */
76+
invalid_seq.resolution = 99;
77+
78+
ret = adc_read(dev_adc, &invalid_seq);
79+
80+
zassert_true(
81+
ret == -EINVAL,
82+
"adc_read() should return -EINVAL,"
83+
" got unexpected value of %d",
84+
ret
85+
);
86+
}
87+
88+
/**
89+
* @brief test adc_read() with invalid channels value
90+
*
91+
* function should return -EINVAL
92+
*/
93+
94+
ZTEST(adc_error_cases, test_adc_read_invalid_channels)
95+
{
96+
int ret;
97+
98+
adc_channel_setup(dev_adc, &valid_channel_cfg);
99+
100+
struct adc_sequence invalid_seq = valid_seq;
101+
/* Set channels configuration to invalid value */
102+
invalid_seq.channels = 0;
103+
104+
ret = adc_read(dev_adc, &invalid_seq);
105+
106+
zassert_true(
107+
ret == -EINVAL,
108+
"adc_read() should return -EINVAL,"
109+
" got unexpected value of %d",
110+
ret
111+
);
112+
}
113+
114+
/**
115+
* @brief test adc_read() with not configured channel
116+
*
117+
* function should return -EINVAL
118+
*/
119+
120+
ZTEST(adc_error_cases, test_adc_read_not_configured_channel)
121+
{
122+
int ret;
123+
124+
adc_channel_setup(dev_adc, &valid_channel_cfg);
125+
126+
struct adc_sequence invalid_seq = valid_seq;
127+
/* Set channels configuration to use not configured channel */
128+
invalid_seq.channels = BIT(1);
129+
130+
ret = adc_read(dev_adc, &invalid_seq);
131+
132+
zassert_true(
133+
ret == -EINVAL,
134+
"adc_read() should return -EINVAL,"
135+
" got unexpected value of %d",
136+
ret
137+
);
138+
}
139+
140+
/**
141+
* @brief test adc_read() with invalid buffer length
142+
*
143+
* function should return -ENOMEM
144+
*/
145+
146+
ZTEST(adc_error_cases, test_adc_read_invalid_buffer)
147+
{
148+
int ret;
149+
150+
adc_channel_setup(dev_adc, &valid_channel_cfg);
151+
152+
struct adc_sequence invalid_seq = valid_seq;
153+
/* set buffer size to 0 bytes */
154+
invalid_seq.buffer_size = 0;
155+
156+
ret = adc_read(dev_adc, &invalid_seq);
157+
158+
zassert_true(
159+
ret == -ENOMEM,
160+
"adc_read() should return -ENOMEM,"
161+
" got unexpected value of %d",
162+
ret
163+
);
164+
}
165+
166+
/**
167+
* @brief test adc_channel_setup() with invalid reference value
168+
*
169+
* function should return -EINVAL
170+
*/
171+
172+
ZTEST(adc_error_cases, test_adc_setup_invalid_reference)
173+
{
174+
int ret;
175+
176+
struct adc_channel_cfg invalid_channel_cfg = valid_channel_cfg;
177+
/* set invalid reference */
178+
invalid_channel_cfg.reference = 99;
179+
180+
ret = adc_channel_setup(dev_adc, &invalid_channel_cfg);
181+
182+
zassert_true(
183+
ret == -EINVAL,
184+
"adc_channel_setup() should return -EINVAL,"
185+
" got unexpected value of %d",
186+
ret
187+
);
188+
}
189+
190+
/**
191+
* @brief test adc_read() with invalid gain value
192+
*
193+
* function should return -EINVAL
194+
*/
195+
196+
ZTEST(adc_error_cases, test_adc_setup_invalid_gain)
197+
{
198+
int ret;
199+
200+
struct adc_channel_cfg invalid_channel_cfg = valid_channel_cfg;
201+
/* set invalid gain value */
202+
invalid_channel_cfg.gain = 99;
203+
ret = adc_channel_setup(dev_adc, &invalid_channel_cfg);
204+
zassert_true(
205+
ret == -EINVAL,
206+
"adc_channel_setup() should return -EINVAL,"
207+
" got unexpected value of %d",
208+
ret
209+
);
210+
}
211+
212+
static void *suite_setup(void)
213+
{
214+
TC_PRINT("Test executed on %s\n", CONFIG_BOARD_TARGET);
215+
TC_PRINT("===================================================================\n");
216+
217+
return NULL;
218+
}
219+
220+
ZTEST_SUITE(adc_error_cases, NULL, suite_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.adc_error_cases:
3+
depends_on: adc
4+
platform_allow:
5+
- nrf52840dk/nrf52840
6+
- nrf54l15dk/nrf54l15/cpuapp
7+
- nrf54h20dk/nrf54h20/cpuapp

0 commit comments

Comments
 (0)