Skip to content

6. Examples

Andrea ZGuz edited this page Jan 3, 2024 · 4 revisions

Programming with OpenOCD

Download arm-none-eabi-gdb

  • In case you don't have installed "arm-none-eabi-gdb", download it. It is included in the "gcc-arm-none-eabi" package from ARM Developer.

Linux Exception

  • Linux users may need to download the pre-compiled version instead of using "apt-get install arm-none-eabi" to get the ARM GDB.

Download OpenOCD

Linux

sudo apt-get install openocd

macOS

sudo brew install openocd

Unofficial Binary Packages

In certain situations where using a package manager or self-compiling OpenOCD is impractical, several community members provide regularly updated binary builds on their websites.

  • Liviu Ionescu maintains multi-platform binaries, including Windows 32/64-bit, Intel GNU/Linux 32/64-bit, Arm GNU/Linux 32/64-bit, and Intel macOS 64-bit as part of The xPack OpenOCD project.

  • The official GitHub mirror automatically generates Windows binary archives for releases and all master commits.

Usage

OpenOCD

  • Execute OpenOCD by calling your configuration file for your microcontroller.

You can create your own configuration file or download the provided ones in the main repository.

Example:

sudo openocd -f saml21.cfg

The flag "-f" loads a specific file from your system. The example assumes you have the file "saml21.cfg" in the same folder you're executing OpenOCD from.

Note: In Windows, there is no "sudo" command. Execute the Command Prompt as an administrator.

If your microcontroller is well-connected, you should see something like this:

At this point, everything is well connected to OpenOCD, and you have a running GDB server on your machine at port 3333.

GDB

Now we need to connect to the GDB server using the client previously installed with the ARM GCC toolchain.

To invoke it, simply type:

arm-none-eabi-gdb

Once GDB is running, we need to connect to OpenOCD with one of the following commands:

  1. When the debugged program exits or you detach from it, GDB disconnects from the target.
target remote localhost:3333
  1. When the debugged program exits or you detach from it, GDB remains connected to the target.
target extended-remote localhost:3333

Choose one of the above options.

Now you're connected to the OpenOCD GDB server.

Once connected, you're able to debug and flash your microcontroller.

To flash, use the following commands:

  1. monitor reset halt
  2. set mem inaccessible-by-default off
  3. load yourBinary.elf
  4. q

GDB Automation

A single command to initialize GDB:

arm-none-eabi-gdb -ex 'target extended-remote localhost:3333'

A single command to flash with GDB:

arm-none-eabi-gdb yourBinary.elf -ex 'target extended-remote localhost:3333' -ex 'load' -ex 'q'

Example

flashComplete