Skip to content

Session API

Session is the primary entry point for all interaction with OpenOCD. It manages the connection lifecycle and provides lazy access to every subsystem (target, memory, registers, flash, JTAG, breakpoints, RTT, SVD, transport).

SyncSession is the synchronous counterpart for use outside async contexts.

Session.__init__(connection: TclRpcConnection, process: OpenOCDProcess | None = None)

You rarely call this directly. Use the factory methods connect() or start() instead.

ParameterTypeDescription
connectionTclRpcConnectionAn established TCL RPC connection
processOpenOCDProcess | NoneOptional managed subprocess (closed on close())
@classmethod
async def connect(
host: str = "localhost",
port: int = 6666,
timeout: float = 10.0,
) -> Session

Connect to an already-running OpenOCD instance.

ParameterTypeDefaultDescription
hoststr"localhost"OpenOCD host address
portint6666TCL RPC port
timeoutfloat10.0Connection timeout in seconds

Returns: Session

Raises: ConnectionError, TimeoutError

async with await Session.connect() as session:
state = await session.target.state()
@classmethod
async def start(
config: str | Path,
*,
tcl_port: int = 6666,
openocd_bin: str | None = None,
timeout: float = 10.0,
extra_args: list[str] | None = None,
) -> Session

Spawn an OpenOCD process with the given configuration, wait for it to become ready, then connect.

ParameterTypeDefaultDescription
configstr | Path(required)Config file path or -f/-c flags string
tcl_portint6666TCL RPC port for the spawned process
openocd_binstr | NoneNoneCustom OpenOCD binary path (auto-detected if None)
timeoutfloat10.0Seconds to wait for OpenOCD readiness
extra_argslist[str] | NoneNoneAdditional CLI arguments

Returns: Session

Raises: ProcessError, ConnectionError, TimeoutError

async with await Session.start("interface/cmsis-dap.cfg -f target/stm32f1x.cfg") as session:
await session.target.halt()

The config parameter accepts several formats:

  • File path: "board/stm32f1discovery.cfg"
  • Flag string: "-f interface/cmsis-dap.cfg -f target/stm32f1x.cfg"
  • List form: ["-f", "interface/cmsis-dap.cfg", "-f", "target/stm32f1x.cfg"]
@classmethod
def connect_sync(
host: str = "localhost",
port: int = 6666,
**kwargs,
) -> SyncSession

Synchronous version of connect(). Returns a SyncSession.

Raises: RuntimeError if called from inside a running async event loop.

with Session.connect_sync() as session:
state = session.target.state()
@classmethod
def start_sync(config: str | Path, **kwargs) -> SyncSession

Synchronous version of start(). Returns a SyncSession.

Session implements __aenter__ and __aexit__. On exit, it calls close().

async with await Session.connect() as session:
# session is open here
pass
# session.close() called automatically
async def close() -> None

Close the TCP connection and stop the OpenOCD subprocess if one was spawned via start().

async def command(cmd: str) -> str

Send a raw OpenOCD command string and return the response. This is the escape hatch for commands not covered by the typed subsystem APIs.

ParameterTypeDescription
cmdstrAny valid OpenOCD command

Returns: The raw response string from OpenOCD.

resp = await session.command("version")
print(resp)
def on_halt(callback: Callable[[str], None]) -> None

Register a callback that fires when a notification containing “halted” is received.

def on_reset(callback: Callable[[str], None]) -> None

Register a callback that fires when a notification containing “reset” is received.

Each property instantiates its subsystem on first access. All subsystems share the same underlying connection.

PropertyAsync TypeDescription
targetTargetHalt, resume, step, reset, state queries
memoryMemoryRead/write target memory at various widths
registersRegistersRead/write CPU registers
flashFlashFlash bank enumeration, programming, erase, verify
jtagJTAGControllerJTAG scan chain, IR/DR scan, boundary scan
breakpointsBreakpointManagerSet/remove breakpoints and watchpoints
rttRTTManagerSEGGER RTT channel read/write
svdSVDManagerSVD file loading, register decoding
transportTransportTransport and adapter queries

Wraps an async Session for synchronous use. Every method runs through loop.run_until_complete() on an internally managed event loop.

with Session.connect_sync() as session:
print(session.target.state())

Calls close() on exit.

def command(cmd: str) -> str

Synchronous version of Session.command().

PropertySync TypeDescription
targetSyncTargetHalt, resume, step, reset, state
memorySyncMemoryRead/write memory
registersSyncRegistersRead/write registers
flashSyncFlashFlash operations
jtagSyncJTAGControllerJTAG operations
breakpointsSyncBreakpointManagerBreakpoints and watchpoints
svdSyncSVDManagerSVD loading and decoding