Skip to content

Commit 24fccac

Browse files
committed
Move serial config module to own file
1 parent c5428fa commit 24fccac

File tree

2 files changed

+146
-146
lines changed

2 files changed

+146
-146
lines changed

src/serial.rs

Lines changed: 1 addition & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -319,152 +319,7 @@ cfg_if! {
319319
}
320320
}
321321

322-
/// Types for configuring a serial interface.
323-
pub mod config {
324-
use crate::pac::usart1::cr2::STOP_A;
325-
use crate::time::rate::{Baud, Extensions};
326-
327-
/// Stop Bit configuration parameter for serial.
328-
///
329-
/// Wrapper around [`STOP_A`]
330-
#[derive(Clone, Copy, Debug, PartialEq)]
331-
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
332-
pub enum StopBits {
333-
/// 0.5 stop bit
334-
Stop0P5,
335-
/// 1 stop bit
336-
Stop1,
337-
/// 1.5 stop bit
338-
Stop1P5,
339-
/// 2 stop bit
340-
Stop2,
341-
}
342-
343-
impl From<StopBits> for STOP_A {
344-
fn from(stopbit: StopBits) -> Self {
345-
match stopbit {
346-
StopBits::Stop0P5 => STOP_A::STOP0P5,
347-
StopBits::Stop1 => STOP_A::STOP1,
348-
StopBits::Stop1P5 => STOP_A::STOP1P5,
349-
StopBits::Stop2 => STOP_A::STOP2,
350-
}
351-
}
352-
}
353-
354-
impl From<STOP_A> for StopBits {
355-
fn from(stopbit: STOP_A) -> Self {
356-
match stopbit {
357-
STOP_A::STOP0P5 => StopBits::Stop0P5,
358-
STOP_A::STOP1 => StopBits::Stop1,
359-
STOP_A::STOP1P5 => StopBits::Stop1P5,
360-
STOP_A::STOP2 => StopBits::Stop2,
361-
}
362-
}
363-
}
364-
365-
/// Parity generation and checking. If odd or even parity is selected, the
366-
/// underlying USART will be configured to send/receive the parity bit in
367-
/// addtion to the data bits.
368-
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
369-
#[derive(Debug, Clone, Copy, PartialEq)]
370-
pub enum Parity {
371-
/// No parity bit will be added/checked.
372-
None,
373-
/// The MSB transmitted/received will be generated/checked to have a
374-
/// even number of bits set.
375-
Even,
376-
/// The MSB transmitted/received will be generated/checked to have a
377-
/// odd number of bits set.
378-
Odd,
379-
}
380-
381-
/// Configuration struct for [`Serial`](super::Serial) providing all
382-
/// communication-related / parameters. `Serial` always uses eight data
383-
/// bits plus the parity bit - if selected.
384-
///
385-
/// Create a configuration by using `default` in combination with the
386-
/// builder methods. The following snippet shows creating a configuration
387-
/// for 19,200 Baud, 8N1 by deriving it from the default value:
388-
/// ```
389-
/// # use stm32f3xx_hal::serial::config::*;
390-
/// # use stm32f3xx_hal::time::rate::{Baud, Extensions};
391-
/// let config = Config::default().baudrate(19_200.Bd());
392-
///
393-
/// assert!(config.baudrate == 19_200.Bd());
394-
/// assert!(config.parity == Parity::None);
395-
/// assert!(config.stopbits == StopBits::STOP1);
396-
/// ```
397-
#[derive(Debug, Clone, Copy, PartialEq)]
398-
#[non_exhaustive]
399-
pub struct Config {
400-
/// Serial interface baud rate
401-
pub baudrate: Baud,
402-
/// Whether and how to generate/check a parity bit
403-
pub parity: Parity,
404-
/// The number of stop bits to follow the last data bit or the parity
405-
/// bit
406-
pub stopbits: StopBits,
407-
}
408-
409-
impl Config {
410-
/// Sets the given baudrate.
411-
pub fn baudrate(mut self, baudrate: impl Into<Baud>) -> Self {
412-
self.baudrate = baudrate.into();
413-
self
414-
}
415-
416-
/// Sets the given parity.
417-
pub fn parity(mut self, parity: Parity) -> Self {
418-
self.parity = parity;
419-
self
420-
}
421-
422-
/// Sets the stop bits to `stopbits`.
423-
pub fn stopbits(mut self, stopbits: StopBits) -> Self {
424-
self.stopbits = stopbits;
425-
self
426-
}
427-
}
428-
429-
impl Default for Config {
430-
/// Creates a new configuration with typically used parameters: 115,200
431-
/// Baud 8N1.
432-
fn default() -> Config {
433-
Config {
434-
baudrate: 115_200.Bd(),
435-
parity: Parity::None,
436-
stopbits: StopBits::Stop1,
437-
}
438-
}
439-
}
440-
441-
impl<T: Into<Baud>> From<T> for Config {
442-
fn from(b: T) -> Config {
443-
Config {
444-
baudrate: b.into(),
445-
..Default::default()
446-
}
447-
}
448-
}
449-
450-
#[cfg(feature = "defmt")]
451-
impl defmt::Format for Config {
452-
fn format(&self, f: defmt::Formatter) {
453-
// Omitting pins makes it:
454-
// 1. Easier.
455-
// 2. Not to specialized to use it ergonimically for users
456-
// even in a generic context.
457-
// 3. Not require specialization.
458-
defmt::write!(
459-
f,
460-
"Serial {{ baudrate: {} Bd , parity: {} , stopbits: {} }}",
461-
self.baudrate.0,
462-
self.parity,
463-
self.stopbits,
464-
);
465-
}
466-
}
467-
}
322+
pub mod config;
468323

469324
/// Serial abstraction
470325
///

src/serial/config.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//! Types for configuring a serial interface.
2+
3+
use crate::pac::usart1::cr2::STOP_A;
4+
use crate::time::rate::{Baud, Extensions};
5+
6+
/// Stop Bit configuration parameter for serial.
7+
///
8+
/// Wrapper around [`STOP_A`]
9+
#[derive(Clone, Copy, Debug, PartialEq)]
10+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
11+
pub enum StopBits {
12+
/// 0.5 stop bit
13+
Stop0P5,
14+
/// 1 stop bit
15+
Stop1,
16+
/// 1.5 stop bit
17+
Stop1P5,
18+
/// 2 stop bit
19+
Stop2,
20+
}
21+
22+
impl From<StopBits> for STOP_A {
23+
fn from(stopbit: StopBits) -> Self {
24+
match stopbit {
25+
StopBits::Stop0P5 => STOP_A::STOP0P5,
26+
StopBits::Stop1 => STOP_A::STOP1,
27+
StopBits::Stop1P5 => STOP_A::STOP1P5,
28+
StopBits::Stop2 => STOP_A::STOP2,
29+
}
30+
}
31+
}
32+
33+
impl From<STOP_A> for StopBits {
34+
fn from(stopbit: STOP_A) -> Self {
35+
match stopbit {
36+
STOP_A::STOP0P5 => StopBits::Stop0P5,
37+
STOP_A::STOP1 => StopBits::Stop1,
38+
STOP_A::STOP1P5 => StopBits::Stop1P5,
39+
STOP_A::STOP2 => StopBits::Stop2,
40+
}
41+
}
42+
}
43+
44+
/// Parity generation and checking. If odd or even parity is selected, the
45+
/// underlying USART will be configured to send/receive the parity bit in
46+
/// addtion to the data bits.
47+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
48+
#[derive(Debug, Clone, Copy, PartialEq)]
49+
pub enum Parity {
50+
/// No parity bit will be added/checked.
51+
None,
52+
/// The MSB transmitted/received will be generated/checked to have a
53+
/// even number of bits set.
54+
Even,
55+
/// The MSB transmitted/received will be generated/checked to have a
56+
/// odd number of bits set.
57+
Odd,
58+
}
59+
60+
/// Configuration struct for [`Serial`](super::Serial) providing all
61+
/// communication-related / parameters. `Serial` always uses eight data
62+
/// bits plus the parity bit - if selected.
63+
///
64+
/// Create a configuration by using `default` in combination with the
65+
/// builder methods. The following snippet shows creating a configuration
66+
/// for 19,200 Baud, 8N1 by deriving it from the default value:
67+
/// ```
68+
/// # use stm32f3xx_hal::serial::config::*;
69+
/// # use stm32f3xx_hal::time::rate::{Baud, Extensions};
70+
/// let config = Config::default().baudrate(19_200.Bd());
71+
///
72+
/// assert!(config.baudrate == 19_200.Bd());
73+
/// assert!(config.parity == Parity::None);
74+
/// assert!(config.stopbits == StopBits::STOP1);
75+
/// ```
76+
#[derive(Debug, Clone, Copy, PartialEq)]
77+
#[non_exhaustive]
78+
pub struct Config {
79+
/// Serial interface baud rate
80+
pub baudrate: Baud,
81+
/// Whether and how to generate/check a parity bit
82+
pub parity: Parity,
83+
/// The number of stop bits to follow the last data bit or the parity
84+
/// bit
85+
pub stopbits: StopBits,
86+
}
87+
88+
impl Config {
89+
/// Sets the given baudrate.
90+
pub fn baudrate(mut self, baudrate: impl Into<Baud>) -> Self {
91+
self.baudrate = baudrate.into();
92+
self
93+
}
94+
95+
/// Sets the given parity.
96+
pub fn parity(mut self, parity: Parity) -> Self {
97+
self.parity = parity;
98+
self
99+
}
100+
101+
/// Sets the stop bits to `stopbits`.
102+
pub fn stopbits(mut self, stopbits: StopBits) -> Self {
103+
self.stopbits = stopbits;
104+
self
105+
}
106+
}
107+
108+
impl Default for Config {
109+
/// Creates a new configuration with typically used parameters: 115,200
110+
/// Baud 8N1.
111+
fn default() -> Config {
112+
Config {
113+
baudrate: 115_200.Bd(),
114+
parity: Parity::None,
115+
stopbits: StopBits::Stop1,
116+
}
117+
}
118+
}
119+
120+
impl<T: Into<Baud>> From<T> for Config {
121+
fn from(b: T) -> Config {
122+
Config {
123+
baudrate: b.into(),
124+
..Default::default()
125+
}
126+
}
127+
}
128+
129+
#[cfg(feature = "defmt")]
130+
impl defmt::Format for Config {
131+
fn format(&self, f: defmt::Formatter) {
132+
// Omitting pins makes it:
133+
// 1. Easier.
134+
// 2. Not to specialized to use it ergonimically for users
135+
// even in a generic context.
136+
// 3. Not require specialization.
137+
defmt::write!(
138+
f,
139+
"Serial {{ baudrate: {} Bd , parity: {} , stopbits: {} }}",
140+
self.baudrate.0,
141+
self.parity,
142+
self.stopbits,
143+
);
144+
}
145+
}

0 commit comments

Comments
 (0)