Rodrigo Caballero | 4f976c2 | 2016-01-28 10:59:49 -0600 | [diff] [blame] | 1 | .. _getting_started: |
| 2 | |
| 3 | Getting Started Guide |
| 4 | ##################### |
| 5 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 6 | Follow this guide to set up a :ref:`Zephyr <introducing_zephyr>` development |
| 7 | environment on your system, and then build and run a sample application. |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 8 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 9 | .. _host_setup: |
David B. Kinder | 8b81294 | 2017-12-01 16:16:47 -0800 | [diff] [blame] | 10 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 11 | Set Up a Development System |
| 12 | *************************** |
David B. Kinder | 8b81294 | 2017-12-01 16:16:47 -0800 | [diff] [blame] | 13 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 14 | Follow one of the following guides for your host operating system. |
Rodrigo Caballero | 4f976c2 | 2016-01-28 10:59:49 -0600 | [diff] [blame] | 15 | |
| 16 | .. toctree:: |
| 17 | :maxdepth: 1 |
| 18 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 19 | Linux <installation_linux.rst> |
| 20 | macOS <installation_mac.rst> |
| 21 | Windows <installation_win.rst> |
Rodrigo Caballero | 4f976c2 | 2016-01-28 10:59:49 -0600 | [diff] [blame] | 22 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 23 | Clone the Zephyr Repository |
| 24 | *************************** |
| 25 | |
| 26 | To clone the Zephyr source code repository from GitHub: |
| 27 | |
| 28 | .. code-block:: console |
| 29 | |
| 30 | git clone https://github.com/zephyrproject-rtos/zephyr |
| 31 | |
| 32 | .. warning:: |
| 33 | |
| 34 | Don't clone Zephyr to a directory with spaces anywhere in the path. |
| 35 | For example, on Windows, :file:`C:\\Users\\YourName\\zephyr` will |
| 36 | work, but :file:`C:\\Users\\Your Name\\zephyr` will cause cryptic |
| 37 | errors when you try to build an application. |
| 38 | |
| 39 | Install Python Dependencies |
| 40 | *************************** |
| 41 | |
| 42 | Next, install additional Python packages required by Zephyr in a shell or |
| 43 | ``cmd.exe`` prompt: |
| 44 | |
| 45 | .. code-block:: console |
| 46 | |
| 47 | # Linux |
Sebastian Bøe | e8420d9 | 2018-10-24 15:35:30 +0200 | [diff] [blame] | 48 | pip3 install --user -r zephyr/scripts/requirements.txt |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 49 | |
| 50 | # macOS and Windows |
| 51 | pip3 install -r zephyr/scripts/requirements.txt |
| 52 | |
| 53 | Some notes on pip's ``--user`` option: |
| 54 | |
| 55 | - Installing with ``--user`` is the default behavior on Debian-based |
| 56 | distributions and is generally recommended on Linux to avoid conflicts with |
| 57 | Python packages installed using the system package manager. |
| 58 | |
David B. Kinder | 6871b92 | 2018-11-02 11:00:47 -0700 | [diff] [blame] | 59 | - On Linux, verify the Python user install directory ``~/.local/bin`` is at the front of |
| 60 | your PATH environment variable, otherwise installed packages won't be |
| 61 | found. |
| 62 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 63 | - On macOS, Homebrew disables the ``--user`` flag\ [#homebrew_user]_. |
| 64 | |
| 65 | - On Windows using ``cmd.exe``, although it's possible to use the ``--user`` |
| 66 | flag, it makes it harder for the command prompt to find executables installed |
| 67 | by pip. |
| 68 | |
David B. Kinder | 6871b92 | 2018-11-02 11:00:47 -0700 | [diff] [blame] | 69 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 70 | Set Up a Toolchain |
| 71 | ****************** |
| 72 | |
| 73 | .. note:: |
| 74 | |
| 75 | On Linux, you can skip this step if you installed the :ref:`Zephyr SDK |
| 76 | <zephyr_sdk>`, which includes toolchains for all supported Zephyr |
| 77 | architectures. |
| 78 | |
| 79 | If you want, you can use the SDK host tools (such as OpenOCD) with a |
| 80 | different toolchain by keeping the :envvar:`ZEPHYR_SDK_INSTALL_DIR` |
| 81 | environment variable set to the Zephyr SDK installation directory, while |
| 82 | setting :envvar:`ZEPHYR_TOOLCHAIN_VARIANT` appropriately for a non-SDK |
| 83 | toolchain. |
| 84 | |
| 85 | Zephyr binaries are compiled using software called a *toolchain*. You need to |
| 86 | *install* and *configure* a toolchain to develop Zephyr applications\ |
| 87 | [#tools_native_posix]_. |
| 88 | |
| 89 | Toolchains can be *installed* in different ways, including using installer |
| 90 | programs, system package managers, or simply downloading a zip file or other |
| 91 | archive and extracting the files somewhere on your computer. You *configure* |
| 92 | the toolchain by setting the environment variable |
| 93 | :envvar:`ZEPHYR_TOOLCHAIN_VARIANT` to a recognized value, along with some |
| 94 | additional variable(s) specific to that toolchain (usually, this is just one |
| 95 | more variable which contains the path where you installed the toolchain on your |
| 96 | file system). |
| 97 | |
| 98 | .. note:: |
| 99 | |
| 100 | In previous releases of Zephyr, the ``ZEPHYR_TOOLCHAIN_VARIANT`` variable |
| 101 | was called ``ZEPHYR_GCC_VARIANT``. |
| 102 | |
| 103 | The following toolchain installation options are available. The right choice |
| 104 | for you depends on where you want to run Zephyr and any other requirements you |
| 105 | may have. Check your :ref:`board-level documentation <boards>` if you are |
| 106 | unsure about what choice to use. |
| 107 | |
| 108 | .. toctree:: |
| 109 | :maxdepth: 2 |
| 110 | |
| 111 | toolchain_3rd_party_x_compilers.rst |
| 112 | toolchain_other_x_compilers.rst |
| 113 | toolchain_custom_cmake.rst |
| 114 | |
| 115 | |
| 116 | To use the same toolchain in new sessions in the future you can make |
| 117 | sure the variables are set persistently. |
| 118 | |
| 119 | On macOS and Linux, you can set the variables by putting the ``export`` lines |
| 120 | setting environment variables in a file :file:`~/.zephyrrc`. On Windows, you |
| 121 | can put the ``set`` lines in :file:`%userprofile%\\zephyrrc.cmd`. These files |
| 122 | are used to modify your environment when you run ``zephyr-env.sh`` (Linux, |
| 123 | macOS) and ``zephyr-env.cmd`` (Windows), which you will learn about in the next |
| 124 | step. |
Rodrigo Caballero | 4f976c2 | 2016-01-28 10:59:49 -0600 | [diff] [blame] | 125 | |
Marti Bolivar | 2e3ecd8 | 2017-11-02 12:43:43 -0400 | [diff] [blame] | 126 | .. _getting_started_run_sample: |
| 127 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 128 | Build and Run an Application |
| 129 | **************************** |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 130 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 131 | Next, build a sample Zephyr application. You can then flash and run it on real |
| 132 | hardware using any supported host system. Depending on your operating system, |
| 133 | you can also run it in emulation with QEMU or as a native POSIX application. |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 134 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 135 | .. _getting_started_cmake: |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 136 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 137 | A Brief Note on the Zephyr Build System |
| 138 | ======================================= |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 139 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 140 | The Zephyr build system uses `CMake`_. CMake creates build systems in different |
| 141 | formats, called `generators`_. Zephyr supports the following generators: |
| 142 | |
| 143 | * ``Unix Makefiles``: Supported on UNIX-like platforms (Linux, macOS). |
| 144 | * ``Ninja``: Supported on all platforms. |
| 145 | |
| 146 | This documentation and Zephyr's continuous integration system mainly use |
| 147 | ``Ninja``, but you should be able to use any supported generator to build |
| 148 | Zephyr applications. |
| 149 | |
| 150 | Build the Application |
| 151 | ===================== |
| 152 | |
| 153 | Follow these steps to build the :ref:`hello_world` sample application provided |
| 154 | with Zephyr. |
| 155 | |
| 156 | Zephyr applications have to be configured and built to run on some hardware |
| 157 | configuration, which is called a "board"\ [#board_misnomer]_. These steps show |
| 158 | how to build the Hello World application for the :ref:`arduino_101` board. You |
| 159 | can build for a different board by changing ``arduino_101`` to another |
| 160 | supported value. See :ref:`boards` for more information, or run ``ninja usage`` |
| 161 | from the build directory (once you've run ``cmake``) to get a list. |
| 162 | |
| 163 | .. note:: |
| 164 | |
| 165 | If you want to re-use your existing build directory to build for another |
| 166 | board, you must delete that directory's contents first by running ``ninja |
| 167 | pristine``. |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 168 | |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 169 | #. Navigate to the main project directory: |
| 170 | |
| 171 | .. code-block:: console |
| 172 | |
Carles Cufi | 72046a8 | 2018-01-17 17:32:31 +0100 | [diff] [blame] | 173 | cd zephyr |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 174 | |
Marti Bolivar | c6c1501 | 2018-05-31 15:50:46 -0400 | [diff] [blame] | 175 | #. Set up your build environment: |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 176 | |
| 177 | .. code-block:: console |
| 178 | |
Carles Cufi | 72046a8 | 2018-01-17 17:32:31 +0100 | [diff] [blame] | 179 | # On Linux/macOS |
| 180 | source zephyr-env.sh |
| 181 | # On Windows |
Carles Cufi | 980c0cf | 2018-02-12 14:33:59 +0100 | [diff] [blame] | 182 | zephyr-env.cmd |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 183 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 184 | #. Build the Hello World sample for the ``arduino_101``: |
| 185 | |
| 186 | .. Note: we don't use :zephyr-app: here because we just told the user to cd |
| 187 | to ZEPHYR_BASE, so it's not necessary for clarity and would clutter the |
| 188 | instructions a bit. |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 189 | |
Carles Cufi | fa0030a | 2017-11-09 13:19:27 +0100 | [diff] [blame] | 190 | .. zephyr-app-commands:: |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 191 | :app: samples/hello_world |
Carles Cufi | fa0030a | 2017-11-09 13:19:27 +0100 | [diff] [blame] | 192 | :board: arduino_101 |
Carles Cufi | fa0030a | 2017-11-09 13:19:27 +0100 | [diff] [blame] | 193 | :goals: build |
Anas Nashif | 602a143 | 2017-10-31 08:35:24 -0400 | [diff] [blame] | 194 | |
Carles Cufi | 72046a8 | 2018-01-17 17:32:31 +0100 | [diff] [blame] | 195 | On Linux/macOS you can also build with ``make`` instead of ``ninja``: |
| 196 | |
| 197 | .. zephyr-app-commands:: |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 198 | :app: samples/hello_world |
Carles Cufi | 72046a8 | 2018-01-17 17:32:31 +0100 | [diff] [blame] | 199 | :generator: make |
| 200 | :host-os: unix |
| 201 | :board: arduino_101 |
Carles Cufi | 72046a8 | 2018-01-17 17:32:31 +0100 | [diff] [blame] | 202 | :goals: build |
| 203 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 204 | The main build products are in :file:`zephyr/samples/hello_world/build/zephyr`. |
| 205 | The final application binary in ELF format is named :file:`zephyr.elf` by |
| 206 | default. Other binary formats and byproducts such as disassembly and map files |
| 207 | will be present depending on the target and build system configuration. |
Anas Nashif | 602a143 | 2017-10-31 08:35:24 -0400 | [diff] [blame] | 208 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 209 | Other sample projects demonstrating Zephyr's features are located in |
| 210 | :file:`zephyr/samples` and are documented in :ref:`samples-and-demos`. |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 211 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 212 | Run the Application by Flashing to Another Board |
| 213 | ================================================ |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 214 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 215 | Most "real hardware" boards supported by Zephyr can be flashed by running |
| 216 | ``ninja flash`` from the build directory. However, this may require |
| 217 | board-specific tool installation and configuration to work properly. |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 218 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 219 | See :ref:`application_run` in the Application Development Primer and the |
| 220 | documentation provided with your board at :ref:`boards` for additional details |
| 221 | if you get an error. |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 222 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 223 | Run the Application in QEMU |
| 224 | =========================== |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 225 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 226 | On Linux and macOS, you can run Zephyr applications in emulation on your host |
| 227 | system using QEMU when targeting either the X86 or ARM Cortex-M3 architectures. |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 228 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 229 | To build and run Hello World using the x86 emulation board configuration |
| 230 | (``qemu_x86``), type: |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 231 | |
Carles Cufi | 72046a8 | 2018-01-17 17:32:31 +0100 | [diff] [blame] | 232 | .. zephyr-app-commands:: |
| 233 | :zephyr-app: samples/hello_world |
| 234 | :host-os: unix |
| 235 | :board: qemu_x86 |
| 236 | :goals: build run |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 237 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 238 | To exit, type :kbd:`Ctrl-a`, then :kbd:`x`. |
Sven Dowideit | 46f530e | 2017-11-17 15:20:24 +1000 | [diff] [blame] | 239 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 240 | Use the ``qemu_cortex_m3`` board configuration to run on an emulated Arm |
| 241 | Cortex-M3. |
Anas Nashif | d0c5eaf | 2016-02-13 07:41:34 -0500 | [diff] [blame] | 242 | |
Anas Nashif | 6b55598 | 2017-12-21 08:14:19 -0500 | [diff] [blame] | 243 | Running a Sample Application natively (POSIX OS) |
| 244 | ================================================ |
| 245 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 246 | Finally, it is also possible to compile some samples to run as native processes |
| 247 | on a POSIX OS. This is currently only tested on Linux hosts. |
Anas Nashif | 6b55598 | 2017-12-21 08:14:19 -0500 | [diff] [blame] | 248 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 249 | On 64 bit host operating systems, you will also need a 32 bit C library |
| 250 | installed. See the :ref:`native_posix` section on host dependencies for more |
| 251 | information. |
| 252 | |
| 253 | To compile and run Hello World in this way, type: |
Anas Nashif | 6b55598 | 2017-12-21 08:14:19 -0500 | [diff] [blame] | 254 | |
Carles Cufi | 72046a8 | 2018-01-17 17:32:31 +0100 | [diff] [blame] | 255 | .. zephyr-app-commands:: |
| 256 | :zephyr-app: samples/hello_world |
| 257 | :host-os: unix |
| 258 | :board: native_posix |
| 259 | :goals: build |
Anas Nashif | 6b55598 | 2017-12-21 08:14:19 -0500 | [diff] [blame] | 260 | |
| 261 | and then: |
| 262 | |
| 263 | .. code-block:: console |
| 264 | |
Carles Cufi | 72046a8 | 2018-01-17 17:32:31 +0100 | [diff] [blame] | 265 | ninja run |
David B. Kinder | 9af44d8 | 2018-05-10 14:29:12 -0700 | [diff] [blame] | 266 | |
Anas Nashif | 6b55598 | 2017-12-21 08:14:19 -0500 | [diff] [blame] | 267 | # or just: |
Carles Cufi | 72046a8 | 2018-01-17 17:32:31 +0100 | [diff] [blame] | 268 | zephyr/zephyr.exe |
Anas Nashif | 6b55598 | 2017-12-21 08:14:19 -0500 | [diff] [blame] | 269 | # Press Ctrl+C to exit |
| 270 | |
David B. Kinder | 9af44d8 | 2018-05-10 14:29:12 -0700 | [diff] [blame] | 271 | You can run ``zephyr/zephyr.exe --help`` to get a list of available |
| 272 | options. See the :ref:`native_posix` document for more information. |
| 273 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 274 | This executable can be instrumented using standard tools, such as gdb or |
| 275 | valgrind. |
Carles Cufi | 72046a8 | 2018-01-17 17:32:31 +0100 | [diff] [blame] | 276 | |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 277 | .. rubric:: Footnotes |
| 278 | |
| 279 | .. [#homebrew_user] |
| 280 | |
| 281 | For details, see |
| 282 | https://docs.brew.sh/Homebrew-and-Python#note-on-pip-install---user. |
| 283 | |
| 284 | .. [#tools_native_posix] |
| 285 | |
| 286 | Usually, the toolchain is a cross-compiler and related tools which are |
| 287 | different than the host compilers and other programs available for |
| 288 | developing software to run natively on your operating system. |
| 289 | |
| 290 | One exception is when building Zephyr as a host binary to run on a POSIX |
| 291 | operating system. In this case, you still need to set up a toolchain, but it |
| 292 | will provide host compilers instead of cross compilers. For details on this |
| 293 | option, see :ref:`native_posix`. |
| 294 | |
| 295 | .. [#board_misnomer] |
| 296 | |
| 297 | This has become something of a misnomer over time. While the target can be, |
| 298 | and often is, a microprocessor running on its own dedicated hardware |
| 299 | board, Zephyr also supports using QEMU to run targets built for other |
| 300 | architectures in emulation, targets which produce native host system |
| 301 | binaries that implement Zephyr's driver interfaces with POSIX APIs, and even |
| 302 | running different Zephyr-based binaries on CPU cores of differing |
| 303 | architectures on the same physical chip. Each of these hardware |
| 304 | configurations is called a "board," even though that doesn't always make |
| 305 | perfect sense in context. |
| 306 | |
Carles Cufi | 72046a8 | 2018-01-17 17:32:31 +0100 | [diff] [blame] | 307 | .. _CMake: https://cmake.org |
Marti Bolivar | 0d811b9 | 2018-10-14 23:20:24 -0600 | [diff] [blame] | 308 | .. _generators: https://cmake.org/cmake/help/v3.8/manual/cmake-generators.7.html |