1.. _beyond-gsg: 2 3Beyond the Getting Started Guide 4################################ 5 6The :ref:`getting_started` gives a straight-forward path to set up 7your Linux, macOS, or Windows environment for Zephyr development. In 8this document, we delve deeper into Zephyr development setup 9issues and alternatives. 10 11.. _python-pip: 12 13Python and pip 14************** 15 16Python 3 and its package manager, pip\ [#pip]_, are used extensively by Zephyr 17to install and run scripts required to compile and run Zephyr 18applications, set up and maintain the Zephyr development environment, 19and build project documentation. 20 21Depending on your operating system, you may need to provide the 22``--user`` flag to the ``pip3`` command when installing new packages. This is 23documented throughout the instructions. 24See `Installing Packages`_ in the Python Packaging User Guide for more 25information about pip\ [#pip]_, including `information on -\\-user`_. 26 27- On Linux, make sure ``~/.local/bin`` is at the front of your :envvar:`PATH` 28 :ref:`environment variable <env_vars>`, or programs installed with ``--user`` 29 won't be found. Installing with ``--user`` avoids conflicts between pip 30 and the system package manager, and is the default on Debian-based 31 distributions. 32 33- On macOS, `Homebrew disables -\\-user`_. 34 35- On Windows, see the `Installing Packages`_ information on ``--user`` if you 36 require using this option. 37 38On all operating systems, pip's ``-U`` flag installs or updates the package if the 39package is already installed locally but a more recent version is available. It 40is good practice to use this flag if the latest version of a package is 41required. (Check the :zephyr_file:`scripts/requirements.txt` file to 42see if a specific Python package version is expected.) 43 44Advanced Platform Setup 45*********************** 46 47Here are some alternative instructions for more advanced platform setup 48configurations for supported development platforms: 49 50.. toctree:: 51 :maxdepth: 1 52 53 Linux setup alternatives <getting_started/installation_linux.rst> 54 macOS setup alternatives <getting_started/installation_mac.rst> 55 Windows setup alternatives <getting_started/installation_win.rst> 56 57.. _gs_toolchain: 58 59Install a Toolchain 60******************* 61 62Zephyr binaries are compiled and linked by a *toolchain* comprised of 63a cross-compiler and related tools which are different from the compiler 64and tools used for developing software that runs natively on your host 65operating system. 66 67You can install the :ref:`Zephyr SDK <toolchain_zephyr_sdk>` to get toolchains for all 68supported architectures, or install an :ref:`alternate toolchain <toolchains>` 69recommended by the SoC vendor or a specific board (check your specific 70:ref:`board-level documentation <boards>`). 71 72You can configure the Zephyr build system to use a specific toolchain by 73setting :ref:`environment variables <env_vars>` such as 74:envvar:`ZEPHYR_TOOLCHAIN_VARIANT <{TOOLCHAIN}_TOOLCHAIN_PATH>` to a supported 75value, along with additional variable(s) specific to the toolchain variant. 76 77.. _gs_toolchain_update: 78 79Updating the Zephyr SDK toolchain 80********************************* 81 82When updating Zephyr SDK, check whether the :envvar:`ZEPHYR_TOOLCHAIN_VARIANT` 83or :envvar:`ZEPHYR_SDK_INSTALL_DIR` environment variables are already set. 84 85* If the variables are not set, the latest compatible version of Zephyr SDK will be selected 86 by default. Proceed to next step without making any changes. 87 88* If :envvar:`ZEPHYR_TOOLCHAIN_VARIANT` is set, the corresponding toolchain will be selected 89 at build time. Zephyr SDK is identified by the value ``zephyr``. 90 If the :envvar:`ZEPHYR_TOOLCHAIN_VARIANT` environment variable is not ``zephyr``, then either 91 unset it or change its value to ``zephyr`` to make sure Zephyr SDK is selected. 92 93* If the :envvar:`ZEPHYR_SDK_INSTALL_DIR` environment variable is set, it will override 94 the default lookup location for Zephyr SDK. If you install Zephyr SDK to one 95 of the :ref:`recommended locations <toolchain_zephyr_sdk_bundle_variables>`, 96 you can unset this variable. Otherwise, set it to your chosen install location. 97 98For more information about these environment variables in Zephyr, see :ref:`env_vars_important`. 99 100Cloning the Zephyr Repositories 101******************************* 102 103The Zephyr project source is maintained in the `GitHub zephyr repo 104<https://github.com/zephyrproject-rtos/zephyr>`_. External modules used 105by Zephyr are found in the parent `GitHub Zephyr project 106<https://github.com/zephyrproject-rtos/>`_. Because of these 107dependencies, it's convenient to use the Zephyr-created :ref:`west 108<west>` tool to fetch and manage the Zephyr and external module source 109code. See :ref:`west-basics` for more details. 110 111Once your development tools are installed, use :ref:`west` to create, 112initialize, and download sources from the zephyr and external module 113repos. We'll use the name ``zephyrproject``, but you can choose any 114name that does not contain a space anywhere in the path. 115 116.. code-block:: console 117 118 west init zephyrproject 119 cd zephyrproject 120 west update 121 122The ``west update`` command fetches and keeps :ref:`modules` in the 123:file:`zephyrproject` folder in sync with the code in the local zephyr 124repo. 125 126.. warning:: 127 128 You must run ``west update`` any time the :file:`zephyr/west.yml` 129 changes, caused, for example, when you pull the :file:`zephyr` 130 repository, switch branches in it, or perform a ``git bisect`` inside of 131 it. 132 133Keeping Zephyr updated 134====================== 135 136To update the Zephyr project source code, you need to get the latest 137changes via ``git``. Afterwards, run ``west update`` as mentioned in 138the previous paragraph. 139Additionally, check for updated or added Python dependencies. 140 141.. tabs:: 142 143 .. group-tab:: Linux/macOS 144 145 .. code-block:: console 146 147 # replace zephyrproject with the path you gave west init 148 cd zephyrproject/zephyr 149 git pull 150 west update 151 west packages pip --install 152 153 .. group-tab:: Windows 154 155 .. tabs:: 156 157 .. code-tab:: bat 158 159 :: replace zephyrproject with the path you gave west init 160 cd zephyrproject\zephyr 161 git pull 162 west update 163 cmd /c scripts\utils\west-packages-pip-install.cmd 164 165 .. code-tab:: powershell 166 167 # replace zephyrproject with the path you gave west init 168 cd zephyrproject\zephyr 169 git pull 170 west update 171 python -m pip install @((west packages pip) -split ' ') 172 173Export Zephyr CMake package 174*************************** 175 176The :ref:`cmake_pkg` can be exported to CMake's user package registry if it has 177not already been done as part of :ref:`getting_started`. 178 179.. _gs-board-aliases: 180 181Board Aliases 182************* 183 184Developers who work with multiple boards may find explicit board names 185cumbersome and want to use aliases for common targets. This is 186supported by a CMake file with content like this: 187 188.. code-block:: cmake 189 190 # Variable foo_BOARD_ALIAS=bar replaces BOARD=foo with BOARD=bar and 191 # sets BOARD_ALIAS=foo in the CMake cache. 192 set(pca10028_BOARD_ALIAS nrf51dk/nrf51822) 193 set(pca10056_BOARD_ALIAS nrf52840dk/nrf52840) 194 set(k64f_BOARD_ALIAS frdm_k64f) 195 set(sltb004a_BOARD_ALIAS efr32mg_sltb004a) 196 197and specifying its location in :envvar:`ZEPHYR_BOARD_ALIASES`. This 198enables use of aliases ``pca10028`` in contexts like 199``cmake -DBOARD=pca10028`` and ``west -b pca10028``. 200 201Build and Run an Application 202**************************** 203 204You can build, flash, and run Zephyr applications on real 205hardware using a supported host system. Depending on your operating system, 206you can also run it in emulation with QEMU, or as a native application with 207:zephyr:board:`native_sim <native_sim>`. 208Additional information about building applications can be found in the 209:ref:`build_an_application` section. 210 211Build Blinky 212============ 213 214Let's build the :zephyr:code-sample:`blinky` sample application. 215 216Zephyr applications are built to run on specific hardware, called a 217"board"\ [#board_misnomer]_. We'll use the Phytec :ref:`reel_board 218<reel_board>` here, but you can change the ``reel_board`` build target 219to another value if you have a different board. See :ref:`boards` or run 220``west boards`` from anywhere inside the ``zephyrproject`` directory for 221a list of supported boards. 222 223#. Go to the zephyr repository: 224 225 .. code-block:: console 226 227 cd zephyrproject/zephyr 228 229#. Build the blinky sample for the ``reel_board``: 230 231 .. zephyr-app-commands:: 232 :zephyr-app: samples/basic/blinky 233 :board: reel_board 234 :goals: build 235 236The main build products will be in :file:`build/zephyr`; 237:file:`build/zephyr/zephyr.elf` is the blinky application binary in ELF 238format. Other binary formats, disassembly, and map files may be present 239depending on your board. 240 241The other sample applications in the :zephyr_file:`samples` folder are 242documented in :zephyr:code-sample-category:`samples`. 243 244.. note:: If you want to reuse an 245 existing build directory for another board or application, you need to 246 add the parameter ``-p=auto`` to ``west build`` to clean out settings 247 and artifacts from the previous build. 248 249Run the Application by Flashing to a Board 250========================================== 251 252Most hardware boards supported by Zephyr can be flashed by running 253``west flash``. This may require board-specific tool installation and 254configuration to work properly. 255 256See :ref:`application_run` and your specific board's documentation in 257:ref:`boards` for additional details. 258 259.. _setting-udev-rules: 260 261Setting udev rules 262=================== 263 264Flashing a board requires permission to directly access the board 265hardware, usually managed by installation of the flashing tools. On 266Linux systems, if the ``west flash`` command fails, you likely need to 267define udev rules to grant the needed access permission. 268 269Udev is a device manager for the Linux kernel and the udev daemon 270handles all user space events raised when a hardware device is added (or 271removed) from the system. We can add a rules file to grant access 272permission by non-root users to certain USB-connected devices. 273 274The OpenOCD (On-Chip Debugger) project conveniently provides a rules 275file that defined board-specific rules for most Zephyr-supported 276arm-based boards, so we recommend installing this rules 277file by downloading it from their sourceforge repo, or if you've 278installed the Zephyr SDK there is a copy of this rules file in the SDK 279folder: 280 281* Either download the OpenOCD rules file and copy it to the right 282 location:: 283 284 wget -O 60-openocd.rules https://sf.net/p/openocd/code/ci/master/tree/contrib/60-openocd.rules?format=raw 285 sudo cp 60-openocd.rules /etc/udev/rules.d 286 287* or copy the rules file from the Zephyr SDK folder:: 288 289 sudo cp ${ZEPHYR_SDK_INSTALL_DIR}/sysroots/x86_64-pokysdk-linux/usr/share/openocd/contrib/60-openocd.rules /etc/udev/rules.d 290 291Then, in either case, ask the udev daemon to reload these rules:: 292 293 sudo udevadm control --reload 294 295Unplug and plug in the USB connection to your board, and you should have 296permission to access the board hardware for flashing. Check your 297board-specific documentation (:ref:`boards`) for further information if 298needed. 299 300Run the Application in QEMU 301=========================== 302 303On Linux and macOS, you can run Zephyr applications via emulation on your host 304system using `QEMU <https://www.qemu.org/>`_ when targeting either 305the x86 or ARM Cortex-M3 architectures. (QEMU is included with the Zephyr 306SDK installation.) 307 308On Windows, you need to install QEMU manually from 309`Download QEMU <https://www.qemu.org/download/#windows>`_. After installation, 310add path to QEMU installation folder to PATH environment variable. 311To enable QEMU in Test Runner (Twister) on Windows, 312:ref:`set the environment variable <env_vars>` 313``QEMU_BIN_PATH`` to the path of QEMU installation folder. 314 315For example, you can build and run the :zephyr:code-sample:`hello_world` sample using 316the x86 emulation board configuration (``qemu_x86``), with: 317 318.. zephyr-app-commands:: 319 :zephyr-app: samples/hello_world 320 :host-os: unix 321 :board: qemu_x86 322 :goals: build run 323 324To exit QEMU, type :kbd:`Ctrl-a`, then :kbd:`x`. 325 326Use ``qemu_cortex_m3`` to target an emulated Arm Cortex-M3 sample. 327 328.. _gs_native: 329 330Run a Sample Application natively (Linux) 331========================================= 332 333You can compile some samples to run as host programs 334on Linux. See :zephyr:board:`native_sim` for more information. On 64-bit host operating systems, you 335need to install a 32-bit C library, or build targeting :ref:`native_sim/native/64<native_sim32_64>`. 336 337First, build Hello World for ``native_sim``. 338 339.. zephyr-app-commands:: 340 :zephyr-app: samples/hello_world 341 :host-os: unix 342 :board: native_sim 343 :goals: build 344 345Next, run the application. 346 347.. code-block:: console 348 349 west build -t run 350 # or just run zephyr.exe directly: 351 ./build/zephyr/zephyr.exe 352 353Press :kbd:`Ctrl-C` to exit. 354 355You can run ``./build/zephyr/zephyr.exe --help`` to get a list of available 356options. 357 358This executable can be instrumented using standard tools, such as gdb or 359valgrind. 360 361.. rubric:: Footnotes 362 363.. [#pip] 364 365 pip is Python's package installer. Its ``install`` command first tries to 366 reuse packages and package dependencies already installed on your computer. 367 If that is not possible, ``pip install`` downloads them from the Python 368 Package Index (PyPI) on the Internet. 369 370 The package versions requested by Zephyr's :file:`requirements.txt` may 371 conflict with other requirements on your system, in which case you may 372 want to set up a virtualenv for Zephyr development. 373 374.. [#board_misnomer] 375 376 This has become something of a misnomer over time. While the target can be, 377 and often is, a microprocessor running on its own dedicated hardware 378 board, Zephyr also supports using QEMU to run targets built for other 379 architectures in emulation, targets which produce native host system 380 binaries that implement Zephyr's driver interfaces with POSIX APIs, and even 381 running different Zephyr-based binaries on CPU cores of differing 382 architectures on the same physical chip. Each of these hardware 383 configurations is called a "board," even though that doesn't always make 384 perfect sense in context. 385 386.. _information on -\\-user: 387 https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site 388.. _Homebrew disables -\\-user: 389 https://docs.brew.sh/Homebrew-and-Python#note-on-pip-install---user 390.. _Installing Packages: 391 https://packaging.python.org/tutorials/installing-packages/ 392