Debugging on STM32 using OpenOCD
General Info:
For MSXIV and MSXV, we use OpenOCD to flash and debug our board. OpenOCD is an OPEN-source, On-Chip Debugger, and it’s a script we run to monitor changes on the chip.
This is OpenOCD’s User Guide: https://openocd.org/doc-release/html/Running.html#Simple-setup_002c-no-customization
It’s full of commands to use for:
chip setup: Things up initializing flash banks, clock and adapter speeds, debug ports, etc.
general commands: https://openocd.org/doc-release/html/General-Commands.html
flash commands: https://openocd.org/doc-release/html/Flash-Commands.html#Flash-Configuration-Commands
gdb setup: https://openocd.org/doc-release/html/GDB-and-OpenOCD.html#Connecting-to-GDB
and others
To use openocd
we use scons flash
which will run openocd
under the hood. It basically setups all the configuration, clock speeds, flash banks, etc. and then it setus FreeRTOS
and flashes and verifies the image on top the board.
It basically runs this:
openocd
-s /usr/share/openocd/scripts \ # This describes the file path to look for configuration files
-f interface/cmsis-dap \ # This is the first configuration file to setup DAP settings
-f target/stm32f1x.cfg \ # This is the most import configuration file. Look below
-f fwvx/platform/stm32f1-openocd.cfg \ # This MSXVs configuration file. Has our routines in there
-c "stm_flash <program to flash>" \ # Uses our routines to flash the image
-c shutdown # Shuts down the server communication with the chip
The most important part is the target/stm32f1x.cfg
. It does all the target
(chip) creation and settings, flash banks and other stuff to run the STM32. It basically looks like below.
OpenOCD General Tips:
Use the https://openocd.org/doc-release/html/General-Commands.html for general commands to look at the chip status.
You’ll need to run the entire setup each time for each command. Like so
openocd -s /usr/share/openocd/scripts -f interface/cmsis-dap.cfg -f target/stm32f1x.cfg -f fwvx/platform/stm32f1-openocd.cfg
-c <command to run>
-c shutdown
Helpful <command to run>
are like:
reg
targets
read_memory
write_memory
mdw
mww
reset halt
You’d mostly just use OpenOCD to check CPU settings: Use these commands: https://openocd.org/doc-release/html/CPU-Configuration.html#CPU-Configuration
Flash Tips:
Use the https://openocd.org/doc-release/html/Flash-Commands.html#Flash-Configuration-Commands to run flash commands. You’d mostly don’t need to use it, but could be helpful if you’re running OpenOCD on its own.
Again you’ll need to run the entire setup each time for each command. Like so
openocd -s /usr/share/openocd/scripts -f interface/cmsis-dap -f target/stm32f1x.cfg -f fwvx/platform/stm32f1-openocd.cfg
-c flash <command to run>
-c shutdown
At the bottom of the page, there are “flash driver commands”. This is what the flash commands use.
Important: New chips may need to use stm32f1x unlock 0
the first time to unlock the flash space.
Hardware Tips:
It’s also important to look at hardware setup to make sure the chip is mounted correctly. Use this link for STM32F1 physical configuration. https://www.st.com/resource/en/reference_manual/rm0008-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf
Important part is the Boot configurations.
These are physical pins that need to be mapped physically correctly in order to setup the chip boot setting correctly.
GDB setup
Use the https://openocd.org/doc-release/html/GDB-and-OpenOCD.html#Connecting-to-GDB in order to setup up a GDB session using OpenOCD. You’ll need at least 2 different terminals for this
1 terminal runs the OpenOCD server. Remove the -c shutdown
command so the server keeps the connection.
This will be how GDB will communicate with the chip.
This sets up ARM’s GDB. You need to specify the program that you want to debug in order to load the program symbols.
Next, you want to connect to the OpenOCD server, which has the default port of 3333
Finally, you want to halt the CPU in order to set breakpoints and other things.