-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserial_config.rs
74 lines (61 loc) · 2.09 KB
/
serial_config.rs
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
67
68
69
70
71
72
73
74
//! Serial interface loopback test
//!
//! You have to short the TX and RX pins to make this program work
#![deny(unsafe_code)]
#![no_main]
#![no_std]
use panic_halt as _;
use cortex_m_rt::entry;
use embedded_io::Write;
use gd32f1x0_hal::{
gpio::{OutputMode, PullMode},
pac,
prelude::*,
serial::{Config, Serial, StopBits},
};
#[entry]
fn main() -> ! {
// Get access to the device specific peripherals from the peripheral access crate.
let p = pac::Peripherals::take().unwrap();
// Take ownership of the RCU and FMC peripheral and convert them into the corresponding HAL
// structs.
let mut rcu = p.rcu.constrain();
let mut flash = p.fmc.constrain();
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
// `clocks`.
let clocks = rcu.cfgr.freeze(&mut flash.ws);
// Prepare the GPIOA peripheral
let mut gpioa = p.gpioa.split(&mut rcu.ahb);
// USART0
// Configure pa9 and pa10 in alternate function mode for the USART.
let tx = gpioa
.pa9
.into_alternate(&mut gpioa.config, PullMode::Floating, OutputMode::PushPull);
let rx = gpioa
.pa10
.into_alternate(&mut gpioa.config, PullMode::Floating, OutputMode::PushPull);
// USART1
// let tx = gpioa
// .pa2
// .into_alternate(&mut gpioa.config, PullMode::Floating, OutputMode::PushPull);
// let rx = gpioa
// .pa3
// .into_alternate(&mut gpioa.config, PullMode::Floating, OutputMode::PushPull);
// Set up the usart device. Takes ownership of the USART registers and tx/rx pins. The rest of
// the registers are used to enable and configure the device.
let serial = Serial::usart(
p.usart0,
(tx, rx),
Config::default()
.baudrate(9600.bps())
.stopbits(StopBits::Stop2)
.parity_odd(),
clocks,
&mut rcu.apb2,
);
// Split the serial struct into a receiving and a transmitting part
let (mut tx, _rx) = serial.split();
tx.write_all(b"UU").unwrap();
#[allow(clippy::empty_loop)]
loop {}
}