-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathusart.rs
More file actions
66 lines (56 loc) · 1.95 KB
/
Copy pathusart.rs
File metadata and controls
66 lines (56 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#![deny(warnings)]
#![no_main]
#![no_std]
use cortex_m_rt::entry;
mod utilities;
use embedded_hal::delay::DelayNs;
use embedded_io::{Read, Write};
use stm32h5xx_hal::{delay::Delay, pac, prelude::*, time::MilliSeconds};
use log::info;
#[entry]
fn main() -> ! {
utilities::logger::init();
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let pwrcfg = pwr.freeze();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc
.sys_ck(192.MHz())
.pll1_q_ck(64.MHz())
.freeze(pwrcfg, &dp.SBS);
// Acquire the GPIOB peripheral. This also enables the clock for
// GPIOB in the RCC register.
let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB);
// Connect TX and RX pins together for this to work. On the Nucleo-H503 this is pins 35 & 37 on
// CN10
let rx = gpiob.pb15.into_alternate();
let tx = gpiob.pb14.into_alternate();
info!("");
info!("stm32h5xx-hal example - USART");
info!("");
// Initialise the USART peripheral.
let mut serial = dp
.USART1
.serial((tx, rx), 115200.Hz(), ccdr.peripheral.USART1, &ccdr.clocks)
.unwrap();
let mut delay = Delay::new(cp.SYST, &ccdr.clocks);
let duration = MilliSeconds::from_secs(1).as_millis();
// Echo what is received on the USART
let read = &mut [0u8; 6];
let mut count = 1;
loop {
// Note: the USART driver uses a FIFO, so as long as we keep the messages below the FIFO
// size we won't miss any data.
write!(serial, "Test {count}").unwrap();
count = (count + 1) % 10;
serial.read_exact(read).unwrap();
let received = core::str::from_utf8(read).unwrap();
info!("Received: {}", received);
delay.delay_ms(duration);
}
}