1#![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}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn serial_event_reports_readiness_and_errors() {
101 let event = SerialEventSet::RX_DATA | SerialEventSet::FAULT;
102
103 assert!(event.has_rx());
104 assert!(!event.has_tx());
105 assert!(event.contains(SerialEventSet::FAULT));
106 }
107}