bus710 / zephyr-rtos-development-in-linux

Apache License 2.0
11 stars 1 forks source link

Zephyr RTOS Development in Linux

Zephyr is an RTOS for IoT projects.
This repo is a walkthrough to prepare Zephyr development environment for nRF52 devices in Ubuntu.



Index



Prerequisites



Reference




1. Linux dependencies

Install some packages:

$ sudo apt update

$ sudo apt install build-essential \
                    git \
                    openocd \
                    libncurses5 \
                    gdb-multiarch \
                    gcc-arm-none-eabi

$ sudo apt-get install --no-install-recommends \
    git cmake ninja-build gperf \
    ccache dfu-util device-tree-compiler wget \
    python3-pip python3-setuptools python3-tk python3-wheel \
    xz-utils file make gcc gcc-multilib

Check the versions (should be newer than these):

$ arm-none-eabi-gcc -v

gcc version 7.3.1 20180622 ...

$ openocd 

Open On-Chip Debugger 0.10.0

$ cmake --version

cmake version 3.13.4

$ dtc --version

Version: DTC 1.4.7

$ pip3 --version

pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

Get west:

$ pip3 install --user -U west
$ echo 'export PATH=~/.local/bin:"$PATH"' >> ~/.bashrc
$ source ~/.bashrc
$ west --version

West version: v0.6.3

(Optional) Do this if ncurses installation shows some error:

$ sudo apt --fix-broken install 



2. Get JLink package

We can download some software tools from Nordic semiconductor:

Download nRF5x-Command-Line-Tools for Linux 64 bit from:

Run these commands to install the tools:

$ tar xvf nRF-Command-Line-Tools_10_4_1_Linux-amd64.tar.gz
$ sudo dpkg -i JLink_Linux_V650b_x86_64.deb
$ sudo dpkg -i nRF-Command-Line-Tools_10_4_1_Linux-amd64.deb

To check the version of installed tools:

$ nrfjprog -v

nrfjprog version: 10.4.1 
JLinkARM.dll version: 6.50b

$ mergehex -v

mergehex version: 10.4.1

$ JLinkExe -v

SEGGER J-Link Commander V6.50b (Compiled Sep  6 2019 17:46:52)
DLL version V6.50b, compiled Sep  6 2019 17:46:40

Unknown command line option -h. (<= Don't worry about this)



3. Get zephyr source code and tools

Here each step downloads many files from the internet:

$ cd ~
$ west init zephyrproject
$ cd zephyrproject
$ west update
$ pip3 install --user -r ~/zephyrproject/zephyr/scripts/requirements.txt



4. Get zephyr SDK

Check the latest stable SDK version first:

Download it:

$ wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.10.3/zephyr-sdk-0.10.3-setup.run

Run the SDK installer:

$ chmod 744 zephyr-sdk-0.10.3-setup.run
$ ./zephyr-sdk-0.10.3-setup.run -- -d ~/zephyr-sdk-0.10.3

...
Success installing SDK. SDK is ready to be used.

Since the extracted files are stored in ~/zephyr-sdk-0.10.3, put these in bashrc or so:

export ZEPHYR_TOOLCHAIN_VARIANT=zephyr
export ZEPHYR_SDK_INSTALL_DIR=$HOME/zephyr-sdk-0.10.3

To apply the variables:

$ source ~/.bashrc

Get an udev rule file for openocd:

$ sudo cp ${ZEPHYR_SDK_INSTALL_DIR}/sysroots/x86_64-pokysdk-linux/usr/share/openocd/contrib/60-openocd.rules /etc/udev/rules.d

$ sudo udevadm control --reload



5. Build the blinky application

Add this to bashrc:

source ~/zephyrproject/zephyr/zephyr-env.sh

Apply the variables:

$ source ~/.bashrc

Then build the project:

$ cd ~/zephyrproject/zephyr/samples/basic/blinky
$ west build -p auto -b nrf52_pca10040 .

To flash the generated image to a connected board:

$ west flash 

...
-- runners.nrfjprog: Board with serial number 682347313 flashed successfully.

(Option) To clean the built images:

$ west build -b nrf52_pca10040 -t clean

(Option) To erase the flash memory (+UICR) of the target:

$ west flash --erase



6. Debug the blinky app in VSCODE

First, install VSCODE:

Open the blinky app in VSCODE:

$ cd ~/zephyrproject/zephyr/samples/basic/blinky
$ code .

To create the launch.json file in VSCODE,



6.1 For Jlink GDB host and the on board probe

Paste this for the launch.json for Cortex-Debug extension:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Zephyr nRF52832",
            "cwd": "${workspaceRoot}",
            "executable": "build/zephyr/zephyr.elf",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "jlink",
            "device": "nrf52832_xxaa",
            "targetId": "nrf52",
            "boardId": "",
            "armToolchainPath": "${HOME}/zephyr-sdk-0.10.3/arm-zephyr-eabi/bin",
            "interface": "swd",
            "gdbpath": "/usr/bin/gdb-multiarch",
        },
    ]
}

To start debugging, press the F5 key twice.
However, this may not support RTOS-awareness.



6.2 For Openocd GDB host and the on board probe

Paste this for the launch.json for Cortex-Debug extension:

{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Zephyr nRF52832 openocd",
            "cwd": "${workspaceRoot}",
            "executable": "build/zephyr/zephyr.elf",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "openocd",
            "device": "nrf52832_xxaa",
            "targetId": "nrf52",
            "boardId": "",
            "armToolchainPath": "${HOME}/zephyr-sdk-0.10.3/arm-zephyr-eabi/bin",
            "interface": "swd",
            "gdbpath": "/usr/bin/gdb-multiarch",
             "configFiles": [
                 "/usr/share/openocd/scripts/board/nordic_nrf52_dk.cfg"
            ]
        },
    ]
}

To start debugging, press the F5 key twice.




What's next

The application doc introduces the basic work flow:

In the same doc, there is a section that describes to make a custom system:

Also, there are tips in the user and developer guide:

Good luck!