protocol-emulator / all protocols
uart
Spec source
| part | document | page |
| UART at 115200 baud | Maxim AN2141 | p.4, +-3/152, 2% |
| UART at 19200 baud | Maxim AN2141 | p.4, +-3/152, 2% |
| UART at 230400 baud | Maxim AN2141 | p.4, +-3/152, 2% |
| UART at 38400 baud | Maxim AN2141 | p.4, +-3/152, 2% |
| UART at 57600 baud | Maxim AN2141 | p.4, +-3/152, 2% |
| UART at 9600 baud | Maxim AN2141 | p.4, +-3/152, 2% |
Datasheet limits
| firmware | parameter | limit | bound by | needs (cycles) | bound (cycles) | bound (ns) | result | margin | sheet |
| uart_tx_9600 | bit | >= 102083 ns | kernel | 4901 | 5000 | 104166.7 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_tx_9600 | bit | <= 106250 ns | run | 5099 | 5000 | 104166.7 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_tx_19200 | bit | >= 51041.7 ns | kernel | 2452 | 2500 | 52083.3 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_tx_19200 | bit | <= 53125 ns | run | 2549 | 2500 | 52083.3 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_tx_38400 | bit | >= 25520.8 ns | kernel | 1226 | 1250 | 26041.7 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_tx_38400 | bit | <= 26562.5 ns | run | 1274 | 1250 | 26041.7 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_tx_57600 | bit | >= 17013.9 ns | kernel | 818 | 833 | 17354.2 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_tx_57600 | bit | <= 17708.3 ns | run | 848 | 833 | 17354.2 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_tx_115200 | bit | >= 8506.94 ns | kernel | 410 | 417 | 8687.5 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_tx_115200 | bit | <= 8854.17 ns | run | 424 | 417 | 8687.5 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_tx_230400 | bit | >= 4253.47 ns | kernel | 206 | 208 | 4333.3 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_tx_230400 | bit | <= 4427.08 ns | run | 211 | 208 | 4333.3 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_log | bit | >= 8506.94 ns | kernel | 410 | 417 | 8687.5 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
| uart_log | bit | <= 8854.17 ns | run | 424 | 417 | 8687.5 | ok | a cycle | Maxim AN2141 p.4, +-3/152, 2% |
Protocol file and firmware
test/uart.ml
open! Core
open Protocol_emulator
open Pin_trace
module Reg = Host_port.Reg
let frame =
{|
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
|}
;;
let tx ~period = [%string " set p, %{period#Int}%{frame}"]
(* [tx ~period:16], checked when it compiles. *)
let tx16 =
[%firmware
{|
set p, 16
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
|}]
;;
(* 26-bit frame: start, the host's byte, the low 16 bits of the cycle the start bit shows
(both LSB first), stop. [mov y, now] reads four cycles before that edge. *)
let tx_stamped ~period =
[%string
{|
set p, %{period#Int}
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov y, now
add y, 4 ; the cycle the start bit shows
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
byte:
wait t+
out pins, 1
jmp x--, byte
mov osr, y ; then the stamp
set x, 15
stamp:
wait t+
out pins, 1
jmp x--, stamp
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
|}]
;;
(* The first word from the host is the bit period. *)
let tx_host_rate = [%string " wait tx\n pull\n mov p, osr%{frame}"]
(* Time-triggered: a frame every ten periods from one anchor. Each byte autopulls at a
cycle fixed from the anchor, so a late byte sets the underflow fault and moves no edge. *)
let tx_stream ~period =
[%string
{|
set p, %{period#Int}
set pins, 1 ; idle high
mov t, now ; the only anchor
add t, p
frame:
wait t+
set pins, 0 ; start bit
set x, 7
bit:
wait t+
out pins, 1 ; autopull takes the byte before its first bit
jmp x--, bit
wait t+
set pins, 1 ; stop bit
jmp frame
|}]
;;
let stream_config = { Program_config.default with autopull = true; pull_threshold = 8 }
(* Half period one short: the sample lands a cycle after the release. The capture is armed
only while the line is high, as the certificate assumes of every arm. [check] (rounded
(9p - 27) / 19 cycles into the stop bit) balances a slow sender's stop bit against a
fast sender's next start edge, within 4% either way from 16 cycles a bit up. *)
let rx_on ~pin ~period =
let check = ((9 * period) - 18) / 19 in
[%string
{|
set p, %{period#Int}
set y, %{(period / 2) - 1#Int}
idle:
wait 1 pin %{pin#Int} ; line idle
arm:
capture_arm
wait 0 pin %{pin#Int} ; start bit, its edge cycle is in capture
mov t, capture
add t, y
add t, p ; middle of bit 0
set x, 7
bit:
wait t+
in pins, 1
jmp x--, bit
in null, 8
push
sub t, %{(period / 2) - check#Int}
wait t ; the check, a slow sender's stop bit has begun
jmp pin, arm ; high: arm before a fast sender's next start edge
irq ; framing error
jmp idle
|}]
;;
let rx ~period = rx_on ~pin:0 ~period
(* [rx_on] with half the bit period from the host, so the first sample, a period and a
half from the start edge, is three halves, a period the kernel bounds at every load.
The stop bit is checked at its middle, which tolerates 4% sender error either way from
52 cycles a bit, and 5% from 104. *)
let rx_host_rate_on ~pin =
[%string
{|
wait tx
pull
mov p, osr ; half the bit period
idle:
wait 1 pin %{pin#Int} ; line idle
arm:
capture_arm
wait 0 pin %{pin#Int} ; start bit, its edge cycle is in capture
mov t, capture
add t, p
add t, p
add t, p
sub t, 1 ; middle of bit 0, less the cycle the sample lands after
set x, 7
bit:
wait t+
in pins, 1
add t, p ; the next bit's middle
jmp x--, bit
in null, 8
push
wait t ; the check, the middle of the stop bit
jmp pin, arm ; high: arm before a fast sender's next start edge
irq ; framing error
jmp idle
|}]
;;
let rx_host_rate = rx_host_rate_on ~pin:0
let rx_config =
{ Program_config.default with
in_base = 0
; jmp_pin = 0
; capture_pin = 0
; capture_rising = false
}
;;
let stamped =
Certified.plain "uart_tx_stamped" (tx_stamped ~period:8) Program_config.default
;;
(* The keyboard demo loads the certified copy, at the period python/demo_usb.py works out
from the clock the same way. *)
let log =
let baud = 115_200 in
let period = (Bench.clock_hz + (baud / 2)) / baud in
{ Bench.name = "uart_log"
; what =
[%string
"Uart.tx_host_rate: the keyboard demo's log to Pico B, %{period#Int} cycles a \
bit for %{Bench.rate ~unit:\"baud\" period}, on OUT0"]
; source = tx_host_rate
; config = Program_config.default
; assumption = Floor 4
; clock_hz = Bench.clock_hz
; load = Some period
; stimulus = Some { bursts = [ [ 0x55; 0x55 ] ]; quiet = 0; cycles = 20_000 }
}
;;
let bauds = [ 9600; 19200; 38400; 57600; 115200; 230400 ]
(* The bit within 2% of the rate, the 'nasty link' budget a receiver shares. *)
let limits_at ~baud firmware =
let sheet =
{ Datasheet.Sheet.part = [%string "UART at %{baud#Int} baud"]
; document = "Maxim AN2141"
; page = "p.4, +-3/152, 2%"
}
in
let bit = 1e9 /. Float.of_int baud in
[ { Datasheet.firmware
; parameter = "bit"
; limit = At_least (bit *. 0.98)
; sheet
; margin = Cycle
; bound = Datasheet.level ~pin:Datasheet.set_pin ~high:false ()
}
; { firmware
; parameter = "bit"
; limit = At_most (bit *. 1.02)
; sheet
; margin = Cycle
; bound =
Datasheet.run ~pin:Datasheet.set_pin (fun ~clock_hz:_ levels ->
Datasheet.lows levels)
}
]
;;
let limits = limits_at ~baud:115_200 "uart_log"
(* [tx_host_rate] on OUT0 at each common rate, with the bit rounded to the nearest cycle. *)
let at_rate baud =
let period = (Bench.clock_hz + (baud / 2)) / baud in
{ Bench.name = [%string "uart_tx_%{baud#Int}"]
; what =
[%string
"Uart.tx_host_rate: the host sends the bit period, %{period#Int} cycles for \
%{Bench.rate ~unit:\"baud\" period}, on OUT0"]
; source = tx_host_rate
; config = Program_config.default
; assumption = Floor 4
; clock_hz = Bench.clock_hz
; load = Some period
; stimulus = Some { bursts = [ [ 0x55; 0x55 ] ]; quiet = 0; cycles = 25 * period }
}
;;
(* Bits of 16 cycles: 3 Mbaud at 48 MHz. *)
let trace_hz = 48_000_000
let sigrok ?(teeth = []) ~channel ~pin bytes =
{ Sigrok.clock_hz = trace_hz
; decoders =
[ Sigrok.decoder
"uart"
~pins:[ channel, pin ]
~options:[ "baudrate", Int.to_string (trace_hz / 16) ]
]
; expect =
[ [%string "uart=%{channel}-data"], List.map bytes ~f:(sprintf "uart-1: %02X") ]
; joins_after = None
; rejected = None
; teeth
}
;;
let tx_scenario =
{ Scenario.name = "uart_tx"
; peer = (fun () -> Peer.idle 0)
; script =
Scenario.load
~config:Program_config.default
~program:(Firmware.assemble (tx ~period:16))
()
@ [ Write (Reg.tx, [ 0x55; 0xa3 ]); Scenario.start; Run 400; Read (Reg.status, 1) ]
; sigrok =
(* The first stop bit, edge 10, 12 cycles late: sampled low, a framing error. *)
Some
(sigrok
~channel:"tx"
~pin:Program_config.default.out_base
~teeth:
[ [ Shift { pin = Program_config.default.out_base; edge = 10; cycles = 12 } ]
]
[ 0x55; 0xa3 ])
}
;;
(* A read frame ends with the next rx head on miso. After emptying the fifo that is
unwritten RAM, zero here but X in Verilog, so the host always leaves a word behind. *)
let rx_scenario =
let period = 16 in
let first = [ 0x55; 0xa3; 0xff; 0x00 ]
and second = [ 0x0f; 0xf0; 0x5a ] in
let drive bytes =
Step.Drive { pin = 0; levels = Protocol_models.serial_levels bytes ~period ~stop:1 }
in
{ Scenario.name = "uart_rx"
; peer = (fun () -> Peer.idle 1)
; script =
Scenario.load
~assumptions:{ System_lockstep.Assumptions.none with single_capture_edge = true }
~config:rx_config
~program:(Firmware.assemble (rx ~period))
()
@ [ Scenario.start
; drive first
; Read (Reg.rx, 3)
; drive second
; Read (Reg.rx, 3)
; Read (Reg.status, 1)
]
; sigrok =
(* The first stop bit, edge 9, 12 cycles late: a framing error. *)
Some
(sigrok
~channel:"rx"
~pin:rx_config.in_base
~teeth:[ [ Shift { pin = rx_config.in_base; edge = 9; cycles = 12 } ] ]
(first @ second))
}
;;
let protocol =
let plain = Certified.plain in
{ Protocol.name = "uart"
; certified =
[ plain "uart_tx" (tx ~period:8) Program_config.default
(* The same UART at the slower bit period the tests use. *)
; plain "uart_tx16" (Timed_program.source tx16) Program_config.default
; plain
~period:434
~period_floor:4
~no_wrap:true
"uart_tx_host_rate"
tx_host_rate
Program_config.default
; Certified.receiver "uart_rx" (rx ~period:16) rx_config
(* Half the bit at 115200 baud and 48 MHz. *)
; Certified.receiver
~period:208
~period_floor:4
"uart_rx_host_rate"
rx_host_rate
rx_config
]
; time_triggered = [ plain "uart_tx_stream" (tx_stream ~period:8) stream_config ]
; bench = List.map bauds ~f:at_rate
; loaded_from_hex = [ log ]
; limits =
limits
@ List.concat_map bauds ~f:(fun baud ->
limits_at ~baud [%string "uart_tx_%{baud#Int}"])
; unlimited = []
; swept =
(let swept ?period name text =
{ Swept.name
; watch = "line"
; on_wire = Swept.line
; period
; bursts = Swept.bytes text
}
in
[ swept "uart_tx" "Jane"
; swept "uart_tx16" "Jane"
; swept ~period:434 "uart_tx_host_rate" "St"
])
; not_swept =
[ "uart_rx", "a receiver: it samples, and nothing on the chip sends to it"
; "uart_rx_host_rate", "a receiver: both_roles.ml sends to it on a wire"
; ( "uart_tx_stream"
, "time-triggered: it underflows, a sticky fault, once the host stops" )
; ( "uart_tx_stamped"
, "26 bits 8 cycles apart, and the chip's clock in them, which the model cannot \
know" )
]
; scenarios = [ tx_scenario; rx_scenario ]
; decoded = []
}
;;
certified firmware uart_tx
set p, 8
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
certified firmware uart_tx16
set p, 16
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
certified firmware uart_tx_host_rate
wait tx
pull
mov p, osr
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
certified firmware uart_rx
set p, 16
set y, 7
idle:
wait 1 pin 0 ; line idle
arm:
capture_arm
wait 0 pin 0 ; start bit, its edge cycle is in capture
mov t, capture
add t, y
add t, p ; middle of bit 0
set x, 7
bit:
wait t+
in pins, 1
jmp x--, bit
in null, 8
push
sub t, 2
wait t ; the check, a slow sender's stop bit has begun
jmp pin, arm ; high: arm before a fast sender's next start edge
irq ; framing error
jmp idle
certified firmware uart_rx_host_rate
wait tx
pull
mov p, osr ; half the bit period
idle:
wait 1 pin 0 ; line idle
arm:
capture_arm
wait 0 pin 0 ; start bit, its edge cycle is in capture
mov t, capture
add t, p
add t, p
add t, p
sub t, 1 ; middle of bit 0, less the cycle the sample lands after
set x, 7
bit:
wait t+
in pins, 1
add t, p ; the next bit's middle
jmp x--, bit
in null, 8
push
wait t ; the check, the middle of the stop bit
jmp pin, arm ; high: arm before a fast sender's next start edge
irq ; framing error
jmp idle
time_triggered firmware uart_tx_stream
set p, 8
set pins, 1 ; idle high
mov t, now ; the only anchor
add t, p
frame:
wait t+
set pins, 0 ; start bit
set x, 7
bit:
wait t+
out pins, 1 ; autopull takes the byte before its first bit
jmp x--, bit
wait t+
set pins, 1 ; stop bit
jmp frame
bench firmware uart_tx_9600, 48 MHz
wait tx
pull
mov p, osr
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
bench firmware uart_tx_19200, 48 MHz
wait tx
pull
mov p, osr
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
bench firmware uart_tx_38400, 48 MHz
wait tx
pull
mov p, osr
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
bench firmware uart_tx_57600, 48 MHz
wait tx
pull
mov p, osr
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
bench firmware uart_tx_115200, 48 MHz
wait tx
pull
mov p, osr
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
bench firmware uart_tx_230400, 48 MHz
wait tx
pull
mov p, osr
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
hex firmware uart_log, 48 MHz
wait tx
pull
mov p, osr
set pins, 1 ; idle high
idle:
wait tx
pull
set x, 7
mov t, now ; anchor the frame
set pins, 0 ; start bit
add t, p
bit:
wait t+
out pins, 1
jmp x--, bit
wait t+
set pins, 1 ; stop bit
wait t
jmp idle
Certificate
uart_log: not in CI's certificate matrixuart_rx: proved in CI by inductive_certificate, ocaml.yamluart_rx_host_rate: proved in CI by inductive_certificate, ocaml.yamluart_tx: proved in CI by inductive_certificate, ocaml.yamluart_tx16: proved in CI by inductive_certificate, ocaml.yamluart_tx_115200: not in CI's certificate matrixuart_tx_19200: not in CI's certificate matrixuart_tx_230400: not in CI's certificate matrixuart_tx_38400: not in CI's certificate matrixuart_tx_57600: not in CI's certificate matrixuart_tx_9600: not in CI's certificate matrixuart_tx_host_rate: proved in CI by inductive_certificate, ocaml.yamluart_tx_stream: proved in CI by inductive_certificate, ocaml.yaml
Clock window
| firmware | shipped Hz | lowest Hz | set by | highest Hz | set by |
| uart_tx_9600 | 48000000 | 47068236 | bit <= 106250 ns | 48969796 | bit >= 102083 ns |
| uart_tx_19200 | 48000000 | 47077648 | bit <= 53125 ns | 48959999 | bit >= 51041.7 ns |
| uart_tx_38400 | 48000000 | 47096471 | bit <= 26562.5 ns | 48940408 | bit >= 25520.8 ns |
| uart_tx_57600 | 48000000 | 47096472 | bit <= 17708.3 ns | 48901224 | bit >= 17013.9 ns |
| uart_tx_115200 | 48000000 | 47209410 | bit <= 8854.17 ns | 48901227 | bit >= 8506.94 ns |
| uart_tx_230400 | 48000000 | 47209416 | bit <= 4427.08 ns | 48666124 | bit >= 4253.47 ns |
| uart_log | 48000000 | 47209410 | bit <= 8854.17 ns | 48901227 | bit >= 8506.94 ns |
Time taken
Not available: no row in demo/speed.tsv.
Board witnesses
| test/board | time | demo | result | commit | windows proved | teeth |
| self_timing | 2026-11-03T07:52:58 | self_timing | PASS | b938551d48 | 3 of 3 (2 spec, 1 own) | half of the first byte's bit 0 inverted; the first frame's second edge 12 samples late |
CI decodes each capture again and holds it to the bytes sent and the windows, each tooth refused: demo/witness.py check.