Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

DMI and the Debug Module

The Debug Module Interface (DMI) is a separate layer above the WCH-Link command protocol: a single 0x08 command carries one DMI transaction. The register addresses and semantics come from the RISC-V Debug Specification 0.13.2 and WCH’s QingKe debug manuals.

Sources: DmiOp / DmiOpResponse in src/commands/mod.rs, src/dmi.rs, src/regs.rs.

0x08 transaction layout

Request payload (6 bytes):

OffsetSizeField
01addr — DMI register address
1–44data (u32_be), ignored for NOP/READ
51op — 0x00 NOP, 0x01 READ, 0x02 WRITE

Response payload uses the same 6-byte shape; op is now a status:

opMeaningDmiOpResponse helper
0x00Successis_success()
0x02Failedis_failed()
0x03Busy / failedis_busy(), also counted by is_failed()

Example — read DMSTATUS (0x11):

→ 81 08 06 11 00 00 00 00 01
← 82 08 06 11 00 00 00 00 00

dmi_read polls on busy (up to 100 retries with 10 ms sleeps) and raises Error::NotAttached when it sees op == 0x03 && data == 0xffffffff && addr == 0x7d.

DMI register addresses

From src/regs.rs:

AddressNameNotes
0x04DMDATA0Abstract command data
0x05DMDATA1
0x10DMCONTROLHalt/resume, dmactive
0x11DMSTATUSallhalted, anyhalted, havereset
0x12DMHARTINFO
0x16DMABSTRACTCScmderr, busy
0x17DMCOMMANDAbstract command register
0x18DMABSTRACTAUTONot available on QingKeV2 / QingKeV3
0x20–0x27DMPROGBUF0–7Program buffer
0x40DMHALTSUM0

CSR addresses used with abstract commands (16-bit command.regno): 0x300 MSTATUS, 0x301 MISA, 0x305 MTVEC, 0x340 MSCRATCH, 0x341 MEPC, 0x342 MCAUSE, 0x343 MTVAL, 0x7b1 DPC, 0xf12 MARCHID, 0xf13 MIMPID. General-purpose registers x0–x31 occupy 0x1000–0x101f.

Abstract command errors

AbstractcsCmdErr::try_from_cmderr maps the cmderr field of DMABSTRACTCS:

ValueMeaning
0No error
1Busy (register access during an abstract command)
2Command not supported
3Exception during execution
4Hart in wrong state (not halted/running)
5Bus error (alignment, size, timeout)
6Parity error (WCH extension)
7Other

ProbeSession::clear_abstractcs_cmderr writes 0b111 to the cmderr field to clear it (W1C), and clear_dmstatus_havereset acknowledges havereset.

Flash/option unlock keys

src/dmi.rs defines two keys used when unlocking flash/option-byte writes through the probe’s debug interface:

KEY1 = 0x45670123
KEY2 = 0xCDEF89AB

Higher-level operations

DebugModuleInterface (implemented for WchLink) provides dmi_nop, dmi_read, dmi_write and typed register helpers. ProbeSession builds on them for:

  • ensure_mcu_halt / ensure_mcu_resume — write 0x80000001 to DMCONTROL (0x10) and poll DMSTATUS;
  • register and memory inspection via abstract commands and the program buffer;
  • dump_regs, dump_core_csrs, dump_pmp_csrs, dump_dmi.

The RISC-V Debug Specification details are out of scope for this project documentation. See External References for the QingKeV2 debug manual and other implementations.