Skip to content

Commit 1c6709c

Browse files
committed
tests: drivers: sensor: Added MTCH9010 Tests
Added testsuite to verify MTCH9010 sensor driver Signed-off-by: Robert Perkel <robert.perkel@microchip.com>
1 parent cac066c commit 1c6709c

File tree

11 files changed

+486
-0
lines changed

11 files changed

+486
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2025 Microchip Technology Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(device)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
10+
11+
zephyr_include_directories(${ZEPHYR_BASE}/drivers/sensor/microchip/mtch9010)
12+
zephyr_include_directories(./src)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_GPIO=y
3+
CONFIG_SERIAL=y
4+
5+
# Device
6+
CONFIG_SENSOR=y
7+
CONFIG_MTCH9010=y
8+
9+
# Emulation Settings
10+
CONFIG_EMUL=y
11+
CONFIG_GPIO_EMUL=y
12+
CONFIG_UART_EMUL=y
13+
CONFIG_LOG=y
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
/*
2+
* Copyright (c) 2025 Microchip Technology Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <string.h>
8+
#include <errno.h>
9+
10+
#include <zephyr/device.h>
11+
#include <zephyr/devicetree.h>
12+
#include <zephyr/drivers/emul.h>
13+
#include <zephyr/drivers/sensor.h>
14+
#include <zephyr/drivers/gpio.h>
15+
#include <zephyr/ztest.h>
16+
#include <zephyr/ztest_assert.h>
17+
18+
#include "../drivers/sensor/microchip/mtch9010/mtch9010_priv.h"
19+
#include "zephyr/drivers/sensor/mtch9010.h"
20+
21+
#define DUT_NODE DT_NODELABEL(dut)
22+
23+
ZTEST_SUITE(mtch9010_utility, NULL, NULL, NULL, NULL, NULL);
24+
25+
ZTEST(mtch9010_utility, test_result_decode)
26+
{
27+
/* Basic Decode Tests */
28+
const char *test_pattern_1 = "12345\n\r";
29+
const char *test_pattern_2 = "10\n\r";
30+
const char *test_pattern_3 = "999 12405\n\r";
31+
const char *test_pattern_4 = "0 1234\n\r";
32+
const char *test_pattern_5 = "100 -99\n\r";
33+
34+
/* Bad Decodes */
35+
const char *bad_decode_pattern_1 = "10\n\r";
36+
const char *bad_decode_pattern_2 = "655636\n\r";
37+
const char *bad_decode_pattern_3 = "-100\n\r";
38+
const char *bad_decode_pattern_4 = "100";
39+
const char *bad_decode_pattern_5 = "100\t\n";
40+
const char *bad_decode_pattern_6 = "a100\n\r";
41+
42+
struct mtch9010_result test_result;
43+
44+
/* Test Current decode */
45+
int ret = mtch9010_decode_char_buffer(test_pattern_1, MTCH9010_OUTPUT_FORMAT_CURRENT,
46+
&test_result);
47+
48+
zassert_equal(ret, 0, "Unable to decode test_pattern_1");
49+
zassert_equal(test_result.measurement, 12345, "Decoded value does not match expected");
50+
51+
/* Test DELTA decode */
52+
ret = mtch9010_decode_char_buffer(test_pattern_2, MTCH9010_OUTPUT_FORMAT_DELTA,
53+
&test_result);
54+
55+
zassert_equal(ret, 0, "Unable to decode test_pattern_2");
56+
zassert_equal(test_result.delta, 10, "Decoded value does not match expected");
57+
58+
/* Test Current and Delta decode */
59+
ret = mtch9010_decode_char_buffer(test_pattern_3, MTCH9010_OUTPUT_FORMAT_BOTH,
60+
&test_result);
61+
62+
zassert_equal(ret, 0, "Unable to decode test_pattern_3");
63+
zassert_equal(test_result.prev_measurement, 12345,
64+
"Previous value does not match expected");
65+
zassert_equal(test_result.measurement, 999, "Decoded value does not match expected");
66+
zassert_equal(test_result.delta, 12405, "Decoded value does not match expected");
67+
68+
/* Test MPLAB Data Visualizer Format (should fail) */
69+
ret = mtch9010_decode_char_buffer(
70+
test_pattern_4, MTCH9010_OUTPUT_FORMAT_MPLAB_DATA_VISUALIZER, &test_result);
71+
zassert_equal(ret, -ENOTSUP, "Incorrectly decoded test_pattern_4");
72+
73+
/* Test Negative Delta */
74+
ret = mtch9010_decode_char_buffer(test_pattern_5, MTCH9010_OUTPUT_FORMAT_BOTH,
75+
&test_result);
76+
zassert_equal(ret, 0, "Unable to decode test_pattern_5");
77+
zassert_equal(test_result.measurement, 100, "Decoded value does not match expected");
78+
zassert_equal(test_result.delta, -99, "Decoded value does not match expected");
79+
80+
/* Test Bad Decode 1 - Incorrect format */
81+
ret = mtch9010_decode_char_buffer(bad_decode_pattern_1, MTCH9010_OUTPUT_FORMAT_BOTH,
82+
&test_result);
83+
zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_1");
84+
85+
/* Test Bad Decode 2 - UINT16 Buffer Overflow */
86+
ret = mtch9010_decode_char_buffer(bad_decode_pattern_2, MTCH9010_OUTPUT_FORMAT_CURRENT,
87+
&test_result);
88+
zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_2");
89+
90+
/* Test Bad Decode 3 - Negative Values */
91+
ret = mtch9010_decode_char_buffer(bad_decode_pattern_3, MTCH9010_OUTPUT_FORMAT_CURRENT,
92+
&test_result);
93+
zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_3");
94+
95+
/* Test Bad Decode 4 - Missing Return */
96+
ret = mtch9010_decode_char_buffer(bad_decode_pattern_4, MTCH9010_OUTPUT_FORMAT_CURRENT,
97+
&test_result);
98+
zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_4");
99+
100+
/* Test Bad Decode 5 - Invalid Return */
101+
ret = mtch9010_decode_char_buffer(bad_decode_pattern_5, MTCH9010_OUTPUT_FORMAT_CURRENT,
102+
&test_result);
103+
zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_5");
104+
105+
/* Test Bad Decode 6 - Invalid Starting Character */
106+
ret = mtch9010_decode_char_buffer(bad_decode_pattern_6, MTCH9010_OUTPUT_FORMAT_CURRENT,
107+
&test_result);
108+
zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_6");
109+
}
110+
111+
struct mtch9010_config_fixture {
112+
const struct device *dev;
113+
};
114+
115+
static void *mtch9010_setup(void)
116+
{
117+
static struct mtch9010_config_fixture fixture = {
118+
.dev = DEVICE_DT_GET(DUT_NODE),
119+
};
120+
121+
/* Verify we found a device */
122+
zassert_not_null(fixture.dev);
123+
124+
/* Create the reference configuration */
125+
return &fixture;
126+
}
127+
128+
ZTEST_SUITE(mtch9010_config, NULL, mtch9010_setup, NULL, NULL, NULL);
129+
130+
/* Check UART */
131+
ZTEST_F(mtch9010_config, test_uart_init)
132+
{
133+
const struct mtch9010_config *config = fixture->dev->config;
134+
135+
/* Verify the boolean flag */
136+
if (config->uart_init) {
137+
zassert_true(DT_PROP_OR(DUT_NODE, mtch9010_uart_config_enable, false),
138+
"UART Init was enabled, but was not set");
139+
} else {
140+
zassert_false(DT_PROP_OR(DUT_NODE, mtch9010_uart_config_enable, false),
141+
"UART Init was disabled, but was set");
142+
}
143+
144+
/* Verify the UART Bus Pointer */
145+
const struct device *bus = DEVICE_DT_GET_OR_NULL(DT_BUS(DUT_NODE));
146+
147+
zassert_equal_ptr(bus, config->uart_dev, "UART Bus is not correctly assigned");
148+
}
149+
150+
/* Check GPIO Assignments */
151+
ZTEST_F(mtch9010_config, test_gpio_bindings)
152+
{
153+
const struct mtch9010_config *config = fixture->dev->config;
154+
155+
/* GPIOs to Test */
156+
const struct gpio_dt_spec mode_gpio =
157+
GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_mode_gpios, {0});
158+
const struct gpio_dt_spec output_gpio =
159+
GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_output_gpios, {0});
160+
const struct gpio_dt_spec lock_gpio =
161+
GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_system_lock_gpios, {0});
162+
const struct gpio_dt_spec reset_gpio =
163+
GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_reset_gpios, {0});
164+
const struct gpio_dt_spec wake_gpio =
165+
GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_wake_gpios, {0});
166+
const struct gpio_dt_spec uart_en_gpio =
167+
GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_uart_en_gpios, {0});
168+
const struct gpio_dt_spec cfg_en_gpio =
169+
GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_cfg_en_gpios, {0});
170+
const struct gpio_dt_spec heartbeat_gpio =
171+
GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_heartbeat_gpios, {0});
172+
173+
if (mode_gpio.port != NULL) {
174+
zassert_not_null(config->mode_gpio.port, "mode_gpio is NULL, but was assigned");
175+
} else {
176+
zassert_is_null(config->mode_gpio.port,
177+
"mode_gpio is not NULL, but was not assigned");
178+
}
179+
180+
if (output_gpio.port != NULL) {
181+
zassert_not_null(config->out_gpio.port, "output_gpio is NULL, but was assigned");
182+
} else {
183+
zassert_is_null(config->out_gpio.port,
184+
"output_gpio is not NULL, but was not assigned");
185+
}
186+
187+
if (lock_gpio.port != NULL) {
188+
zassert_not_null(config->lock_gpio.port, "lock_gpio is NULL, but was assigned");
189+
} else {
190+
zassert_is_null(config->lock_gpio.port,
191+
"lock_gpio is not NULL, but was not assigned");
192+
}
193+
194+
if (reset_gpio.port != NULL) {
195+
zassert_not_null(config->reset_gpio.port, "reset_gpio is NULL, but was assigned");
196+
} else {
197+
zassert_is_null(config->reset_gpio.port,
198+
"reset_gpio is not NULL, but was not assigned");
199+
}
200+
201+
if (wake_gpio.port != NULL) {
202+
zassert_not_null(config->wake_gpio.port, "wake_gpio is NULL, but was assigned");
203+
} else {
204+
zassert_is_null(config->wake_gpio.port,
205+
"wake_gpio is not NULL, but was not assigned");
206+
}
207+
208+
if (uart_en_gpio.port != NULL) {
209+
zassert_not_null(config->enable_uart_gpio.port,
210+
"uart_en_gpio is NULL, but was assigned");
211+
} else {
212+
zassert_is_null(config->enable_uart_gpio.port,
213+
"uart_en_gpio is not NULL, but was not assigned");
214+
}
215+
216+
if (cfg_en_gpio.port != NULL) {
217+
zassert_not_null(config->enable_cfg_gpio.port,
218+
"cfg_en_gpio is NULL, but was assigned");
219+
} else {
220+
zassert_is_null(config->enable_cfg_gpio.port,
221+
"cfg_en_gpio is not NULL, but was not assigned");
222+
}
223+
224+
if (heartbeat_gpio.port != NULL) {
225+
zassert_not_null(config->heartbeat_gpio.port,
226+
"heartbeat_gpio is NULL, but was assigned");
227+
} else {
228+
zassert_is_null(config->heartbeat_gpio.port,
229+
"heartbeat_gpio is not NULL, but was not assigned");
230+
}
231+
}
232+
233+
ZTEST_F(mtch9010_config, test_sleep_time)
234+
{
235+
const struct mtch9010_config *config = fixture->dev->config;
236+
237+
zassert((config->sleep_time == DT_PROP_OR(DUT_NODE, mtch9010_sleep_period, 0)),
238+
"sleepTime was not correctly assigned.");
239+
}
240+
241+
ZTEST_F(mtch9010_config, test_output_format)
242+
{
243+
const struct mtch9010_config *config = fixture->dev->config;
244+
245+
if (config->extended_mode_enable) {
246+
zassert_true(DT_PROP_OR(DUT_NODE, mtch9010_extended_output_enable, false),
247+
"Extended output was disabled, but was set");
248+
} else {
249+
zassert_false(DT_PROP_OR(DUT_NODE, mtch9010_extended_output_enable, false),
250+
"Extended output was enabled, but not set");
251+
252+
zassert_true((config->format == MTCH9010_OUTPUT_FORMAT_CURRENT),
253+
"Current output format was not correctly implied");
254+
}
255+
}
256+
257+
ZTEST_F(mtch9010_config, test_custom_value)
258+
{
259+
const struct mtch9010_config *config = fixture->dev->config;
260+
const struct mtch9010_data *data = fixture->dev->data;
261+
int custom_value = DT_PROP_OR(DUT_NODE, mtch9010_reference_value, -1);
262+
263+
switch (config->ref_mode) {
264+
case MTCH9010_REFERENCE_CURRENT_VALUE: {
265+
zassert_equal(custom_value, -1, "Incorrect reference initialization mode set");
266+
break;
267+
}
268+
case MTCH9010_REFERENCE_CUSTOM_VALUE: {
269+
zassert_not_equal(custom_value, -1, "Incorrect reference initialization mode set");
270+
zassert_equal(custom_value, data->reference,
271+
"Reference value was not set to custom value");
272+
break;
273+
}
274+
case MTCH9010_REFERENCE_RERUN_VALUE: {
275+
zassert_unreachable("Illegal reference value mode set");
276+
break;
277+
}
278+
default: {
279+
zassert_unreachable("Unknown Reference Value set");
280+
break;
281+
}
282+
}
283+
}
284+
285+
ZTEST_F(mtch9010_config, test_threshold_value)
286+
{
287+
const struct mtch9010_data *data = fixture->dev->data;
288+
int custom_value = DT_PROP(DUT_NODE, mtch9010_detect_value);
289+
290+
zassert_equal(data->threshold, custom_value, "Threshold value was not set to custom value");
291+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2025 Microchip Technology Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
&uart1 {
7+
status = "okay";
8+
current-speed = <38400>;
9+
10+
dut: mtch9010 {
11+
status = "okay";
12+
compatible = "microchip,mtch9010";
13+
14+
/* Device Configuration */
15+
operating-mode = "MTCH9010_CONDUCTIVE";
16+
extended-output-enable;
17+
extended-output-format = "MTCH9010_OUTPUT_FORMAT_BOTH";
18+
reference-value = <0>;
19+
detect-value = <600>;
20+
};
21+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2025 Microchip Technology Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
&uart1 {
7+
status = "okay";
8+
current-speed = <38400>;
9+
10+
dut: mtch9010 {
11+
status = "okay";
12+
compatible = "microchip,mtch9010";
13+
14+
/* Device Configuration */
15+
operating-mode = "MTCH9010_CAPACITIVE";
16+
extended-output-enable;
17+
extended-output-format = "MTCH9010_OUTPUT_FORMAT_CURRENT";
18+
reference-value = <0>;
19+
detect-value = <600>;
20+
};
21+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2025 Microchip Technology Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
&uart1 {
7+
status = "okay";
8+
current-speed = <38400>;
9+
10+
dut: mtch9010 {
11+
status = "okay";
12+
compatible = "microchip,mtch9010";
13+
14+
/* Device Configuration */
15+
operating-mode = "MTCH9010_CONDUCTIVE";
16+
extended-output-enable;
17+
extended-output-format = "MTCH9010_OUTPUT_FORMAT_MPLAB_DATA_VISUALIZER";
18+
reference-value = <0>;
19+
detect-value = <600>;
20+
};
21+
};

0 commit comments

Comments
 (0)