Host-pod protocol
How a host talks to the pod: over USB, or over a stream socket to
anything that answers as the pod does (bdm-stand-in). It is the complete specification: a stand-in written from
it alone answers as the pod does. The pod’s implementation is
crates/proto/src/usb.rs; where the two disagree, that is a bug
in one of them.
Protocol version: 11 (PROTOCOL_VERSION), raised on any change to a
request or reply.
Transport
USB:
- VID:PID 1209:0001 (pid.codes’ shared test pair).
- Serial number string (iSerialNumber): the chip’s unique ID, 8
bytes, as 16 uppercase hex digits, first byte first (pico-sdk’s
pico_get_unique_board_id_string): the RP2040’s flash unique ID
(SPI command 0x4B), the RP2350’s OTP chip ID (rows 0-3, most
significant byte first). Example: E6613852833B2A2F. A pod whose
ID cannot be read has no serial number string.
- Vendor class (0xFF), interface 0, one bulk OUT and one bulk IN
endpoint, 64-byte packets (full speed).
- Interface 1: picotool’s reset interface (vendor class, subclass 0,
protocol 1, no endpoints; pico-sdk
reset_interface.h). A class or
vendor control OUT request 1 to it, wIndex its number, reboots the
RP2040 into its USB bootloader; wValue’s low 7 bits are the boot
ROM’s interface-disable mask. bdm bootsel and picotool -f send
it.
- One request per OUT packet, one reply per IN packet. An OUT
packet longer than 64 bytes is answered BadRequest, tagged 0.
- The pod writes each reply before it reads the next request, so a
reply the host has not read holds back every later request: a host
that gives up on a reply keeps a read waiting for it, or sends no
request until it has read it.
- A request that goes 8 s without a word over the BDM link is a
firmware fault: the pod’s watchdog resets it (PLAN.md, Firmware,
Watchdog), so it leaves the bus without a reply and enumerates
again. A host that waits for a reply gives up well before. An idle
host never trips it.
Stream (Unix or TCP socket):
- Each message, either way, is one length byte (1-64) followed by that
many bytes: the packet USB would carry.
- A length byte of 0 or over 64 is a broken stream: the receiver
closes the connection.
- One client at a time; a server closes any other connection at once
while its client is connected. A client that has closed its end is
gone even if the server has not yet read its end of file: before
refusing a connection, a server waits up to 100 ms for the current
client’s end of file. Each
bdm run is one connection, so runs
follow each other within milliseconds.
Either way:
- The pod answers requests one at a time, in order: each reply before
the next request is read. A host may send a few requests before
reading the first one’s reply (the
bdm host keeps up to 4
ReadMemory requests of a block outstanding, IN_FLIGHT; writes go
one at a time, so none runs behind a failed one); the later ones wait
in the endpoint or the socket, and the pod starts each as soon as it
has written the reply before. Tags tell their replies apart. A
reply’s timeout runs from when the host starts waiting for it, since
the pod starts a request no earlier than its reply to the one
before. A host that stops at a failed reply sends nothing more;
the requests already sent behind it still run, and their replies,
read later, are stale. No batching beyond what one request carries
(15 raw words, 56 memory bytes).
- Every request but Hello starts with a tag byte, which the reply
echoes. A host that gave up waiting for a reply (its timeout, an
interrupt) may later read it ahead of the next request’s: it takes
only the reply with its request’s tag and discards any other. A
host’s tags are 1-255, a new one for each request, so the last 254
requests’ replies are told apart; tag 0 marks the pod’s reply to a
packet with no tag.
- Multi-byte fields of this protocol are little-endian. Memory bytes
are the target’s, in address order (big-endian values).
- A host sends Hello first and stops if the version is not its own.
Hello is untagged and the same in every version, so the check works
across versions: the request is the one byte 0x0E, the reply status
0 (Ok) and the u32 version, 5 bytes. No tagged reply has a first
byte of 0, so a host reading for Hello’s reply discards any other.
- Then a host sends GetClock, timed at 10 kHz, and sizes each later
request’s timeout from the rate it gives (Semantics, a request’s
time).
Hello
| Request |
Reply |
| 0x0E (the whole packet) |
0x00, then u32 protocol version |
A one-byte packet other than 0x0E is a tag with no request:
BadRequest.
Requests
Byte 0 is the tag, byte 1 the code; the body follows. Every body
length is exact, as the table gives it; any other length, a packet
with no code or an unknown code is answered BadRequest. “FREEZE” means
refused with NoFreeze unless the target is in background mode, since a
command’s clocks would otherwise be BKPT pulses that halt it.
| Code |
Request |
Body |
Reply body (status Ok) |
FREEZE |
| 0x01 |
Transfer |
1-15 words, u16 each (2-30 bytes, even) |
one u32 per word: the 17 bits shifted in, bit 16 the status bit, bits 31-17 zero |
no |
| 0x02 |
Pins |
none |
u8 levels, u16 VDD in mV, ATEMP field |
no |
| 0x03 |
ResetHalt |
none |
ATEMP field |
no |
| 0x04 |
Halt |
none |
ATEMP field |
no |
| 0x05 |
Go |
none |
none |
yes |
| 0x06 |
SetClock |
u32 DSCLK Hz |
u32 DSCLK Hz set |
no |
| 0x07 |
ReadRegister |
u8 register code |
u32 value |
yes |
| 0x08 |
WriteRegister |
u8 register code, u32 value |
none |
yes |
| 0x09 |
ReadSystemRegister |
u8 select code |
u32 value |
yes |
| 0x0A |
WriteSystemRegister |
u8 select code, u32 value |
none |
yes |
| 0x0B |
ReadMemory |
u32 address, u8 size, u8 length |
exactly length bytes |
yes |
| 0x0C |
WriteMemory |
u32 address, u8 size, 1-56 data bytes (7-63 bytes with tag and code) |
none |
yes |
| 0x0D |
Call |
u32 address |
none |
yes |
| 0x0F |
ResetRun |
none |
none |
no |
| 0x10 |
Resync |
none |
ATEMP field, kind 2 |
yes |
| 0x11 |
Version |
none |
the firmware’s version, ASCII: crate version and git commit (0.1.0 1a2b3c4, -dirty if built with uncommitted changes) |
no |
| 0x12 |
Hold |
none |
none |
no |
| 0x13 |
BerrPulse |
none |
u32: 1 if a hung access ended, else 0 |
yes |
| 0x14 |
GetClock |
none |
u32 DSCLK Hz in effect |
no |
Fields:
- Transfer word: the 16 data bits sent. The 17th, the control bit
(CPU32RM 7.2.7), always goes out 0.
- Register code: D0-D7 are 0-7, A0-A7 are 8-15 (RDREG/RAREG’s field,
CPU32RM 7.2.8.4). 16-255 are BadRequest.
- System register select code (CPU32RM 7.2.8.6): RPC 0, PCC 1, ATEMP 8,
FAR 9, VBR 0xA, SR 0xB, USP 0xC, SSP 0xD, SFC 0xE, DFC 0xF, for reads
and writes alike. Any other byte is BadRequest.
- Size: 0 byte, 1 word, 2 long (CPU32RM 7.2.8.1). Any other byte, 3
included, is BadRequest.
- Memory length: 1-56 bytes, a multiple of the size; otherwise
BadRequest. Reads go through SFC, writes through DFC; the host sets
them.
- Address: any 32-bit value. The pod does not check alignment: a word
or long access at an odd address is the target’s address error,
BusError.
- DSCLK Hz: any u32 (below).
- Pins levels: bit 0 FREEZE, bit 1 RESET, bit 2 DS, bit 3 BERR; set
means high; bits 4-7 zero. DSO is not reported.
- VDD: millivolts, read through the pod’s divider, saturating at
- A stand-in with no supply to measure reports 5000.
- ATEMP field: u8 kind, then u32 ATEMP. Kind 0: not in background
mode, ATEMP 0 (Pins with FREEZE low only). Kind 1, fresh: read first
after entry, so the entry source. Kind 2, stale: the pod did not see
the entry (it started with FREEZE high), so commands may have come
first and the value is not an entry source. Kind 3, unread: in
background mode, but the read after entry failed; ATEMP 0. Pins
carries kind 0 exactly when FREEZE is low; Halt and ResetHalt never
carry it.
Semantics:
- Order of checks: the request is parsed (BadRequest); then, except
for Version, SetClock, GetClock, ResetHalt, ResetRun, Resync, Hold
and BerrPulse, the ATEMP read below, whose failure is the reply’s status
(once: the pod keeps kind 3 and does not read again until FREEZE
moves, so Pins, Transfer and ResetHalt still work); then the
request’s own checks (NoTarget, then NoFreeze); then the command.
- Transfer is raw: the words go out as given, with no guard and no DSI
release, whether or not the target is in background mode, and on the
link as the last request left it: undriven after the pod’s boot until
a request drives it. Every other command is sequenced by the pod
(CPU32RM 7.2.8): BKPT/DSCLK high and DSI driven once FREEZE is
checked (no DSCLK edge; BerrPulse and Resync too), operands out, NOPs
until the result, not-ready retried at each step up to 1000 NOPs or
20 ms, whichever comes first, then NotReady.
- In a sequenced command (Resync’s NOPs and edges, the ATEMP read and
the BERR abort’s read included) FREEZE is read from the pin before
every word: once it is low the CPU has left background mode (a reset
from the target’s side, say), and the pod stops clocking at once and
answers NoFreeze, since each further clock would be a BKPT pulse.
What came before that word has happened.
- ReadMemory and WriteMemory: the accesses in address order, each
size bytes, as READ then DUMPs or WRITE then FILLs. On BusError
the accesses before the failing one have happened; a write is not
undone.
- Before carrying out any request, the pod reads ATEMP if FREEZE is
high and FREEZE has had an edge since its last read, so ATEMP is the
first command after every entry, the CPU’s own (BGND, a breakpoint, a
double bus fault) included. It keeps the value until FREEZE moves,
and reports it in Pins, Halt and ResetHalt: 0 hardware breakpoint, 1
BGND, SSW in the high word and 0xFFFF in the low for a double bus
fault (CPU32RM Table 7-2).
- Halt on a target already in background mode returns the kept ATEMP
and does nothing else. Otherwise Halt holds BKPT low until FREEZE
asserts. ResetHalt holds BKPT low and pulses RESET low for 10 ms,
whatever the target’s state. Both are refused with NoTarget below
4.5 V VDD, and NoFreeze if FREEZE does not assert within 100 ms
(then BKPT is released); ResetHalt with ResetHeld instead if RESET
is still low then.
- ResetRun drives BKPT high and DSI off, pulses RESET low for 10 ms
and waits up to 100 ms for RESET to rise: the target comes out of
reset running, background mode off until the next ResetHalt.
Refused with NoTarget below 4.5 V VDD; ResetHeld if RESET is still
low after the 100 ms (the target holds it; it runs, background mode
off, once it lets go).
- Halt guard: after the pod’s own ResetRun, until its next ResetHalt
or until FREEZE’s edge count moves (an entry shows background mode
on), Halt on a running target and Hold are refused with BdmDisabled,
BKPT untouched: BKPT low would be a breakpoint exception in the
running program. After the pod’s start, or a reset it did not make,
it cannot know, and goes ahead.
- Resync brings a halted CPU’s serial link back into frame, as after a
pod restart under it (lone DSCLK edges while the Pico boots): it
clocks NOP pairs, and while the second does not read “command
complete” (CPU32RM 7.2.8.15) adds one lone DSCLK edge, at most 17
times; Unexpected if it never does. Then it reads ATEMP and
reports it stale. On request only. A command the CPU
took out of frame may take the NOPs as operands. NoTarget below
4.5 V; NoFreeze if FREEZE is low.
- Hold drives BKPT low and replies, clocking nothing and reading no
ATEMP: a running CPU with background mode enabled halts, and a CPU
that resets meanwhile (its watchdog, say) comes out of reset in
background mode, BKPT being low as RESET rises. The next request’s
first word releases BKPT, after its ATEMP read. NoTarget below
4.5 V; BdmDisabled under the halt guard.
- A halted CPU still not ready after the retries (at the ATEMP read or
in a command) is taken to be in a bus cycle no device answers, as
happens with SIMCR’s FRZBM set: the pod pulses BERR for 10 us, reads
the bus error that ends the access, and replies BusError instead of
NotReady; the CPU then takes commands again. BerrPulse does the same
on request. Never with FREEZE low: a running program would take
BERR as a bus error. FREEZE is read from the pin right before the
pulse; low, the reply is NoFreeze.
- Halt works only if BDM was enabled at the target’s last reset (BKPT
low as RESET rises, MC68331UM 5.10.2.1), as ResetHalt does. Without
it BKPT is a breakpoint exception, vector $0C (MC68331UM 5.10.2.2.1,
Table 5-3): the program takes it, and Halt answers NoFreeze. The
halt guard refuses it first where the pod knows.
- Go and Call release DSI after their last word, holding it to the end
of that word’s last DSCLK high phase, at least one CLKOUT at any
target clock the rate is in spec for (PLAN.md, PIO program); nothing is clocked after them while the target runs.
After their Ok the target runs: Go at RPC, Call at the address with
the return address stacked (CPU32RM 7.2.8.12, 7.2.8.13).
- Call waits up to 100 ms for FREEZE to fall. If it stays high,
stacking took a bus or address error and the CPU stayed in
background mode (CPU32RM 7.2.8.13): the pod drives DSI again, reads
the error with a NOP and answers BusError, the target halted, its
registers and the kept ATEMP as before. FREEZE is read from the pin
before DSI is driven and before each NOP: found low, stacking took
longer than the wait and worked, and the reply is Ok. Stacking hung
on a bus cycle no device answers reads not ready, and gets the BERR
abort.
- SetClock sets DSCLK’s rate and replies with the rate in effect:
10 kHz to 4 MHz, never faster than asked unless asked for less than
10 kHz, which it then sets (never slower). The floor bounds every
request’s time, so a host’s timeout can cover the slowest (below).
A stand-in with no clock answers with the asked rate held to
10 kHz-4 MHz.
- GetClock replies with the rate in effect, the value the last
SetClock replied with; before any SetClock since the pod started,
its starting rate, 1 MHz (
dsclk::DEFAULT_HZ). The rate carries
from one host to the next.
- A request’s time: its words at the rate in effect (17 bits each,
plus the pod’s handling) and its not-ready waits, each at most
1000 NOPs or 20 ms, whichever comes first (
pod::Workload counts
both for a request; a host takes the rate GetClock or SetClock last
gave, and 10 kHz for its first GetClock), plus fixed waits: a reset’s 10 ms pulse and 100 ms for
RESET or FREEZE, Call’s 100 ms. mc683xx-bdm waits for each reply
1 s beyond its words and waits.
- No state carries from one request to the next except the target’s
own, DSCLK’s rate, the kept ATEMP and the halt guard.
Replies
Byte 0 is the request’s tag, byte 1 FREEZE’s edge count, byte 2 the
status. The reply body follows only for Ok, exactly as the request
table gives it; every other status is a three-byte reply.
- FREEZE’s edge count: its low 8 bits, as the reply is written (the
pod’s FREEZE watch, PLAN.md, Firmware). A count that moved between
two replies means the CPU left background mode in between, perhaps
to enter it again: a reset under Hold, say, which FREEZE’s level
alone does not show. A stand-in counts its model’s entries and
exits.
| Status |
Name |
Meaning |
| 0 |
Ok |
Done; the body is the result |
| 1 |
BadRequest |
Not a request the pod parses: unknown code, wrong length, field out of range |
| 2 |
NoTarget |
Target VDD below 4.5 V |
| 3 |
NoFreeze |
FREEZE did not assert, or is not asserted for a request that needs it |
| 4 |
BusError |
The access ended in a bus or address error |
| 5 |
Illegal |
The target refused the command |
| 6 |
NotReady |
Still not ready after the retries |
| 7 |
Unexpected |
A response the command’s sequence does not allow, or the pod could not read VDD |
| 8 |
BdmDisabled |
Halt or Hold under the halt guard: the pod’s last reset was ResetRun |
| 9 |
ResetHeld |
RESET still low 100 ms after the pod’s reset pulse: the target holds it |
Checking
cargo test -p mc683xx-bdm-proto round-trips every request and reply; a
change here goes with a change to usb.rs and its tests, and a raised
PROTOCOL_VERSION if the wire changes.