SpaceWire (ECSS-E-ST-50-12C) and RMAP (ECSS-E-ST-50-52C)
README.md

spacewire #

SpaceWire (ECSS-E-ST-50-12C) packets and RMAP (ECSS-E-ST-50-52C) command headers in pure OCaml.

SpaceWire is the serial link most spacecraft use between instruments, processors and mass memory. Links are point to point, and routers join them into a network. RMAP (Remote Memory Access Protocol) runs on top and lets one node read or write the memory of another, which is how most SpaceWire instruments get configured and polled.

A packet starts with its address. A path address is a list of router output ports, each between 1 and 31: every router reads the first byte, drops it and sends the rest out of that port, so [1; 5] in the example below goes through two routers. A logical address (32 to 255) stays in the packet, and each router looks it up in its routing table. RMAP names both its target and its initiator by logical address (ECSS-E-ST-50-12C section 5.6).

A packet ends with EOP in the normal case. When a link fails halfway through a packet, the router inserts EEP (error end of packet) instead, and the receiver throws the truncated cargo away. RMAP commands also carry an 8-bit key, which the target compares with its own before it runs the command. The key is sent in clear. It stops a command that reached the wrong node by mistake and does nothing against an attacker (ECSS-E-ST-50-52C section 5.3.1.9).

The library encodes and decodes SpaceWire packets (path address, cargo and end marker) and RMAP command and reply headers, with the RMAP CRC-8 over each header (polynomial 0x07, ECSS-E-ST-50-52C Annex A). It supports the read, write, write-with-verify and read-modify-write commands and the reply status codes. It does not model the character or physical layers, and the caller builds the data field of a write command and its data CRC.

Installation #

Install with opam:

$ opam install spacewire

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install spacewire

Usage #

(* Create and encode a SpaceWire packet. *)
let pkt = Spacewire.packet_exn ~address:[ 1; 5 ] ~cargo:"sensor data" EOP
let bytes = Spacewire.encode_packet pkt

let () =
  match Spacewire.decode_packet bytes with
  | Ok decoded -> Fmt.pr "cargo: %s@." (Spacewire.cargo decoded)
  | Error _ -> Fmt.epr "decode error@."
(* RMAP read command. *)
let cmd =
  Spacewire.rmap_command_exn ~command:Read ~ack:Ack ~increment:Increment
    ~key:42 ~initiator_logical_address:0xFE ~transaction_id:1
    ~address:0x1000L ~data_length:256 ()

let encoded = Spacewire.encode_rmap_command ~target_logical_address:0x50 cmd

let () =
  match Spacewire.decode_rmap_command encoded with
  | Ok (target, decoded) ->
      Fmt.pr "target: 0x%02X, tid: %d@." target
        (Spacewire.rmap_cmd_transaction_id decoded)
  | Error _ -> Fmt.epr "decode error@."

Protocol layers #

+------------------------------------------+
| RMAP (Remote Memory Access Protocol)     |
|   ECSS-E-ST-50-52C                       |
+------------------------------------------+
| SpaceWire Packet                         |
|   destination address + cargo + EOP/EEP  |
+------------------------------------------+
| SpaceWire Character (not modelled)       |
|   data char (8-bit + parity) | control   |
+------------------------------------------+
| SpaceWire Physical (not modelled)        |
|   LVDS, encoding, link state machine     |
+------------------------------------------+

References #

Licence #

ISC License. See LICENSE.md for details.