Testing BlackMagicProbe with EPM

Koen has provided Zubax with a patch to the BlackMagicProbe firmware, which adds support for SAMC21, used on EPM/FluxGrip.

Before we can start we need to flash our probe with the latest firmware. We will be using the Dronecode Probe.

To patch the probe:

  • Clone sources and compile:
git clone https://github.com/blackmagic-debug/blackmagic/
cd blackmagic
make PROBE_HOST=native #compiles into src subfolder
lsusb #verify that new usb device detected
sudo dfu-util -d 1d50:6017 -s 0x08002000:leave -R -D src/blackmagic.bin #flashes

Now we should be able to use the probe to flash our EPM board. The pinout of the wires is as follows:

(This is from Pixhawk DS-009 standard)

After connecting this to the corresponding pins on the board, we can do the following:

  • Make the following change in epm/boards/arm/epm/board.cmake:

  • Flash the board: west flash --runner blackmagicprobe

The last remaining issue: semihosting.

This is how it is done when using the Atmel debugger:

  $workspace_path/scripts/openocd.cfg &
  # record the PID of the background process
  openocd_pid=$!

  sleep 4s

  # loads the image, runs the tests
  arm-zephyr-eabi-gdb ./build/zephyr/zephyr.elf \
    -batch \
    -silent \
    -ex "tar ext:3333" \
    -ex "load" \
    -ex "monitor arm semihosting enable" \
    -ex "monitor arm semihosting_fileio enable" \
    -ex "run"

  kill $openocd_pid

As far as I understand, the BlackMagicProbe does not require running openocd ourselves, so I’m doing the following steps:

west build -b epm ./verification/on_target/200_command_handler #this builds a test which uses semihosting
arm-zephyr-eabi-gdb-py ./build/zephyr/zephyr.elf #launch GDB
(gdb) target extended-remote /dev/ttyACM0
(gdb) attach 1
(gdb) load
(gdb) monitor arm semihosting enable #these don't seem to work
Target does not support this command.
(gdb) monitor arm semihosting_fileio enable #these don't seem to work
Target does not support this command.
(gdb) set remote system-call-allowed 1 #Pavel suggested this

If I then start running, it seems to “hang”, and if I check test_log.bin (the file where semihosting should be writing its output): it’s empty.

After pressing CTRL+C, I get the following:

Clone and compile blackmagic

git clone git@github.com:blackmagic-debug/blackmagic.git

This issue:

We need to remove stm32 support so the compilation won’t fail:

make PROBE_HOST=native

Yeah, we really need to make a new version of DroneCode Probe with a larger MCU. The Black Magic Probe project has several larger MCU options supported out of the box. This could be one of the possible tasks for the intern to work on initially: it is simple and well-contained.

Let’s start using consistent naming in posts and documentation, otherwise it’s gonna be harder to find.

It works using BugFace:

I’ve had to make the following change:

Now it’s failing again, even when using BugFace.

Flashing FluxGrip with BugFace2

You will need:

  • FluxGrip
  • CanFace 2

BugFace2

Before flashing, we should update the firmware on the BlackMagicProbe module present on the board.

We’re using commit f1cd9f2b which introduced SAMC21 support.

We make the following change due to flash memory constraints in our MCU:

diff --git a/src/Makefile b/src/Makefile
index 0e096d18..ae8fac3f 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -73,13 +73,6 @@ SRC =              \
        spi.c          \
        stm32f1.c      \
        ch32f1.c       \
-       stm32f4.c      \
-       stm32h5.c      \
-       stm32h7.c      \
-       stm32mp15.c    \
-       stm32l0.c      \
-       stm32l4.c      \
-       stm32g0.c      \
        renesas.c      \
        target.c       \
        target_flash.c \
(END)

Compile the firmware:

make clean
make PROBE_HOST=native

To update CF2, do the following:

  1. Disconnect from power/usb
  2. Put CF2 into bootloader mode by pushing the switch towards the ON state:

  1. Connect back to USB
  2. Now using lsusb, you should be able to see that the BlackMagicProbe is connected in DFU mode:

  1. Update/flash the firmware:

sudo dfu-util -d 1d50:6017 -s 0x08002000:leave -R -D src/blackmagic.bin

  1. Disconnect the USB

  2. Put the BlackMagicProbe back into application mode (toggle the switch from before).

  3. Re-connect the USB:

Linux

Now we need to do some Linux setup:

  1. Copy/download some udev rules for automatically detecting BlackMagicProbe:
sudo wget -O /etc/udev/rules.d/99-blackmagic.rules https://gist.github.com/maksimdrachov/15f8f4297eb02fea2aff7cd5e5a1b294/raw/2d8a93484712bcfb8ff126fc5d69de902c640b2f/99-blackmagic.rules

  1. Reload udev rules and trigger them:
sudo udevadm control --reload-rules
sudo udevadm trigger
  1. Now we should see the following 2 entries under /dev/:

Flash

  1. Make the following change in fluxgrip_software:
diff --git a/epm/boards/arm/epm/board.cmake b/epm/boards/arm/epm/board.cmake
index e181152..a493645 100644
--- a/epm/boards/arm/epm/board.cmake
+++ b/epm/boards/arm/epm/board.cmake
@@ -1,4 +1,4 @@
 # Copyright (c) 2022 Kamil Serwus
 # SPDX-License-Identifier: Apache-2.0

-include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake)
+include(${ZEPHYR_BASE}/boards/common/blackmagicprobe.board.cmake)
  1. west flash

To upload the bootloader:

(This should be done once for each board. After that, as long as you’re using west flash with the memory set correctly, the bootloader will remain in place.)

arm-zephyr-eabi-gdb -q
(gdb) set confirm off
(gdb) target extended-remote /dev/ttyBmpGdb
(gdb) monitor swdp_scan
(gdb) attach 1
(gdb) load /home/kanye/fluxgrip_bootloader/bootloader.elf
(gdb) kill
(gdb) quit

To upload the bootloader:

You can also go like: ./commission.py

No arguments needed

Here’s the built binary:

blackmagic.bin (103.6 KB)

It should not be released because it omits support for popular STM32 MCUs. We should prepare another build that removes something else instead, not STM32.

With this binary semihosting works:

backup.bin (120 KB)

(I don’t know what’s different about it, downloaded it from my previous CanFace)