Zephyr Project API 4.4.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
13
14#ifndef ZEPHYR_INCLUDE_DRIVERS_EMUL_H_
15#define ZEPHYR_INCLUDE_DRIVERS_EMUL_H_
16
23
24struct emul;
25
26/* #includes required after forward-declaration of struct emul later defined in this header. */
27#include <zephyr/device.h>
28#include <zephyr/devicetree.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif /* __cplusplus */
39
51
56 const struct device *dev;
57};
58
64 unsigned int num_children;
65};
66
74typedef int (*emul_init_t)(const struct emul *emul, const struct device *parent);
75
80 void *api;
82};
83
88struct emul {
92 const struct device *dev;
94 const void *cfg;
96 void *data;
100 union bus {
101 struct i2c_emul *i2c;
102 struct espi_emul *espi;
103 struct spi_emul *spi;
104 struct mspi_emul *mspi;
105 struct uart_emul *uart;
107 } bus;
109 const void *backend_api;
110};
111
117#define EMUL_DT_NAME_GET(node_id) _CONCAT(__emulreg_, node_id)
118
119/* Get a unique identifier based on the given _dev_node_id's reg property and
120 * the bus its on. Intended for use in other internal macros when declaring {bus}_emul
121 * structs in peripheral emulators.
122 */
123#define Z_EMUL_REG_BUS_IDENTIFIER(_dev_node_id) _CONCAT(_CONCAT(__emulreg_, _dev_node_id), _bus)
124
125/* Conditionally places text based on what bus _dev_node_id is on. */
126#define Z_EMUL_BUS(_dev_node_id, _i2c, _espi, _spi, _mspi, _uart, _none) \
127 COND_CASE_1(DT_ON_BUS(_dev_node_id, i2c), (_i2c), \
128 DT_ON_BUS(_dev_node_id, espi), (_espi), \
129 DT_ON_BUS(_dev_node_id, spi), (_spi), \
130 DT_ON_BUS(_dev_node_id, mspi), (_mspi), \
131 DT_ON_BUS(_dev_node_id, uart), (_uart), \
132 (_none))
133
148#define EMUL_DT_DEFINE(node_id, init_fn, data_ptr, cfg_ptr, bus_api, _backend_api) \
149 static struct Z_EMUL_BUS(node_id, i2c_emul, espi_emul, spi_emul, mspi_emul, uart_emul, \
150 no_bus_emul) Z_EMUL_REG_BUS_IDENTIFIER(node_id) = { \
151 .api = bus_api, \
152 IF_ENABLED(DT_NODE_HAS_PROP(node_id, reg), \
153 (.Z_EMUL_BUS(node_id, addr, chipsel, chipsel, dev_idx, dummy, addr) = \
154 DT_REG_ADDR(node_id),))}; \
155 const STRUCT_SECTION_ITERABLE(emul, EMUL_DT_NAME_GET(node_id)) __used = { \
156 .init = (init_fn), \
157 .dev = DEVICE_DT_GET(node_id), \
158 .cfg = (cfg_ptr), \
159 .data = (data_ptr), \
160 .bus_type = Z_EMUL_BUS(node_id, EMUL_BUS_TYPE_I2C, EMUL_BUS_TYPE_ESPI, \
161 EMUL_BUS_TYPE_SPI, EMUL_BUS_TYPE_MSPI, EMUL_BUS_TYPE_UART, \
162 EMUL_BUS_TYPE_NONE), \
163 .bus = {.Z_EMUL_BUS(node_id, i2c, espi, spi, mspi, uart, none) = \
164 &(Z_EMUL_REG_BUS_IDENTIFIER(node_id))}, \
165 .backend_api = (_backend_api), \
166 };
167
176#define EMUL_DT_INST_DEFINE(inst, ...) EMUL_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
177
192#define EMUL_DT_GET(node_id) (&EMUL_DT_NAME_GET(node_id))
193
203#define EMUL_DT_GET_OR_NULL(node_id) \
204 COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(node_id), (EMUL_DT_GET(node_id)), (NULL))
205
213int emul_init_for_bus(const struct device *dev);
214
227const struct emul *emul_get_binding(const char *name);
228
232
234
235#define Z_MAYBE_EMUL_DECLARE_INTERNAL(node_id) extern const struct emul EMUL_DT_NAME_GET(node_id);
236
237DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_EMUL_DECLARE_INTERNAL);
238
240
241#ifdef __cplusplus
242}
243#endif /* __cplusplus */
244
245#endif /* ZEPHYR_INCLUDE_DRIVERS_EMUL_H_ */
APIs and macros for the Zephyr device model.
Devicetree main header.
Main header file for eSPI emulation driver API.
#define DT_FOREACH_STATUS_OKAY_NODE(fn)
Invokes fn for every status okay node in the tree.
Definition devicetree.h:3593
emul_bus_type
The types of supported buses.
Definition emul.h:43
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:74
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
SPI bus.
Definition emul.h:46
@ EMUL_BUS_TYPE_I2C
I2C bus.
Definition emul.h:44
@ EMUL_BUS_TYPE_MSPI
MSPI bus.
Definition emul.h:47
@ EMUL_BUS_TYPE_ESPI
eSPI bus
Definition emul.h:45
@ EMUL_BUS_TYPE_NONE
Emulator is not attached to a bus.
Definition emul.h:49
@ EMUL_BUS_TYPE_UART
UART bus.
Definition emul.h:48
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:543
List of emulators attached to a bus.
Definition emul.h:60
const struct emul_link_for_bus * children
Identifiers for children of the node.
Definition emul.h:62
unsigned int num_children
Number of children of the node.
Definition emul.h:64
An emulator instance - represents the target emulated device/peripheral that is interacted with throu...
Definition emul.h:88
const struct device * dev
handle to the device for which this provides low-level emulation
Definition emul.h:92
emul_init_t init
function used to initialise the emulator state
Definition emul.h:90
void * data
Emulator-specific data.
Definition emul.h:96
enum emul_bus_type bus_type
The bus type that the emulator is attached to.
Definition emul.h:98
union emul::bus bus
Emulated bus node.
const void * backend_api
Address of the API structure exposed by the emulator instance.
Definition emul.h:109
const void * cfg
Emulator-specific configuration data.
Definition emul.h:94
Node in a linked list of emulators for eSPI devices.
Definition espi_emul.h:117
Node in a linked list of emulators for I2C devices.
Definition i2c_emul.h:39
Node in a linked list of emulators for MSPI devices.
Definition mspi_emul.h:90
Emulator API stub when an emulator is not actually placed on a bus.
Definition emul.h:79
void * api
Emulator-specific bus API.
Definition emul.h:80
uint16_t addr
Emulated device address, from the devicetree reg property.
Definition emul.h:81
Node in a linked list of emulators for SPI devices.
Definition spi_emul.h:39
Node in a linked list of emulators for UART devices.
Definition uart_emul.h:48
Iterable sections helpers.
Public APIs for the UART device emulation drivers.
Pointer to the emulated bus node.
Definition emul.h:100
struct i2c_emul * i2c
I2C emulated bus node.
Definition emul.h:101
struct uart_emul * uart
UART emulated bus node.
Definition emul.h:105
struct no_bus_emul * none
Stub used when the emulator is not on a bus.
Definition emul.h:106
struct spi_emul * spi
SPI emulated bus node.
Definition emul.h:103
struct mspi_emul * mspi
MSPI emulated bus node.
Definition emul.h:104
struct espi_emul * espi
eSPI emulated bus node
Definition emul.h:102