Implementation of Silicon Labs' Asynchronous Serial Host protocol v2 (ASHv2), host side.
Silicon Labs publishes the documentation online:
The crate currently provides:
- Frame parsing/encoding for
DATA,ACK,NAK,RST,RST-ACK, andERROR. - CRC-16 validation/generation for all supported frame types.
- Byte stuffing/unstuffing and ASH payload randomization (masking/unmasking).
- Async actor futures created with
start(...), with caller-owned transmitter/receiver execution. - Transport-independent async I/O over caller-provided
tokio::io::AsyncReadandtokio::io::AsyncWriteimplementations. - Bounded initial reset handshake (
RST->RST-ACK) before normal traffic. - Cumulative acknowledgements and timed retransmission of unacknowledged
DATAframes. - Optional EZSP adapters implementing
ezsp::Transmitandezsp::Receive.
Important behavior details:
start(reader, writer, response)accepts separate async reader and writer values. The caller is responsible for opening and configuring the transport and splitting it when necessary. Both transport values must beSend + Unpinand may borrow from the caller.- The core crate does not depend on
serialportorasync-serialport. Serial ports, sockets, in-memory streams, and other transports can be used when they implement the required Tokio I/O traits. start(...)returns transmitter and receiver futures for the caller to poll concurrently on an executor compatible with the transport. On native targets, ASH deadlines use a shared timer thread.- The crate does not spawn Tokio tasks internally.
- Dropping every
Handleclone closes the outbound queue. Both futures end once queued and in-flight transmissions finish or the link fails. Handle::send(payload).awaitcompletes when the NCP acknowledges the ASH frame. The inbound response channel deliversDATApayloads, including any EZSP response.- During the initial handshake or while the transmit window is full, requests wait in the
bounded queue. When that queue fills, further calls to
Handle::sendwait for capacity. Incoming control frames and retry deadlines remain active. - The host acknowledges inbound
DATAafter buffering it. A full response channel sets the ASH not-ready flag to pause NCP callbacks. - An exhausted retry budget, transport failure, or unexpected NCP reset ends the session and
fails queued and in-flight sends with the terminal I/O error. Retry exhaustion returns
TimedOut. Start a fresh ASH session and renegotiate EZSP before sending more commands. Commands already written can have an unknown remote outcome. - Payload type is
heapless::Vec<u8, MAX_PAYLOAD_SIZE>(MAX_PAYLOAD_SIZEdefaults to128).
Compile-time tunables (via const_env):
ASHV2_MAX_PAYLOAD_SIZE(default:128)ASHV2_T_RSTACK_MAX_MILLIS(default:3200)ASHV2_TX_K(default:5, range:1to7)ASHV2_T_RX_ACK_MAX_MILLIS(default:3200, also bounds each transport write)
To run the actors as separate Tokio tasks, pass transport types that are both 'static.
Each returned future's opaque type captures both transport types, so this applies when spawning
either future.
use ashv2::start;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::sync::mpsc::channel;
async fn run<R, W>(reader: R, writer: W)
where
R: AsyncRead + Send + Unpin + 'static,
W: AsyncWrite + Send + Unpin + 'static,
{
// Channel for inbound ASH DATA payloads from the NCP.
let (response_tx, mut response_rx) = channel(64);
// Create ASH actor futures from the transport halves and spawn them on the
// application's runtime.
let (handle, futures) = start(reader, writer, response_tx);
let transmitter = tokio::spawn(futures.transmitter);
let receiver = tokio::spawn(futures.receiver);
// Example EZSP "version" request payload.
let request_payload = [0x00, 0x00, 0x00, 0x02].into_iter().collect();
handle
.send(request_payload)
.await
.expect("Failed to transmit request frame");
// Receive inbound DATA payload from NCP.
if let Some(response_payload) = response_rx.recv().await {
println!("Received response payload: {response_payload:?}");
}
// Dropping the final Handle closes the outbound queue. The transmitter
// finishes outstanding transmissions and signals the receiver to terminate.
drop(handle);
transmitter.await.expect("Transmitter task failed");
receiver.await.expect("Receiver task failed");
}The reader and writer can come from any transport integration. For a bidirectional type that
implements both traits, use that transport's split operation (for example,
tokio::io::split) before calling start(...).
To use borrowed I/O, pass values such as &mut reader and &mut writer to start and poll
both actor futures concurrently with the application work in the scope of the borrows.
For graceful shutdown, drop every clone of Handle, including handles used through the
optional EZSP transmitter adapter.
Enable the ezsp feature to get typed EZSP adapters:
[dependencies]
ashv2 = { version = "19", features = ["ezsp"] }With the feature enabled:
ashv2::ezsp::Transmitteris an alias forashv2::Handle, which implementsezsp::Transmit.ashv2::ezsp::Receiverowns the inbound ASHv2 payload receiver and implementsezsp::Receive. The EZSP layer supplies the currently negotiated protocol version to each receive call.
The same payload channel connects the core ASHv2 actor to the EZSP receiver:
use ashv2::ezsp::{Receiver as EzspReceiver, Transmitter as EzspTransmitter};
use ashv2::start;
use tokio::sync::mpsc::channel;
let (payload_tx, payload_rx) = channel(64);
let (ash_handle, futures) = start(reader, writer, payload_tx);
let ezsp_transmitter: EzspTransmitter = ash_handle;
let ezsp_receiver = EzspReceiver::new(payload_rx);
// Spawn or otherwise poll futures.transmitter and futures.receiver.
// Pass ezsp_transmitter and ezsp_receiver to the EZSP API.Without the feature, the EZSP dependency and adapter module are not compiled.
The CI workflow currently runs:
cargo +nightly fmt --checkcargo clippy --all-features -- -A clippy::multiple_crate_versions -D warningscargo test --all-featurescargo build --all-features --releasecargo vet check
This project is free software and is not affiliated with Silicon Labs.
Special thanks to Simon Farnsworth, Kevin Reid, and the community at https://users.rust-lang.org/.