Skip to content

Commit 6062914

Browse files
keith-zephyrstephanosio
authored andcommitted
emul: Add support for backend APIs
The emulator structure currently initializes a bus-level API that is used to communicated register accesses to the emulator from the parent bus. Add support for declaring a backend API for emulators. This provides tests a common way to interact with emulators of the same device type. Signed-off-by: Keith Short <[email protected]>
1 parent 2d53ce6 commit 6062914

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

include/zephyr/drivers/emul.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ struct emul {
8181
struct espi_emul *espi;
8282
struct spi_emul *spi;
8383
} bus;
84+
/** Address of the API structure exposed by the emulator instance */
85+
const void *backend_api;
8486
};
8587

8688
/**
@@ -120,8 +122,9 @@ extern const struct emul __emul_list_end[];
120122
* @param data_ptr emulator-specific data
121123
* @param cfg_ptr emulator-specific configuration data
122124
* @param bus_api emulator-specific bus api
125+
* @param _backend_api emulator-specific backend api
123126
*/
124-
#define EMUL_DT_DEFINE(node_id, init_fn, data_ptr, cfg_ptr, bus_api) \
127+
#define EMUL_DT_DEFINE(node_id, init_fn, data_ptr, cfg_ptr, bus_api, _backend_api) \
125128
static struct Z_EMUL_BUS(node_id, i2c_emul, espi_emul, spi_emul) \
126129
Z_EMUL_REG_BUS_IDENTIFIER(node_id) = { \
127130
.api = bus_api, \
@@ -137,6 +140,7 @@ extern const struct emul __emul_list_end[];
137140
EMUL_BUS_TYPE_SPI), \
138141
.bus = {.Z_EMUL_BUS(node_id, i2c, espi, spi) = \
139142
&(Z_EMUL_REG_BUS_IDENTIFIER(node_id))}, \
143+
.backend_api = (_backend_api), \
140144
};
141145

142146
/**

subsys/emul/emul_bmi160.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,9 @@ static int emul_bosch_bmi160_init(const struct emul *target, const struct device
318318
static uint8_t bmi160_emul_reg_##n[BMI160_REG_COUNT]; \
319319
static struct bmi160_emul_data bmi160_emul_data_##n;
320320

321-
#define BMI160_EMUL_DEFINE(n, api) \
321+
#define BMI160_EMUL_DEFINE(n, bus_api) \
322322
EMUL_DT_INST_DEFINE(n, emul_bosch_bmi160_init, &bmi160_emul_data_##n, \
323-
&bmi160_emul_cfg_##n, &api)
323+
&bmi160_emul_cfg_##n, &bus_api, NULL)
324324

325325
/* Instantiation macros used when a device is on a SPI bus */
326326
#define BMI160_EMUL_SPI(n) \

subsys/emul/espi/emul_espi_host.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,6 @@ static int emul_host_init(const struct emul *emul, const struct device *bus)
263263
.chipsel = DT_INST_REG_ADDR(n), \
264264
}; \
265265
EMUL_DT_INST_DEFINE(n, emul_host_init, &espi_host_emul_data_##n, &espi_host_emul_cfg_##n, \
266-
&ap_emul_api)
266+
&ap_emul_api, NULL)
267267

268268
DT_INST_FOREACH_STATUS_OKAY(HOST_EMUL)

subsys/emul/i2c/emul_atmel_at24.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,6 @@ static int emul_atmel_at24_init(const struct emul *target, const struct device *
162162
.addr_width = 8, \
163163
}; \
164164
EMUL_DT_INST_DEFINE(n, emul_atmel_at24_init, &at24_emul_data_##n, &at24_emul_cfg_##n, \
165-
&bus_api)
165+
&bus_api, NULL)
166166

167167
DT_INST_FOREACH_STATUS_OKAY(EEPROM_AT24_EMUL)

subsys/emul/i2c/emul_sbs_gauge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,6 @@ static int emul_sbs_sbs_gauge_init(const struct emul *target, const struct devic
174174
.addr = DT_INST_REG_ADDR(n), \
175175
}; \
176176
EMUL_DT_INST_DEFINE(n, emul_sbs_sbs_gauge_init, &sbs_gauge_emul_data_##n, \
177-
&sbs_gauge_emul_cfg_##n, &sbs_gauge_emul_api_i2c)
177+
&sbs_gauge_emul_cfg_##n, &sbs_gauge_emul_api_i2c, NULL)
178178

179179
DT_INST_FOREACH_STATUS_OKAY(SBS_GAUGE_EMUL)

0 commit comments

Comments
 (0)