Skip to content

Commit 10007a1

Browse files
ahdinosaurmaximeborges
authored andcommitted
add serial parity
1 parent aaf2034 commit 10007a1

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

examples/serial_delay.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ fn main() -> ! {
4343
oversampling: serial::Oversampling::By16,
4444
character_match: None,
4545
sysclock: false,
46+
parity: serial::Parity::ParityNone,
4647
},
4748
);
4849
let (mut tx, _) = serial.split();

examples/serial_dma.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ fn main() -> ! {
5151
oversampling: serial::Oversampling::By16,
5252
character_match: None,
5353
sysclock: false,
54+
parity: serial::Parity::ParityNone,
5455
},
5556
);
5657
let (mut tx, mut rx) = serial.split();

examples/serial_echo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn main() -> ! {
4040
oversampling: serial::Oversampling::By16,
4141
character_match: None,
4242
sysclock: false,
43+
parity: serial::Parity::ParityNone,
4344
},
4445
);
4546

src/serial.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,23 @@ where
163163
let ch = config.character_match.unwrap_or(0);
164164
usart.cr2.write(|w| w.add().bits(ch));
165165

166-
// Enable transmission and receiving
167-
usart
168-
.cr1
169-
.modify(|_, w| w.te().enabled().re().enabled().ue().enabled());
166+
// Enable tx / rx and configure parity
167+
usart.cr1.modify(|_, w| {
168+
w.te()
169+
.enabled()
170+
.re()
171+
.enabled()
172+
.ue()
173+
.enabled()
174+
.m1()
175+
.m0()
176+
.m0()
177+
.bit(!matches!(config.parity, Parity::ParityNone))
178+
.pce()
179+
.bit(!matches!(config.parity, Parity::ParityNone))
180+
.ps()
181+
.bit(matches!(config.parity, Parity::ParityOdd))
182+
});
170183

171184
// Enable DMA
172185
usart.cr3.write(|w| w.dmat().enabled().dmar().enabled());
@@ -415,20 +428,36 @@ pub struct Config {
415428
pub oversampling: Oversampling,
416429
pub character_match: Option<u8>,
417430
pub sysclock: bool,
431+
pub parity: Parity,
418432
}
419433

420434
pub enum Oversampling {
421435
By8,
422436
By16,
423437
}
424438

439+
/// Parity generation and checking. If odd or even parity is selected, the
440+
/// underlying USART will be configured to send/receive the parity bit in
441+
/// addtion to the data bits.
442+
pub enum Parity {
443+
/// No parity bit will be added/checked.
444+
ParityNone,
445+
/// The MSB transmitted/received will be generated/checked to have a
446+
/// even number of bits set.
447+
ParityEven,
448+
/// The MSB transmitted/received will be generated/checked to have a
449+
/// odd number of bits set.
450+
ParityOdd,
451+
}
452+
425453
impl Default for Config {
426454
fn default() -> Self {
427455
Self {
428456
baud_rate: 115_200.bps(),
429457
oversampling: Oversampling::By16,
430458
character_match: None,
431459
sysclock: false,
460+
parity: Parity::ParityNone,
432461
}
433462
}
434463
}

0 commit comments

Comments
 (0)