Serial Wire Debug (SWD) using OpenOCD on a Raspberry Pi 4 (Pi4)

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

[Update 20210413: All issues resolved. See below.]

 

This experiment ran OpenOCD on a Pi4 under Ubuntu 20.10.

Source code for OpenOCD compiled with no errors on 20210411.

Done with all recent updates applied before compiling.

The Pi4 runs faster than earlier versions, so changed two config files:

 

  usr/local/share/openocd/scripts/interface/raspberrypi4-native.cfg

  /home/pi/bootloader/openocd.cfg

 

Connected the Pi4 to a target board, and ran OpenOCD as follows:

 

  cd /home/pi/bootloader
  sudo openocd

 

 

On the first run, the output hung:

 

  pi@ubuntu:~/bootloader$ sudo openocd
  Open On-Chip Debugger 0.11.0+dev-00069-g68e50415a-dirty (2021-04-10-15:03)
  Licensed under GNU GPL v2
  For bug reports, read
      http://openocd.org/doc/doxygen/b...
  Info : BCM2835 GPIO JTAG/SWD bitbang driver
  Info : clock speed 400 kHz
  Info : SWD DPIDR 0x0bc11477

  Info : Listening on port 6666 for tcl connections
  Info : Listening on port 4444 for telnet connections
  ^Cshutdown command invoked  

 

 

On a second run, it hung differently:

 

  pi@ubuntu:~/bootloader$ sudo openocd
  Open On-Chip Debugger 0.11.0+dev-00069-g68e50415a-dirty (2021-04-10-15:03)
  Licensed under GNU GPL v2
  For bug reports, read
      http://openocd.org/doc/doxygen/b...
  Info : BCM2835 GPIO JTAG/SWD bitbang driver
  Info : clock speed 400 kHz
  Info : SWD DPIDR 0x0bc11477
  Info : at91samd21g18.cpu: hardware has 4 breakpoints, 2 watchpoints
  Info : starting gdb server for at91samd21g18.cpu on 3333
  Info : Listening on port 3333 for gdb connections
  target halted due to debug-request, current mode: Thread
  xPSR: 0x81000000 pc: 0x00000294 msp: 0x20002de0
  Info : Listening on port 6666 for tcl connections
  Info : Listening on port 4444 for telnet connections
  ^Cshutdown command invoked  

 

 

On a third run, it hung in yet another way:

 

  pi@ubuntu:~/bootloader$ sudo openocd
  Open On-Chip Debugger 0.11.0+dev-00069-g68e50415a-dirty (2021-04-10-15:03)
  Licensed under GNU GPL v2
  For bug reports, read
      http://openocd.org/doc/doxygen/b...
  Info : BCM2835 GPIO JTAG/SWD bitbang driver
  Info : clock speed 400 kHz
  Info : SWD DPIDR 0x0bc11477
  Info : at91samd21g18.cpu: hardware has 4 breakpoints, 2 watchpoints
  Info : starting gdb server for at91samd21g18.cpu on 3333
  Info : Listening on port 3333 for gdb connections
  Error: DP initialisation failed
  Info : SWD DPIDR 0x0bc11477
  target halted due to debug-request, current mode: Thread
  xPSR: 0x61000000 pc: 0x00000294 msp: 0x20002de0
  Polling target at91samd21g18.cpu failed, trying to reexamine
  Info : SWD DPIDR 0x0bc11477
  Info : at91samd21g18.cpu: hardware has 4 breakpoints, 2 watchpoints
  Info : Listening on port 6666 for tcl connections
  Info : Listening on port 4444 for telnet connections
  Polling target at91samd21g18.cpu failed, trying to reexamine
  Info : SWD DPIDR 0x0bc11477
  Info : at91samd21g18.cpu: hardware has 4 breakpoints, 2 watchpoints
  ^Cshutdown command invoked

 

 

The tested configuration used an Adafruit Metro M0 as a target board.

 

Connections:

Pi4 Pin        Target Pin
--------------------------
22 (GPIO25)    SWDCLK
18 (GPIO24)    SWDIO
14 (GND)       GND
12 (GPIO18)    nRST
--------------------------

 

Created "usr/local/share/openocd/scripts/interface/raspberrypi4-native.cfg" file.
Content of the configuration file was as follows:

 

  adapter driver bcm2835gpio

  bcm2835gpio_peripheral_base 0xFE000000

  # Transition delay calculation: SPEED_COEFF/khz - SPEED_OFFSET
  # These depend on system clock, calibrated for stock 700MHz
  # bcm2835gpio_speed SPEED_COEFF SPEED_OFFSET
  bcm2835gpio_speed_coeffs 236181 60

  # Each of the JTAG lines need a gpio number set: tck tms tdi tdo
  # Header pin numbers: 22 18 26 10
  # bcm2835gpio_jtag_nums 25 24  7 15

  # Each of the SWD lines need a gpio number set: swclk swdio
  # Header pin numbers: 22 18
  bcm2835gpio_swd_nums 25 24

  # If you define trst or srst, use appropriate reset_config
  # Original header pin numbers: TRST - 26, SRST - 18

  # bcm2835gpio_trst_num 7
  # reset_config trst_only

  # bcm2835gpio_srst_num 18
  # reset_config srst_only srst_push_pull

  # or if you have both connected,
  # reset_config trst_and_srst srst_push_pull

 

Adjusted settings in the other config file before running.

Content of "/home/pi/bootloader/openocd.cfg" file:

 

  source [find interface/raspberrypi4-native.cfg]
  transport select swd
  set CHIPNAME at91samd21g18
  source [find target/at91samdXX.cfg]
  #reset_config srst_only
  #reset_config  srst_nogate
  adapter srst delay 100
  adapter srst pulse_width 100
  init
  targets
  reset halt

 

That is the basic configuration.

Anyone who has this working, please explain config file changes.

 

 

 

~Bill

Last Edited: Tue. Apr 13, 2021 - 09:45 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did play with this some time ago but gave up and bought a cheap knockoff j-link. I can't legally recommend that option, but the educational version of the j-link is c. $20 for non-commercial use. It begs the question of why you would bother with the Pi, unless you can't comply with the edu licence and would need the full $400 version.

 

I recall some useful articles on the Adafruit site. Worth a search: "Adafruit Pi OpenOCD".

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There does not seem to be a RP2040 Software Pack for Keil or Rowley yet.

 

I guess that Raspberry will release a Pack soon.

In which case you will be able to use any SWD debug tool e.g. CMSIS-DAP, JLINK, ULINK, ...

and examine the peripheral registers.

 

Otherwise you are stuck with a "generic" ARM which seems more trouble than it is worth.

 

My PICO boards only arrived on Friday.    So I have not tried SWD yet.

I have Arduino working fairly well on my elderly Win7-32 Laptop.

The CDC Serial does not work properly on my Win10-64 Desktop.   I would appreciate any advice on correct USB Driver,  installation, ...

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Musings...

 

OpenOCD works great on older Pi platforms, so the goal here is to consolidate. Figure out how to make all of the stuff we can do with different Pi units work together when installed on one Pi platform. This would be better than having a small army of different Pi units proliferating like tribbles from Star Trek.

 

I am curious to know if the people developing and testing OpenOCD are testing it on the Pi4.

 

When compiling the latest OpenOCD, I noticed they did away with the previous openocd-code folder, so the structure of the source code continues to evolve.

 

I wonder if possibly I missed an earlier release of OpenOCD source code that worked OK on the Pi4, but possibly the newest release is broken?

 

Probably not. If that were the case then there would be at least one Pi4 example config file in the current package, and none is visible.

 

No, it really looks as if no brave explorers have made it to this north pole yet.

 

Adafruit has a really nice guide to compiling the source here:

 

https://learn.adafruit.com/progr...

 

Follow it closely, and watch out for the missing openocd-code folder if you would like to have the latest.

 

 

~Bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When I awoke this morning, I had a dream image in mind. Of all things, it was part of a schematic with one resistor slightly glowing.

 

Now with my conscious mind I see the Metro M0 has an extra low impedance pull-up resistor connected to the SWDIO pin.

 

Subconscious me is telling conscious me to check out this pin with an oscilloscope. If a Pi4 GPIO pin has any difficulty pulling this connection fully low, then communications could be hit or miss at high speeds.

 

I will check it out tonight and report back. Much to do today.

 

 

~Bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Haha nice, let us know how it goes!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Swapped out the Metro M0. Replaced it with a Xiao.

Xiao uses the same ATSAMD21G18 chip with no pull-up on SWDIO.

 

First attempt failed. One jumper was poorly connected.

Soldered the wire. It worked, sort of. Big improvement.

 

Connection was stable, but showed an error when burning a bootloader.

Added statements to the openocd.cfg script as follows:

 

source [find interface/raspberrypi4-native.cfg]
transport select swd

set CHIPNAME at91samd21g18
source [find target/at91samdXX.cfg]

#reset_config srst_only
#reset_config  srst_nogate

adapter srst delay 100
adapter srst pulse_width 100

init
targets
reset halt
at91samd bootloader 0
program XIAO_m0-v3.7.0.bin verify
at91samd bootloader 8192
reset
shutdown

Output was this:

 

----------------------------------------------------------------------------------------------------

pi@ubuntu:~/bootloader$ sudo openocd
Open On-Chip Debugger 0.11.0+dev-00069-g68e50415a-dirty (2021-04-10-15:03)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/b...
Info : BCM2835 GPIO JTAG/SWD bitbang driver
Info : clock speed 400 kHz
Info : SWD DPIDR 0x0bc11477
Info : at91samd21g18.cpu: hardware has 4 breakpoints, 2 watchpoints
Info : starting gdb server for at91samd21g18.cpu on 3333
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, current mode: Thread
xPSR: 0x21000000 pc: 0x00000288 msp: 0x20002dd8
target halted due to debug-request, current mode: Thread
xPSR: 0x21000000 pc: 0x00000288 msp: 0x20002dd8
** Programming Started **
Info : SAMD MCU: SAMD21G18A (256KB Flash, 32KB RAM)
Error: SAMD: NVM programming error
Error: Failed to erase row containing 00000100
Error: SAMD: failed to erase sector 1 at 0x00000100
Error: failed erasing sectors 0 to 31
embedded:startup.tcl:472: Error: ** Programming Failed **
in procedure 'script'
at file "embedded:startup.tcl", line 26
in procedure 'program' called at file "openocd.cfg", line 18
in procedure 'program_error' called at file "embedded:startup.tcl", line 537
at file "embedded:startup.tcl", line 472
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections

 

----------------------------------------------------------------------------------------------------

 

The remaining error could be due to needing fuses set differently.
This is progress. Next, study OpenOCD scripting command options.

 

 

 

 

 

 

~Bill

Last Edited: Mon. Apr 12, 2021 - 07:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

The resistor was important, but success also needed a timing correction.

Added "Adapter Speed 400" to the openocd.cfg file.

Oscilloscope still showed pulses at  694.4kHz.

 

Tried "Adapter Speed 100" as a wild guess.

 

Result was 162.3kHz. Weird, but it worked.

 

 

The output looked like this:

 

 

--------------------------------------------------------------------------

pi@ubuntu:~/bootloader$ sudo openocd
Open On-Chip Debugger 0.11.0+dev-00069-g68e50415a-dirty (2021-04-10-15:03)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/b...
Info : BCM2835 GPIO JTAG/SWD bitbang driver
Info : clock speed 100 kHz
Info : SWD DPIDR 0x0bc11477
Info : at91samd21g18.cpu: hardware has 4 breakpoints, 2 watchpoints
Info : starting gdb server for at91samd21g18.cpu on 3333
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, current mode: Thread
xPSR: 0x21000000 pc: 0x00000288 msp: 0x20002dd8
target halted due to debug-request, current mode: Thread
xPSR: 0x21000000 pc: 0x00000288 msp: 0x20002dd8
** Programming Started **
Info : SAMD MCU: SAMD21G18A (256KB Flash, 32KB RAM)
** Programming Finished **
** Verify Started **
** Verified OK **
shutdown command invoked
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections

--------------------------------------------------------------------------

 

 

Did some Bodine ciphers. Tried "Adapter Speed 240" in the openocd.cfg file.

Xiao's bootloader burned successfully again. Clock pulses were at 403.2kHz.

 

Without an oscilloscope and a bit of dreaming, I don't think both problems would have resolved.

 

The parameter "Adapter Speed 240" is supposed to produce 240kHz clocking.

Why does it not?

 

I went back and tried a 162kHz pulse frequency on the Metro M0 with no success.

 

Prognosis:

Possibly install a small MOSFET impedance buffer for working with boards like the Metro.

 

 

 

~Bill

Last Edited: Tue. Apr 13, 2021 - 12:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When using the Metro M0, I tried kicking it down to "Adapter Speed 10."

It burned and verified, but with an actual speed of 16.2kHz.

Tests confirmed it worked many times without failure.

 

This is exceedingly slow, but better than nothing.

 

Eventually, install a transistor pulse shaper.

I feel the need, the need for speed.

 

 

~Bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Out of curiosity, I tried recompiling OpenOCD under Raspbian.

Moved the experiment from a Pi4 to a Pi2. The Pi2 output pins

seem to have no problem driving the Metro pull-up resistor low.

Changed "Adapter Speed 10" to "Adapter Speed 400" and tested.

It worked in a blink. Now it is crazy fast. Works every time.

 

 

~Bill

Last Edited: Wed. Apr 14, 2021 - 04:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Awoke from dreaming of Big Ben clock chimes.

Slowly realized it was not the resistor.

Went back to the abandoned Pi4.

Reconnected it to the Metro M0.

Changed "adapter srst delay 100" to 167.

Changed "adapter srst pulse width 100" to 167.

OpenOCD works crazy fast, every time.

1.5GHz processor vice 900MHz.

All errors are clues.

 

 

~Bill

Last Edited: Fri. Apr 16, 2021 - 11:21 AM