Skip to content

Commit 2c62bd9

Browse files
Only enable usart tx/rx when needed
1 parent ee11a82 commit 2c62bd9

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/serial.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ macro_rules! usart {
239239
pub fn $usart(usart: $USART, pins: (TXPIN, RXPIN), baud_rate: Bps, rcc: &mut Rcc) -> Self
240240
{
241241
let mut serial = Serial { usart, pins };
242-
serial.enable(baud_rate, rcc);
242+
serial.configure(baud_rate, rcc);
243+
// Enable transmission and receiving
244+
serial.usart.cr1.modify(|_, w| w.te().set_bit().re().set_bit().ue().set_bit());
243245
serial
244246
}
245247
}
@@ -253,7 +255,9 @@ macro_rules! usart {
253255
{
254256
let rxpin = ();
255257
let mut serial = Serial { usart, pins: (txpin, rxpin) };
256-
serial.enable(baud_rate, rcc);
258+
serial.configure(baud_rate, rcc);
259+
// Enable transmission
260+
serial.usart.cr1.modify(|_, w| w.te().set_bit().ue().set_bit());
257261
serial
258262
}
259263
}
@@ -267,13 +271,15 @@ macro_rules! usart {
267271
{
268272
let txpin = ();
269273
let mut serial = Serial { usart, pins: (txpin, rxpin) };
270-
serial.enable(baud_rate, rcc);
274+
serial.configure(baud_rate, rcc);
275+
// Enable receiving
276+
serial.usart.cr1.modify(|_, w| w.re().set_bit().ue().set_bit());
271277
serial
272278
}
273279
}
274280

275281
impl<TXPIN, RXPIN> Serial<$USART, TXPIN, RXPIN> {
276-
fn enable(&mut self, baud_rate: Bps, rcc: &mut Rcc) {
282+
fn configure(&mut self, baud_rate: Bps, rcc: &mut Rcc) {
277283
// Enable clock for USART
278284
rcc.regs.$apbenr.modify(|_, w| w.$usartXen().set_bit());
279285

@@ -284,9 +290,6 @@ macro_rules! usart {
284290
// Reset other registers to disable advanced USART features
285291
self.usart.cr2.reset();
286292
self.usart.cr3.reset();
287-
288-
// Enable transmission and receiving
289-
self.usart.cr1.modify(|_, w| w.te().set_bit().re().set_bit().ue().set_bit());
290293
}
291294

292295
/// Starts listening for an interrupt event

0 commit comments

Comments
 (0)