rp_hal_common/uart/
utils.rs

1//! Useful UART types
2
3use fugit::HertzU32;
4
5/// Data bits
6pub enum DataBits {
7    /// 5 bits
8    Five,
9    /// 6 bits
10    Six,
11    /// 7 bits
12    Seven,
13    /// 8 bits
14    Eight,
15}
16
17/// Stop bits
18pub enum StopBits {
19    /// 1 bit
20    One,
21    /// 2 bits
22    Two,
23}
24
25/// Parity
26///
27/// The "none" state of parity is represented with the Option type (None).
28pub enum Parity {
29    /// Odd parity
30    Odd,
31    /// Even parity
32    Even,
33}
34
35/// A struct holding the configuration for an UART device.
36///
37/// The `Default` implementation implements the following values:
38/// ```ignore
39/// # // can't actually create this with the non_exhaustive attribute
40/// UartConfig {
41///    baudrate: Baud(115_200),
42///    data_bits: DataBits::Eight,
43///    stop_bits: StopBits::One,
44///    parity: None,
45///}
46/// ```
47#[non_exhaustive]
48pub struct UartConfig {
49    /// The baudrate the uart will run at.
50    pub baudrate: HertzU32,
51
52    /// The amount of data bits the uart should be configured to.
53    pub data_bits: DataBits,
54
55    /// The amount of stop bits the uart should be configured to.
56    pub stop_bits: StopBits,
57
58    /// The parity that this uart should have
59    pub parity: Option<Parity>,
60}
61
62impl UartConfig {
63    /// Create a new instance of UartConfig
64    pub const fn new(
65        baudrate: HertzU32,
66        data_bits: DataBits,
67        parity: Option<Parity>,
68        stop_bits: StopBits,
69    ) -> UartConfig {
70        UartConfig {
71            baudrate,
72            data_bits,
73            stop_bits,
74            parity,
75        }
76    }
77}
78
79impl Default for UartConfig {
80    fn default() -> Self {
81        Self {
82            baudrate: HertzU32::from_raw(115_200),
83            data_bits: DataBits::Eight,
84            stop_bits: StopBits::One,
85            parity: None,
86        }
87    }
88}