lpc8xx_hal/usart/
settings.rs

1use core::marker::PhantomData;
2
3use crate::pac::usart0::cfg::{
4    self, CLKPOL_A, DATALEN_A, PARITYSEL_A, RXPOL_A, STOPLEN_A, TXPOL_A,
5};
6
7/// USART settings
8///
9/// Expected as an argument by the various enable methods on [`USART`].
10///
11/// [`USART`]: struct.USART.html
12pub struct Settings<Word = u8> {
13    pub(super) data_len: DATALEN_A,
14    pub(super) parity: PARITYSEL_A,
15    pub(super) stop_len: STOPLEN_A,
16    pub(super) clock_pol: CLKPOL_A,
17    pub(super) rx_pol: RXPOL_A,
18    pub(super) tx_pol: TXPOL_A,
19
20    _word: PhantomData<Word>,
21}
22
23impl<Word> Settings<Word> {
24    /// Set data length to 7 bits
25    ///
26    /// Overwrites the previous data length setting.
27    pub fn data_len_7(mut self) -> Settings<u8> {
28        self.data_len = DATALEN_A::BIT_7;
29        self.transmute()
30    }
31
32    /// Set data length to 8 bits
33    ///
34    /// Overwrites the previous data length setting. This is the default.
35    pub fn data_len_8(mut self) -> Settings<u8> {
36        self.data_len = DATALEN_A::BIT_8;
37        self.transmute()
38    }
39
40    /// Set data length to 9 bits
41    ///
42    /// Overwrites the previous data length setting.
43    pub fn data_len_9(mut self) -> Settings<u16> {
44        self.data_len = DATALEN_A::BIT_9;
45        self.transmute()
46    }
47
48    /// Add no parity bit
49    ///
50    /// Overwrites the previous parity setting. This is the default.
51    pub fn parity_none(mut self) -> Self {
52        self.parity = PARITYSEL_A::NO_PARITY;
53        self
54    }
55
56    /// Add even parity bit
57    ///
58    /// Overwrites the previous parity setting.
59    pub fn parity_even(mut self) -> Self {
60        self.parity = PARITYSEL_A::EVEN_PARITY;
61        self
62    }
63
64    /// Add odd parity bit
65    ///
66    /// Overwrites the previous parity setting.
67    pub fn parity_odd(mut self) -> Self {
68        self.parity = PARITYSEL_A::ODD_PARITY;
69        self
70    }
71
72    /// Add one stop bit
73    ///
74    /// Overwrites the previous stop length setting. This is the default.
75    pub fn stop_len_1(mut self) -> Self {
76        self.stop_len = STOPLEN_A::BIT_1;
77        self
78    }
79
80    /// Add two stop bits
81    ///
82    /// Overwrites the previous stop length setting.
83    pub fn stop_len_2(mut self) -> Self {
84        self.stop_len = STOPLEN_A::BITS_2;
85        self
86    }
87
88    /// Sample on falling clock edge
89    ///
90    /// This is only relevant when receiving data in synchronous mode.
91    ///
92    /// Overwrites the previous clock polarity setting. This is the default.
93    pub fn clock_pol_falling(mut self) -> Self {
94        self.clock_pol = CLKPOL_A::FALLING_EDGE;
95        self
96    }
97
98    /// Sample on rising clock edge
99    ///
100    /// This is only relevant when receiving data in synchronous mode.
101    ///
102    /// Overwrites the previous clock polarity setting.
103    pub fn clock_pol_rising(mut self) -> Self {
104        self.clock_pol = CLKPOL_A::RISING_EDGE;
105        self
106    }
107
108    /// Don't invert RX signal
109    ///
110    /// Overwrites the previous RX polarity setting. This is the default.
111    pub fn rx_pol_standard(mut self) -> Self {
112        self.rx_pol = RXPOL_A::STANDARD;
113        self
114    }
115
116    /// Invert RX signal
117    ///
118    /// Overwrites the previous RX polarity setting.
119    pub fn rx_pol_inverted(mut self) -> Self {
120        self.rx_pol = RXPOL_A::INVERTED;
121        self
122    }
123
124    /// Don't invert TX signal
125    ///
126    /// Overwrites the previous TX polarity setting. This is the default.
127    pub fn tx_pol_standard(mut self) -> Self {
128        self.tx_pol = TXPOL_A::STANDARD;
129        self
130    }
131
132    /// Invert TX signal
133    ///
134    /// Overwrites the previous TX polarity setting.
135    pub fn tx_pol_inverted(mut self) -> Self {
136        self.tx_pol = TXPOL_A::INVERTED;
137        self
138    }
139
140    fn transmute<NewW>(self) -> Settings<NewW> {
141        Settings {
142            data_len: self.data_len,
143            parity: self.parity,
144            stop_len: self.stop_len,
145            clock_pol: self.clock_pol,
146            rx_pol: self.rx_pol,
147            tx_pol: self.tx_pol,
148            _word: PhantomData,
149        }
150    }
151
152    pub(super) fn apply(&self, w: &mut cfg::W) {
153        w.datalen().variant(self.data_len);
154        w.paritysel().variant(self.parity);
155        w.stoplen().variant(self.stop_len);
156        w.clkpol().variant(self.clock_pol);
157        w.rxpol().variant(self.rx_pol);
158        w.txpol().variant(self.tx_pol);
159    }
160}
161
162impl Default for Settings {
163    fn default() -> Self {
164        Settings {
165            data_len: DATALEN_A::BIT_8,
166            parity: PARITYSEL_A::NO_PARITY,
167            stop_len: STOPLEN_A::BIT_1,
168            clock_pol: CLKPOL_A::FALLING_EDGE,
169            rx_pol: RXPOL_A::STANDARD,
170            tx_pol: TXPOL_A::STANDARD,
171            _word: PhantomData,
172        }
173    }
174}