nRF52をHCIデバイスとして使用する
#Bluetooth
RF-BM-ND04I というモジュールがあったのでこれで試してみる
https://www.rfstariot.com/uploadfile/downloads/RF-BM-ND04_ND04I%20Hardware%20Datasheet%20V1.2_20230526.pdf
nRF52832搭載のモジュールだったらだいたい同じ手順でいけるはず
SWDデバッガとしてRPi4を用いる
RPi3でも同じように動くはず
openocdというホスト名でsshログインできるように~/.ssh/configが書いてある前提ですすめる
作業用マシンはDebian12
ツールチェーンのインストール
Debianマシンにはチュートリアル通りいれていく
https://docs.zephyrproject.org/latest/develop/getting_started/index.html#install-dependencies
DebianもUbuntuと同じ手順でokだった
RPi4にはOpenOCDをいれておく
結線
RPi4 23 (GPIO 11) -> RF-BM-ND04I SWCLK
RPi4 22 (GPIO 25) -> RF-BM-ND04I SWDIO
RPi4 1 (3v3 Power) -> RF-BM-ND04I 3.3V
RPi4 6 (GND) -> RF-BM-ND04I GND
RPi4のGNDと5v Powerが近いので誤接続に注意する
OpenOCDのcfg
以下の内容でRPi4のホームディレクトリに置く
code:nrf52.cfg
bindto 0.0.0.0
adapter driver bcm2835gpio
# Raspi1 peripheral_base address
# bcm2835gpio_peripheral_base 0x20000000
# Raspi2 and Raspi3 peripheral_base address
bcm2835gpio peripheral_base 0xfe000000
# Raspi1 BCM2835: (700Mhz)
# bcm2835gpio_speed_coeffs 113714 28
# Raspi2 BCM2836 (900Mhz):
# bcm2835gpio_speed_coeffs 146203 36
# Raspi3 BCM2837 (1200Mhz):
bcm2835gpio speed_coeffs 236181 60
# pin 23 22
adapter gpio swclk 11
adapter gpio swdio 25
transport select swd
set CHIPNAME nrf52832
source find target/nrf52.cfg
# Uncomment & lower speed to address errors
# adapter_khz 1000
init
targets
reset halt
OpenOCDのアタッチ確認
code:sh
ssh pi@openocd.lan sudo openocd -f nrf52.cfg
以下のように Info : Listening on port 4444 for telnet connections が出てきたらSWDの接続まで成功している
code:txt
$ ssh pi@openocd.lan sudo openocd -f nrf52.cfg
Open On-Chip Debugger 0.12.0
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : BCM2835 GPIO JTAG/SWD bitbang driver
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x2ba01477
Info : nrf52832.cpu Cortex-M4 r0p1 processor detected
Info : nrf52832.cpu target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for nrf52832.cpu on 3333
Info : Listening on port 3333 for gdb connections
nrf52832.cpu halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000718 msp: 0x200015c0
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
まずはLチカ
Getting Started Guideに載っている以下の手順をやっていく
https://docs.zephyrproject.org/latest/develop/getting_started/index.html#build-the-blinky-sample
nRF52832のGPIOがどのくらい電流流せるかわかんなかったのでLEDのかわりにオシロで観察した
ボードはいったんnrf52dk/nrf52832として進める
ビルド
code:sh
west build -p always -b nrf52dk/nrf52832 samples/basic/blinky
ビルドされたelfを指定してgdbを起動する
code:sh
gdb-multiarch zephyr/build/zephyr/zephyr.elf
gdbからアタッチしてファームウェアを書き込む
code:gdb
target remote openocd.lan:3333
monitor reset init
load
monitor reset run
なんか書き込みできてるっぽい
code:txt
(gdb) target remote openocd.lan:3333
Remote debugging using openocd.lan:3333
z_arm_reset () at /home/miminashi/zephyrproject/zephyr/arch/arm/core/cortex_m/reset.S:102
102 bl soc_reset_hook
(gdb) monitor reset init
nrf52832.cpu halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000718 msp: 0x200015c0
(gdb) load
Loading section rom_start, size 0xdc lma 0x0
Loading section text, size 0x4228 lma 0xdc
Loading section initlevel, size 0x40 lma 0x4304
Loading section device_area, size 0x3c lma 0x4344
Loading section sw_isr_table, size 0x138 lma 0x4380
Loading section gpio_driver_api_area, size 0x24 lma 0x44b8
Loading section clock_control_driver_api_area, size 0x1c lma 0x44dc
Loading section uart_driver_api_area, size 0xc lma 0x44f8
Loading section rodata, size 0x1a0 lma 0x4510
Loading section datas, size 0x84 lma 0x46b8
Loading section device_states, size 0x6 lma 0x473c
Loading section .last_section, size 0x4 lma 0x4742
Start address 0x00000718, load size 18226
Transfer rate: 30 KB/sec, 1402 bytes/write.
(gdb) monitor reset run
nrf52dk/nrf52832のdtsを見た感じ、17番ピンがLED 0になっていそうなので、RF-BM-ND04IのP17にオシロスコープをあててみる
https://github.com/zephyrproject-rtos/zephyr/blob/0c0644bd68176512b9d2718f95bfbb697916155c/boards/nordic/nrf52dk/nrf52dk_nrf52832.dts#L30
HCI UART サンプルコードを動かす
https://docs.zephyrproject.org/latest/samples/bluetooth/hci_uart/README.html
Hardware Flow Control (RTS/CTS) enabled
と書いてあるけど、手元の環境でハードウェアフロー制御が使えるのかどうかをまず調べる必要がありそう
参考
https://chatgpt.com/share/67900a4a-5b94-8010-9569-ff5745c90784