Skip to content

Commit 474c117

Browse files
committed
Add builder methods for usart configuration fields
1 parent b5b1d56 commit 474c117

File tree

1 file changed

+55
-21
lines changed

1 file changed

+55
-21
lines changed

src/serial.rs

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,12 @@ pub mod config {
7272
DataBits8,
7373
DataBits9,
7474
}
75-
7675
#[derive(Copy, Clone, PartialEq)]
7776
pub enum Parity {
7877
ParityNone,
7978
ParityEven,
8079
ParityOdd,
8180
}
82-
8381
#[derive(Copy, Clone, PartialEq)]
8482
pub enum StopBits {
8583
#[doc = "1 stop bit"]
@@ -91,34 +89,60 @@ pub mod config {
9189
#[doc = "1.5 stop bits"]
9290
STOP1P5,
9391
}
94-
92+
#[derive(Copy, Clone, PartialEq)]
9593
pub enum BitOrder {
9694
LsbFirst,
9795
MsbFirst,
9896
}
99-
97+
#[derive(Copy, Clone, PartialEq)]
10098
pub enum ClockPhase {
10199
First,
102100
Second,
103101
}
104-
102+
#[derive(Copy, Clone, PartialEq)]
105103
pub enum ClockPolarity {
106104
IdleHigh,
107105
IdleLow,
108106
}
109107

108+
/// A structure for specifying the USART or UART configuration. Fields
109+
/// relating to synchronous mode are ignored for UART peripherals.
110+
///
111+
/// This structure uses builder semantics to generate the configuration.
112+
///
113+
/// ```
114+
/// let config = Config::new().partity_odd();
115+
/// ```
116+
#[derive(Copy, Clone)]
110117
pub struct Config {
111118
pub baudrate: Hertz,
112119
pub wordlength: WordLength,
113120
pub parity: Parity,
114121
pub stopbits: StopBits,
115-
pub clockphase: ClockPhase,
116122
pub bitorder: BitOrder,
123+
pub clockphase: ClockPhase,
117124
pub clockpolarity: ClockPolarity,
118125
pub lastbitclockpulse: bool,
119126
}
120127

121128
impl Config {
129+
/// Create a default configuration for the USART or UART interface
130+
///
131+
/// * 8 bits, 1 stop bit, no parity (8N1)
132+
/// * LSB first
133+
pub fn new<T: Into<Hertz>>(frequency: T) -> Self {
134+
Config {
135+
baudrate: frequency.into(),
136+
wordlength: WordLength::DataBits8,
137+
parity: Parity::ParityNone,
138+
stopbits: StopBits::STOP1,
139+
bitorder: BitOrder::LsbFirst,
140+
clockphase: ClockPhase::First,
141+
clockpolarity: ClockPolarity::IdleLow,
142+
lastbitclockpulse: false,
143+
}
144+
}
145+
122146
pub fn baudrate(mut self, baudrate: impl Into<Hertz>) -> Self {
123147
self.baudrate = baudrate.into();
124148
self
@@ -149,36 +173,46 @@ pub mod config {
149173
self
150174
}
151175

176+
/// Specify the number of stop bits
152177
pub fn stopbits(mut self, stopbits: StopBits) -> Self {
153178
self.stopbits = stopbits;
154179
self
155180
}
181+
/// Specify the bit order
182+
pub fn bitorder(mut self, bitorder: BitOrder) -> Self {
183+
self.bitorder = bitorder;
184+
self
185+
}
186+
/// Specify the clock phase. Only applies to USART peripherals
187+
pub fn clockphase(mut self, clockphase: ClockPhase) -> Self {
188+
self.clockphase = clockphase;
189+
self
190+
}
191+
/// Specify the clock polarity. Only applies to USART peripherals
192+
pub fn clockpolarity(mut self, clockpolarity: ClockPolarity) -> Self {
193+
self.clockpolarity = clockpolarity;
194+
self
195+
}
196+
/// Specify if the last bit transmitted in each word has a corresponding
197+
/// clock pulse in the SCLK pin. Only applies to USART peripherals
198+
pub fn lastbitclockpulse(mut self, lastbitclockpulse: bool) -> Self {
199+
self.lastbitclockpulse = lastbitclockpulse;
200+
self
201+
}
156202
}
157203

158204
#[derive(Debug)]
159205
pub struct InvalidConfig;
160206

161207
impl Default for Config {
162208
fn default() -> Config {
163-
Config {
164-
baudrate: Hertz(19_200), // 19k2 baud
165-
wordlength: WordLength::DataBits8,
166-
parity: Parity::ParityNone,
167-
stopbits: StopBits::STOP1,
168-
clockphase: ClockPhase::First,
169-
bitorder: BitOrder::LsbFirst,
170-
clockpolarity: ClockPolarity::IdleLow,
171-
lastbitclockpulse: false,
172-
}
209+
Self::new(Hertz(19_200)) // 19k2 baud
173210
}
174211
}
175212

176213
impl<T: Into<Hertz>> From<T> for Config {
177-
fn from(f: T) -> Config {
178-
Config {
179-
baudrate: f.into(),
180-
..Default::default()
181-
}
214+
fn from(frequency: T) -> Config {
215+
Self::new(frequency)
182216
}
183217
}
184218
}

0 commit comments

Comments
 (0)