Zephyr Project API  3.3.0
A Scalable Open Source RTOS
emul.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Nordic Semiconductor ASA
3 * Copyright 2020 Google LLC
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ZEPHYR_INCLUDE_DRIVERS_EMUL_H_
9#define ZEPHYR_INCLUDE_DRIVERS_EMUL_H_
10
17#ifdef __cplusplus
18extern "C" {
19#endif /* __cplusplus */
20
21struct emul;
22
23/* #includes required after forward-declaration of struct emul later defined in this header. */
24#include <zephyr/device.h>
25#include <zephyr/devicetree.h>
29
37};
38
43 const struct device *dev;
44};
45
51 unsigned int num_children;
52};
53
61typedef int (*emul_init_t)(const struct emul *emul, const struct device *parent);
62
67struct emul {
71 const struct device *dev;
73 const void *cfg;
75 void *data;
79 union bus {
80 struct i2c_emul *i2c;
81 struct espi_emul *espi;
82 struct spi_emul *spi;
83 } bus;
85 const void *backend_api;
86};
87
92extern const struct emul __emul_list_start[];
93extern const struct emul __emul_list_end[];
94
100#define EMUL_DT_NAME_GET(node_id) _CONCAT(__emulreg_, node_id)
101
102/* Get a unique identifier based on the given _dev_node_id's reg property and
103 * the bus its on. Intended for use in other internal macros when declaring {bus}_emul
104 * structs in peripheral emulators.
105 */
106#define Z_EMUL_REG_BUS_IDENTIFIER(_dev_node_id) (_CONCAT(_CONCAT(__emulreg_, _dev_node_id), _bus))
107
108/* Conditionally places text based on what bus _dev_node_id is on. */
109#define Z_EMUL_BUS(_dev_node_id, _i2c, _espi, _spi) \
110 COND_CODE_1(DT_ON_BUS(_dev_node_id, i2c), (_i2c), \
111 (COND_CODE_1(DT_ON_BUS(_dev_node_id, espi), (_espi), \
112 (COND_CODE_1(DT_ON_BUS(_dev_node_id, spi), (_spi), (-EINVAL))))))
127#define EMUL_DT_DEFINE(node_id, init_fn, data_ptr, cfg_ptr, bus_api, _backend_api) \
128 static struct Z_EMUL_BUS(node_id, i2c_emul, espi_emul, spi_emul) \
129 Z_EMUL_REG_BUS_IDENTIFIER(node_id) = { \
130 .api = bus_api, \
131 .Z_EMUL_BUS(node_id, addr, chipsel, chipsel) = DT_REG_ADDR(node_id), \
132 }; \
133 const struct emul EMUL_DT_NAME_GET(node_id) __attribute__((__section__(".emulators"))) \
134 __used = { \
135 .init = (init_fn), \
136 .dev = DEVICE_DT_GET(node_id), \
137 .cfg = (cfg_ptr), \
138 .data = (data_ptr), \
139 .bus_type = Z_EMUL_BUS(node_id, EMUL_BUS_TYPE_I2C, EMUL_BUS_TYPE_ESPI, \
140 EMUL_BUS_TYPE_SPI), \
141 .bus = {.Z_EMUL_BUS(node_id, i2c, espi, spi) = \
142 &(Z_EMUL_REG_BUS_IDENTIFIER(node_id))}, \
143 .backend_api = (_backend_api), \
144 };
145
154#define EMUL_DT_INST_DEFINE(inst, ...) EMUL_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
155
170#define EMUL_DT_GET(node_id) (&EMUL_DT_NAME_GET(node_id))
171
179int emul_init_for_bus(const struct device *dev);
180
193const struct emul *emul_get_binding(const char *name);
194
199#if defined(CONFIG_HAS_DTS) || defined(__DOXYGEN__)
200#define Z_MAYBE_EMUL_DECLARE_INTERNAL(node_id) extern const struct emul EMUL_DT_NAME_GET(node_id);
201
202DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_EMUL_DECLARE_INTERNAL);
203#endif /* CONFIG_HAS_DTS || __DOXYGEN__ */
204
205#ifdef __cplusplus
206}
207#endif /* __cplusplus */
208
209#endif /* ZEPHYR_INCLUDE_DRIVERS_EMUL_H_ */
Devicetree main header.
DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_EMUL_DECLARE_INTERNAL)
Public APIs for the eSPI emulation drivers.
emul_bus_type
Definition: emul.h:33
int(* emul_init_t)(const struct emul *emul, const struct device *parent)
Definition: emul.h:61
const struct emul * emul_get_binding(const char *name)
Retrieve the emul structure for an emulator by name.
int emul_init_for_bus(const struct device *dev)
Set up a list of emulators.
@ EMUL_BUS_TYPE_SPI
Definition: emul.h:36
@ EMUL_BUS_TYPE_I2C
Definition: emul.h:34
@ EMUL_BUS_TYPE_ESPI
Definition: emul.h:35
Public APIs for the I2C emulation drivers.
Public APIs for the SPI emulation drivers.
Runtime device structure (in ROM) per driver instance.
Definition: device.h:378
Definition: emul.h:47
const struct emul_link_for_bus * children
Definition: emul.h:49
unsigned int num_children
Definition: emul.h:51
Definition: emul.h:67
const struct device * dev
Definition: emul.h:71
emul_init_t init
Definition: emul.h:69
void * data
Definition: emul.h:75
enum emul_bus_type bus_type
Definition: emul.h:77
union emul::bus bus
const void * backend_api
Definition: emul.h:85
const void * cfg
Definition: emul.h:73
Definition: espi_emul.h:111
Definition: i2c_emul.h:36
Definition: spi_emul.h:37
Definition: emul.h:79
struct i2c_emul * i2c
Definition: emul.h:80
struct spi_emul * spi
Definition: emul.h:82
struct espi_emul * espi
Definition: emul.h:81