Skip to main content

rdif_serial/
lib.rs

1//! Portable UART capability boundary.
2//!
3//! This crate contains no software queues, task policy, IRQ registration, or
4//! OS wakeups. Concrete drivers split into task-owned control, IRQ-owned event,
5//! and emergency-only TX endpoints; the consuming runtime owns all buffering,
6//! exclusion, and scheduling policy.
7
8#![no_std]
9
10mod raw;
11mod types;
12
13pub use self::{raw::*, types::*};
14
15#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
16pub enum ConfigError {
17    #[error("invalid baud rate")]
18    InvalidBaudrate,
19    #[error("unsupported data bits")]
20    UnsupportedDataBits,
21    #[error("unsupported stop bits")]
22    UnsupportedStopBits,
23    #[error("unsupported parity")]
24    UnsupportedParity,
25    #[error("UART register access failed")]
26    RegisterError,
27    #[error("UART operation timed out")]
28    Timeout,
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32#[repr(u8)]
33pub enum DataBits {
34    Five  = 5,
35    Six   = 6,
36    Seven = 7,
37    Eight = 8,
38}
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41#[repr(u8)]
42pub enum StopBits {
43    One = 1,
44    Two = 2,
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum Parity {
49    None,
50    Even,
51    Odd,
52    Mark,
53    Space,
54}
55
56#[derive(Debug, Clone, Default)]
57pub struct Config {
58    pub baudrate: Option<u32>,
59    pub data_bits: Option<DataBits>,
60    pub stop_bits: Option<StopBits>,
61    pub parity: Option<Parity>,
62}
63
64impl Config {
65    pub const fn new() -> Self {
66        Self {
67            baudrate: None,
68            data_bits: None,
69            stop_bits: None,
70            parity: None,
71        }
72    }
73
74    pub const fn baudrate(mut self, baudrate: u32) -> Self {
75        self.baudrate = Some(baudrate);
76        self
77    }
78
79    pub const fn data_bits(mut self, data_bits: DataBits) -> Self {
80        self.data_bits = Some(data_bits);
81        self
82    }
83
84    pub const fn stop_bits(mut self, stop_bits: StopBits) -> Self {
85        self.stop_bits = Some(stop_bits);
86        self
87    }
88
89    pub const fn parity(mut self, parity: Parity) -> Self {
90        self.parity = Some(parity);
91        self
92    }
93}