Skip to content

Commit a9afc5a

Browse files
yperesscarlescufi
authored andcommitted
emul: Add support for non-bus emulators
Adds a stub API for a non bus emulators. The stub is used to keep the rest of the emulation consistent. Signed-off-by: Yuval Peress <peress@google.com>
1 parent 56de0a3 commit a9afc5a

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

include/zephyr/drivers/emul.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ enum emul_bus_type {
3636
EMUL_BUS_TYPE_I2C,
3737
EMUL_BUS_TYPE_ESPI,
3838
EMUL_BUS_TYPE_SPI,
39+
EMUL_BUS_TYPE_NONE,
3940
};
4041

4142
/**
@@ -62,6 +63,14 @@ struct emul_list_for_bus {
6263
*/
6364
typedef int (*emul_init_t)(const struct emul *emul, const struct device *parent);
6465

66+
/**
67+
* Emulator API stub when an emulator is not actually placed on a bus.
68+
*/
69+
struct no_bus_emul {
70+
void *api;
71+
uint16_t addr;
72+
};
73+
6574
/** An emulator instance - represents the *target* emulated device/peripheral that is
6675
* interacted with through an emulated bus. Instances of emulated bus nodes (e.g. i2c_emul)
6776
* and emulators (i.e. struct emul) are exactly 1..1
@@ -82,6 +91,7 @@ struct emul {
8291
struct i2c_emul *i2c;
8392
struct espi_emul *espi;
8493
struct spi_emul *spi;
94+
struct no_bus_emul *none;
8595
} bus;
8696
/** Address of the API structure exposed by the emulator instance */
8797
const void *backend_api;
@@ -101,10 +111,10 @@ struct emul {
101111
#define Z_EMUL_REG_BUS_IDENTIFIER(_dev_node_id) (_CONCAT(_CONCAT(__emulreg_, _dev_node_id), _bus))
102112

103113
/* Conditionally places text based on what bus _dev_node_id is on. */
104-
#define Z_EMUL_BUS(_dev_node_id, _i2c, _espi, _spi) \
114+
#define Z_EMUL_BUS(_dev_node_id, _i2c, _espi, _spi, _none) \
105115
COND_CODE_1(DT_ON_BUS(_dev_node_id, i2c), (_i2c), \
106116
(COND_CODE_1(DT_ON_BUS(_dev_node_id, espi), (_espi), \
107-
(COND_CODE_1(DT_ON_BUS(_dev_node_id, spi), (_spi), (-EINVAL))))))
117+
(COND_CODE_1(DT_ON_BUS(_dev_node_id, spi), (_spi), (_none))))))
108118
/**
109119
* @brief Define a new emulator
110120
*
@@ -120,10 +130,10 @@ struct emul {
120130
* @param _backend_api emulator-specific backend api
121131
*/
122132
#define EMUL_DT_DEFINE(node_id, init_fn, data_ptr, cfg_ptr, bus_api, _backend_api) \
123-
static struct Z_EMUL_BUS(node_id, i2c_emul, espi_emul, spi_emul) \
133+
static struct Z_EMUL_BUS(node_id, i2c_emul, espi_emul, spi_emul, no_bus_emul) \
124134
Z_EMUL_REG_BUS_IDENTIFIER(node_id) = { \
125135
.api = bus_api, \
126-
.Z_EMUL_BUS(node_id, addr, chipsel, chipsel) = DT_REG_ADDR(node_id), \
136+
.Z_EMUL_BUS(node_id, addr, chipsel, chipsel, addr) = DT_REG_ADDR(node_id), \
127137
}; \
128138
const STRUCT_SECTION_ITERABLE(emul, EMUL_DT_NAME_GET(node_id)) \
129139
__used = { \
@@ -132,8 +142,8 @@ struct emul {
132142
.cfg = (cfg_ptr), \
133143
.data = (data_ptr), \
134144
.bus_type = Z_EMUL_BUS(node_id, EMUL_BUS_TYPE_I2C, EMUL_BUS_TYPE_ESPI, \
135-
EMUL_BUS_TYPE_SPI), \
136-
.bus = {.Z_EMUL_BUS(node_id, i2c, espi, spi) = \
145+
EMUL_BUS_TYPE_SPI, EMUL_BUS_TYPE_NONE), \
146+
.bus = {.Z_EMUL_BUS(node_id, i2c, espi, spi, none) = \
137147
&(Z_EMUL_REG_BUS_IDENTIFIER(node_id))}, \
138148
.backend_api = (_backend_api), \
139149
};

subsys/emul/emul.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ int emul_init_for_bus(const struct device *dev)
5555
case EMUL_BUS_TYPE_SPI:
5656
emul->bus.spi->target = emul;
5757
break;
58+
case EMUL_BUS_TYPE_NONE:
59+
break;
5860
}
5961
int rc = emul->init(emul, dev);
6062

0 commit comments

Comments
 (0)