我的工作流程:在 Apple Silicon (M4 Pro) 設定 nRF52 DK 開發環境

Post Title Image (圖說:開箱 Apple MacBook Pro M4 ProreMarkable Paper Pro。拍攝於 AWS re:Invent 2024拉斯維加斯。圖片來源:Ernest)

今天花了一些時間在一台 macOS Sequoia 15.1.1 機器上設定 nRF52 DK (PCA10040) 開發板的開發環境,這台機器是 Apple Macbook Pro M4 Pro (Apple Silicon)。這篇部落格文章筆記了這個過程,留給自己以及同為使用 Nordic Semiconductor nRF52 DK 朋友們參考。

工作流程

以下是本文關於 nRF52 DK 開發環境設定過程的流程概念圖:

flowchart TD A[Start Setup] --> B[Install Prerequisites] B --> B1[Xcode Command Line Tools] B --> B2[Homebrew] B1 & B2 --> C[Install Core Tools] C --> C1[GNU Arm Embedded Toolchain] C --> C2[Install nrfutil Components] C --> C3[Install Optional Tools] C1 --> C1a[gcc-arm-embedded] C2 --> C2a[nrf5sdk-tools] C2 --> C2b[device] C2 --> C2c[completion] C3 --> C3a[OpenOCD] C1a & C2a & C2b & C2c & C3a --> F[Verify Installation] F --> F1[Verify arm-none-eabi-gcc] F --> F2[Verify nrfjprog] F --> F3[Check Device Connection] F1 & F2 & F3 --> G[Development Environment Ready] %% Set background color style C1 fill:#f9d5e5,stroke:#333,stroke-width:1px style C2 fill:#d5f9e5,stroke:#333,stroke-width:1px style C3 fill:#d5e5f9,stroke:#333,stroke-width:1px

這個流程圖展示了從 安裝先決條件 到 擁有開發環境 的過程。按照這個工作流程設定 nRF52 DK 開發環境。主要步驟包括安裝基本工具,然後是 Nordic 特定的開發工具,最後驗證安裝成功。


先決條件

在深入設定之前,先確保已安裝了這些必要的元件:

  • Xcode 指令列工具:透過 xcode-select --install 安裝
  • Homebrew:macOS 的套件管理器

安裝過程

GNU Arm Embedded Toolchain

homebrew 裡頭有一包打包好的 gcc-arm-embedded

brew install --cask gcc-arm-embedded

安裝後,檢查一下是否正確安裝:

$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (Arm GNU Toolchain 14.2.Rel1 (Build arm-14.52)) 14.2.1 20241119
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ make -v
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

This program built for i386-apple-darwin11.3.0

重要:不要安裝 arm-none-eabi-gcc。請安裝 gcc-arm-embedded,以避免兼容性問題。


nRF Util (nrfutil)

nRF Util 是一個指令列工具,整合了一些 Nordic 功能:

  1. Nordic Semiconductor nRF Util 官方網頁 下載最新版本,應該要大於 v7.0.0。目前這個步驟無法使用 homebrew 或 pip 完成,得要過去官網下載。(不要使用 pip 安裝,最多只能安裝到舊版本 v5.2.0。官方整個改寫了 nrfutil。)

  2. 然後使用這些指令進行設定:

$ cd Downloads
$ chmod +x nrfutil
$ ./nrfutil --version
$ sudo mv nrfutil /usr/local/bin

$ nrfutil --version
nrfutil 7.12.0 (39003c9 2024-06-10)
commit-hash: 39003c9d0e5d946af0f417cfadf4ac55a6d03c19
commit-date: 2024-06-10
host: aarch64-apple-darwin
build-timestamp: 2024-06-10T15:50:53.137997000Z
classification: nrf-external

接下來,使用 nrfutil 安裝其他元件:

$ nrfutil install nrf5sdk-tools
$ nrfutil install device
$ nrfutil install completion
$ nrfutil completion install zsh

也可以一次性安裝所有元件:

$ nrfutil install nrf5sdk-tools; nrfutil install device; nrfutil install completion; nrfutil completion install zsh

安裝後,將以下內容添加到 .zshrc 文件中以啟用指令自動完成:

# From nrfutil completion install
[[ -r "${HOME}/.nrfutil/share/nrfutil-completion/scripts/zsh/setup.zsh" ]] && . "${HOME}/.nrfutil/share/nrfutil-completion/scripts/zsh/setup.zsh"

# Initialize zsh completion system
autoload -Uz compinit
compinit

驗證是否安裝成功,應該要有 nrfjprog 工具可以使用:

$ nrfjprog -v
nrfjprog version: 10.24.2 external
JLinkARM.dll version: 8.16

檢查 nRF52 DK 裝置是否可被識別:

$ nrfutil device list
682629006
product         J-Link
board version   PCA10040
ports           /dev/tty.usbmodem0006826290061
traits          devkit, jlink, seggerUsb, serialPorts, usb

Found 1 supported device(s)

OpenOCD(Optional)

為了獲得 on-chip debugging 功能,可以安裝 OpenOCD 備用:

brew install open-ocd

關於 nRF SDK

nRF5 SDK v.s nRF Connect SDK

值得注意的是,nRF5 SDK 現在處於維護模式,Nordic Semiconductor 建議對新專案使用 nRF Connect SDK:

  • nRF Connect SDK 是一個統一的 SDK,橫跨基於 nRF52、nRF53、nRF70 和 nRF91 等系列裝置
  • 它將 Zephyr RTOS 與各種樣本、協議和驅動程式整合
  • 該 SDK 公開放在 GitHub 上,並支援免費的 nRF Connect for VS Code IDE
  • 👉 nRF Connect SDK and nRF5 SDK statement - Nordic Blog

我有幾個使用 nRF5 SDK 開發的現有專案,但未來可能需要研究一下 nRF Connect SDK。


nRF Util 的主要功能

nRF Util 提供了幾個有用的指令:

nrfutil nrf5sdk-tools 指令

  • 產生 Device firmware update (DFU) package
  • 加密金鑰管理
  • 產生 Bootloader 程式設定
  • 透過各種傳輸方式(BLE、UART、USB、Thread、Zigbee)執行 DFU 程式

nrfutil device 指令

  • Listing connected devices
  • Programming devices
  • Erasing and recovery operations
  • Various device management tasks

nrfutil completion 指令

透過 shell 中的指令自動完成功能,更方便使用 nrfutil 指令。


Deprecated Tools

nRF Command Line Tools 正在被歸檔 (archived) 並逐漸被新版本 nRF Util (> v7.0.0) 取代。雖然它們仍然可以透過以下方式安裝:

brew install --cask nordic-nrf-command-line-tools

但建議對所有新的開發工作使用新版本 nRF Util (> v7.0.0) 。


總結

安裝了這些工具後,我現在在 macOS (Apple Silicon) 上擁有了完整的(希望是這樣,哈哈) nRF52 DK 開發板開發環境。接下來可以重新把玩過往的專案。

nRF5 SDK 轉移到 nRF Connect SDK 代表 Nordic 開發生態系統的重要轉變,提供更好的整合、更多功能和更廣泛的裝置支援。對於任何新專案,我將按照 Nordic 的建議使用 nRF Connect SDK 而不是較舊的 nRF5 SDK。因為我有一些使用 nRF5 SDK 的現有專案,看來未來得花些時間研究 nRF Connect SDK。