protocol-emulator / all protocols

can

Spec source

partdocumentpage
CAN 2.0ABosch CAN 2.0, BCANPSV2.0 Rev. 3s.2.15 p.2-6, eleven recessive bits
SN65HVD230TI SLOS346Op.1, up to 1 Mbps

Datasheet limits

firmwareparameterlimitbound byneeds (cycles)bound (cycles)bound (ns)resultmarginsheet
canrecessive>= 1000 nskernel49962000.0oka cycleTI SLOS346O p.1, up to 1 Mbps
candominant>= 1000 nskernel49962000.0oka cycleTI SLOS346O p.1, up to 1 Mbps
canidle to SOF>= 22000 nsrun1057116524270.8oka cycleBosch CAN 2.0, BCANPSV2.0 Rev. 3 s.2.15 p.2-6, eleven recessive bits
can_senderrecessive>= 1000 nskernel49962000.0oka cycleTI SLOS346O p.1, up to 1 Mbps
can_senderdominant>= 1000 nskernel49962000.0oka cycleTI SLOS346O p.1, up to 1 Mbps
can_senderidle to SOF>= 22000 nsrun1057116524270.8oka cycleBosch CAN 2.0, BCANPSV2.0 Rev. 3 s.2.15 p.2-6, eleven recessive bits
can_receiverACK>= 1000 nskernel49962000.0oka cycleTI SLOS346O p.1, up to 1 Mbps

Protocol file and firmware

test/can.ml
open! Core
open Protocol_emulator

let tx_pin = 6
let period = 96
let shortest_period = 15

let config =
  { Program_config.default with
    in_base = tx_pin
  ; in_count = 1
  ; out_base = tx_pin
  ; set_base = tx_pin
  ; jmp_pin = tx_pin
  ; out_shift = Left
  ; autopull = true
  ; crc_width = 15
  ; crc_poly = 0x4599
  ; crc_init = 0
  ; crc_reflect = false
  }
;;

(* The level before each bit is read back from the pin. A change starts a run, an equal
   level counts it down in y, and y running out means five equal bits. A stuff bit is the
   complement of the pin and starts a run. SOF isn't shifted through the CRC, but a
   dominant bit from 0 leaves it at 0. *)
let firmware =
  [%firmware
    {|
    wait tx
    pull
    mov p, osr               ; the bit period
    mov t, now
    add t, p
    wait t+
    set pins, 1              ; recessive
frame:
    wait tx
    pull
    mov x, osr               ; bits after SOF, less one
    out null, 16             ; the next out autopulls the first word of them
    crc_init
    mov t, now
    add t, p
    wait t+
    set pins, 0              ; SOF
    set y, 3
dbit:
    jmp pin, dhigh
    wait t+
    out pins, 1
    jmp pin, dchanged
    jmp dsame
dhigh:
    wait t+
    out pins, 1
    jmp pin, dsame
dchanged:
    set y, 3
    jmp dnext
dsame:
    jmp y--, dnext
    wait t+
    mov pins, !pins          ; stuff
    set y, 3
dnext:
    jmp x--, dbit
    in crc, 15
    mov osr, isr
    set x, 14
cbit:
    jmp pin, chigh
    wait t+
    out pins, 1
    jmp pin, cchanged
    jmp csame
chigh:
    wait t+
    out pins, 1
    jmp pin, csame
cchanged:
    set y, 3
    jmp cnext
csame:
    jmp y--, cnext
    wait t+
    mov pins, !pins          ; stuff
    set y, 3
cnext:
    jmp x--, cbit
    wait t+
    set pins, 1              ; CRC delimiter
    set x, 11                ; ACK slot and delimiter, EOF, intermission
tail:
    wait t+
    jmp x--, tail
    jmp frame
|}
      ~config
      ~period_floor:shortest_period]
;;

module Frame = struct
  type t =
    { id : int
    ; rtr : bool
    ; dlc : int
    ; data : int list
    }
  [@@deriving sexp_of, compare, equal]

  let data ~id data = { id; rtr = false; dlc = List.length data; data }
  let remote ~id ~dlc = { id; rtr = true; dlc; data = [] }
end

let msb_first value ~width =
  List.init width ~f:(fun i -> (value lsr (width - 1 - i)) land 1 = 1)
;;

let crc15 bits =
  List.fold bits ~init:0 ~f:(fun crc bit ->
    Crc.step ~width:15 ~poly:0x4599 ~reflect:false crc ~bit:(Bool.to_int bit))
;;

(* ID, RTR, IDE, r0, DLC and data: what the host sends *)
let fields (f : Frame.t) =
  if f.id < 0 || f.id >= 1 lsl 11 || f.dlc < 0 || f.dlc > 15
  then raise_s [%message "BUG: not a CAN 2.0A frame" (f : Frame.t)];
  msb_first f.id ~width:11
  @ [ f.rtr; false; false ]
  @ msb_first f.dlc ~width:4
  @ List.concat_map f.data ~f:(msb_first ~width:8)
;;

let stuff bits =
  List.fold bits ~init:([], None, 0) ~f:(fun (sent, last, run) bit ->
    let run = if Option.equal Bool.equal last (Some bit) then run + 1 else 1 in
    if run = 5
    then not bit :: bit :: sent, Some (not bit), 1
    else bit :: sent, Some bit, run)
  |> fun (sent, _, _) -> List.rev sent
;;

let crc f = crc15 (false :: fields f)

let line f =
  let unstuffed = false :: fields f in
  stuff (unstuffed @ msb_first (crc15 unstuffed) ~width:15)
  @ List.init 13 ~f:(fun _ -> true)
;;

let words f =
  let fields = fields f in
  (List.length fields - 1)
  :: (List.chunks_of fields ~length:16
      |> List.map ~f:(fun chunk ->
        List.foldi chunk ~init:0 ~f:(fun i word bit ->
          word lor (Bool.to_int bit lsl (15 - i)))))
;;

(* A cursor over the line that removes stuff bits while [stuffed], and fails on one that
   doesn't change the level. *)
let decode levels =
  let open Or_error.Let_syntax in
  let rest = ref levels
  and last = ref None
  and run = ref 0
  and index = ref 0 in
  let raw () =
    match !rest with
    | [] -> Or_error.error_s [%message "the line ends" (!index : int)]
    | bit :: tail ->
      rest := tail;
      incr index;
      Ok bit
  in
  let rec stuffed () =
    let%bind bit = raw () in
    if !run = 5
    then
      if Option.equal Bool.equal !last (Some bit)
      then Or_error.error_s [%message "stuff error" ~bit:(!index - 1 : int)]
      else (
        last := Some bit;
        run := 1;
        stuffed ())
    else (
      run := if Option.equal Bool.equal !last (Some bit) then !run + 1 else 1;
      last := Some bit;
      return bit)
  in
  let rec take n ~f =
    if n = 0
    then return []
    else (
      let%bind bit = f () in
      let%map bits = take (n - 1) ~f in
      bit :: bits)
  in
  let value bits = List.fold bits ~init:0 ~f:(fun v b -> (v lsl 1) lor Bool.to_int b) in
  let%bind sof = stuffed () in
  let%bind () = if sof then Or_error.error_s [%message "no SOF"] else return () in
  let%bind id = take 11 ~f:stuffed in
  let%bind rtr = stuffed () in
  let%bind ide_r0 = take 2 ~f:stuffed in
  let%bind () =
    if List.exists ide_r0 ~f:Fn.id
    then Or_error.error_s [%message "IDE or r0 recessive"]
    else return ()
  in
  let%bind dlc = take 4 ~f:stuffed in
  let dlc = value dlc in
  let bytes = if rtr then 0 else Int.min dlc 8 in
  let%bind data = take (8 * bytes) ~f:stuffed in
  let unstuffed = (false :: id) @ (rtr :: ide_r0) @ msb_first dlc ~width:4 @ data in
  let%bind crc = take 15 ~f:stuffed in
  (* a stuff bit after the CRC's last five, if they were equal *)
  let%bind () =
    if !run = 5
    then (
      let%bind bit = raw () in
      if Option.equal Bool.equal !last (Some bit)
      then Or_error.error_s [%message "stuff error after the CRC"]
      else return ())
    else return ()
  in
  let%bind () =
    if value crc = crc15 unstuffed
    then return ()
    else
      Or_error.error_s
        [%message
          "CRC" ~sent:(value crc : Int.Hex.t) ~expected:(crc15 unstuffed : Int.Hex.t)]
  in
  let%bind tail = take 13 ~f:raw in
  let%bind () =
    if List.for_all tail ~f:Fn.id
    then return ()
    else Or_error.error_s [%message "delimiter, ACK or EOF dominant" (tail : bool list)]
  in
  return
    { Frame.id = value id
    ; rtr
    ; dlc
    ; data = List.chunks_of data ~length:8 |> List.map ~f:value
    }
;;
certified firmware can
    wait tx
    pull
    mov p, osr               ; the bit period
    mov t, now
    add t, p
    wait t+
    set pins, 1              ; recessive
frame:
    wait tx
    pull
    mov x, osr               ; bits after SOF, less one
    out null, 16             ; the next out autopulls the first word of them
    crc_init
    mov t, now
    add t, p
    wait t+
    set pins, 0              ; SOF
    set y, 3
dbit:
    jmp pin, dhigh
    wait t+
    out pins, 1
    jmp pin, dchanged
    jmp dsame
dhigh:
    wait t+
    out pins, 1
    jmp pin, dsame
dchanged:
    set y, 3
    jmp dnext
dsame:
    jmp y--, dnext
    wait t+
    mov pins, !pins          ; stuff
    set y, 3
dnext:
    jmp x--, dbit
    in crc, 15
    mov osr, isr
    set x, 14
cbit:
    jmp pin, chigh
    wait t+
    out pins, 1
    jmp pin, cchanged
    jmp csame
chigh:
    wait t+
    out pins, 1
    jmp pin, csame
cchanged:
    set y, 3
    jmp cnext
csame:
    jmp y--, cnext
    wait t+
    mov pins, !pins          ; stuff
    set y, 3
cnext:
    jmp x--, cbit
    wait t+
    set pins, 1              ; CRC delimiter
    set x, 11                ; ACK slot and delimiter, EOF, intermission
tail:
    wait t+
    jmp x--, tail
    jmp frame
bench firmware can, 48 MHz
    set pins, 1              ; recessive
    wait tx
    pull
    mov p, osr               ; the bit period
    mov t, now
    add t, p
    set x, 10
bus_idle:
    wait t+
    jmp x--, bus_idle
frame:
    wait tx
    pull
    mov x, osr               ; bits after SOF, less one
    out null, 16             ; the next out autopulls the first word of them
    crc_init
    mov t, now
    add t, p
    wait t+
    set pins, 0              ; SOF
    set y, 3
dbit:
    jmp pin, dhigh
    wait t+
    out pins, 1
    jmp pin, dchanged
    jmp dsame
dhigh:
    wait t+
    out pins, 1
    jmp pin, dsame
dchanged:
    set y, 3
    jmp dnext
dsame:
    jmp y--, dnext
    wait t+
    mov pins, !pins          ; stuff
    set y, 3
dnext:
    jmp x--, dbit
    in crc, 15
    mov osr, isr
    set x, 14
cbit:
    jmp pin, chigh
    wait t+
    out pins, 1
    jmp pin, cchanged
    jmp csame
chigh:
    wait t+
    out pins, 1
    jmp pin, csame
cchanged:
    set y, 3
    jmp cnext
csame:
    jmp y--, cnext
    wait t+
    mov pins, !pins          ; stuff
    set y, 3
cnext:
    jmp x--, cbit
    wait t+
    set pins, 1              ; CRC delimiter
    set x, 11                ; ACK slot and delimiter, EOF, intermission
tail:
    wait t+
    jmp x--, tail
    jmp frame
bench firmware can_sender, 48 MHz
    set pins, 1              ; recessive
    wait tx
    pull
    mov p, osr               ; the bit period
    mov t, now
    add t, p
    set x, 10
bus_idle:
    wait t+
    jmp x--, bus_idle
frame:
    wait tx
    pull
    mov x, osr               ; bits after SOF, less one
    out null, 16             ; the next out autopulls the first word of them
    crc_init
    mov t, now
    add t, p
    wait t+
    set pins, 0              ; SOF
    set y, 3
dbit:
    jmp pin, dhigh
    wait t+
    out pins, 1
    jmp pin, dchanged
    jmp dsame
dhigh:
    wait t+
    out pins, 1
    jmp pin, dsame
dchanged:
    set y, 3
    jmp dnext
dsame:
    jmp y--, dnext
    wait t+
    mov pins, !pins          ; stuff
    set y, 3
dnext:
    jmp x--, dbit
    in crc, 15
    mov osr, isr
    set x, 14
cbit:
    jmp pin, chigh
    wait t+
    out pins, 1
    jmp pin, cchanged
    jmp csame
chigh:
    wait t+
    out pins, 1
    jmp pin, csame
cchanged:
    set y, 3
    jmp cnext
csame:
    jmp y--, cnext
    wait t+
    mov pins, !pins          ; stuff
    set y, 3
cnext:
    jmp x--, cbit
    wait t+
    set pins, 1              ; CRC delimiter
    wait t+                  ; the ACK slot
    sub t, 7
    sub t, 5
    wait t
    mov isr, pins            ; the bus: 0 is an ACK
    push
    add t, 7
    add t, 5
    set x, 10                ; ACK delimiter, EOF, intermission
tail:
    wait t+
    jmp x--, tail
    jmp frame
bench firmware can_receiver, 48 MHz
    set p, 24
    add p, p
    add p, p
    set pins, 1              ; recessive
    mov t, now
    add t, p
idle:
    set x, 10
quiet:
    wait t+
    jmp pin, recessive
    jmp idle
recessive:
    jmp x--, quiet           ; eleven recessive bits: the bus is idle
sof:
    wait 0 pin 1             ; SOF, or one a cycle or two before
    mov t, now
    sub t, 1                 ; the cycle it showed, as a capture has it
sofed:
    set y, 23
    add t, y
    add t, y
    add t, y
    add t, 2
    wait t+
    jmp pin, sof             ; recessive again: no SOF
    crc_init
    in null, 5               ; so the ID fills the first word
    set y, 3
    set x, 17                ; ID, RTR, IDE, r0 and the DLC
    jmp header_low
header_high:
    wait t+
    in pins, 1
    jmp pin, header_held_high
header_fell:
    wait 0 pin 1
    mov t, capture
    add t, p
    set y, 23
    add t, y
    add t, y
    add t, y
    add t, 2
    set y, 3
    jmp x--, header_low
    jmp header_low_end
header_held_high:
    capture_arm
    jmp y--, header_next_high
    wait t+                  ; a stuff bit, dominant
    jmp pin, stuffing
    jmp header_fell
header_next_high:
    jmp x--, header_high
    jmp header_high_end
header_low:
    wait t+
    in pins, 1
    jmp pin, header_rose
    jmp y--, header_next_low
    wait t+                  ; a stuff bit, recessive
    jmp pin, header_rose
    jmp stuffing
header_rose:
    capture_arm
    set y, 3
    jmp x--, header_high
    jmp header_high_end
header_next_low:
    jmp x--, header_low
    jmp header_low_end
header_high_end:
    mov osr, isr
    out null, 4
    out x, 2                 ; r0 and IDE
    jmp x--, refused
    mov x, osr               ; RTR
    jmp x--, remote_high
    mov osr, isr
    out null, 3
    mov x, osr               ; the DLC's top bit
    jmp x--, eight_high
    mov osr, ::isr
    out null, 15
    mov x, osr               ; the DLC's low bit
    jmp x--, odd_high
    mov x, isr
    push
    add x, x
    add x, x
    add x, x
    jmp x--, data_high
    set x, 14                ; no data: the CRC
    jmp crc_high
odd_high:
    mov x, isr
    push
    in null, 8
    add x, x
    add x, x
    add x, x
    sub x, 1
    jmp data_high
eight_high:
    push
    set x, 31
    add x, x
    add x, 1                 ; eight bytes
    jmp data_high
remote_high:
    push
    set x, 14
    jmp crc_high
header_low_end:
    mov osr, isr
    out null, 4
    out x, 2                 ; r0 and IDE
    jmp x--, refused
    mov x, osr               ; RTR
    jmp x--, remote_low
    mov osr, isr
    out null, 3
    mov x, osr               ; the DLC's top bit
    jmp x--, eight_low
    mov osr, ::isr
    out null, 15
    mov x, osr               ; the DLC's low bit
    jmp x--, odd_low
    mov x, isr
    push
    add x, x
    add x, x
    add x, x
    jmp x--, data_low
    set x, 14                ; no data: the CRC
    jmp crc_low
odd_low:
    mov x, isr
    push
    in null, 8
    add x, x
    add x, x
    add x, x
    sub x, 1
    jmp data_low
eight_low:
    push
    set x, 31
    add x, x
    add x, 1                 ; eight bytes
    jmp data_low
remote_low:
    push
    set x, 14
    jmp crc_low
data_high:
    wait t+
    in pins, 1
    jmp pin, data_held_high
data_fell:
    wait 0 pin 1
    mov t, capture
    add t, p
    set y, 23
    add t, y
    add t, y
    add t, y
    add t, 2
    set y, 3
    jmp x--, data_low
    jmp data_low_end
data_held_high:
    capture_arm
    jmp y--, data_next_high
    wait t+                  ; a stuff bit, dominant
    jmp pin, stuffing
    jmp data_fell
data_next_high:
    jmp x--, data_high
    jmp data_high_end
data_low:
    wait t+
    in pins, 1
    jmp pin, data_rose
    jmp y--, data_next_low
    wait t+                  ; a stuff bit, recessive
    jmp pin, data_rose
    jmp stuffing
data_rose:
    capture_arm
    set y, 3
    jmp x--, data_high
    jmp data_high_end
data_next_low:
    jmp x--, data_low
    jmp data_low_end
data_high_end:
    set x, 14
    jmp crc_high
data_low_end:
    set x, 14
    jmp crc_low
crc_high:
    wait t+
    in pins, 1
    jmp pin, crc_held_high
crc_fell:
    wait 0 pin 1
    mov t, capture
    add t, p
    set y, 23
    add t, y
    add t, y
    add t, y
    add t, 2
    set y, 3
    jmp x--, crc_low
    jmp crc_low_end
crc_held_high:
    capture_arm
    jmp y--, crc_next_high
    wait t+                  ; a stuff bit, dominant
    jmp pin, stuffing
    jmp crc_fell
crc_next_high:
    jmp x--, crc_high
    jmp crc_high_end
crc_low:
    wait t+
    in pins, 1
    jmp pin, crc_rose
    jmp y--, crc_next_low
    wait t+                  ; a stuff bit, recessive
    jmp pin, crc_rose
    jmp stuffing
crc_rose:
    capture_arm
    set y, 3
    jmp x--, crc_high
    jmp crc_high_end
crc_next_low:
    jmp x--, crc_low
    jmp crc_low_end
crc_high_end:
    set y, 1                 ; armed after its last bit
    jmp crc_end
crc_low_end:
    set y, 0
crc_end:
    mov isr, null
    in crc, 15
    mov x, isr               ; zero when the CRC holds
    wait t+                  ; the CRC delimiter
    jmp pin, delimited
    jmp form
delimited:
    jmp x--, crc_error
    nop [18]
    set pins, 0              ; ACK, from the slot's start
    wait t+
    jmp pin, unechoed        ; not on CRX: its transceiver in standby
    wait 0 pin 1             ; at once, which releases an armed capture
    mov t, now
    nop [18]
    set pins, 1              ; to its end
    add t, p
    sub t, 4                 ; the deadline again
    jmp acked
unechoed:
    nop [20]
    set pins, 1              ; to its end
acked:
    mov isr, null
    push
    set x, 9                 ; ACK delimiter, EOF, two of the intermission
eof:
    wait t+
    jmp pin, eof_recessive
    wait 0 pin 1             ; an error or overload flag, at once
    mov t, now
    add t, p
    jmp idle
eof_recessive:
    capture_arm              ; for the SOF, in step with the frame
    jmp x--, eof
    wait 0 pin 1
    mov t, capture
    jmp sofed
stuffing:
    set x, 1
    jmp error_armed
refused:
    set x, 2
    jmp error_armed
form:
    wait 0 pin 1             ; at once, which releases an armed capture
    set x, 3
    jmp error
error_armed:
    mov isr, x
    push
    irq
    wait 0 pin 1             ; the bus's next dominant bit releases the capture
    jmp host
crc_error:
    set x, 4
    jmp y--, error_armed
error:
    mov isr, x
    push
    irq
host:
    wait tx                  ; the host has read the words and cleared irq
    pull
    mov t, now
    add t, p
    jmp idle

Certificate

Clock window

firmwareshipped Hzlowest Hzset byhighest Hzset by
can48000000--52909090idle to SOF >= 22000 ns
can_sender48000000--52909090idle to SOF >= 22000 ns
can_receiver4800000047500000sender faster, read at pc 24051599998sender slower, read at pc 191

Time taken

Not available: no row in demo/speed.tsv.

Board witnesses

test/boardtimedemoresultcommitwindows provedteeth
can2026-11-03T07:59:17canPASSb938551d485 of 7 (5 spec)CAN R inverted over the first data byte's fourth bit; an edge of the first frame a quarter bit late

CI decodes each capture again and holds it to the bytes sent and the windows, each tooth refused: demo/witness.py check.