stm32g0xx_hal/serial/
config.rs

1use crate::prelude::*;
2use crate::time::Bps;
3
4#[derive(Eq, PartialEq, PartialOrd, Clone, Copy)]
5pub enum WordLength {
6    DataBits7,
7    DataBits8,
8    DataBits9,
9}
10
11#[derive(Eq, PartialEq, PartialOrd, Clone, Copy)]
12pub enum Parity {
13    ParityNone,
14    ParityEven,
15    ParityOdd,
16}
17
18#[derive(Eq, PartialEq, PartialOrd, Clone, Copy, Debug)]
19pub enum StopBits {
20    #[doc = "1 stop bit"]
21    STOP1 = 0b00,
22    #[doc = "0.5 stop bits"]
23    STOP0P5 = 0b01,
24    #[doc = "2 stop bits"]
25    STOP2 = 0b10,
26    #[doc = "1.5 stop bits"]
27    STOP1P5 = 0b11,
28}
29
30impl StopBits {
31    pub fn bits(self) -> u8 {
32        self as u8
33    }
34}
35
36#[derive(Eq, PartialEq, PartialOrd, Clone, Copy, Debug)]
37pub enum FifoThreshold {
38    #[doc = "1/8 of its depth"]
39    FIFO_1_BYTE = 0b000,
40    #[doc = "1/4 of its depth"]
41    FIFO_2_BYTES = 0b001,
42    #[doc = "1/2 of its depth"]
43    FIFO_4_BYTES = 0b010,
44    #[doc = "3/4 of its depth"]
45    FIFO_6_BYTES = 0b011,
46    #[doc = "7/8 of its depth"]
47    FIFO_7_BYTES = 0b100,
48    #[doc = "fifo empty/full"]
49    FIFO_8_BYTES = 0b101,
50}
51
52impl FifoThreshold {
53    pub fn bits(self) -> u8 {
54        self as u8
55    }
56}
57#[derive(Eq, PartialEq, PartialOrd, Clone, Copy)]
58pub struct BasicConfig {
59    pub(crate) baudrate: Bps,
60    pub(crate) wordlength: WordLength,
61    pub(crate) parity: Parity,
62    pub(crate) stopbits: StopBits,
63    pub(crate) inverted_tx: bool,
64    pub(crate) inverted_rx: bool,
65    pub(crate) swap: bool,
66}
67
68#[derive(Eq, PartialEq, PartialOrd, Clone, Copy)]
69pub struct FullConfig {
70    pub(crate) baudrate: Bps,
71    pub(crate) wordlength: WordLength,
72    pub(crate) parity: Parity,
73    pub(crate) stopbits: StopBits,
74    pub(crate) inverted_tx: bool,
75    pub(crate) inverted_rx: bool,
76    pub(crate) swap: bool,
77    pub(crate) fifo_enable: bool,
78    pub(crate) tx_fifo_threshold: FifoThreshold,
79    pub(crate) rx_fifo_threshold: FifoThreshold,
80    pub(crate) tx_fifo_interrupt: bool,
81    pub(crate) rx_fifo_interrupt: bool,
82    #[doc = "Number of bits no activity on rx line"]
83    pub(crate) receiver_timeout: Option<u32>,
84}
85
86impl BasicConfig {
87    pub fn baudrate(mut self, baudrate: Bps) -> Self {
88        self.baudrate = baudrate;
89        self
90    }
91
92    pub fn parity_none(mut self) -> Self {
93        self.parity = Parity::ParityNone;
94        self
95    }
96
97    pub fn parity_even(mut self) -> Self {
98        self.parity = Parity::ParityEven;
99        self
100    }
101
102    pub fn parity_odd(mut self) -> Self {
103        self.parity = Parity::ParityOdd;
104        self
105    }
106
107    pub fn wordlength_7(mut self) -> Self {
108        self.wordlength = WordLength::DataBits7;
109        self
110    }
111
112    pub fn wordlength_8(mut self) -> Self {
113        self.wordlength = WordLength::DataBits8;
114        self
115    }
116
117    pub fn wordlength_9(mut self) -> Self {
118        self.wordlength = WordLength::DataBits9;
119        self
120    }
121
122    pub fn stopbits(mut self, stopbits: StopBits) -> Self {
123        self.stopbits = stopbits;
124        self
125    }
126
127    pub fn invert_tx(mut self) -> Self {
128        self.inverted_tx = true;
129        self
130    }
131
132    pub fn invert_rx(mut self) -> Self {
133        self.inverted_rx = true;
134        self
135    }
136
137    /// Swap the Tx/Rx pins
138    ///
139    /// The peripheral will transmit on the pin given as the `rx` argument.
140    pub fn swap_pins(mut self) -> Self {
141        self.swap = true;
142        self
143    }
144}
145
146impl FullConfig {
147    pub fn baudrate(mut self, baudrate: Bps) -> Self {
148        self.baudrate = baudrate;
149        self
150    }
151
152    pub fn parity_none(mut self) -> Self {
153        self.parity = Parity::ParityNone;
154        self
155    }
156
157    pub fn parity_even(mut self) -> Self {
158        self.parity = Parity::ParityEven;
159        self
160    }
161
162    pub fn parity_odd(mut self) -> Self {
163        self.parity = Parity::ParityOdd;
164        self
165    }
166
167    pub fn wordlength_7(mut self) -> Self {
168        self.wordlength = WordLength::DataBits7;
169        self
170    }
171
172    pub fn wordlength_8(mut self) -> Self {
173        self.wordlength = WordLength::DataBits8;
174        self
175    }
176
177    pub fn wordlength_9(mut self) -> Self {
178        self.wordlength = WordLength::DataBits9;
179        self
180    }
181
182    pub fn stopbits(mut self, stopbits: StopBits) -> Self {
183        self.stopbits = stopbits;
184        self
185    }
186
187    pub fn invert_tx(mut self) -> Self {
188        self.inverted_tx = true;
189        self
190    }
191
192    pub fn invert_rx(mut self) -> Self {
193        self.inverted_rx = true;
194        self
195    }
196
197    /// Swap the Tx/Rx pins
198    ///
199    /// The peripheral will transmit on the pin given as the `rx` argument.
200    pub fn swap_pins(mut self) -> Self {
201        self.swap = true;
202        self
203    }
204
205    pub fn fifo_enable(mut self) -> Self {
206        self.fifo_enable = true;
207        self
208    }
209
210    pub fn tx_fifo_threshold(mut self, threshold: FifoThreshold) -> Self {
211        self.tx_fifo_threshold = threshold;
212        self
213    }
214
215    pub fn rx_fifo_threshold(mut self, threshold: FifoThreshold) -> Self {
216        self.rx_fifo_threshold = threshold;
217        self
218    }
219
220    pub fn tx_fifo_enable_interrupt(mut self) -> Self {
221        self.tx_fifo_interrupt = true;
222        self
223    }
224
225    pub fn rx_fifo_enable_interrupt(mut self) -> Self {
226        self.rx_fifo_interrupt = true;
227        self
228    }
229
230    /// Configure receiver timout in microseconds. Call after baudrate is set.
231    pub fn receiver_timeout_us(mut self, timeout_us: u32) -> Self {
232        let t = timeout_us as u64 * self.baudrate.0 as u64 / 1_000_000u64;
233        self.receiver_timeout = Some(t as u32);
234        self
235    }
236}
237
238#[derive(Debug)]
239pub struct InvalidConfig;
240
241impl Default for BasicConfig {
242    fn default() -> BasicConfig {
243        let baudrate = 19_200.bps();
244        BasicConfig {
245            baudrate,
246            wordlength: WordLength::DataBits8,
247            parity: Parity::ParityNone,
248            stopbits: StopBits::STOP1,
249            inverted_tx: false,
250            inverted_rx: false,
251            swap: false,
252        }
253    }
254}
255
256impl Default for FullConfig {
257    fn default() -> FullConfig {
258        let baudrate = 115_200.bps();
259        FullConfig {
260            baudrate,
261            wordlength: WordLength::DataBits8,
262            parity: Parity::ParityNone,
263            stopbits: StopBits::STOP1,
264            inverted_tx: false,
265            inverted_rx: false,
266            swap: false,
267            fifo_enable: false,
268            tx_fifo_threshold: FifoThreshold::FIFO_8_BYTES,
269            rx_fifo_threshold: FifoThreshold::FIFO_8_BYTES,
270            tx_fifo_interrupt: false,
271            rx_fifo_interrupt: false,
272            receiver_timeout: None,
273        }
274    }
275}