stm32f4xx_hal/serial/
config.rs

1use crate::time::Bps;
2use crate::time::U32Ext;
3
4#[cfg_attr(feature = "defmt", derive(defmt::Format))]
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum WordLength {
7    DataBits8,
8    DataBits9,
9}
10
11/// Parity generation and checking. If odd or even parity is selected, the
12/// underlying USART will be configured to send/receive the parity bit in
13/// addition to the data bits.
14#[cfg_attr(feature = "defmt", derive(defmt::Format))]
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum Parity {
17    /// No parity bit will be added/checked.
18    ParityNone,
19    /// The MSB transmitted/received will be generated/checked to have a
20    /// even number of bits set.
21    ParityEven,
22    /// The MSB transmitted/received will be generated/checked to have a
23    /// odd number of bits set.
24    ParityOdd,
25}
26
27/// Stop Bit configuration parameter for serial.
28///
29/// Wrapper around `STOP_A`
30#[cfg_attr(feature = "defmt", derive(defmt::Format))]
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub enum StopBits {
33    #[doc = "1 stop bit"]
34    STOP1,
35    #[doc = "0.5 stop bits"]
36    STOP0P5,
37    #[doc = "2 stop bits"]
38    STOP2,
39    #[doc = "1.5 stop bits"]
40    STOP1P5,
41}
42
43#[cfg_attr(feature = "defmt", derive(defmt::Format))]
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45pub enum DmaConfig {
46    None,
47    Tx,
48    Rx,
49    TxRx,
50}
51
52#[cfg_attr(feature = "defmt", derive(defmt::Format))]
53#[derive(Clone, Copy, Debug, PartialEq, Eq)]
54pub enum IrdaMode {
55    #[doc = "IrDA mode disabled"]
56    None,
57    #[doc = "IrDA SIR rx/tx enabled in 'normal' mode"]
58    Normal,
59    #[doc = "IrDA SIR 'low-power' mode"]
60    LowPower,
61}
62
63#[cfg_attr(feature = "defmt", derive(defmt::Format))]
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub struct Config {
66    pub baudrate: Bps,
67    pub wordlength: WordLength,
68    pub parity: Parity,
69    pub stopbits: StopBits,
70    pub dma: DmaConfig,
71    pub irda: IrdaMode,
72}
73
74impl Config {
75    pub fn baudrate(mut self, baudrate: Bps) -> Self {
76        self.baudrate = baudrate;
77        self
78    }
79
80    pub fn parity_none(mut self) -> Self {
81        self.parity = Parity::ParityNone;
82        self
83    }
84
85    pub fn parity_even(mut self) -> Self {
86        self.parity = Parity::ParityEven;
87        self
88    }
89
90    pub fn parity_odd(mut self) -> Self {
91        self.parity = Parity::ParityOdd;
92        self
93    }
94
95    pub fn wordlength_8(mut self) -> Self {
96        self.wordlength = WordLength::DataBits8;
97        self
98    }
99
100    pub fn wordlength_9(mut self) -> Self {
101        self.wordlength = WordLength::DataBits9;
102        self
103    }
104
105    pub fn stopbits(mut self, stopbits: StopBits) -> Self {
106        self.stopbits = stopbits;
107        self
108    }
109
110    pub fn dma(mut self, dma: DmaConfig) -> Self {
111        self.dma = dma;
112        self
113    }
114
115    pub fn irda(mut self, irda: IrdaMode) -> Self {
116        self.irda = irda;
117        self
118    }
119}
120
121#[derive(Debug)]
122pub struct InvalidConfig;
123
124impl Default for Config {
125    fn default() -> Config {
126        let baudrate = 115_200_u32.bps();
127        Config {
128            baudrate,
129            wordlength: WordLength::DataBits8,
130            parity: Parity::ParityNone,
131            stopbits: StopBits::STOP1,
132            dma: DmaConfig::None,
133            irda: IrdaMode::None,
134        }
135    }
136}
137
138impl<T: Into<Bps>> From<T> for Config {
139    fn from(b: T) -> Config {
140        Config {
141            baudrate: b.into(),
142            ..Default::default()
143        }
144    }
145}