Skip to content

Commit a806ce2

Browse files
committed
serial: Add databits, use default config in examples
1 parent f31d858 commit a806ce2

File tree

5 files changed

+46
-36
lines changed

5 files changed

+46
-36
lines changed

examples/serial_delay.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,8 @@ fn main() -> ! {
3939
(tx, rx),
4040
&clocks,
4141
serial::Config {
42-
baud_rate: 115_200.bps(),
43-
oversampling: serial::Oversampling::By16,
44-
character_match: None,
45-
sysclock: false,
46-
parity: serial::Parity::ParityNone,
42+
// Default to 115_200 bauds
43+
..Default::default()
4744
},
4845
);
4946
let (mut tx, _) = serial.split();

examples/serial_dma.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ fn main() -> ! {
4747
(tx, rx),
4848
&clocks,
4949
serial::Config {
50-
baud_rate: 115_200.bps(),
51-
oversampling: serial::Oversampling::By16,
52-
character_match: None,
53-
sysclock: false,
54-
parity: serial::Parity::ParityNone,
50+
// Default to 115_200 bauds
51+
..Default::default()
5552
},
5653
);
5754
let (mut tx, mut rx) = serial.split();

examples/serial_echo.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ fn main() -> ! {
3636
(tx, rx),
3737
&clocks,
3838
serial::Config {
39-
baud_rate: 115_200.bps(),
40-
oversampling: serial::Oversampling::By16,
41-
character_match: None,
42-
sysclock: false,
43-
parity: serial::Parity::ParityNone,
39+
// Default to 115_200 bauds
40+
..Default::default()
4441
},
4542
);
4643

examples/serial_parity.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use cortex_m_rt::entry;
1515
use stm32f7xx_hal::{
1616
pac,
1717
prelude::*,
18-
serial::{self, Serial},
18+
serial::{self, Serial, DataBits},
1919
};
2020

2121
#[entry]
@@ -37,11 +37,11 @@ fn main() -> ! {
3737
(tx, rx),
3838
&clocks,
3939
serial::Config {
40-
baud_rate: 56_700.bps(),
41-
oversampling: serial::Oversampling::By16,
42-
character_match: None,
43-
sysclock: false,
44-
parity: serial::Parity::ParityEven,
40+
// Using 8 bits of data + 1 for even parity
41+
data_bits: DataBits::Bits9,
42+
parity: Parity::ParityEven,
43+
// Default to 115_200 bauds
44+
..Default::default()
4545
},
4646
);
4747

src/serial.rs

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

166-
// Enable tx / rx and configure parity
166+
// Enable tx / rx, configure data bits and parity
167167
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))
168+
w
169+
.te().enabled()
170+
.re().enabled()
171+
.ue().enabled();
172+
173+
// M[1:0] are used to set data bits
174+
// M[1:0] = 00: 1 Start bit, 8 data bits, n stop bits
175+
// M[1:0] = 01: 1 Start bit, 9 data bits, n stop bits
176+
// M[1:0] = 10: 1 Start bit, 7 data bits, n stop bits
177+
match config.data_bits {
178+
DataBits::Bits8 => w.m1().clear_bit().m0().bit8(),
179+
DataBits::Bits9 => w.m1().clear_bit().m0().bit9(),
180+
DataBits::Bits7 => w.m0().clear_bit().m1().bit7(),
181+
};
182+
183+
match config.parity {
184+
Parity::ParityEven => w.ps().even().pce().enabled(),
185+
Parity::ParityOdd => w.ps().odd().pce().enabled(),
186+
Parity::ParityNone => w.pce().disabled(),
187+
}
182188
});
183189

184190
// Enable DMA
@@ -429,13 +435,25 @@ pub struct Config {
429435
pub character_match: Option<u8>,
430436
pub sysclock: bool,
431437
pub parity: Parity,
438+
pub data_bits: DataBits,
439+
432440
}
433441

434442
pub enum Oversampling {
435443
By8,
436444
By16,
437445
}
438446

447+
/// Number of data bits
448+
pub enum DataBits {
449+
/// 8 bits of data
450+
Bits8,
451+
/// 9 bits of data
452+
Bits9,
453+
/// 7 bits of data
454+
Bits7,
455+
}
456+
439457
/// Parity generation and checking. If odd or even parity is selected, the
440458
/// underlying USART will be configured to send/receive the parity bit in
441459
/// addtion to the data bits.
@@ -458,6 +476,7 @@ impl Default for Config {
458476
character_match: None,
459477
sysclock: false,
460478
parity: Parity::ParityNone,
479+
data_bits: DataBits::Bits8,
461480
}
462481
}
463482
}

0 commit comments

Comments
 (0)