Skip to content

Commit f9adaca

Browse files
keith-zephyrstephanosio
authored andcommitted
docs: Update emulator documentation
Update emulator documentation with a diagram and description for how to use the .bus_api and ._backend_api parameters with EMUL_DT_DEFINE(). Signed-off-by: Keith Short <[email protected]>
1 parent 9664f86 commit f9adaca

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed
30.1 KB
Loading

doc/hardware/emulator/index.rst

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,70 @@ With this approach we can:
7373
* Transfer the application to any board which provides the required features
7474
(e.g. I2C, enough GPIOs), by adding Kconfig and devicetree fragments.
7575

76-
Available emulators
76+
Creating a Device Driver Emulator
77+
=================================
78+
79+
The emulator subsystem is modeled on the :ref:`device_model_api`. You create
80+
an emulator instance using one of the :c:func:`EMUL_DT_DEFINE()` or
81+
:c:func:`EMUL_DT_INST_DEFINE()` APIs.
82+
83+
Emulators for peripheral devices reuse the same devicetree node as the real
84+
device driver. This means that your emulator defines `DT_DRV_COMPAT` using the
85+
same ``compat`` value from the real driver.
86+
87+
.. code-block:: C
88+
89+
/* From drivers/sensor/bm160/bm160.c */
90+
#define DT_DRV_COMPAT bosch_bmi160
91+
92+
/* From subsys/emul/emul_bmi160.c */
93+
#define DT_DRV_COMPAT bosch_bmi160
94+
95+
The ``EMUL_DT_DEFINE()`` function accepts two API types:
96+
97+
#. ``bus_api`` - This points to the API for the upstream bus that the emulator
98+
connects to. The ``bus_api`` parameter is required. The supported
99+
emulated bus types include I2C, SPI, and eSPI.
100+
#. ``_backend_api`` - This points to the device-class specific backend API for
101+
the emulator. The ``_backend_api`` parameter is optional.
102+
103+
The diagram below demonstrates the logical organization of the ``bus_api`` and
104+
``_backend_api`` using the BC1.2 charging detector driver as the model
105+
device-class.
106+
107+
.. figure:: img/device_class_emulator.png
108+
:align: center
109+
:alt: Device class example, demonstrating BC1.2 charging detectors.
110+
111+
The real code is shown in green, while the emulator code is shown in yellow.
112+
113+
The ``bus_api`` connects the BC1.2 emulators to the ``native_posix`` I2C
114+
controller. The real BC1.2 drivers are unchanged and operate exactly as if there
115+
was a physical I2C controller present in the system. The ``native_posix`` I2C
116+
controller uses the ``bus_api`` to initiate register reads and writes to the
117+
emulator.
118+
119+
The ``_backend_api`` provides a mechanism for tests to manipulate the emulator
120+
out of band. Each device class defines it's own API functions. The backend API
121+
functions focus on high-level behavior and do not provide hooks for specific
122+
emulators.
123+
124+
In the case of the BC1.2 charging detector the backend API provides functions
125+
to simulate connecting and disconnecting a charger to the emulated BC1.2 device.
126+
Each emulator is responsible for updating the correct vendor specific registers
127+
and potentially signalling an interrupt.
128+
129+
Example test flow:
130+
131+
#. Test registers BC1.2 detection callback using the Zephyr BC1.2 driver API.
132+
#. Test connects a charger using the BC1.2 emulator backend.
133+
#. Test verifies B1.2 detection callback invoked with correct charger type.
134+
#. Test disconnects a charger using the BC1.2 emulator backend.
135+
136+
With this architecture, the same test can be used will all supported drivers in
137+
the same driver class.
138+
139+
Available Emulators
77140
===================
78141

79142
Zephyr includes the following emulators:

0 commit comments

Comments
 (0)