0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SWDでArduinoデバッグ

0
Last updated at Posted at 2026-03-04

概要

これを使って、Arduino Uno R4 MINIMAでgdbによるデバッグを行う。
ホストPCはUbuntu24.04

環境設定

ubuntu24.04では、arm-none-eabi-gdbがgdb-multiarchになっているので、
以下でgdbをインストールする。

$ sudo apt install gdb-multiarch

上記を参照して、Arduino CLIは以下でインストールする。
今回の例ではインストール先はhomeのbinを指定する。

$ curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=~/bin sh

Uno R4 MINIMAのRenesasコアのインストール

$arduino-cli core update-index
Downloading index: package_index.tar.bz2 downloaded

$arduino-cli core install arduino:renesas_uno
Tool arduino:arm-none-eabi-gcc@7-2017q4 already installed
Tool arduino:bossac@1.9.1-arduino5 already installed
Tool arduino:dfu-util@0.11.0-arduino5 already installed
Tool arduino:openocd@0.11.0-arduino2 already installed
Tool builtin:dfu-discovery@0.1.2 already installed
Downloading packages...
arduino:renesas_uno@1.5.3 downloaded                                            
Replacing platform arduino:renesas_uno@1.5.2 with arduino:renesas_uno@1.5.3...
Uninstalling arduino:renesas_uno@1.5.2...
Running pre_uninstall script....
Platform arduino:renesas_uno@1.5.2 uninstalled
Uninstalling arduino:arm-none-eabi-gcc@7-2017q4, tool is no more required...
Uninstalling arduino:bossac@1.9.1-arduino5, tool is no more required...
Uninstalling arduino:dfu-util@0.11.0-arduino5, tool is no more required...
Uninstalling arduino:openocd@0.11.0-arduino2, tool is no more required...
Uninstalling builtin:dfu-discovery@0.1.2, tool is no more required...
Configuring platform....

You might need to configure permissions for uploading.
To do so, run the following command from the terminal:
sudo "/home/yopen/.arduino15/packages/arduino/hardware/renesas_uno/1.5.3/post_install.sh"


Platform arduino:renesas_uno@1.5.3 installed

$arduino-cli board listall | grep R4
Arduino Nano R4                             arduino:renesas_uno:nanor4
Arduino UNO R4 Minima                       arduino:renesas_uno:minima
Arduino UNO R4 WiFi                         arduino:renesas_uno:unor4wifi

サンプルをビルド

ビルド対象のinoのあるディレクトリで
$mkdir build
$arduino-cli compile   --fqbn arduino:renesas_uno:minima  --build-path build --build-property compiler.cpp.extra_flags="-g -O0"   --build-property compiler.c.extra_flags="-g -O0" --verbose

build/*.ino.elfができたらOK

デバッグイメージの書き込み

ボードのRESETを2回押してLEDがゆるく点滅したら、
$arduino-cli upload --fqbn arduino:renesas_uno:minima
dfu-util 0.11-arduino4

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2021 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

Opening DFU capable USB device...
Device ID 2341:0369
Device DFU version 0101
Claiming USB DFU Interface...
Setting Alternate Interface #0 ...
Determining device status...
DFU state(2) = dfuIDLE, status(0) = No error condition is present
DFU mode device DFU version 0101
Device returned transfer size 64
Copying data from PC to DFU device
Download	[=========================] 100%        39344 bytes
Download done.
DFU state(7) = dfuMANIFEST, status(0) = No error condition is present
DFU state(2) = dfuIDLE, status(0) = No error condition is present
Done!
New upload port:  (default)

なお、arduino-cliでは書き込むelfを指定できない。直前にビルドした内容を自動で使うので、連続して行うこと。
何故、elfを指定できないんだろう。。

書き込んだのと同じelfでGDBを実行

$gdb-multiarch ./build/0304.ino.elf 
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./build/sample.ino.elf...

gdbの中で以下を実行する

(gdb) target extended-remote /dev/ttyACM0
Remote debugging using /dev/ttyACM0

(gdb) monitor swdp_scan
Target voltage: 4.4V
Available Targets:
No. Att Driver
 1      ARM Cortex-M

(gdb) attach 1
Attaching to program: path/build/sample.ino.elf, Remote target
warning: while parsing target memory map (at line 1): Required element <memory> is missing
0x0000961e in IRQManager::set_sci_tx_link_event (this=0x0, li=0, ch=0)
    at /home/USER/.arduino15/packages/arduino/hardware/renesas_uno/1.5.3/cores/arduino/IRQManager.cpp:1577
1577	    else if(ch == 2) {  R_ICU->IELSR[li] = BSP_PRV_IELS_ENUM(EVENT_SCI2_TXI);}

(gdb) bt
#0  0x0000961e in IRQManager::set_sci_tx_link_event (this=0x0, li=0, ch=0)
    at /home/USER/.arduino15/packages/arduino/hardware/renesas_uno/1.5.3/cores/arduino/IRQManager.cpp:1577

#1  0x00009624 in IRQManager::set_sci_tx_link_event (this=0x735, li=1889, ch=7489)
    at /home/USER/.arduino15/packages/arduino/hardware/renesas_uno/1.5.3/cores/arduino/IRQManager.cpp:1577
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) info registers
r0             0x7d0               2000
r1             0x10b               267
r2             0x800               2048
r3             0x5ab               1451
r4             0x20000950          536873296
r5             0xafc80             720000
r6             0x7d0               2000
r7             0x40046f00          1074032384
r8             0x200010dc          536875228
r9             0x6                 6
r10            0x1                 1
r11            0x4b08ca90          1258867344
r12            0x2                 2
sp             0x20007ec8          0x20007ec8 <g_main_stack+968>
lr             0x9625              0x9625 <IRQManager::set_sci_tx_link_event(int, int)+68>
pc             0x961e              0x961e <IRQManager::set_sci_tx_link_event(int, int)+62>
xpsr           0x81000000          -2130706432
fpscr          0x60000010          1610612752
msp            0x20007ec8          0x20007ec8 <g_main_stack+968>
psp            0xb10c34e0          0xb10c34e0
special        0x4000000           67108864

無事つながった。割り込みの中で止まっている?

(gdb) break main
(gdb) break setup
(gdb) info breakpoints
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x0000413c in main 
                                           at /home/USER/.arduino15/packages/arduino/hardware/renesas_uno/1.5.3/variants/MINIMA/tmp_gen_c_files/main.c:5
2       breakpoint     keep y   0x00004104 in setup() at /path/sample.ino:28

ブレークポイントもセットできた。
ボードのRESETを押したらmain()やsetup()で止まるかなと思ったが、来ない。
調査中。

トラブルシュート

/dev/ttyACMが認識されない

MINIMAに何を書いたかが定かではないが(多分Zephyr RTOS)、USB type-cをつないでも、デバイスが認識されなかった。
これを復旧した手順。
ボードのResetを2回押して、オレンジのLEDが弱く点滅する書き込みモードにして、
スケッチを書き込もうとすると、

dfu-util: Cannot open DFU device 2341:0069 found on devnum 10 (LIBUSB_ERROR_ACCESS) 
dfu-util: No DFU capable USB device available Failed uploading: uploading error: exit status 74

こんな感じのエラーが出た。
ユーザはdialoutグループに入っているので、以下の手順でudevルールを追加して、復旧した

udevルールを追加する(ファイルを新設する)

$ sudo nano /etc/udev/rules.d/60-arduino-dfu.rules

SUBSYSTEM=="usb", ATTR{idVendor}=="2341", MODE="0666"
を追記して、Ctrl + s -> Ctrl + x
上記の2341はボードを接続したときのlsusbで、

Bus 003 Device 013: ID 2341:0069 Arduino SA UNO R4 Minima

のIDで調べた。

ルールを適用する

$ sudo udevadm control --reload-rules
$ sudo udevadm trigger

ボードをType-cケーブルで接続して、RESETを2回押して、LEDがゆっくり点滅することを確認する。
Aruduino IDEのBoadr Managerで、ポートが認識されていることを確認して、適当なSampleのスケッチを書き込む。

これで復旧した。

Invalid FQBN: board arduino:renesas_uno:unor4minima not found

$arduino-cli compile   --fqbn arduino:renesas_uno:unor4minima   --build-property compiler.cpp.extra_flags="-g -O0"   --build-property compiler.c.extra_flags="-g -O0" --verbose
Error during build: Invalid FQBN: board arduino:renesas_uno:unor4minima not found

デバッグ用にinoをコンパイルすると、
FQBN(Fully Qualified Board Name)が変わった/未インストール
のエラーが出た

現在のコアを確認する

$arduino-cli core list
ID                  インストール済          Latest           Name
arduino:avr         1.8.7            1.8.7            Arduino AVR Boards
arduino:esp32       2.0.18-arduino.5 2.0.18-arduino.5 Arduino ESP32 Boards
arduino:megaavr     1.8.8            1.8.8            Arduino megaAVR Boards
arduino:renesas_uno 1.5.3            1.5.3            Arduino UNO R4 Boards
arduino:zephyr      0.53.1           0.53.1           Arduino UNO Q Board
esp32:esp32         3.3.7            3.3.7            esp32
m5stack:esp32       3.2.5            3.2.5            M5Stack
Seeeduino:avr       1.3.0            1.3.0            Seeeduino AVR

arduino:renesas_uno があるのでOK

次に、FQBNを確認する

$arduino-cli board listall | grep -i r4
Arduino Nano R4                             arduino:renesas_uno:nanor4
Arduino UNO R4 Minima                       arduino:renesas_uno:minima

minimaが正解なので、改めてコンパイルするとできた。

$arduino-cli compile   --fqbn arduino:renesas_uno:minima   --build-property compiler.cpp.extra_flags="-g -O0"   --build-property compiler.c.extra_flags="-g -O0" --verbose
FQBN: arduino:renesas_uno:minima
Using board 'minima' from platform in folder: /home/USER/.arduino15/packages/arduino/hardware/renesas_uno/1.5.3

なお、デフォルトのパスでビルドすると、$USER以下のテンポラリに生成物ができるので、「--build-path」でカレントのビルド用ディレクトリを指定すると良い。

monitor swdp_scanがfail

(gdb) target extended-remote /dev/ttyACM0 Remote debugging using /dev/ttyACM0 
(gdb) monitor swdp_scan 
Target voltage: 2.8V 
SW-DP scan failed!

これはSWDのコネクタを挿す方向が左右逆。コネクタに出っ張りがある方が外側になる。
入れ替えると以下の通り成功する。

(gdb) target extended-remote /dev/ttyACM1
Remote debugging using /dev/ttyACM1
(gdb) monitor swdp_scan
Target voltage: 4.4V
Available Targets:
No. Att Driver
 1      ARM Cortex-M
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?