前言

nRF Connect SDK 包括 libs 和 app demo,該 SDK + DEMO 是基於 ZephyrOS 實現的(因此,環境搭建會稍微麻煩億點點)!

該 SDK 支援 nRF52, nRF53, and nRF91 系列晶片。

接下來會一步步介紹如何在 Arch Linux 上搭建該 SDK 的開發環境:

  • Zephyr requirements and GNU ARM Embedded Toolchain
  • nRF Connect SDK
  • SEGGER Embedded Studio

1、概述

首先下載 nRF Connect for Desktop,linux上是個綠色版本,直接啟動即可:

然後根據 Getting Started Assistant 指導進行安裝。

更詳細的指導在(推薦用更詳細的指導,因為上面的指導受GUI限制,不能放很詳細的說明,新手手動安裝,容易出現問題):

https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/gs_installing.html#build-environment-cli

2、安裝工具

安裝:git、wget、cmake、make、dtc、tk

注: 輸入 cmake --version 校驗 cmake 的版本,nRF Connect SDK 需要 cmake 的版本 ≥ 3.13.1。

安裝 ninja-build:

Ninja is a small build system with a focus on speed. It differs from other build systems in two major respects: it is designed to have its input files generated by a higher-level build system, and it is designed to run builds as fast as possible.

  1. sudo pacman -S ninja

安裝 GPerf:

GNU gperf is a perfect hash function generator. For a given list of strings, it produces a hash function and hash table, in form of C or C++ code, for looking up a value depending on the input string. The hash function is perfect, which means that the hash table has no collisions, and the hash table lookup needs a single string comparison only.

  1. sudo pacman -S gperf

安裝 ccache:

Ccache (or “ccache”) is a compiler cache. It speeds up recompilation by caching previous compilations and detecting when the same compilation is being done again. Ccache is free software, released under the GNU General Public License version 3 or later. See also the license page.

  1. sudo pacman -S ccache

安裝 dfu-util:

dfu-util is a host side implementation of the DFU 1.0 and DFU 1.1 specifications of the USB forum.

  1. sudo pacman -S dfu-util

安裝 python-pip (3):

  1. sudo pacman -S python-pip

安裝 python-setuptools(3):

  1. sudo pacman -S python-setuptools

安裝 python 輪子工具 python-wheel (3):

  1. sudo pacman -S python-wheel

安裝壓縮工具 xz-utils:

  1. sudo pacman -S xz

安裝 file (linux 的 file 命令):

  1. sudo pacman -S file

安裝 west:

west 是 ZephyrOS 用來做 CI 的工具。

west 不能pip直接安裝,因為安裝的版本太老了,0.8.0的,而我們這裡需要用最新的版本(0.11.1),因此要拉取原始碼,自己安裝:

  1. git clone git@github.com:zephyrproject-rtos/west.git
  2. cd west
  3. git checkout v0.11.1
  4. sudo pip install . #一定要sudo

3、獲取 nRF Connect SDK 原始碼

nRF Connect SDK 包含 4 個 GitHub 倉庫,設定 nRF Connect SDK,需要將這些倉庫都拉取下來,並且安裝必要的 python modules。

  1. mkdir ncs
  2. cd ncs
  3. west init -m https://github.com/nrfconnect/sdk-nrf --mr master
  4. west update #clone the project repositories, long time
  5. west zephyr-export

操作完之後,NCS 的目錄結構如下:

  1. ncs
  2. |___ .west
  3. |___ bootloader
  4. |___ modules
  5. |___ nrf
  6. |___ nrfxlib
  7. |___ zephyr
  8. |___ ...

4、安裝 Python modules

  1. cd <sourcecode_root>/ncs
  2. pip3 install --user -r zephyr/scripts/requirements.txt
  3. pip3 install --user -r nrf/scripts/requirements.txt
  4. pip3 install --user -r bootloader/mcuboot/scripts/requirements.txt

5、安裝 toolchain

為了實現交叉編譯應用程式,我們需要下載安裝 GNU Arm Embedded Toolchain。

注意:一定要注意選擇的 Toolchain 的版本一定要 ≥ 9-2019-q4-major 版本要求。

設定 toolchain 一般需要如下步驟:

  • 下載
  • 解壓到 ~/gnuarmemb 下(推薦)
  • 加入環境變數
  1. #Install the GNU Arm Embedded Toolchain
  2. #https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads
  3. wget https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.07/gcc-arm-none-eabi-10.3-2021.07-x86_64-linux.tar.bz2
  4. tar xvf gcc-arm-none-eabi-10.3-2021.07-x86_64-linux.tar.bz2
  5. mv gcc-arm-none-eabi-10.3-2021.07 gnuarmemb
  6. mv gnuarmemb ~/
  7. export ZEPHYR_TOOLCHAIN_VARIANT=gnuarmemb
  8. export GNUARMEMB_TOOLCHAIN_PATH="~/gnuarmemb"

6、下載 nRF Command Line Tools

The nRF Command Line Tools is used for development, programming and debugging of Nordic Semiconductor's nRF51, nRF52, nRF53 and nRF91 Series devices.

主要功能:

  • nrfjprog executable - tool for programming through SEGGER J-LINK programmers and debuggers
  • mergehex executable - enables you to combine up to three .HEX files into one single file
  • nrfjprog DLL - a DLL that exports functions for programming and controlling nRF51, nRF52, nRF53 and nRF91 Series devices and lets developers create their own development tools using the DLLs API
  • SEGGER J-Link software and documentation pack

我們主要用其 nrfjprog 燒寫韌體。

https://www.nordicsemi.com/Products/Development-tools/nRF-Command-Line-Tools/Download#infotabs

nordic 將其工具弄個俄羅斯套娃,大家直接看命令:

  1. #Install nRF-command-Line-Tools
  2. wget https://www.nordicsemi.com/-/media/Software-and-other-downloads/Desktop-software/nRF-command-line-tools/sw/Versions-10-x-x/10-13-0/nRF-Command-Line-Tools_10_13_0_Linux64.zip
  3. unzip nRF-Command-Line-Tools_10_13_0_Linux64.zip
  4. cd nRF-Command-Line-Tools_10_13_0_Linux64
  5. tar -xzf nRF-Command-Line-Tools_10_13_0_Linux-amd64.tar.gz
  6. tar xvf nRF-Command-Line-Tools_10_13_0.tar
  7. mv nrfjprog ~/
  8. export PATH=${HOME}/nrfjprog:"$PATH"

注:可能會報下面的錯誤:

-- west flash: using runner nrfjprog

ERROR: JLinkARM DLL is invalid. Please reinstall latest JLinkARM DLL.

FATAL ERROR: command exited with status 31: nrfjprog --ids

make[3]: *** [zephyr/cmake/flash/CMakeFiles/flash.dir/build.make:72: zephyr/cmake/flash/CMakeFiles/flash] Error 31

make[2]: *** [CMakeFiles/Makefile2:4072: zephyr/cmake/flash/CMakeFiles/flash.dir/all] Error 2

make[1]: *** [CMakeFiles/Makefile2:4079: zephyr/cmake/flash/CMakeFiles/flash.dir/rule] Error 2

make: *** [Makefile:878: flash] Error 2

需要安裝 Jlink:yaourt -S jlink (網速要好!!!)

make flash 時報錯

-- runners.nrfjprog: Flashing file: /home/btfz/Desktop/apple/ncs/nrf/samples/bluetooth/peripheral_bms/build/zephyr/zephyr.hex

ERROR: JLinkARM DLL reported an error. Try again. If error condition

ERROR: persists, run the same command again with argument --log, contact Nordic

ERROR: Semiconductor and provide the generated log.log file to them.

FATAL ERROR: command exited with status 33: nrfjprog --program /home/btfz/Desktop/apple/ncs/nrf/samples/bluetooth/peripheral_bms/build/zephyr/zephyr.hex --sectoranduicrerase -f NRF52 --snr 682428062

make[3]: *** [zephyr/cmake/flash/CMakeFiles/flash.dir/build.make:72: zephyr/cmake/flash/CMakeFiles/flash] Error 33

make[2]: *** [CMakeFiles/Makefile2:4072: zephyr/cmake/flash/CMakeFiles/flash.dir/all] Error 2

make[1]: *** [CMakeFiles/Makefile2:4079: zephyr/cmake/flash/CMakeFiles/flash.dir/rule] Error 2

make: *** [Makefile:878: flash] Error 2

需要用 sudo 許可權執行 nrfjprog --program /home/btfz/Desktop/apple/ncs/nrf/samples/bluetooth/peripheral_bms/build/zephyr/zephyr.hex --sectoranduicrerase -f NRF52 --snr 682428062

7、編譯執行

  1. ncs cd nrf/samples/bluetooth/peripheral_bms/
  2. peripheral_bms git:(master) source ../../../../zephyr/zephyr-env.sh
  3. peripheral_bms git:(master) rm -rf build && mkdir build && cd build && cmake -DBOARD=nrf52dk_nrf52832 .. && make
  4. ...
  5. [ 94%] Building C object modules/segger/CMakeFiles/modules__segger.dir/home/btfz/Desktop/apple/ncs/modules/debug/segger/SEGGER/SEGGER_RTT.c.obj
  6. [ 94%] Building C object modules/segger/CMakeFiles/modules__segger.dir/SEGGER_RTT_zephyr.c.obj
  7. [ 95%] Linking C static library libmodules__segger.a
  8. [ 95%] Built target modules__segger
  9. [ 95%] Building C object zephyr/CMakeFiles/zephyr_prebuilt.dir/misc/empty_file.c.obj
  10. [ 96%] Linking C executable zephyr_prebuilt.elf
  11. Logical command for additional byproducts on target: zephyr_prebuilt
  12. [ 96%] Built target zephyr_prebuilt
  13. Scanning dependencies of target linker_zephyr_final_script_target
  14. [ 96%] Generating linker.cmd
  15. [ 96%] Built target linker_zephyr_final_script_target
  16. [ 96%] Generating isr_tables.c, isrList.bin
  17. [ 97%] Generating dev_handles.c
  18. [ 97%] Building C object zephyr/CMakeFiles/zephyr_final.dir/misc/empty_file.c.obj
  19. [ 98%] Building C object zephyr/CMakeFiles/zephyr_final.dir/isr_tables.c.obj
  20. [ 98%] Building C object zephyr/CMakeFiles/zephyr_final.dir/dev_handles.c.obj
  21. [100%] Linking C executable zephyr.elf
  22. Memory region Used Size Region Size %age Used
  23. FLASH: 168248 B 512 KB 32.09%
  24. SRAM: 29325 B 64 KB 44.75%
  25. IDT_LIST: 0 GB 2 KB 0.00%
  26. Generating files from zephyr.elf for board: nrf52dk_nrf52832
  27. [100%] Built target zephyr_final

8、燒寫

燒寫直接用 sudo make flash,注意一定要用 sudo,此外,在安裝 west 的時候也一定要用 sudo,否則會報如下錯誤:

  1. /usr/bin/python3.9: No module named west
  2. make[3]: *** [zephyr/cmake/flash/CMakeFiles/flash.dir/build.make:72: zephyr/cmake/flash/CMakeFiles/flash] Error 1
  3. make[2]: *** [CMakeFiles/Makefile2:4072: zephyr/cmake/flash/CMakeFiles/flash.dir/all] Error 2
  4. make[1]: *** [CMakeFiles/Makefile2:4079: zephyr/cmake/flash/CMakeFiles/flash.dir/rule] Error 2
  5. make: *** [Makefile:878: flash] Error 2

9、全自動環境構建指令碼

以我的風格,必須要寫一個自動化構建環境指令碼:(執行該指令碼時,務必保證網速到位!!!)

  1. #!/bin/bash
  2. #https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/gs_installing.html#id10
  3. CUR_PATH=$(cd `dirname $0`; pwd)
  4. TOOLS_PATH=$CUR_PATH/tools
  5. NCS_PATH=$CUR_PATH/ncs
  6. mkdir $TOOLS_PATH
  7. #Install the required tools
  8. sudo pacman -S git cmake ninja gperf ccache dfu-util dtc wget \
  9. python-pip python-setuptools python-wheel tk xz file make cmake
  10. cmake --version
  11. dtc --version
  12. python3 --version
  13. #To install the GN tool, complete the following steps:
  14. mkdir $TOOLS_PATH/gn && cd $TOOLS_PATH/gn
  15. wget -O gn.zip https://chrome-infra-packages.appspot.com/dl/gn/gn/linux-amd64/+/latest
  16. unzip gn.zip
  17. rm gn.zip
  18. export PATH=$TOOLS_PATH/gn:"$PATH"
  19. #Install west
  20. mkdir $TOOLS_PATH/west && cd $TOOLS_PATH/west
  21. git clone git@github.com:zephyrproject-rtos/west.git
  22. cd west
  23. git checkout v0.11.1
  24. sudo pip install . ###mast sudo
  25. #Install the GNU Arm Embedded Toolchain
  26. #https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads
  27. cd $TOOLS_PATH
  28. wget https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.07/gcc-arm-none-eabi-10.3-2021.07-x86_64-linux.tar.bz2
  29. tar xvf gcc-arm-none-eabi-10.3-2021.07-x86_64-linux.tar.bz2
  30. mv gcc-arm-none-eabi-10.3-2021.07 gnuarmemb
  31. export ZEPHYR_TOOLCHAIN_VARIANT=gnuarmemb
  32. export GNUARMEMB_TOOLCHAIN_PATH=$TOOLS_PATH/gnuarmemb
  33. #Install nRF-command-Line-Tools
  34. cd $TOOLS_PATH
  35. wget https://www.nordicsemi.com/-/media/Software-and-other-downloads/Desktop-software/nRF-command-line-tools/sw/Versions-10-x-x/10-13-0/nRF-Command-Line-Tools_10_13_0_Linux64.zip
  36. unzip nRF-Command-Line-Tools_10_13_0_Linux64.zip
  37. cd nRF-Command-Line-Tools_10_13_0_Linux64
  38. tar -xzf nRF-Command-Line-Tools_10_13_0_Linux-amd64.tar.gz
  39. tar xvf nRF-Command-Line-Tools_10_13_0.tar
  40. mv nrfjprog ../
  41. export PATH=$TOOLS_PATH/nrfjprog:"$PATH"
  42. #Install JLink
  43. tar zxvf JLink_Linux_V750a_x86_64.tgz
  44. mv JLink_Linux_V750a_x86_64 ../JLink
  45. export PATH=$TOOLS_PATH/JLink:"$PATH"
  46. cd ..
  47. rm -rf nRF-Command-Line-Tools_10_13_0_Linux64
  48. #Get the nRF Connect SDK code
  49. mkdir $NCS_PATH && cd $NCS_PATH
  50. west init -m https://github.com/nrfconnect/sdk-nrf --mr master
  51. west update
  52. west zephyr-export
  53. tree -L 1
  54. #Install additional Python dependencies
  55. pip3 install --user -r zephyr/scripts/requirements.txt
  56. pip3 install --user -r nrf/scripts/requirements.txt
  57. pip3 install --user -r bootloader/mcuboot/scripts/requirements.txt

連結


: ** 我做了一系列全自動構建編譯環境的工程,目前可以覆蓋:51微控制器、stm8、esp8266、esp32、nrf51822、nrf52832、android,歡迎到我的 nbtool github 下交流:https://github.com/nbtool **