canadensis_serial/
lib.rs

1#![cfg_attr(not(test), no_std)]
2
3extern crate alloc;
4extern crate canadensis_core;
5extern crate canadensis_header;
6extern crate fallible_collections;
7extern crate heapless;
8extern crate log;
9extern crate zerocopy;
10
11use canadensis_core::crc::Crc32c;
12use canadensis_core::transport::Transport;
13use canadensis_core::{OutOfMemoryError, Priority};
14use canadensis_header::{NodeId16, TransferId64};
15
16pub use crate::rx::{SerialReceiver, Subscription};
17pub use crate::tx::SerialTransmitter;
18
19pub(crate) mod cobs;
20pub mod driver;
21pub(crate) mod header_collector;
22mod rx;
23mod tx;
24
25/// The Cyphal/Serial transport
26///
27/// This matches [the pycyphal implementation](https://pycyphal.readthedocs.io/en/latest/api/pycyphal.transport.serial.html).
28pub struct SerialTransport(());
29
30impl Transport for SerialTransport {
31    type NodeId = SerialNodeId;
32    type TransferId = SerialTransferId;
33    type Priority = Priority;
34}
35
36/// A serial node identifier, which allows the values 0..=65534
37///
38/// 65535 is reserved as a broadcast address
39pub type SerialNodeId = NodeId16;
40
41/// A serial transfer identifier
42///
43/// This is just a `u64`.
44pub type SerialTransferId = TransferId64;
45
46/// Calculates the CRC of a payload
47fn make_payload_crc(payload: &[u8]) -> u32 {
48    let mut crc = Crc32c::new();
49    crc.digest_bytes(payload);
50    crc.get_crc()
51}
52
53/// Serial transport errors
54#[derive(Debug)]
55pub enum Error<E> {
56    /// Memory allocation failed
57    Memory(OutOfMemoryError),
58    /// The serial driver reported an error
59    Driver(E),
60}
61
62impl<E> From<OutOfMemoryError> for Error<E> {
63    fn from(oom: OutOfMemoryError) -> Self {
64        Error::Memory(oom)
65    }
66}