Skip to content

Commit bba94e7

Browse files
bors[bot]mattico
andauthored
Merge #297
297: serial: document parity and word length configuration, set word length based on parity r=richardeoin a=mattico The first commit documents how serial data word length and parity are related. The second commit removes manual control over serial data word length but configures it automatically based on the parity. This seems reasonable to do since we only support reading/writing 8-bit data. closes #296 Co-authored-by: Matt Ickstadt <[email protected]>
2 parents 9a65919 + 35c6b23 commit bba94e7

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
constants to specify how many transmit / receive buffers to include in
2222
`ethernet::DesRing`. To replicate the previous behaviour, use `DesRing<4, 4>`
2323
* spi: Utilise FIFO in `Transfer` and `Write` implementations
24+
* **Breaking**: manual control over serial data length removed, now set automatically based on parity
2425

2526
## [v0.10.0] 2021-07-xx
2627

src/serial.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ pub enum Event {
7676
pub mod config {
7777
use crate::time::Hertz;
7878

79-
#[derive(Copy, Clone, PartialEq)]
80-
pub enum WordLength {
81-
DataBits8,
82-
DataBits9,
83-
}
79+
/// The parity bits appended to each serial data word
80+
///
81+
/// When enabled parity bits will be automatically added by hardware on transmit, and automatically checked by
82+
/// hardware on receive. For example, `read()` would return [`Error::Parity`](super::Error::Parity).
83+
///
84+
/// Note that parity bits are included in the serial word length, so if parity is used word length will be set to 9.
8485
#[derive(Copy, Clone, PartialEq)]
8586
pub enum Parity {
8687
ParityNone,
@@ -125,7 +126,6 @@ pub mod config {
125126
#[derive(Copy, Clone)]
126127
pub struct Config {
127128
pub baudrate: Hertz,
128-
pub wordlength: WordLength,
129129
pub parity: Parity,
130130
pub stopbits: StopBits,
131131
pub bitorder: BitOrder,
@@ -142,7 +142,6 @@ pub mod config {
142142
pub fn new<T: Into<Hertz>>(frequency: T) -> Self {
143143
Config {
144144
baudrate: frequency.into(),
145-
wordlength: WordLength::DataBits8,
146145
parity: Parity::ParityNone,
147146
stopbits: StopBits::STOP1,
148147
bitorder: BitOrder::LsbFirst,
@@ -162,26 +161,24 @@ pub mod config {
162161
self
163162
}
164163

164+
/// Enables Even Parity
165+
///
166+
/// Note that parity bits are included in the serial word length, so if parity is used word length will be set
167+
/// to 9.
165168
pub fn parity_even(mut self) -> Self {
166169
self.parity = Parity::ParityEven;
167170
self
168171
}
169172

173+
/// Enables Odd Parity
174+
///
175+
/// Note that parity bits are included in the serial word length, so if parity is used word length will be set
176+
/// to 9.
170177
pub fn parity_odd(mut self) -> Self {
171178
self.parity = Parity::ParityOdd;
172179
self
173180
}
174181

175-
pub fn wordlength_8(mut self) -> Self {
176-
self.wordlength = WordLength::DataBits8;
177-
self
178-
}
179-
180-
pub fn wordlength_9(mut self) -> Self {
181-
self.wordlength = WordLength::DataBits9;
182-
self
183-
}
184-
185182
/// Specify the number of stop bits
186183
pub fn stopbits(mut self, stopbits: StopBits) -> Self {
187184
self.stopbits = stopbits;
@@ -595,9 +592,9 @@ macro_rules! usart {
595592
.m1()
596593
.clear_bit()
597594
.m0()
598-
.variant(match config.wordlength {
599-
WordLength::DataBits8 => M0::BIT8,
600-
WordLength::DataBits9 => M0::BIT9,
595+
.variant(match config.parity {
596+
Parity::ParityNone => M0::BIT8,
597+
_ => M0::BIT9,
601598
}).pce()
602599
.variant(match config.parity {
603600
Parity::ParityNone => PCE::DISABLED,

0 commit comments

Comments
 (0)