Skip to content

Commit afd9563

Browse files
committed
Change StopBit to a new type to impl Format
1 parent 42dc60e commit afd9563

File tree

2 files changed

+69
-14
lines changed

2 files changed

+69
-14
lines changed

src/serial.rs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,17 +321,52 @@ cfg_if! {
321321

322322
/// Types for configuring a serial interface.
323323
pub mod config {
324+
use crate::pac::usart1::cr2::STOP_A;
324325
use crate::time::rate::{Baud, Extensions};
325326

326-
// Reexport stop bit enum from PAC. In case there is a breaking change,
327-
// provide a compatible enum from this HAL.
328-
pub use crate::pac::usart1::cr2::STOP_A as StopBits;
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+
}
329364

330365
/// Parity generation and checking. If odd or even parity is selected, the
331366
/// underlying USART will be configured to send/receive the parity bit in
332367
/// addtion to the data bits.
333368
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
334-
#[derive(Clone, Copy, PartialEq)]
369+
#[derive(Debug, Clone, Copy, PartialEq)]
335370
pub enum Parity {
336371
/// No parity bit will be added/checked.
337372
None,
@@ -359,7 +394,7 @@ pub mod config {
359394
/// assert!(config.parity == Parity::None);
360395
/// assert!(config.stopbits == StopBits::STOP1);
361396
/// ```
362-
#[derive(Clone, Copy, PartialEq)]
397+
#[derive(Debug, Clone, Copy, PartialEq)]
363398
#[non_exhaustive]
364399
pub struct Config {
365400
/// Serial interface baud rate
@@ -398,7 +433,7 @@ pub mod config {
398433
Config {
399434
baudrate: 115_200.Bd(),
400435
parity: Parity::None,
401-
stopbits: StopBits::STOP1,
436+
stopbits: StopBits::Stop1,
402437
}
403438
}
404439
}
@@ -411,6 +446,24 @@ pub mod config {
411446
}
412447
}
413448
}
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+
}
414467
}
415468

416469
/// Serial abstraction
@@ -586,7 +639,9 @@ where
586639
Parity::Odd => (M_A::BIT9, PS_A::ODD, PCE_A::ENABLED),
587640
};
588641

589-
usart.cr2.modify(|_, w| w.stop().variant(config.stopbits));
642+
usart
643+
.cr2
644+
.modify(|_, w| w.stop().variant(config.stopbits.into()));
590645
usart.cr1.modify(|_, w| {
591646
w.ps().variant(ps); // set parity mode
592647
w.pce().variant(pce); // enable parity checking/generation

testsuite/tests/uart.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,14 @@ mod tests {
214214
let built_1 = Config::default()
215215
.baudrate(123_456.Bd())
216216
.parity(Parity::Even)
217-
.stopbits(StopBits::STOP0P5);
217+
.stopbits(StopBits::Stop0P5);
218218
assert!(built_1.baudrate == 123_456.Bd());
219219
assert!(built_1.parity == Parity::Even);
220-
assert!(built_1.stopbits == StopBits::STOP0P5);
220+
assert!(built_1.stopbits == StopBits::Stop0P5);
221221

222222
let built_1_different_order = Config::default()
223223
.baudrate(42.Bd())
224-
.stopbits(StopBits::STOP0P5)
224+
.stopbits(StopBits::Stop0P5)
225225
.parity(Parity::Even)
226226
.baudrate(123_456.Bd());
227227
assert!(built_1 == built_1_different_order);
@@ -268,10 +268,10 @@ mod tests {
268268
#[test]
269269
fn stopbits_loopback(state: &mut super::State) {
270270
for stopbits in &[
271-
StopBits::STOP0P5,
272-
StopBits::STOP1,
273-
StopBits::STOP1P5,
274-
StopBits::STOP2,
271+
StopBits::Stop0P5,
272+
StopBits::Stop1,
273+
StopBits::Stop1P5,
274+
StopBits::Stop2,
275275
] {
276276
let config = Config::default().stopbits(*stopbits);
277277
test_test_msg_loopback(state, config);

0 commit comments

Comments
 (0)