nRF Development Environment

---------- Here you'll find some information for setting up the nRF development environment. We'll see more on detail how to set up the environment for Linux and program the nRF52 with Zephyr on CLion. # 1. Installing the nRF Connect SDK & Toolchain #### **Linux** 1. Install the `libfuse2` library ```bash sudo apt install libfuse2 ``` 2. Download the [nRF Connect for Desktop](https://www.nordicsemi.com/Products/Development-tools/nrf-connect-for-desktop) 3. Make the file executable ```bash chmod a+x nrfconnect-*.appimage ``` 4. Run the appimage (double-click on nrfconnect) - If it asks for update, click "yes" - If it asks is you want to help them improve the app, I recommend declining it. - If it asks is you want to start the "Quick Start" app, click "Close" 5. Install necessary tools - Toolchain Manager - Programmer 6. Open the toolchain manager and install "nRF Connect SDK v2.6.0" - Confirm the installation directory by clicking "Continue installation" - Wait for the toolchain to download and install. (this may take a while) - You can close the toolchain manager now 7. Download [nRF-udev](https://github.com/NordicSemiconductor/nrf-udev/releases/download/v1.0.1/nrf-udev_1.0.1-all.deb) and install the nRF udev ```bash sudo dpkg -i nrf-udev_1.0.1-all.deb ``` 8. Install [JLink](https://www.segger.com/downloads/jlink/JLink_Linux_x86_64.deb) and install it ```bash sudo dpkg -i JLink_Linux_*_x86_64.deb ``` #### **Windows** Use Linux or WSL #### **WSL** Cry # 2. Installing Zephyr [↗](https://builds.zephyrproject.io/zephyr/pr/60204/docs/develop/getting_started/index.html) Zephyr is an open-source real-time operating system (RTOS) that is designed to be small, scalable, and secure for use on resource-constrained systems. It is a Linux Foundation project and is governed by a Technical Steering Committee (TSC). ## Install dependencies | Tool | Min. Version | |---------------------|--------------| | CMake | 3.20.5 | | Python | 3.8 | | Devicetree compiler | 1.4.6 | #### **Linux** 1. You'll probably need to update your package list ```bash wget https://apt.kitware.com/kitware-archive.sh sudo bash kitware-archive.sh sudo apt update ``` 2. Use apt to install the required dependencies: ```bash sudo apt install --no-install-recommends git cmake ninja-build gperf \ ccache dfu-util device-tree-compiler wget \ python3-dev python3-pip python3-setuptools python3-tk python3-wheel xz-utils file \ make gcc gcc-multilib g++-multilib libsdl2-dev libmagic1 ``` 3. Verify the versions of the main dependencies installed on your system ```bash cmake --version python3 --version dtc --version ``` #### **Windows** Use Linux or WSL #### **WSL** Cry ## Get Zephyr and install Python dependencies We'll see here only how to install within virtual environment because for CLion we'll need to use the virtual environment. #### **Linux** 1. Install Python venv package ```bash sudo apt install python3-venv ``` 2. Create a new virtual environment ```bash python3 -m venv ~/zephyrproject/.venv ``` 3. Activate the virtual environment ```bash source ~/zephyrproject/.venv/bin/activate ``` Once activated your shell will be prefixed with `(.venv)`. The virtual environment can be deactivated at any time by running `deactivate`. 4. Install west ```bash pip install west ``` 5. Get the Zephyr source code ```bash west init ~/zephyrproject cd ~/zephyrproject west update ``` 6. Export a [Zephyr CMake package](https://builds.zephyrproject.io/zephyr/pr/60204/docs/build/zephyr_cmake_package.html#cmake-pkg). This allows CMake to automatically load boilerplate code required for building Zephyr applications. ```bash west zephyr-export ``` 7. Zephyr’s `scripts/requirements.txt` file declares additional Python dependencies. ```bash pip install -r ~/zephyrproject/zephyr/scripts/requirements.txt ``` #### **Windows** Use Linux or WSL #### **WSL** Cry ## Install the Zephyr SDK #### **Linux** 1. Download and verify the [Zephyr SDK bundle](https://github.com/zephyrproject-rtos/sdk-ng/releases/tag/v0.16.5-1) ```bash cd ~ wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.5-1/zephyr-sdk-0.16.5-1_linux-x86_64.tar.xz wget -O - https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.5-1/sha256.sum | shasum --check --ignore-missing ``` 2. Extract the Zephyr SDK bundle archive: ```bash tar xf zephyr-sdk-0.16.5-1_linux-x86_64.tar.xz ``` 3. Run the Zephyr SDK bundle setup script ```bash ~/zephyr-sdk-0.16.5-1/setup.sh ``` You only need to run the setup script once after extracting the Zephyr SDK bundle. You must rerun the setup script if you relocate the Zephyr SDK bundle directory after the initial setup. 4. Install udev rules, which allow you to flash most Zephyr boards as a regular user. ```bash sudo cp ~/zephyr-sdk-0.16.5-1/sysroots/x86_64-pokysdk-linux/usr/share/openocd/contrib/60-openocd.rules /etc/udev/rules.d sudo udevadm control --reload ``` #### **Windows** Use Linux or WSL #### **WSL** Cry # 3. Configure CLion for Zephyr [↗](https://builds.zephyrproject.io/zephyr/pr/60204/docs/develop/tools/clion.html#clion) #### **Linux** 1. Create new Toolchain 1. Open CLion, go to `File` > `Settings` > `Build, Execution, Deployment` > `Toolchain` 2. Create a new `Zephyr` toolchain 3. `Add environment ‣ From file` and select `~/zephyrproject/.venv/bin/activate`. 2. Create a new project in `~/zephyrproject/zephyr/myApps/` 3. On CMake profile setting dialog, set the following: - `Name`: `Zephyr` - `Toolchain`: `Zephyr` - `CMake options`: `-DBOARD=nrf52840dk_nrf52840` (or other board you want to use) 4. Create the project configuration file (`prj.conf`): ``` CONFIG_PRINTK=y CONFIG_GPIO=y CONFIG_DEBUG_THREAD_INFO=y CONFIG_THREAD_RUNTIME_STATS=y CONFIG_DEBUG_OPTIMIZATIONS=y ``` 5. Create a new `main.c` file in the `src` directory. For example: ```c #include #include #include #define LED0_NODE DT_ALIAS(led0) static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); int main(void){ int ret; bool led_state = true; if (!gpio_is_ready_dt(&led)) return 0; ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); if (ret < 0) return 0; while (1) { ret = gpio_pin_toggle_dt(&led); if (ret < 0) return 0; led_state = !led_state; printf("LED state: %s\n", led_state ? "ON" : "OFF"); k_msleep(1000); } } ``` 6. Create the `CMakeLists.txt` file in the root of the project ```cmake cmake_minimum_required(VERSION 3.13.1) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(my_project_name) target_sources(app PRIVATE src/main.c) ``` 7. Enable RTOS integration 1. Go to `Settings ‣ Build, Execution, Deployment ‣ Embedded Development ‣ RTOS Integration`. 2. Set the Enable RTOS Integration checkbox. You can leave the option set to `Auto`. CLion will detect Zephyr automatically. 8. Configure the debugger 1. Go to `Run ‣ New Embedded Configuration`. 2. Set the GDB settings:
![GDB settings](medias/GDB_settings.png) 3. Click on `Next`, select your board and click `Create`. 9. Build and debug the project #### **Windows** Use Linux or WSL #### **WSL** Cry