OpenOCD Debugger Software

Guide using OpenOCD


This is a brief guide to using OpenOCD on Windows and linux.

Command line with OpenOCD

These commands assume that the following variables are initialized

  • OPENOCD this variable stores the path to the OpenOCD folder
    e.g. C:\OpenOCD-20170821\bin on a windows PC and possibly
    /usr/bin/ on Linux

  • OPENOCD_SCRIPT this variable stores the path to the OpenOCD scripts folder
    e.g. C:\OpenOCD-20170821\share\openocd\scripts on a windows PC and possibly
    /usr/lib/openocd/scripts or /usr/share/openocd/scripts on Linux

Based on these assumptions we now start writing the Commands.

Know your Processor

On Windows:

%OPENOCD%\openocd -f %OPENOCD_SCRIPT%\interface\cmsis-dap.cfg -f %OPENOCD_SCRIPT%\target\nrf51.cfg -c "init" -c "flash probe 0" -c "exit 0x0"

Let’s look at what essential parts form this command:

  • Invocation of OpenOCD:
    %OPENOCD%\openocd this actually executes the openocd.exe on Windows or the Linux executable.

  • Programmer / Debugger Specification:
    This is also known as the interface specification. In the above example we have a CMSIS-DAP interface.
    -f %OPENOCD_SCRIPT%\interface\cmsis-dap.cfg This part is where its specified
    This can be of various depending on the type of programmer or debugger available with the target.
    To explore more see the directory %OPENOCD_SCRIPT%\interface folder or $OPENOCD_SCRIPT/interface on Linux

    Also note that not all the interfaces support the JTAG and SWD together.
    In our case CMSIS-DAP or DAPLink support both based on the ARM Hardware Interface Circuit chip used.

  • Process / MCU Specification:
    This details the actual microcontroller type as well as memory interface.
    -f %OPENOCD_SCRIPT%\target\nrf51.cfg Here we are specifying the nRF51822 as type of MCU

  • Command Parameter List:
    This gives the exact sequence in which commands can be sent to the debugger as command arguments. In case no commands are specified then the OpenOCD enters the Daemon mode where we can connect through telnet.
    -c "init" -c "flash probe 0" -c "exit 0x0" This the command list from the above command.

Now, Let’s look at the sequence of commands-parameter-list sent for Know your Processor command:

  • Initialize the Processor:
    -c "init" This command would begin debugger execution

  • Check what type of Flash is attached:
    -c "flash probe 0" This command would print details about the size, Chip and type of memory.

  • Terminate the Command-line after executing the Command Parameter sequence: -c "exit 0x0" This would prevent the OpenOCD from going into the Daemon Mode and exit after completion of the Command Parameter List.

Halt your processor

SET CMD1="%OPENOCD%\openocd -f %OPENOCD_SCRIPT%\interface\cmsis-dap.cfg -f %OPENOCD_SCRIPT%\target\nrf51.cfg"
%CMD1% -c "init" -c "halt" -c "exit 0x0"

Reset you processor

SET CMD1="%OPENOCD%\openocd -f %OPENOCD_SCRIPT%\interface\cmsis-dap.cfg -f %OPENOCD_SCRIPT%\target\nrf51.cfg"
%CMD1% -c "init" -c "reset" -c "exit 0x0"

Mass Erase your Processor

SET CMD1="%OPENOCD%\openocd -f %OPENOCD_SCRIPT%\interface\cmsis-dap.cfg -f %OPENOCD_SCRIPT%\target\nrf51.cfg"
%CMD1% -c "init" -c "halt" -c "nrf51 mass_erase 0" -c "exit 0x0"

Since here our target is nRF51822 so we use the nrf51 specific commands.

Similar commands are available for stm32 etc.

Command format is <Specific Processor Commands> mass_erase <Bank No>

Sector Erase

SET CMD1="%OPENOCD%\openocd -f %OPENOCD_SCRIPT%\interface\cmsis-dap.cfg -f %OPENOCD_SCRIPT%\target\nrf51.cfg"
%CMD1% -c "init" -c "halt" -c "flash probe 0" -c "flash erase_sector 0 0 5" -c "exit 0x0"

The Flash Erase command is generic and does not need a special Processor part.

Here is the command Format flash erase_sector <Bank No> <Start Sector> <End Sector>

In the above case from Bank 0 we are erasing Sectors 0 through 5.

One needs to know the actual number of sectors the memory is divided in else you might not be able to use this command properly.

Read DWORD from memory

SET CMD1="%OPENOCD%\openocd -f %OPENOCD_SCRIPT%\interface\cmsis-dap.cfg -f %OPENOCD_SCRIPT%\target\nrf51.cfg"
%CMD1% -c "init" -c "halt" -c "flash probe 0" -c "mdw 0x200 5" -c "exit 0x0"

This would read double words from the memory of the target.

The format is mdw <Start Memory address in Hex> <Number of DWORDs to display>

This command allows read from the entire memory space in the chip.

Note that keeping the chip in halt state helps as the values would not change abruptly.

 
comments powered by Disqus