Files
Yggdrasil/Embed/nRF/development_environment.md
T
2024-03-20 15:54:39 +01:00

7.6 KiB
Raw Blame History

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
     sudo apt install libfuse2
    
  2. Download the nRF Connect for Desktop
  3. Make the file executable
    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 and install the nRF udev
    sudo dpkg -i nrf-udev_1.0.1-all.deb
    
  8. Install JLink and install it
    sudo dpkg -i JLink_Linux_*_x86_64.deb
    

Windows

Use Linux or WSL

WSL

Cry

2. Installing Zephyr ↗

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
wget https://apt.kitware.com/kitware-archive.sh
sudo bash kitware-archive.sh
sudo apt update
  1. Use apt to install the required dependencies:
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
  1. Verify the versions of the main dependencies installed on your system
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
sudo apt install python3-venv
  1. Create a new virtual environment
python3 -m venv ~/zephyrproject/.venv
  1. Activate the virtual environment
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.

  1. Install west
pip install west
  1. Get the Zephyr source code
west init ~/zephyrproject
cd ~/zephyrproject
west update
  1. Export a Zephyr CMake package. This allows CMake to automatically load boilerplate code required for building Zephyr applications.
west zephyr-export
  1. Zephyr’s scripts/requirements.txt file declares additional Python dependencies.
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
    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:
    tar xf zephyr-sdk-0.16.5-1_linux-x86_64.tar.xz
    
  3. Run the Zephyr SDK bundle setup script
    ~/zephyr-sdk-0.16.5-1_linux-x86_64/install.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.
    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 ↗

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/<my_project_name>

  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:

    #include <stdio.h>
    #include <zephyr/kernel.h>
    #include <zephyr/drivers/gpio.h>
    
    #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_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
    3. Select your board and click OK.
  9. Build and debug the project

Windows

Use Linux or WSL

WSL

Cry