musstd
MUSST zap master daemon over a serial line.
musstd drives an ESRF MUSST through its serial line, reached as the TCP port that rioproxy gives the line, and serves it to Pylatus as the master of continuous (zap) scans: the MUSST fires the gate for the counting cards and the fluorescence detector and stores its timer and an encoder at every point. It is part of the Pylatus software stack.
Overview
The spec zapintgen.mprg program is embedded in the binary (prg/) and uploaded whenever
the MUSST does not carry it. On every connection the daemon checks the firmware, aborts a
program left running, sets the 1 MHz timer base and reads the channel configuration. The
MUSST is retried forever when the line is silent and clients are told it is offline.
The serial protocol is ASCII: \r terminates a command, \r\n an answer, #command
answers OK or ERROR, multi-line answers are framed by $ lines, and ?*EDAT returns
stored events in binary (0xFF, a big endian u16 byte count, little endian i32 words,
and a checksum of the byte count and the payload). The line runs at 9600 baud only.
An encoder channel becomes usable once its counts per user unit are given on the command line; positions in the Pylatus protocol are then user units and the daemon converts.
A scan is armed with the start and point conditions of the spec program: the start is an
encoder position on channels 1 to 6 or a delay in seconds on channel 0, the timer, and
points follow either every period in seconds or every step of the encoder. The program
stores one event per point, the timer word and the encoder word, and a scan of N steps
yields N+1 events: points in ARM and expected in STATUS count events. While a scan runs
the daemon polls the program state and the event pointer every 100 ms, accepts a pointer
read that is at least the previous one and within the planned words, reads the stored
events in binary chunks of at most 128 words and broadcasts each event as a POINT as soon
as it is read, settling the scan only when two consecutive reads agree. The scan ends when
the program stops and its return code matches the number of steps; ABORT stops the
program, clears the gate and the timer and still reads the events stored so far. The
events of the last scan stay available to READ_FROM until the next ARM, and an idle STATUS
reports them as points of expected. The timer word is a 32 bit count of the timer base
and wraps after 2^32 ticks, 71.6 minutes at 1 MHz: take differences as u32. A scan on
channel 0 stores no encoder and its positions are NaN.
Components
src/main.rs- entry point, logging, mode dispatch.src/params.rs- clap definitions: serial line, channel factors-1 .. -6,probeandservemodes.src/musst.rs- the serial protocol: queries, acknowledged commands, binary event reads, program upload.src/engine.rs- the device loop: connect forever, start-up checks, requests from Pylatus, scan polling and event read-out, status and configuration broadcasts.src/protocol.rs- binary codec of the Pylatus protocol.src/server.rs- TCP listener, client sessions, fan-out of responses.src/probe.rs- one-shot dump of the firmware, settings and channels.
Build
Requires a recent Rust toolchain and Cargo. Dependencies from crates.io: bytes, clap,
tokio. Internal dependencies:
- hub - the framed TCP server and the client protocol codec shared by the Pylatus daemons.
- besy - logging and signal handling used across Pylatus.
cargo build --release
The binary is placed in target/release/musstd. build.rs emits the build timestamp and
git information via vergen-gitcl.
Usage
musstd lbm313:7900 -1 360000 probe
musstd lbm313:7900 -1 360000 serve -p 8505 -vv -m
lbm313:7900- the rioproxy port of the MUSST serial line-1 360000- channel 1 is an encoder with that many counts per user unitprobe- print firmware, settings and channels, then exitserve -p 8505- TCP port for Pylatus clients, default 8505
Protocol to Pylatus
Both directions send a little endian header u32 type, u32 len followed by len payload
bytes. Requests:
0x100 HELLO no payload, answered with CONFIG and STATUS
0x101 SYNC u32 channel, u32 reserved, f64 position: preset the encoder to the
motor position
0x102 ARM u32 channel (0 timer, 1..6 encoder), u32 mode (0 time, 1 position),
u32 direction (0 positive, 1 negative), u32 points (events, N+1),
f64 start (encoder position, or seconds on channel 0),
f64 period in seconds or step in user units
0x103 ABORT no payload, stops the program, clears the gate and the timer and
reads the events stored so far
0x104 STATUS no payload
0x105 READ_FROM u32 first: resend the stored events with an index >= first, then
STATUS
Every response is broadcast to all clients. Responses:
1 POINT u32 index, u32 timer ticks (wraps at 2^32), f64 position (NaN on channel 0)
2 STATUS u32 version (1), u32 state (0 idle, 1 armed, 2 running, 3 offline),
u32 points read, u32 expected, f64 live position of the current channel
3 ERROR utf8 text
4 CONFIG f64 timer frequency in Hz, u32 channels, u32 reserved, then per channel
u32 kind (0 CNT, 1 ENC, 2 other), u32 reserved, f64 counts per unit
(0 when not given)
The current channel is the one last armed or synchronized. SYNC and ARM are refused with ERROR while a scan is armed or running; an idle STATUS describes the last scan. Values that do not fit the 32 bit registers of the MUSST are refused as well.
License
GPL-3.0+