Skip to content

Commit 0bc46f9

Browse files
author
Mathias Gottschlag
committed
Polish the UART examples.
1 parent 2bc2a93 commit 0bc46f9

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

examples/uart-dma.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn main() -> ! {
2424
utils::logger::init();
2525

2626
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
27+
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
2728

2829
let rcc = dp.RCC.constrain();
2930
let mut rcc = rcc.freeze(rcc::Config::hsi());
@@ -48,26 +49,29 @@ fn main() -> ! {
4849
)
4950
.unwrap();
5051

52+
let mut delay_syst = cp.SYST.delay(&rcc.clocks);
5153
let mut led = gpioa.pa5.into_push_pull_output();
5254

5355
info!("Start writing");
5456

55-
writeln!(usart, "Hello without DMA\r\n").unwrap();
57+
writeln!(usart, "Hello without DMA\r").unwrap();
5658

57-
let tx_buffer = cortex_m::singleton!(: [u8; 16] = *b"Hello with DMA!\n").unwrap();
59+
let tx_buffer = cortex_m::singleton!(: [u8; 17] = *b"Hello with DMA!\r\n").unwrap();
5860

5961
let (tx, _rx) = usart.split();
6062

61-
// Setup DMA for USART1 TX with dma channel 1.
63+
// Setup DMA for USART2 TX with dma channel 1.
6264
let mut transfer =
6365
streams
6466
.0
6567
.into_memory_to_peripheral_transfer(tx.enable_dma(), &mut tx_buffer[..], config);
6668

69+
transfer.start(|_tx| {});
6770
loop {
68-
transfer.start(|_tx| {});
6971
while !transfer.get_transfer_complete_flag() {}
7072

73+
delay_syst.delay(1000.ms());
7174
led.toggle().unwrap();
75+
transfer.restart(|_tx| {});
7276
}
7377
}

examples/uart-fifo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn main() -> ! {
6161
// Handle other error Overrun, Framing, Noise or Parity
6262
}
6363
Ok(byte) => {
64-
writeln!(tx1, "{}: {}\n", cnt, byte).unwrap();
64+
writeln!(tx1, "{}: {}\r", cnt, byte).unwrap();
6565
cnt += 1;
6666
}
6767
}

examples/uart.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ fn main() -> ! {
2626
let mut rcc = dp.RCC.freeze(rcc::Config::hsi());
2727

2828
info!("Init UART");
29+
// on Nucleo-G474, the pins marked TX/RX are connected to USART1 by default, whereas USART2 is
30+
// connected to the ST-Link's UART.
2931
let gpioa = dp.GPIOA.split(&mut rcc);
3032
let tx = gpioa.pa2.into_alternate();
3133
let rx = gpioa.pa3.into_alternate();
@@ -41,12 +43,14 @@ fn main() -> ! {
4143
.usart(tx, rx, FullConfig::default(), &mut rcc)
4244
.unwrap();*/
4345

44-
writeln!(usart, "Hello USART2\r\n").unwrap();
46+
writeln!(usart, "Hello USART1\r\n").unwrap();
4547

4648
let mut cnt = 0;
4749
loop {
48-
let byte = block!(usart.read()).unwrap();
49-
writeln!(usart, "{}: {}\r", cnt, byte).unwrap();
50+
match block!(usart.read()) {
51+
Ok(byte) => writeln!(usart, "{}: {}\r", cnt, byte).unwrap(),
52+
Err(e) => writeln!(usart, "E: {:?}\r", e).unwrap(),
53+
};
5054
cnt += 1;
5155
}
5256
}

0 commit comments

Comments
 (0)