Zephyr Project API  3.2.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;
84};
85
90extern const struct emul __emul_list_start[];
91extern const struct emul __emul_list_end[];
92
98#define EMUL_DT_NAME_GET(node_id) _CONCAT(__emulreg_, node_id)
99
100/* Get a unique identifier based on the given _dev_node_id's reg property and
101 * the bus its on. Intended for use in other internal macros when declaring {bus}_emul
102 * structs in peripheral emulators.
103 */
104#define Z_EMUL_REG_BUS_IDENTIFIER(_dev_node_id) (_CONCAT(_CONCAT(__emulreg_, _dev_node_id), _bus))
105
106/* Conditionally places text based on what bus _dev_node_id is on. */
107#define Z_EMUL_BUS(_dev_node_id, _i2c, _espi, _spi) \
108 COND_CODE_1(DT_ON_BUS(_dev_node_id, i2c), (_i2c), \
109 (COND_CODE_1(DT_ON_BUS(_dev_node_id, espi), (_espi), \
110 (COND_CODE_1(DT_ON_BUS(_dev_node_id, spi), (_spi), (-EINVAL))))))
124#define EMUL_DT_DEFINE(node_id, init_fn, data_ptr, cfg_ptr, bus_api) \
125 static struct Z_EMUL_BUS(node_id, i2c_emul, espi_emul, spi_emul) \
126 Z_EMUL_REG_BUS_IDENTIFIER(node_id) = { \
127 .api = bus_api, \
128 .Z_EMUL_BUS(node_id, addr, chipsel, chipsel) = DT_REG_ADDR(node_id), \
129 }; \
130 const struct emul EMUL_DT_NAME_GET(node_id) __attribute__((__section__(".emulators"))) \
131 __used = { \
132 .init = (init_fn), \
133 .dev = DEVICE_DT_GET(node_id), \
134 .cfg = (cfg_ptr), \
135 .data = (data_ptr), \
136 .bus_type = Z_EMUL_BUS(node_id, EMUL_BUS_TYPE_I2C, EMUL_BUS_TYPE_ESPI, \
137 EMUL_BUS_TYPE_SPI), \
138 .bus = {.Z_EMUL_BUS(node_id, i2c, espi, spi) = \
139 &(Z_EMUL_REG_BUS_IDENTIFIER(node_id))}, \
140 };
141
142#define Z_MAYBE_EMUL_DECLARE_INTERNAL(node_id) extern const struct emul EMUL_DT_NAME_GET(node_id);
143
144DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_EMUL_DECLARE_INTERNAL);
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
195#ifdef __cplusplus
196}
197#endif /* __cplusplus */
198
203#endif /* ZEPHYR_INCLUDE_DRIVERS_EMUL_H_ */
Devicetree main header.
Public APIs for the eSPI emulation drivers.
emul_bus_type
Definition: emul.h:33
DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_EMUL_DECLARE_INTERNAL)
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)
@ 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:435
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 * cfg
Definition: emul.h:73
Definition: espi_emul.h:110
Definition: i2c_emul.h:35
Definition: spi_emul.h:36
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