Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
18#ifdef __cplusplus
19extern "C" {
20#endif /* __cplusplus */
21
22struct emul;
23
24/* #includes required after forward-declaration of struct emul later defined in this header. */
25#include <zephyr/device.h>
26#include <zephyr/devicetree.h>
33
45
50 const struct device *dev;
51};
52
58 unsigned int num_children;
59};
60
68typedef int (*emul_init_t)(const struct emul *emul, const struct device *parent);
69
74 void *api;
76};
77
82struct emul {
86 const struct device *dev;
88 const void *cfg;
90 void *data;
94 union bus {
95 struct i2c_emul *i2c;
96 struct espi_emul *espi;
97 struct spi_emul *spi;
98 struct mspi_emul *mspi;
99 struct uart_emul *uart;
103 const void *backend_api;
104};
105
111#define EMUL_DT_NAME_GET(node_id) _CONCAT(__emulreg_, node_id)
112
113/* Get a unique identifier based on the given _dev_node_id's reg property and
114 * the bus its on. Intended for use in other internal macros when declaring {bus}_emul
115 * structs in peripheral emulators.
116 */
117#define Z_EMUL_REG_BUS_IDENTIFIER(_dev_node_id) _CONCAT(_CONCAT(__emulreg_, _dev_node_id), _bus)
118
119/* Conditionally places text based on what bus _dev_node_id is on. */
120#define Z_EMUL_BUS(_dev_node_id, _i2c, _espi, _spi, _mspi, _uart, _none) \
121 COND_CODE_1(DT_ON_BUS(_dev_node_id, i2c), (_i2c), \
122 (COND_CODE_1(DT_ON_BUS(_dev_node_id, espi), (_espi), \
123 (COND_CODE_1(DT_ON_BUS(_dev_node_id, spi), (_spi), \
124 (COND_CODE_1(DT_ON_BUS(_dev_node_id, mspi), (_mspi), \
125 (COND_CODE_1(DT_ON_BUS(_dev_node_id, uart), (_uart), \
126 (_none))))))))))
127
142#define EMUL_DT_DEFINE(node_id, init_fn, data_ptr, cfg_ptr, bus_api, _backend_api) \
143 static struct Z_EMUL_BUS(node_id, i2c_emul, espi_emul, spi_emul, mspi_emul, uart_emul, \
144 no_bus_emul) Z_EMUL_REG_BUS_IDENTIFIER(node_id) = { \
145 .api = bus_api, \
146 IF_ENABLED(DT_NODE_HAS_PROP(node_id, reg), \
147 (.Z_EMUL_BUS(node_id, addr, chipsel, chipsel, dev_idx, dummy, addr) = \
148 DT_REG_ADDR(node_id),))}; \
149 const STRUCT_SECTION_ITERABLE(emul, EMUL_DT_NAME_GET(node_id)) __used = { \
150 .init = (init_fn), \
151 .dev = DEVICE_DT_GET(node_id), \
152 .cfg = (cfg_ptr), \
153 .data = (data_ptr), \
154 .bus_type = Z_EMUL_BUS(node_id, EMUL_BUS_TYPE_I2C, EMUL_BUS_TYPE_ESPI, \
155 EMUL_BUS_TYPE_SPI, EMUL_BUS_TYPE_MSPI, EMUL_BUS_TYPE_UART, \
156 EMUL_BUS_TYPE_NONE), \
157 .bus = {.Z_EMUL_BUS(node_id, i2c, espi, spi, mspi, uart, none) = \
158 &(Z_EMUL_REG_BUS_IDENTIFIER(node_id))}, \
159 .backend_api = (_backend_api), \
160 };
161
170#define EMUL_DT_INST_DEFINE(inst, ...) EMUL_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
171
186#define EMUL_DT_GET(node_id) (&EMUL_DT_NAME_GET(node_id))
187
197#define EMUL_DT_GET_OR_NULL(node_id) \
198 COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(node_id), (EMUL_DT_GET(node_id)), (NULL))
199
207int emul_init_for_bus(const struct device *dev);
208
221const struct emul *emul_get_binding(const char *name);
222
227#define Z_MAYBE_EMUL_DECLARE_INTERNAL(node_id) extern const struct emul EMUL_DT_NAME_GET(node_id);
228
229DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_EMUL_DECLARE_INTERNAL);
230
231#ifdef __cplusplus
232}
233#endif /* __cplusplus */
234
235#endif /* ZEPHYR_INCLUDE_DRIVERS_EMUL_H_ */
Devicetree main header.
Public APIs for the eSPI emulation drivers.
#define DT_FOREACH_STATUS_OKAY_NODE(fn)
Invokes fn for every status okay node in the tree.
Definition devicetree.h:2960
emul_bus_type
The types of supported buses.
Definition emul.h:37
int(* emul_init_t)(const struct emul *emul, const struct device *parent)
Standard callback for emulator initialisation providing the initialiser record and the device that ca...
Definition emul.h:68
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:40
@ EMUL_BUS_TYPE_I2C
Definition emul.h:38
@ EMUL_BUS_TYPE_MSPI
Definition emul.h:41
@ EMUL_BUS_TYPE_ESPI
Definition emul.h:39
@ EMUL_BUS_TYPE_NONE
Definition emul.h:43
@ EMUL_BUS_TYPE_UART
Definition emul.h:42
Public APIs for the I2C emulation drivers.
Public APIs for the MSPI emulation drivers.
Public APIs for the SPI emulation drivers.
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:412
List of emulators attached to a bus.
Definition emul.h:54
const struct emul_link_for_bus * children
Identifiers for children of the node.
Definition emul.h:56
unsigned int num_children
Number of children of the node.
Definition emul.h:58
An emulator instance - represents the target emulated device/peripheral that is interacted with throu...
Definition emul.h:82
const struct device * dev
handle to the device for which this provides low-level emulation
Definition emul.h:86
emul_init_t init
function used to initialise the emulator state
Definition emul.h:84
void * data
Emulator-specific data.
Definition emul.h:90
enum emul_bus_type bus_type
The bus type that the emulator is attached to.
Definition emul.h:92
union emul::bus bus
const void * backend_api
Address of the API structure exposed by the emulator instance.
Definition emul.h:103
const void * cfg
Emulator-specific configuration data.
Definition emul.h:88
Node in a linked list of emulators for eSPI devices.
Definition espi_emul.h:111
Node in a linked list of emulators for I2C devices.
Definition i2c_emul.h:37
Node in a linked list of emulators for MSPI devices.
Definition mspi_emul.h:87
Emulator API stub when an emulator is not actually placed on a bus.
Definition emul.h:73
void * api
Definition emul.h:74
uint16_t addr
Definition emul.h:75
Node in a linked list of emulators for SPI devices.
Definition spi_emul.h:37
Node in a linked list of emulators for UART devices.
Definition uart_emul.h:46
Public APIs for the UART device emulation drivers.
Pointer to the emulated bus node.
Definition emul.h:94
struct i2c_emul * i2c
Definition emul.h:95
struct uart_emul * uart
Definition emul.h:99
struct no_bus_emul * none
Definition emul.h:100
struct spi_emul * spi
Definition emul.h:97
struct mspi_emul * mspi
Definition emul.h:98
struct espi_emul * espi
Definition emul.h:96