1.. _nrf52_bsim:
2
3NRF52 simulated board (BabbleSim)
4#################################
5
6.. contents::
7   :depth: 1
8   :backlinks: entry
9   :local:
10
11
12Overview
13********
14
15To allow simulating a nRF52833 SOC a Zephyr target boards is provided: the
16nrf52_bsim.
17
18This uses `BabbleSim`_ to simulate the radio activity, and the
19:ref:`POSIX architecture<Posix arch>` and the `native simulator`_ to
20run applications natively on the development system. This has the benefit of
21providing native code execution performance and easy debugging using
22native tools, but inherits :ref:`its limitations <posix_arch_limitations>`.
23
24This board includes models of some of the nRF52 SOC peripherals:
25
26* Radio
27* Timers
28* AAR (Accelerated Address Resolver)
29* AES CCM & AES ECB encryption HW
30* CLOCK (Clock control)
31* EGU (Event Generator Unit)
32* FICR (Factory Information Configuration Registers)
33* GPIO & GPIOTE
34* NVMC (Non-Volatile Memory Controller / Flash)
35* PPI (Programmable Peripheral Interconnect)
36* RNG (Random Number Generator)
37* RTC (Real Time Counter)
38* TEMP (Temperature sensor)
39* UART & UARTE (UART with Easy DMA)
40* UICR (User Information Configuration Registers)
41
42and will use the same drivers as the nrf52 dk targets for these.
43For more information on what is modelled to which level of detail,
44check the `HW models implementation status`_.
45
46Note that unlike a real nrf52 device, the nrf52_bsim has unlimited RAM and flash for code.
47
48.. _BabbleSim:
49   https://BabbleSim.github.io
50
51.. _native simulator:
52   https://github.com/BabbleSim/native_simulator/blob/main/docs/README.md
53
54.. _HW models implementation status:
55   https://github.com/BabbleSim/ext_nRF_hw_models/blob/main/docs/README_impl_status.md
56
57.. _nrf52bsim_build_and_run:
58
59Building and running
60********************
61
62This board requires the host 32 bit C library. See
63:ref:`POSIX Arch dependencies<posix_arch_deps>`.
64
65To target this board you also need to have `BabbleSim`_ compiled in your system.
66If you do not have it yet, the easiest way to get it, is to enable the babblesim group
67in your local west configuration, running west update, and building the simulator:
68
69.. code-block:: console
70
71   west config manifest.group-filter -- +babblesim
72   west update
73   cd ${ZEPHYR_BASE}/../tools/bsim
74   make everything -j 8
75
76.. note::
77
78   If you need more BabbleSim components, or more up to date versions,
79   you can check the `BabbleSim web page <https://BabbleSim.github.io>`_
80   for instructions on how to
81   `fetch <https://babblesim.github.io/fetching.html>`_ and
82   `build <https://babblesim.github.io/building.html>`_ it.
83
84You will now need to define two environment variables to point to your BabbleSim
85installation, ``BSIM_OUT_PATH`` and ``BSIM_COMPONENTS_PATH``.
86If you followed the previous steps, you can just do:
87
88.. code-block:: console
89
90   export BSIM_OUT_PATH=${ZEPHYR_BASE}/../tools/bsim
91   export BSIM_COMPONENTS_PATH=${BSIM_OUT_PATH}/components/
92
93.. note::
94
95   You can add these two lines to your ``~/.zephyrrc`` file, or to your shell
96   initialization script (``~/.bashrc``), so you won't need to rerun them
97   manually for each new shell.
98
99You're now ready to build applications targeting this board, for example:
100
101.. zephyr-app-commands::
102   :zephyr-app: samples/hello_world
103   :host-os: unix
104   :board: nrf52_bsim
105   :goals: build
106   :compact:
107
108Then you can execute your application using:
109
110.. code-block:: console
111
112   $ ./build/zephyr/zephyr.exe -nosim
113   # Press Ctrl+C to exit
114
115Note that the executable is a BabbleSim executable. The ``-nosim`` command line
116option indicates you want to run it detached from a BabbleSim simulation. This
117is possible only while there is no radio activity. But is perfectly fine for
118most Zephyr samples and tests.
119
120When you want to run a simulation with radio activity you need to run also the
121BableSim 2G4 (2.4GHz) physical layer simulation (phy).
122
123For example, if you would like to run a simple case with a BLE :zephyr:code-sample:`ble_central_hr`
124sample application connecting to a BLE :zephyr:code-sample:`ble_peripheral_hr` sample application:
125Build the :zephyr:code-sample:`ble_central_hr` application targeting this board and copy the
126resulting executable to the simulator bin folder with a sensible name:
127
128.. zephyr-app-commands::
129   :zephyr-app: samples/bluetooth/central_hr
130   :host-os: unix
131   :board: nrf52_bsim
132   :goals: build
133   :compact:
134
135.. code-block:: console
136
137   $ cp build/zephyr/zephyr.exe \
138     ${BSIM_OUT_PATH}/bin/bs_nrf52_bsim_samples_bluetooth_central_hr
139
140Do the same for the :zephyr:code-sample:`ble_peripheral_hr` sample app:
141
142.. zephyr-app-commands::
143   :zephyr-app: samples/bluetooth/peripheral_hr
144   :host-os: unix
145   :board: nrf52_bsim
146   :goals: build
147   :compact:
148
149.. code-block:: console
150
151   $ cp build/zephyr/zephyr.exe \
152     ${BSIM_OUT_PATH}/bin/bs_nrf52_bsim_samples_bluetooth_peripheral_hr
153
154And then run them together with BabbleSim's 2G4 physical layer simulation:
155
156.. code-block:: console
157
158   cd ${BSIM_OUT_PATH}/bin/
159   ./bs_nrf52_bsim_samples_bluetooth_peripheral -s=trial_sim -d=0 &
160   ./bs_nrf52_bsim_samples_bluetooth_central_hr -s=trial_sim -d=1 &
161   ./bs_2G4_phy_v1 -s=trial_sim -D=2 -sim_length=10e6 &
162
163Where the ``-s`` command line option provides a string which uniquely identifies
164this simulation; the ``-D`` option tells the Phy how many devices will be run
165in this simulation; the ``-d`` option tells each device which is its device
166number in the simulation; and the ``-sim_length`` option specifies the length
167of the simulation in microseconds.
168BabbleSim devices and Phy support many command line switches.
169Run them with ``-help`` for more information.
170
171You can find more information about how to run BabbleSim simulations in
172`this BabbleSim example <https://babblesim.github.io/example_2g4.html>`_.
173
174Running an application using the console
175========================================
176
177Some applications require the use of a console to interact with the user.
178These applications typically enable :kconfig:option:`CONFIG_CONSOLE_SUBSYS` and :kconfig:option:`CONFIG_CONSOLE_GETCHAR`.
179The UART console is disabled by default for BabbleSim boards, to enable it simply add the snippet :ref:`snippet-uart-console`.
180
181.. code-block:: console
182
183   west build -S serial-console [...]
184
185To view the output and interact with the application the user needs to connect a terminal to this pseudoterminal.
186
187.. code-block:: console
188
189   # Automatically attach to the terminal
190   ./build/zephyr/zephyr.exe --uart<uart_id>_pty_attach
191   # Use a custom command to attach to the terminal, for example 'xterm -e screen %s &'
192   ./build/zephyr/zephyr.exe --uart<uart_id>_attach_cmd=<cmd>
193   # Use a custom way to connect to the pseudoterminal
194   ./build/zephyr/zephyr.exe --uart<uart_id>_pty --uart_pty_wait
195   minicom -D /dev/pts/<pts_id>
196
197The command line option ``--uart_list`` prints out the mapping between ``uart_id`` and the UART peripherals.
198The overlay files describes which UART peripheral is being used as the console output.
199
200For more details about attaching to the UART output, refer to the output of the ``-help`` option of the executable.
201
202C library choice
203****************
204
205These nRF bsim boards use the `native simulator`_ at their core, so you can chose with which
206C library you want to build your embedded code.
207Check the :ref:`native simulator C library choice section<native_sim_Clib_choice>` for more info.
208
209
210Debugging, coverage and address sanitizer
211*****************************************
212
213Just like with :ref:`native_sim<native_sim_debug>`, the resulting
214executables are Linux native applications.
215Therefore they can be debugged or instrumented with the same tools as any other
216native application, like for example ``gdb`` or ``valgrind``.
217
218The same
219:ref:`code coverage analysis means from the POSIX arch<coverage_posix>`
220are inherited in this board.
221Similarly, the
222:ref:`address and undefined behavior sanitizers can be used as in native_sim<native_sim_asan>`.
223
224
225Note that BabbleSim will run fine if one or several of its components are
226being run in a debugger or instrumented. For example, pausing a device in a
227breakpoint will pause the whole simulation.
228
229BabbleSim is fully deterministic by design and the results are not affected by
230the host computing speed. All randomness is controlled by random seeds which can
231be provided as command line options.
232
233
234About time in BabbleSim
235************************
236
237Note that time in BabbleSim is simulated and decoupled from real time. Normally
238simulated time will pass several orders of magnitude faster than real time,
239only limited by your workstation compute power.
240If for some reason you want to limit the speed of the simulation to real
241time or a ratio of it, you can do so by connecting the `handbrake device`_
242to the BabbleSim Phy.
243
244.. _handbrake device:
245   https://github.com/BabbleSim/base/tree/master/device_handbrake
246