spiritlevel-async
Part of Ashlar — a display connection stack in Rust, HDMI 2.1 first: from EDID parsing through mode negotiation to FRL link training. See all crates.
Async TMDS link bring-up for HDMI sources.
spiritlevel-async is the async companion to
spiritlevel. It has no procedures of its
own: it runs spiritlevel's — bring-up, tear-down, the reconnect check after a hotplug, and
the quick restore — over an async SCDC implementation and
hdmi-hal-async's PHY, clock and delay,
.awaiting each operation, so a bring-up yields to the executor wherever the I/O or a
wait does. Because the procedures are written once, sync and async bring-up take the same
steps and record the same events given the same I/O, and every fix reaches both.
Status
The TmdsLink driver runs all four of spiritlevel's procedures, over the async TmdsScdc
trait and its Send twin, with the culvert-async adapter behind a feature. What is under
consideration is on the roadmap.
Usage
Choose the link with spiritlevel's TmdsLinkParams::choose (re-exported here), then bring
it up over the parts: the sink's SCDC (TmdsScdc, implemented for culvert-async's client
by the culvert-async feature), an async TmdsPhy, a Clock and an async Delay. Here
the parts are doubles (hidden), each run answering at once, so a one-poll block_on runs
the example; on hardware, the executor awaits it:
// spiritlevel-async re-exports the spiritlevel and hdmi-hal-async types its API uses, so
// it is the only dependency.
use ;
# use Cell;
# use AtomicBool;
# use Duration;
# use ;
/// How a bring-up went.
/// Brings up 4K60 RGB over TMDS, and stops it again once it is up.
async
# // Doubles that answer at once: a sink with `TMDS_Config` and `Scrambling_Status`, a PHY
# // that fails `configure` when told to, and one value as clock and delay.
#
#
#
#
#
# ;
#
#
# /// Polls a future whose I/O completes at once.
#
#
// A sink with SCDC that takes scrambling, without low-rate scrambling.
let facts = new;
let time = default;
let mut tmds = new;
assert_eq!;
// A flag already set: the bring-up stops at entry.
let unplugged = new;
let mut tmds =
new.with_cancel;
assert_eq!;
// A PHY that refuses its configuration.
let mut tmds = new;
assert_eq!;
The procedures, their settings, outcomes and events are spiritlevel's: see
spiritlevel's README and its
architecture document for what each step does and why. Each procedure has a _with_events
form that calls a closure with every step, timestamped from the caller's clock;
MAX_BRING_UP_EVENTS and its siblings bound how many, so a caller without alloc sizes a
channel or buffer from them. The closure runs between .awaits and cannot await: it hands
each event on, with a channel's try_send for example.
Stopping a procedure
Stop a running procedure with its cancel flag, never by dropping its future. The flag
(Cancel, implemented for AtomicBool and references to one) is given with with_cancel,
and each procedure reads it where spiritlevel says:
bring_upreads it at entry, before each SCDC step, before each slice of a wait and before each read of the scrambling check. Seen at entry, the bring-up endsBringUpOutcome::Cancelledbefore any I/O, with nothing to undo (Cleanup::Skipped); seen later, it endsCancelledafter the tear-down it carries.quick_restorereads it only before each read of its scrambling check: its write and its scrambler call always run, and on a plain link, which has no check, it is not read. A cancelled check isCheckResult::Cancelled, with the link left running.tear_downandcheck_reconnectnever read it: the tear-down is the recovery, and the check is one read.
No procedure clears the flag: reset it before the next call, or the next bring_up ends
Cancelled at entry.
Dropping the future instead — losing a select!, hitting a timeout, aborting the task —
stops the procedure at whichever .await it was suspended on, and a dropped future cannot
run its tear-down: the sink may hold a new TMDS_Config and the PHY may be transmitting.
After a dropped bring_up or quick_restore, call tear_down with the same link before
anything else (a quick_restore may simply be called again); after a dropped
tear_down, call it again; a dropped check_reconnect needs nothing. The procedures bound
their own timing, so a timeout around them adds nothing, provided each call of the parts
completes in bounded time.
Multi-threaded executors
The plain traits promise nothing about their futures, so code generic over them cannot
spawn a procedure on a multi-threaded executor. For that, implement the Send twins:
SendTmdsScdc, and hdmi-hal-async's SendTmdsPhy and SendDelay (re-exported). Every
procedure's future is then Send in generic code bounded by the twins alone, with a clock
and a flag that are Send (they need not be Sync) and, for the _with_events forms, a
Send closure:
use ;
use ;
# // A stand-in with tokio::spawn's bound: the future must be `Send + 'static`.
#
/// Set by the task that watches the hotplug line; the bring-up stops at its next check.
static UNPLUGGED: AtomicBool = new;
Bound the code by the twins alone: adding + TmdsScdc, + TmdsPhy or + Delay, which the
twins imply anyway, makes the compiler use that bound instead of the blanket
implementation, and the futures are no longer known to be Send. With a twin and its plain
trait both in scope, a method call on a twin implementation is ambiguous: import the trait
your code is bounded by.
On embassy, the clock and delay come from embassy-time, events go into an
embassy_sync channel with try_send, and the task that watches the hotplug line sets a
static flag as above. On tokio, the clock is measured from a start
tokio::time::Instant, on the same timebase as sleep, and a flag shared as an
Arc<AtomicBool> is passed as with_cancel(&*flag) inside the task. On Linux the stack's
backend is synchronous: run spiritlevel's sync TmdsLink on a thread of its own there.
doc/architecture.md, "On embassy and tokio", has the details.
The culvert-async adapter
With the culvert-async feature, TmdsScdc is implemented for culvert-async's client
Scdc<T> over any async ScdcTransport, and for a mutable borrow of it, so one client
serves SCDC setup, FRL training and TMDS bring-up in turn. Errors are spiritlevel's
CulvertError, the type spiritlevel's own culvert adapter returns. The client has methods
of its own named like five of the trait's (read_tmds_config, write_tmds_config,
update_tmds_config, read_sink_version, write_source_version), which a method call
picks over the trait's: call the trait's as TmdsScdc::read_tmds_config(&mut scdc). In
code generic over T: SendScdcTransport that names Scdc<T>, the futures are Send
already. Code bounded
by S: SendTmdsScdc cannot take Scdc<T> itself; until culvert-async's planned Send
wrapper is supported (see the roadmap), it wraps the client in a local
type that implements SendTmdsScdc by forwarding to Scdc<T>.
Features
| Feature | Default | Description |
|---|---|---|
display-types |
yes | spiritlevel's From conversions from display-types' HdmiForumSinkCap into the sink facts |
culvert-async |
no | TmdsScdc for culvert-async's client and a borrow of it, and culvert-async's types re-exported |
no_std builds
spiritlevel-async is #![no_std] without alloc in every build, and
#![forbid(unsafe_code)]. It depends on no executor or runtime crate.
Stack position
spiritlevel-async drives spiritlevel's
procedures, over hdmi-hal-async's
async TMDS PHY and delay and its clock, and, behind a feature,
culvert-async's SCDC client.
plumbob-async, which trains FRL links,
is its counterpart beside it.
Out of scope
The procedures themselves (spiritlevel's), choosing the link, the hardware, powering the
PHY, retries, hotplug handling, and an executor. See
doc/architecture.md.
Documentation
doc/architecture.md— role, scope, the traits and the driver,Sendfutures, cancellation, embassy and tokio, and design principlesdoc/roadmap.md— what is under considerationdoc/setup.md— build, test, MSRV and coverage commandsdoc/testing.md— what the tests check and how CI runs themCONTRIBUTING.md— issues, pull requests and coding standards
Verifying releases
Releases are built on GitHub Actions and attested with
SLSA Build Level 2 provenance. To verify a release .crate against
its signed provenance, install the GitHub CLI and run:
The attested .crate is attached to each
GitHub release.
The first release, 0.1.0, is uploaded by the publish workflow with a crates.io API token scoped to publishing this new crate, because trusted publishing can only be set up for a crate already on crates.io; the token is revoked afterwards. Later releases are uploaded through crates.io trusted publishing: the publish workflow obtains a short-lived token via OpenID Connect instead of using a stored API token. Once trusted publishing is set up, the crate is switched to trusted publishing only, so that crates.io rejects uploads made with an API token for it; the badge above shows whether that setting is on. Each version's publishing details, including the workflow run and commit, are available from the crates.io API:
# see .version.trustpub_data