Full name: Open On-Chip Debugger.
Definition
OpenOCD is a free, open-source host program that controls a target chip through a debug adapter, also called a debug probe: a device that connects the host to the chip’s debug port. Configuration scripts describe the adapter, the chip and its flash memory §6.6. Commands then halt, reset and resume the processor, read and write memory, and erase and program flash memory. OpenOCD also acts as a debug server: it accepts connections from a debugger over the network and carries out the debugger’s requests on the target.
The idea
There are many debug adapters, each with its own USB protocol, and many chips, each with its own flash controller and its own way of being reset. Without a common layer, every debugger would need code for every combination. OpenOCD is that layer. Adapter drivers sit on one side, drivers for processors and flash memories on the other, and a command language and network servers on top. A debugger, a script or an editor can then control any supported combination in the same way.
The command language hides detail, and the hidden detail decides what happens to the device. A command name such as program or reset stands for a sequence of hardware actions: resets, halts, erases, writes and reads. The configuration can add more, because it can attach scripts to events that fire during that sequence. Calling a command a debugging command says nothing about whether it only looks at the target or also changes it. To know what a command does, read its definition and the configuration it runs under.
How it works
OpenOCD works in three phases.
Configuration. OpenOCD starts with configuration files, which are scripts in the Tcl (Tool Command Language) scripting language:
openocd -f interface/X.cfg -f target/Y.cfg, where the first file describes the adapter and the second the chip. Further commands follow the option-c. The configuration chooses the transport, JTAG §6.26 (Joint Test Action Group) or SWD §6.25 (Serial Wire Debug); the adapter’s clock speed; the processor type; a work area in the target’s RAM that OpenOCD may use; the flash banks with their drivers and addresses; and the reset configuration, which says whether a system reset wire, called SRST (system reset), connects the adapter to the chip’s reset pin.Initialization. The
initcommand, which runs automatically after the configuration, examines the target and starts three servers: one for the GNU Debugger §6.28 (GDB) on network port 3333, a text command console on port 4444 that a person reaches with the programtelnet, and a Tcl interface for programs on port 6666. GNU is a free-software project whose name stands for GNU’s Not Unix.Commands, typed into the console, sent by a program, or given with
-c.
The OpenOCD user’s guide documents these commands, among many others:
halt [ms]sends a halt request and waits up tomsmilliseconds, 5 seconds by default, for the target to halt.wait_halt [ms]waits in the same way but sends no request.resume [address]lets the processor run from its current position or from the address given, andstepexecutes one instruction.reset run,reset haltandreset initperform “as hard a reset as possible, using SRST if possible” on every defined target, and fire the target’s reset events. Afterwards the processor runs; or halts immediately; or halts immediately and thereset-initevent script runs.resetalone meansreset run.mdwdisplays andmwwwrites 32-bit words of memory.flash write_image [erase] [unlock] filewrites the loadable sections of an image file into flash memory. Witherase, it first erases every flash sector the image touches, a sector being the smallest part of flash memory that can be erased; withunlock, it first removes the sectors’ write protection.program file [verify] [reset] [exit]is a helper script that runs other commands in order:reset init; thenflash write_image erase file; withverify, a comparison of the flash content with the file; withreset,reset run; withexit,shutdown.exitcloses the current telnet or Tcl session and leaves OpenOCD running;shutdowndisconnects every client and ends the OpenOCD process.
A configuration can attach a script to a named event of a target, such as reset-start, reset-init, halted or gdb-attach. The script runs whenever the event fires, so a command that fires the event also performs whatever the script does, for example writing clock registers or configuring a watchdog timer §6.16.
How a reset happens depends on the reset configuration. If an SRST wire is connected and configured, OpenOCD drives the chip’s reset pin. Otherwise it uses a reset that the chip can trigger itself; on an Arm Cortex-M processor, it writes a reset-request bit in one of the processor’s control registers. On some chips the two kinds of reset clear different sets of registers; the chip’s reference manual says which. Flash drivers usually run a small programming routine in the target’s work area, so programming also overwrites that part of RAM and uses the processor.
Worked example
A chip’s flash memory starts at address 0x00000000 and is divided into sectors of 4096 bytes. The image file app.hex holds one section of 70,000 bytes, starting at address 0. Earlier, a technician stored a 16-byte serial number at address 0x00011F00, which is 73,472. A developer runs OpenOCD with the adapter and chip configuration files and the extra command program app.hex verify reset exit. OpenOCD carries out these steps:
reset init: it resets the chip, through SRST if that is configured, halts the processor immediately, and runs thereset-initevent script if the configuration defines one.flash write_image erase app.hex: the image covers bytes 0 to 69,999. Since , the image reaches into an 18th sector, so sectors 0 to 17 are erased: bytes, from address0x00000000to0x00011FFF. Then 70,000 bytes are programmed.verify: it compares the flash content with the file.reset: it resets the chip a second time and lets the processor run the new program.exit: it shuts down.
One command line thus requested two resets, a halt, the erasure of 73,728 bytes and the writing of 70,000 bytes. The erased range includes bytes that the image does not cover, and the serial number at address 73,472 lies among them: it is lost. With an assumed erase time of up to 85 ms per sector, the erasing alone took up to ms. Data from sector 18 onward survives, because erase touches only the sectors the image covers. The OpenOCD documentation warns that this includes the gaps between an image’s sections: an image with one byte at the start of a flash bank and one byte at its end erases the whole bank.
Leaving out erase does not make the write safe. Programming flash memory can change bits only from 1 to 0, so writing over sectors that were not erased leaves the bitwise AND of the old and the new data, which is neither.
Stated precisely
The effect of an OpenOCD command combines three things:
the command’s own definition, built into OpenOCD or written as a Tcl procedure, as
programis;every event script that fires while the command runs;
the configuration the command runs under: the reset configuration, the flash bank definitions and the transport.
The same command line with different configuration files can therefore perform different hardware actions.
Completion means different things at different levels. halt returning without an error means that the processor entered debug state within the time limit. flash write_image returning means that the sectors were erased and programmed; only a verification shows that the content matches the file. The OpenOCD process ending means that the server stopped; it says nothing about whether the target runs. Whether the new program started and works is shown only by the program itself, for example by a message it sends after it starts.
Limits and common mistakes
A command name does not reveal the command’s effects. program sounds like a single write, and it performs two resets, a halt, an erase, a write and a comparison. Read the definition and the configuration before running a command on a device whose content matters.
The result of a reset depends on the board and the chip: whether the SRST wire is connected, which kind of reset OpenOCD uses, and what that kind of reset clears on that chip.
The unlock flag removes flash write protection, and the OpenOCD documentation notes that the protection must be applied again afterwards.
A chip whose debug access is locked cannot be read or programmed until it is unlocked, which on many chips erases all of flash memory; see the entry on destructive debug recovery §16.6.
exit in a telnet session leaves the OpenOCD process running and connected to the target; only shutdown ends it. A script that needs the target running afterwards should end with an explicit reset run or resume instead of relying on what happens when OpenOCD ends.
Questions answered
Which reset and flash operations does a command actually request?
A command requests exactly the operations in its own definition, plus those of every event script it fires, carried out under the reset and flash configuration in force; its name alone does not tell. To find them, expand the command step by step. OpenOCD’s program app.hex verify reset exit, for example, requests a reset with an immediate halt and the reset-init event script (reset init); an erase of every flash sector the image touches, including the unwritten parts of those sectors and any gaps between the image’s sections, followed by programming (flash write_image erase); a comparison of the flash content with the file (verify); a second reset, after which the processor runs (reset run); and the end of the OpenOCD process (shutdown).
Whether each reset uses the chip’s reset pin or a reset request inside the processor depends on the reset configuration, and what the reset clears depends on the chip. Without the erase flag nothing is erased, and programming over old data corrupts it. None of these operations shows, by itself, that the new program started and works.
Related entries: J-Link (§6.27), SWD (§6.25), GDB (§6.28), Flash memory (§6.6), ERASEALL and destructive debug recovery (§16.6). Further reading: [95].