本文講述如何在 ubuntu 22.04 下開發 nRF52832.
host 環境說明:
$ uname -a
Linux leo 6.8.0-60-generic #63~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Apr 22 19:00:15 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
1. 安裝軟件
sudo apt install gcc-arm-none-eabisudo apt-get install build-essential checkinstall
2. nRF5 SDK
1. 下載 SDK
Nordic官方對nRF51、nRF52系列提供了SDK,SDK中提供了大量的BLE和ANT示例。
1. SDK 15.3 版本
下載 15.3版本
將其解壓:
leo@leo:~/data_4t/nrf_project/nrf5sdk153059ac345/nRF5_SDK_15.3.0_59ac345$ tree -L 2
.
├── components
│ ├── 802_15_4
│ ├── ant
│ ├── ble
│ ├── boards
│ ├── drivers_ext
│ ├── drivers_nrf
│ ├── iot
│ ├── libraries
│ ├── nfc
│ ├── proprietary_rf
│ ├── sdk_validation.h
│ ├── serialization
│ ├── softdevice
│ └── toolchain
├── config
│ ├── nrf52810
│ ├── nrf52811
│ ├── nrf52832
│ └── nrf52840
├── documentation
│ ├── index.html
│ ├── licenses.html
│ ├── licenses.txt
│ ├── NordicS.jpg
│ ├── nRF5_Garmin_Canada_license.txt
│ ├── nRF5_Nordic_license.txt
│ ├── nRF5x_series_logo.png
│ └── release_notes.txt
├── examples
│ ├── 802_15_4
│ ├── ant
│ ├── ble_central
│ ├── ble_central_and_peripheral
│ ├── ble_peripheral
│ ├── connectivity
│ ├── crypto
│ ├── dfu
│ ├── dtm
│ ├── iot
│ ├── multiprotocol
│ ├── nfc
│ ├── peripheral
│ ├── proprietary_rf
│ ├── readme.txt
│ └── usb_drivers
├── external
│ ├── cifra_AES128-EAX
│ ├── cJSON
│ ├── fatfs
│ ├── fnmatch
│ ├── fprintf
│ ├── freertos
│ ├── infineon
│ ├── licenses_external.txt
│ ├── lwip
│ ├── mbedtls
│ ├── micro-ecc
│ ├── nano
│ ├── nano-pb
│ ├── nfc_adafruit_library
│ ├── nrf_cc310
│ ├── nrf_cc310_bl
│ ├── nrf_oberon
│ ├── nrf_tls
│ ├── protothreads
│ ├── segger_rtt
│ ├── thedotfactory_fonts
│ └── utf_converter
├── external_tools
│ └── cmsisconfig
├── integration
│ └── nrfx
├── license.txt
├── modules
│ └── nrfx
├── nRF5x_MDK_8_24_1_IAR_NordicLicense.msi
└── nRF5x_MDK_8_24_1_Keil4_NordicLicense.msi64 directories, 14 files
2. SDK 17.1.0 版本
現在官網上最新的是 17.1.0 版本:
- 下載
nRF5_SDK_17.1.0_ddde560$ tree -L 2
.
├── components
│ ├── 802_15_4
│ ├── ant
│ ├── ble
│ ├── boards
│ ├── drivers_ext
│ ├── drivers_nrf
│ ├── libraries
│ ├── nfc
│ ├── proprietary_rf
│ ├── sdk_validation.h
│ ├── serialization
│ ├── softdevice
│ └── toolchain
├── config
│ ├── nrf52810
│ ├── nrf52811
│ ├── nrf52820
│ ├── nrf52832
│ ├── nrf52833
│ └── nrf52840
├── documentation
│ ├── index.html
│ ├── licenses.html
│ ├── licenses.txt
│ ├── NordicS.jpg
│ ├── nRF5_Garmin_Canada_license.txt
│ ├── nRF5_Nordic_license.txt
│ ├── nRF5x_series_logo.png
│ └── release_notes.txt
├── examples
│ ├── 802_15_4
│ ├── ant
│ ├── ble_central
│ ├── ble_central_and_peripheral
│ ├── ble_peripheral
│ ├── connectivity
│ ├── crypto
│ ├── dfu
│ ├── dtm
│ ├── multiprotocol
│ ├── nfc
│ ├── peripheral
│ ├── proprietary_rf
│ ├── readme.txt
│ └── usb_drivers
├── external
│ ├── cifra_AES128-EAX
│ ├── cJSON
│ ├── fatfs
│ ├── fnmatch
│ ├── fprintf
│ ├── freertos
│ ├── infineon
│ ├── licenses_external.txt
│ ├── lwip
│ ├── mbedtls
│ ├── micro-ecc
│ ├── nano
│ ├── nano-pb
│ ├── nfc_adafruit_library
│ ├── nrf_cc310
│ ├── nrf_cc310_bl
│ ├── nrf_oberon
│ ├── nrf_tls
│ ├── protothreads
│ ├── segger_rtt
│ ├── thedotfactory_fonts
│ ├── tile
│ └── utf_converter
├── external_tools
│ └── cmsisconfig
├── integration
│ └── nrfx
├── license.txt
├── modules
│ └── nrfx
└── nRF_MDK_8_40_3_IAR_BSDLicense.msi65 directories, 13 files
2. 配置編譯環境
15.3 版本和 17.1.0 版本我都驗證了步驟是一樣的:
修改如下文件:
components/toolchain/gcc/Makefile.posix
// 15.3
GNU_INSTALL_ROOT ?= /usr/bin/
GNU_VERSION ?= 7.3.1
GNU_PREFIX ?= arm-none-eabi
// 17.1.0 components/toolchain/gcc/Makefile.posix
GNU_INSTALL_ROOT ?= /usr/bin/
GNU_VERSION ?= 9.3.1
GNU_PREFIX ?= arm-none-eabi
GNU_INSTALL_ROOT
: 主要配置交叉工具 按照目錄, 編譯時可以從當前目錄中找 gcc.
查看安裝目錄:
leo@leo:~$ which arm-none-eabi-gcc
/usr/bin/arm-none-eabi-gcc
3. 編譯
這里拿 一個官方跑馬燈的例子來驗證。
pca10040
: 我手里板子,對應官網的PCA10040 型號
examples/peripheral/blinky/pca10040/blank/armgcc$ tree
.
├── blinky_gcc_nrf52.ld
└── Makefile0 directories, 2 files
在當前目錄 make
examples/peripheral/blinky/pca10040/blank/armgcc$ make
mkdir _build
cd _build && mkdir nrf52832_xxaa
Assembling file: gcc_startup_nrf52.S
Compiling file: nrf_log_frontend.c
Compiling file: nrf_log_str_formatter.c
Compiling file: boards.c
Compiling file: app_error.c
Compiling file: app_error_handler_gcc.c
Compiling file: app_error_weak.c
Compiling file: app_util_platform.c
Compiling file: nrf_assert.c
Compiling file: nrf_atomic.c
Compiling file: nrf_balloc.c
Compiling file: nrf_fprintf.c
Compiling file: nrf_fprintf_format.c
Compiling file: nrf_memobj.c
Compiling file: nrf_ringbuf.c
Compiling file: nrf_strerror.c
Compiling file: nrfx_atomic.c
Compiling file: main.c
Compiling file: system_nrf52.c
Linking target: _build/nrf52832_xxaa.outtext data bss dec hex filename2760 112 172 3044 be4 _build/nrf52832_xxaa.out
Preparing: _build/nrf52832_xxaa.hex
Preparing: _build/nrf52832_xxaa.bin
DONE nrf52832_xxaa# 編譯結束leo@leo:~/data_4t/nrf_project/nrf5sdk153059ac345/nRF5_SDK_15.3.0_59ac345/examples/peripheral/blinky/pca10040/blank/armgcc$ tree -L 2
.
├── blinky_gcc_nrf52.ld
├── _build
│ ├── nrf52832_xxaa
│ ├── nrf52832_xxaa.bin
│ ├── nrf52832_xxaa.hex # 他就是我們要燒錄進板子的 文件
│ ├── nrf52832_xxaa.in
│ ├── nrf52832_xxaa.map
│ └── nrf52832_xxaa.out
└── Makefile2 directories, 7 files
同樣在 17.1.0 下的操作
leo@leo:~/data_4t/nrf_project/nRF5_SDK_17.1.0_ddde560/examples/peripheral/blinky/pca10040/blank/armgcc/_build$ tree -L 1
.
├── nrf52832_xxaa
├── nrf52832_xxaa.bin
├── nrf52832_xxaa.hex
├── nrf52832_xxaa.in
├── nrf52832_xxaa.map
└── nrf52832_xxaa.out1 directory, 5 files
3. 燒錄
1. 軟件安裝
下載 nRF-Command-Line-Tools_10_9_0_Linux-amd64
https://www.nordicsemi.com/Products/Development-tools/nrf-command-line-tools/download#infotabs
解壓:
nrfcommandlinetools1090linuxamd64$ tree
.
├── JLink_Linux_V680a_x86_64.deb
├── JLink_Linux_V680a_x86_64.tgz
├── nRF-Command-Line-Tools_10_9_0_Linux-amd64.deb
├── nRF-Command-Line-Tools_10_9_0.tar
└── README.txt0 directories, 5 files
安裝:
# 第一步
sudo dpkg -i JLink_Linux_V680a_x86_64.deb# 第二步
sudo dpkg -i nRF-Command-Line-Tools_10_9_0_Linux-amd64.deb
- 注意安裝先后順序
查看安裝后的版本:
$ nrfjprog -v
nrfjprog version: 10.9.0
JLinkARM.dll version: 6.80a$ JLinkExe -v
SEGGER J-Link Commander V6.80a (Compiled May 29 2020 16:30:49)
DLL version V6.80a, compiled May 29 2020 16:30:38
- 如果能看到上面的信息,表示下載環境已經ok 了。
2. 連接板子
1. 硬件接線:
我這里使用 Jlink-v9 仿真器 SWD 接口和板子進行連接:
2. 軟件側
執行如下命令 :
JLinkExe -device NRF52 -if SWD -speed 4000 -autoconnect 1
如果有如下輸出信息:表示已經識別到板子了。
SEGGER J-Link Commander V6.80a (Compiled May 29 2020 16:30:49)
DLL version V6.80a, compiled May 29 2020 16:30:38Connecting to J-Link via USB...O.K.
Firmware: J-Link V9 compiled May 7 2021 16:26:12
Hardware version: V9.70
S/N: 69706099
License(s): RDI, FlashBP, FlashDL, JFlash, GDB
VTref=3.261V
Device "NRF52" selected.Connecting to target via SWD
InitTarget() start
InitTarget() end
Found SW-DP with ID 0x2BA01477
DPIDR: 0x2BA01477
Scanning AP map to find all available APs
AP[2]: Stopped AP scan as end of AP map has been reached
AP[0]: AHB-AP (IDR: 0x24770011)
AP[1]: JTAG-AP (IDR: 0x02880000)
Iterating through AP map to find AHB-AP to use
AP[0]: Core found
AP[0]: AHB-AP ROM base: 0xE00FF000
CPUID register: 0x410FC241. Implementer code: 0x41 (ARM)
Found Cortex-M4 r0p1, Little endian.
FPUnit: 6 code (BP) slots and 2 literal slots
CoreSight components:
ROMTbl[0] @ E00FF000
ROMTbl[0][0]: E000E000, CID: B105E00D, PID: 000BB00C SCS-M7
ROMTbl[0][1]: E0001000, CID: B105E00D, PID: 003BB002 DWT
ROMTbl[0][2]: E0002000, CID: B105E00D, PID: 002BB003 FPB
ROMTbl[0][3]: E0000000, CID: B105E00D, PID: 003BB001 ITM
ROMTbl[0][4]: E0040000, CID: B105900D, PID: 000BB9A1 TPIU
ROMTbl[0][5]: E0041000, CID: B105900D, PID: 000BB925 ETM
Cortex-M4 identified.
J-Link>
3. 燒錄
1. 方式1: 使用 Jlink 進行燒錄
// 1. 首先輸入上面的命令 連接板子:
JLinkExe -device NRF52 -if SWD -speed 4000 -autoconnect 1// 2. 擦除之前的程序
J-Link> erase
Erasing device...
Comparing flash [100%] Done.
Erasing flash [100%] Done.
J-Link: Flash download: Total time needed: 0.026s (Prepare: 0.015s, Compare: 0.000s, Erase: 0.007s, Program: 0.000s, Verify: 0.000s, Restore: 0.003s)
Erasing done.// 3. 燒錄之前 編譯的 跑馬燈 程序
J-Link> loadfile nrf52832_xxaa.hex
Downloading file [nrf52832_xxaa.hex]...
Comparing flash [100%] Done.
Erasing flash [100%] Done.
Programming flash [100%] Done.
J-Link: Flash download: Bank 0 @ 0x00000000: 1 range affected (4096 bytes)
J-Link: Flash download: Total: 0.092s (Prepare: 0.026s, Compare: 0.002s, Erase: 0.000s, Program & Verify: 0.052s, Restore: 0.011s)
J-Link: Flash download: Program & Verify speed: 76 KB/s
O.K.// 4. 退出, 此時 程序就開始執行了。
J-Link>exit
2. 方式2: nrfjprog 燒錄
全片擦除 +下載+ 復位運行。
$ nrfjprog -f NRF52 --program ./IK-52832DK-V2-TEST.hex --chiperase -rParsing hex file.
Erasing user available code and UICR flash areas.
Applying system reset.
Checking that the area to write is not protected.
Programming device.
Applying system reset.
Run.
4. nrfjprog 命令介紹
nrfjprog 是使用 J-Link 仿真器對 nRF5x 系列 SoC 進行編程的命令行工具。
1. 全片擦除
執行全片擦除后,會擦除片內所有 Flash 和掉電保持的寄存器。
// 擦除
$ nrfjprog -f NRF52 -eErasing user available code and UICR flash areas.
Applying system reset.
2. 下載
nrfjprog 的命令是可以組合的,對于程序下載,可以使用下面兩種組合命令,注意他們
的區別。
1. 僅下載+復位運行:芯片必須是空的(擦除過的),否則下載會失敗
$ nrfjprog -f NRF52 --program ./IK-52832DK-V2-TEST.hex Parsing hex file.
Reading flash area to program to guarantee it is erased.
Checking that the area to write is not protected.
Programming device.
- 此時重新掉電啟動即可
2.全片擦除 +下載+ 復位運行
$ nrfjprog -f NRF52 --program ./IK-52832DK-V2-TEST.hex --chiperase -rParsing hex file.
Erasing user available code and UICR flash areas.
Applying system reset.
Checking that the area to write is not protected.
Programming device.
Applying system reset.
Run.
- 我一般情況會使用這種方式燒錄。
3.啟用回讀保護機制
nrfjprog -f NRF52 --rbp ALL
該命令執行后,無法使用仿真器通過 DEBUG 接口讀出芯片的數據,當我們發布產品的
時候,可以通過啟用回讀保護防止別人讀取芯片中燒寫的固件,從而保護產品的權益不被侵犯。
3. 讀出 SoftDevice 的 FWID
只有已經下載了 SoftDevice 的芯片才可以讀出 FWID。
- nRF51xx 系列命令:nrfjprog --family NRF51 --memrd 0x0000300C
- nRF52xx 系列命令:nrfjprog --family NRF52 --memrd 0x0000300C
$ nrfjprog --family NRF52 --memrd 0x0000300C
0x0000300C: FFFF00AF
- 所示,讀出的數據是:0xFFFF00AF,表示協議棧的 FWID 是 0xAF,根據 FWID 即可知道
協議棧的版本(FWID 在固件更新 DFU 中會用到)
4. 恢復
恢復:全片擦除并禁用回讀保護機制(如果啟用)
命令:nrfjprog -f NRF52 -recover
該命令執行后,會擦除片內所有 Flash 和掉電保持的寄存器。
5. ps
本文檔 所涉及的軟件包我已經打包,請點擊下載。
- nRF52832-ubuntu-開關環境資源打包
nrf-52832-env-soft$ tree
.
├── IK-52832DK-V2-TEST.hex
├── nrf52832_xxaa.hex
├── nrf5sdk153059ac345.zip
└── nrfcommandlinetools1090linuxamd64.tar.gz
$ md5sum nrf-52832-env-soft.7z
b61a6911bab6684f9721648740448926 nrf-52832-env-soft.7z