Skip to content

Commit 9664f86

Browse files
keith-zephyrstephanosio
authored andcommitted
test: emul: Verify backend API operation
Verify backend API is functional with the emulator subsystem. Signed-off-by: Keith Short <[email protected]>
1 parent 6062914 commit 9664f86

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2023, Google, LLC
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: VND Emul tester
5+
6+
compatible: "vnd,emul-tester"
7+
8+
include: base.yaml
9+
10+
properties:
11+
scale:
12+
type: int
13+
required: true
14+
description: |
15+
Integer scale applied to the set action for testing.

tests/subsys/emul/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
88
project(emul)
99

1010
target_sources(app PRIVATE src/main.c)
11+
target_sources(app PRIVATE src/emul_tester.c)

tests/subsys/emul/app.overlay

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,17 @@
1616
reg = <0x68>;
1717
status = "okay";
1818
};
19+
20+
emul_tester_a: driver@aa {
21+
compatible = "vnd,emul-tester";
22+
reg = <0xaa>;
23+
status = "okay";
24+
scale = <1>;
25+
};
26+
emul_tester_b: driver@bb {
27+
compatible = "vnd,emul-tester";
28+
reg = <0xbb>;
29+
status = "okay";
30+
scale = <10>;
31+
};
1932
};

tests/subsys/emul/src/emul_tester.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "emul_tester.h"
8+
9+
#include <zephyr/device.h>
10+
#include <zephyr/drivers/emul.h>
11+
#include <zephyr/drivers/emul_stub_device.h>
12+
#include <zephyr/drivers/i2c.h>
13+
#include <zephyr/drivers/i2c_emul.h>
14+
15+
#define DT_DRV_COMPAT vnd_emul_tester
16+
17+
struct emul_tester_cfg {
18+
int scale;
19+
};
20+
21+
struct emul_tester_data {
22+
int action;
23+
};
24+
25+
static int emul_tester_set_action(const struct emul *target, int action)
26+
{
27+
const struct emul_tester_cfg *cfg = target->cfg;
28+
struct emul_tester_data *data = target->data;
29+
30+
data->action = action * cfg->scale;
31+
32+
return 0;
33+
}
34+
35+
static int emul_tester_get_action(const struct emul *target, int *action)
36+
{
37+
struct emul_tester_data *data = target->data;
38+
39+
*action = data->action;
40+
41+
return 0;
42+
}
43+
44+
static int emul_tester_transfer(const struct emul *target, struct i2c_msg *msgs, int num_msgs,
45+
int addr)
46+
{
47+
ARG_UNUSED(target);
48+
ARG_UNUSED(msgs);
49+
ARG_UNUSED(num_msgs);
50+
ARG_UNUSED(addr);
51+
52+
return -ENOTSUP;
53+
}
54+
55+
static struct i2c_emul_api bus_api = {
56+
.transfer = emul_tester_transfer,
57+
};
58+
59+
static const struct emul_tester_backend_api emul_tester_backend_api = {
60+
.set_action = emul_tester_set_action,
61+
.get_action = emul_tester_get_action,
62+
};
63+
64+
static int emul_tester_init(const struct emul *target, const struct device *parent)
65+
{
66+
ARG_UNUSED(target);
67+
ARG_UNUSED(parent);
68+
69+
return 0;
70+
}
71+
72+
#define EMUL_TESTER(n) \
73+
static struct emul_tester_data emul_tester_data_##n; \
74+
static const struct emul_tester_cfg emul_tester_cfg_##n = { \
75+
.scale = DT_INST_PROP(n, scale), \
76+
}; \
77+
EMUL_DT_INST_DEFINE(n, emul_tester_init, &emul_tester_data_##n, &emul_tester_cfg_##n, \
78+
&bus_api, &emul_tester_backend_api);
79+
80+
DT_INST_FOREACH_STATUS_OKAY(EMUL_TESTER)
81+
82+
DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE)

tests/subsys/emul/src/emul_tester.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef TEST_SUBSYS_EMUL_SRC_EMUL_TESTER_H_
8+
#define TEST_SUBSYS_EMUL_SRC_EMUL_TESTER_H_
9+
10+
#include <zephyr/drivers/emul.h>
11+
12+
struct emul_tester_backend_api {
13+
int (*set_action)(const struct emul *target, int action);
14+
int (*get_action)(const struct emul *target, int *action);
15+
};
16+
17+
static inline int emul_tester_backend_set_action(const struct emul *target, int action)
18+
{
19+
const struct emul_tester_backend_api *api =
20+
(const struct emul_tester_backend_api *)target->backend_api;
21+
22+
return api->set_action(target, action);
23+
}
24+
25+
static inline int emul_tester_backend_get_action(const struct emul *target, int *action)
26+
{
27+
const struct emul_tester_backend_api *api =
28+
(const struct emul_tester_backend_api *)target->backend_api;
29+
30+
return api->get_action(target, action);
31+
}
32+
33+
#endif /* TEST_SUBSYS_EMUL_SRC_EMUL_TESTER_H_ */

tests/subsys/emul/src/main.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include "emul_tester.h"
8+
79
#include <zephyr/device.h>
810
#include <zephyr/devicetree.h>
911
#include <zephyr/drivers/emul.h>
@@ -13,6 +15,9 @@
1315

1416
#define TEST_ACCEL DT_NODELABEL(test_bmi)
1517

18+
#define TEST_EMUL_A DT_NODELABEL(emul_tester_a)
19+
#define TEST_EMUL_B DT_NODELABEL(emul_tester_b)
20+
1621
ZTEST(emul, test_emul_dt_get)
1722
{
1823
/* This variable is static to verify that the result of EMUL_DT_GET is a
@@ -26,4 +31,27 @@ ZTEST(emul, test_emul_dt_get)
2631
"Unexpected device name %s", emul_static->dev->name);
2732
}
2833

34+
ZTEST(emul, test_emul_backend_api)
35+
{
36+
static const struct emul *emul_a = EMUL_DT_GET(TEST_EMUL_A);
37+
static const struct emul *emul_b = EMUL_DT_GET(TEST_EMUL_B);
38+
int set_action_value;
39+
int scale;
40+
int get_action_value;
41+
42+
set_action_value = 5;
43+
44+
scale = DT_PROP(TEST_EMUL_A, scale);
45+
zassert_not_null(emul_a, "emul_tester_a not found");
46+
zassert_ok(emul_tester_backend_set_action(emul_a, set_action_value));
47+
zassert_ok(emul_tester_backend_get_action(emul_a, &get_action_value));
48+
zassert_equal(get_action_value, set_action_value * scale);
49+
50+
scale = DT_PROP(TEST_EMUL_B, scale);
51+
zassert_not_null(emul_b, "emul_tester_b not found");
52+
zassert_ok(emul_tester_backend_set_action(emul_b, set_action_value));
53+
zassert_ok(emul_tester_backend_get_action(emul_b, &get_action_value));
54+
zassert_equal(get_action_value, set_action_value * scale);
55+
}
56+
2957
ZTEST_SUITE(emul, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)