Debugging embedded systems like Cortex-M1 on Arty board without a debug adaptor or DAPLink can be challenging, but is possible with some effort. This article will guide you through the process step-by-step.
Overview
The key steps are:
- Enable SWD pins on Arty board
- Build and flash debugger firmware
- Connect to GDB debugger
- Debug code with GDB
Enabling SWD Pins on Arty
Arty board has SWD pins broken out to the headers, but they are not enabled by default. To enable them:
- Locate the 2-pin shunt jumper near the Cortex-M1 module
- Shunt the jumper to connect the pins and enable SWD
- The SWD pins are now available on the board headers
Building and Flashing Debugger Firmware
Next, we need to build and flash a small debugger firmware onto the Cortex-M1 that will allow GDB to connect to it directly over SWD.
Compiling Debugger
You can use the open source CMSIS-DAP firmware for this.
- Download the firmware source code
- Open the project in Keil MDK toolkit
- Configure the project settings for your Arty board and Cortex-M1 module
- Build the project
This will produce a binary firmware image you can flash.
Flashing Debugger Firmware
To flash the debugger image:
- Connect Arty board SWD pins to another SWD debugger
- Load the compiled firmware binary
- Halt the target Cortex-M1 before flashing
- Flash the debugger firmware onto Cortex-M1
Once flashed, the debugger will start running on Cortex-M1 on next boot.
Connecting GDB Debugger
With debugger firmware running on the target, you can now connect to it directly from GDB over SWD interface.
- Connect Arty SWD pins to your PC using an SWD adapter
- Power on Arty board
- Launch GDB and connect to the SWD interface
- The debugger firmware will halt the Cortex-M1 on boot
- Cortex-M1 is now ready for debugging in GDB
GDB Commands
Useful GDB commands:
- continue – resume execution
- next – step over next instruction
- step – step into next instruction
- break – set breakpoint
- print – print variable value
- backtrace – show stack trace
Debugging Code
With GDB connected, you can now debug code running on Cortex-M1 just like using a dedicated debugger.
Loading Code
To load code:
- Compile your code into ELF file
- In GDB, load the ELF file using ‘file’ command
- Set any breakpoints you need
- Use ‘continue’ to run to first breakpoint
Stepping Through Code
Use GDB commands like step and next to step through code.
Set additional breakpoints as needed to pause execution.
Inspect variables, registers, memory etc.
Sample Session
Here is an example debugging session: $ arm-none-eabi-gdb demo.elf (gdb) target remote localhost:3333 (gdb) load Loading section .text, size 0x258 lma 0x0 Loading section .data, size 0x8 lma 0x258 Start address 0x0, load size 616 Transfer rate: 18 KB/sec, 616 bytes/write. (gdb) break main Breakpoint 1 at 0x104: file demo.c, line 5. (gdb) continue Continuing. Breakpoint 1, main () at demo.c:5 5 int i = 10; (gdb) print i $1 = 0 (gdb) next 7 for(i=0; i<10; i++) { (gdb) print i $2 = 0 (gdb) step 9 printf(“Hello %d\n”, i); (gdb) next Hello 0 7 for(i=0; i<10; i++) { (gdb) print i $3 = 1
Troubleshooting
Here are some common issues and solutions:
GDB Won’t Connect
- Check SWD pins are connected properly
- Verify debugger firmware is flashed and running
- Try resetting the board
Breakpoints Not Hitting
- Make sure code is compiled with debug symbols
- Breakpoint may be set at invalid address
- Code region might not be loaded in memory
Variables Show Wrong Value
- Rebuild code from scratch, old binaries may be stale
- Optimize variable placement in memory
SWD Adapter Issues
- Update adapter firmware or drivers if needed
- Check adapter power – some may need external power
- Try a different SWD adapter
Conclusion
Debugging Cortex-M1 on Arty without a dedicated debugger is very doable with some effort. The key steps are enabling SWD pins, flashing debugger firmware, connecting GDB via SWD, and using GDB to control execution and inspect state. With a few tweaks and customizations, this approach can work for many different boards and projects.