How to use the command 'st-util' (with examples)
The st-util
command serves as a GDB server that facilitates interaction with STM32 ARM Cortex microcontrollers via the GNU Debugger. It is part of the open-source stlink
project, which provides utilities for programming and debugging STM32 microcontrollers. The main advantage of this command is its ability to perform real-time debugging, allowing developers to seamlessly upload firmware, set breakpoints, and inspect memory, among other capabilities. By running a GDB server instance, st-util
enables a smooth debugging experience for embedded systems developers who work with STM32 hardware.
Use case 1: Run GDB server on port 4500
Code:
st-util -p 4500
Motivation:
Running a GDB server on a specific port is essential when there are multiple debugging sessions happening on different systems within the same network. By designating a port, such as 4500, developers can manage their project environment, ensuring that their instance of the GDB server does not clash with others. This port specification is crucial for remote debugging or when operating in a shared development environment.
Explanation:
st-util
: This invokes thest-util
command, starting the GDB server.-p 4500
: The-p
flag specifies which port the server should listen to. Port 4500 is arbitrarily chosen here, demonstrating how you can select a non-default port for specific use cases.
Example Output:
2018-05-21T14:12:00 INFO src/main.c: Starting st-util 1.4.0
2018-05-21T14:12:00 INFO src/usb.c: Found STLINK V2J27S6
2018-05-21T14:12:00 INFO src/usb.c: Target voltage: 3.3V
2018-05-21T14:12:00 INFO src/gdbserver/gdb-server.c: GDB server started at port :4500
Use case 2: Connect to GDB server
Code:
(gdb) target extended-remote localhost:4500
Motivation:
Connecting to a GDB server running on a particular port is necessary when you want to initiate a debugging session from a different location, perhaps even remotely, depending on your network setup. The extended-remote
feature of GDB allows for enhanced control during debugging sessions, such as downloading executables, performing specific remote commands, or even starting new processes. This aspect is crucial for embedded developers needing flexibility when debugging across various environments.
Explanation:
(gdb)
: This indicates the GDB command line, which accepts subsequent commands for debugging purposes.target extended-remote
: This GDB command connects to a remote target, offering extended features compared to the standardremote
command.localhost:4500
: Specifies the IP address and port of the GDB server to connect to. Here,localhost
refers to the local machine, and4500
represents the port assigned to thest-util
server.
Example Output:
Remote debugging using localhost:4500
0x08000400 in ?? ()
Use case 3: Write firmware to device
Code:
(gdb) load firmware.elf
Motivation:
Loading firmware onto a device is an essential step in the development cycle, as it ensures the latest software is running on the microcontroller. By utilizing GDB to load firmware, developers can debug and verify their code while it’s running on the actual hardware. This direct application of firmware files (.elf format) authenticates the communication channel between the debugger and the target device, ensuring that the embedded system is tested in conditions similar to its eventual deployment.
Explanation:
(gdb)
: As before, this indicates that the following is a command for the GDB environment.load
: This GDB command loads the specified firmware into the target’s memory.firmware.elf
: The ELF file is a standard file format for executables, object code, and shared libraries, often used in embedded systems. In this context, it contains the compiled firmware ready to be uploaded to the STM32 microcontroller.
Example Output:
Loading section .text, size 0x2000 lma 0x8000000
Loading section .data, size 0x8 lma 0x8002000
Start address 0x8000000, load size 8192
Transfer rate: 1 KB/sec, 2730 bytes/write.
Conclusion
The st-util
command is a versatile tool for developers working with STM32 ARM Cortex microcontrollers, providing a robust gateway to utilize GDB for real-time debugging. Each of the use cases highlighted demonstrates critical functionalities, from starting a server on a specified port, connecting remotely, to loading firmware directly onto the device. Leveraging st-util
enhances the debugging process, providing developers with the necessary controls to ensure software operates as intended on their embedded systems.