Overview
The debug module provides low-level functions for embedded debugging operations. For most use cases, the CLI commands (lager debug) are the recommended interface. Python scripts running on the Lager Box can use these functions for advanced automation scenarios.
Note: Debug operations require a J-Link debug probe connected to the Lager Box.
Import
Functions
Core API
| Function | Description |
|---|---|
connect_jlink() | Connect to target via J-Link |
disconnect() | Disconnect from debug target |
reset_device() | Reset the connected device |
flash_device() | Flash firmware to device |
chip_erase() | Perform full chip erase |
erase_flash() | Erase specific flash region |
Status & Diagnostics
| Function | Description |
|---|---|
get_jlink_status() | Check J-Link connection status |
get_arch() | Get device architecture (arm, cortex-m, etc.) |
get_controller() | Get controller type |
gdb_reset() | Reset device via GDB |
GDB Server Management
| Function | Description |
|---|---|
start_jlink_gdbserver() | Start J-Link GDB server |
stop_jlink_gdbserver() | Stop J-Link GDB server |
get_jlink_gdbserver_status() | Get GDB server status |
Exception Classes
| Exception | Description |
|---|---|
DebugError | Base class for debug errors |
JLinkStartError | J-Link failed to start or connect |
JLinkAlreadyRunningError | J-Link is already running |
JLinkNotRunning | J-Link is not running (operation requires connection) |
DebuggerNotConnectedError | GDB debugger is not connected |
Function Reference
connect_jlink(speed, device, transport, **kwargs)
Connect to target device via J-Link debug probe.
| Parameter | Type | Default | Description |
|---|---|---|---|
speed | str | None | Interface speed in kHz (e.g., ‘4000’) or ‘adaptive’. Defaults to ‘4000’. |
device | str | - | J-Link device name (e.g., ‘NRF52840_XXAA’, ‘R7FA0E107’) |
transport | str | - | Transport protocol (‘SWD’ or ‘JTAG’) |
force | bool | False | Force connection even if already connected |
ignore_if_connected | bool | False | Return success if already connected |
attach | str | 'attach' | Attach mode: ‘attach’, ‘reset’, or ‘reset-halt’ |
vardefs | list | None | List of (varname, varvalue) tuples for RTOS support |
idcode | str | None | 16-byte IDCODE for locked devices (e.g., Renesas) |
dict - Status dictionary with connection information
Raises:
JLinkAlreadyRunningError- If J-Link is running andforce=FalseJLinkStartError- If J-Link fails to start or connect to target
disconnect(mcu=None, keep_jlink_running=False)
Disconnect from debug target.
| Parameter | Type | Default | Description |
|---|---|---|---|
mcu | str | None | MCU identifier (optional) |
keep_jlink_running | bool | False | Keep J-Link running for external GDB clients |
dict - Status dictionary
reset_device(halt=False, mcu=None)
Reset the connected device.
| Parameter | Type | Default | Description |
|---|---|---|---|
halt | bool | False | Halt CPU after reset |
mcu | str | None | MCU identifier (optional) |
JLinkNotRunning - If J-Link is not running
flash_device(files, preverify=False, verify=True, run_after=False, mcu=None)
Flash firmware to the connected device.
| Parameter | Type | Description |
|---|---|---|
files | tuple | Tuple of (hexfiles, binfiles, elffiles) where binfiles are (path, address) tuples |
preverify | bool | Verify before flashing (not currently used) |
verify | bool | Verify after flashing (not currently used) |
run_after | bool | Reset and run after flashing |
mcu | str | MCU identifier (e.g., ‘nRF52833_XXAA’) |
JLinkNotRunning - If J-Link GDB server is not running
chip_erase(device, speed='4000', transport='SWD', mcu=None)
Perform full chip erase. This erases ALL flash memory including protection settings.
| Parameter | Type | Default | Description |
|---|---|---|---|
device | str | - | J-Link device name |
speed | str | '4000' | Interface speed in kHz |
transport | str | 'SWD' | Transport protocol (‘SWD’ or ‘JTAG’) |
mcu | str | None | MCU identifier (optional) |
JLinkStartError - If J-Link fails to start
erase_flash(start_addr, length, mcu=None)
Erase a specific region of flash memory.
| Parameter | Type | Description |
|---|---|---|
start_addr | int | Starting address to erase |
length | int | Number of bytes to erase |
mcu | str | MCU identifier (optional) |
JLinkNotRunning - If J-Link is not running
get_jlink_status()
Check the current J-Link connection status.
dict - Status dictionary with connection information
get_arch()
Get the device architecture.
str - Architecture string
Raises: DebuggerNotConnectedError - If debugger is not connected
get_controller()
Get the controller type.
str - Controller type string
Raises: DebuggerNotConnectedError - If debugger is not connected
gdb_reset(halt=False)
Reset the device via GDB.
| Parameter | Type | Default | Description |
|---|---|---|---|
halt | bool | False | Halt CPU after reset |
DebuggerNotConnectedError - If debugger is not connected
start_jlink_gdbserver(device, speed='adaptive', transport='SWD', halt=False, gdb_port=2331)
Start the J-Link GDB server for remote debugging.
| Parameter | Type | Default | Description |
|---|---|---|---|
device | str | - | J-Link device name (e.g., ‘R7FA0E107’, ‘NRF52840_XXAA’) |
speed | str | 'adaptive' | Interface speed in kHz or ‘adaptive’ |
transport | str | 'SWD' | Transport protocol (‘SWD’ or ‘JTAG’) |
halt | bool | False | Whether to halt the device when connecting |
gdb_port | int | 2331 | GDB server port number |
dict with keys:
pid- Process ID of the GDB serverstatus- Status messagegdb_port- Port number the server is listening on
JLinkStartError - If GDB server fails to start
stop_jlink_gdbserver()
Stop the running J-Link GDB server.
get_jlink_gdbserver_status()
Get the current GDB server status.
dict - Status dictionary with keys:
running(bool) - Whether GDB server is runningport(int) - GDB server port (if running)pid(int) - Process ID (if running)
Examples
Flash Firmware and Reset
Chip Erase Before Programming
Error Handling
CLI Commands (Recommended)
For most use cases, the CLI provides a simpler interface:RTT Streaming
For SEGGER Real-Time Transfer (RTT) communication, see the RTT documentation.Supported Devices
J-Link supports a wide range of ARM Cortex-M and other microcontrollers. Common device names:| Manufacturer | Device Name | Description |
|---|---|---|
| Nordic | NRF52840_XXAA | nRF52840 |
| Nordic | NRF52833_XXAA | nRF52833 |
| Nordic | NRF5340_XXAA_APP | nRF5340 Application Core |
| Renesas | R7FA0E107 | RA0E1 Series |
| Renesas | R7FA2L1 | RA2L1 Series |
| STMicro | STM32F103C8 | STM32F1 Series |
| STMicro | STM32F407VG | STM32F4 Series |
| STMicro | STM32L476RG | STM32L4 Series |
Notes
- Debug operations require a J-Link debug probe connected to the Lager Box
- The CLI (
lager debug) is recommended for most use cases - Python API is intended for advanced automation scripts running on the Lager Box
- Always disconnect when finished to release the debug probe
- Flash operations automatically reset and run the device
- Use
chip_erase()to clear protection settings on locked devices - RTT requires an active J-Link connection (see RTT documentation)

