Skip to content

OpenOCD

Experimental

Tested against a simulated OpenOCD server only so far; the API may change in minor releases.

See the JTAG / SWD guide for an introduction.

openocd

Client for OpenOCD's TCL RPC server (default port 6666).

Example

from hwntools import OpenOCD

ocd = OpenOCD()                 # openocd -f interface/... -f target/... running locally
ocd.halt()
boot = ocd.read_memory(0x08000000, 0x100)
ocd.dump(0x08000000, 0x10000, out="flash.bin")

OpenOCDError

Bases: HwnError

OpenOCD reported an error for a command.

OpenOCD

OpenOCD(host: str = '127.0.0.1', port: int = 6666, timeout: float = 10.0)

Talk to a running OpenOCD instance over its TCL RPC socket.

Each command is sent as <cmd>\x1a and the reply is read until \x1a. Errors are detected by wrapping commands in catch so they are reported as OpenOCDError.

raw

raw(command: str, timeout: float | None = None) -> str

Send a raw TCL command and return OpenOCD's reply (no error checking).

cmd

cmd(command: str, timeout: float | None = None) -> str

Run command; raise OpenOCDError if it fails. Returns its result.

close

close() -> None

Close the RPC connection (OpenOCD keeps running).

version

version() -> str

Return OpenOCD's version string.

targets

targets() -> list[str]

Return configured target names.

halt

halt(timeout_ms: int = 1000) -> None

Halt the current target, waiting up to timeout_ms milliseconds.

resume

resume(address: int | None = None) -> None

Resume execution, at address if given.

step

step() -> None

Single-step one instruction.

reset

reset(mode: str = 'halt') -> None

reset run|halt|init.

state

state() -> str

Current target state (halted, running, ...).

reg

reg(name: str, value: int | None = None) -> int

Read (or write then read) a CPU register.

read_memory

read_memory(address: int, size: int, width: int = 8) -> bytes

Read size bytes at address.

Uses read_memory (OpenOCD >= 0.12) and falls back to mem2array on older builds. width is the access width in bits (8/16/32/64); size must be a multiple of width // 8.

write_memory

write_memory(address: int, data: bytes, width: int = 8) -> None

Write data at address (RAM / MMIO; use flash_write_image for flash).

read_u32

read_u32(address: int) -> int

Read one little-endian 32-bit word at address (32-bit access).

write_u32

write_u32(address: int, value: int) -> None

Write one little-endian 32-bit word at address (32-bit access).

dump

dump(address: int, size: int, out: str | PathLike[str] | None = None, chunk: int = 4096, width: int = 8) -> bytes

Read a large region over RPC in chunks with progress; optionally save it.

dump_image

dump_image(path: str, address: int, size: int) -> None

Have OpenOCD write memory to path on the OpenOCD host (fast, server side).

load_image

load_image(path: str, address: int | None = None, file_type: str | None = None) -> str

Load an image file (on the OpenOCD host) into target RAM.

flash_banks

flash_banks() -> str

Return OpenOCD's flash banks listing.

flash_read_bank

flash_read_bank(bank: int, path: str, offset: int = 0, length: int | None = None) -> None

Dump a flash bank to path on the OpenOCD host.

flash_write_image

flash_write_image(path: str, offset: int = 0, erase: bool = True, force: bool = False) -> str

Program flash from an image file. Destructive: requires force=True.