7.6 KiB
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
- Install the
libfuse2librarysudo apt install libfuse2 - Download the nRF Connect for Desktop
- Make the file executable
chmod a+x nrfconnect-*.appimage - 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"
- Install necessary tools
- Toolchain Manager
- Programmer
- 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
- Download nRF-udev
and install the nRF udev
sudo dpkg -i nrf-udev_1.0.1-all.deb - 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
- 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
- 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
- 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
- Install Python venv package
sudo apt install python3-venv
- Create a new virtual environment
python3 -m venv ~/zephyrproject/.venv
- 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.
- Install west
pip install west
- Get the Zephyr source code
west init ~/zephyrproject
cd ~/zephyrproject
west update
- Export a Zephyr CMake package. This allows CMake to automatically load boilerplate code required for building Zephyr applications.
west zephyr-export
- Zephyr’s
scripts/requirements.txtfile declares additional Python dependencies.
pip install -r ~/zephyrproject/zephyr/scripts/requirements.txt
Windows
Use Linux or WSL
WSL
Cry
Install the Zephyr SDK
Linux
- 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 - Extract the Zephyr SDK bundle archive:
tar xf zephyr-sdk-0.16.5-1_linux-x86_64.tar.xz - Run the Zephyr SDK bundle setup script
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.
~/zephyr-sdk-0.16.5-1_linux-x86_64/install.sh - 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
-
Create new Toolchain
- Open CLion, go to
File>Settings>Build, Execution, Deployment>Toolchain - Create a new
Zephyrtoolchain Add environment ‣ From fileand select~/zephyrproject/.venv/bin/activate.
- Open CLion, go to
-
Create a new project in
~/zephyrproject/zephyr/myApps/<my_project_name> -
On CMake profile setting dialog, set the following:
Name:ZephyrToolchain:ZephyrCMake options:-DBOARD=nrf52840dk_nrf52840(or other board you want to use)
-
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 -
Create a new
main.cfile in thesrcdirectory. 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); } } -
Create the
CMakeLists.txtfile in the root of the projectcmake_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) -
Enable RTOS integration
- Go to
Settings ‣ Build, Execution, Deployment ‣ Embedded Development ‣ RTOS Integration. - Set the Enable RTOS Integration checkbox. You can leave the option set to
Auto. CLion will detect Zephyr automatically.
- Go to
-
Configure the debugger
-
Build and debug the project
Windows
Use Linux or WSL
WSL
Cry
