stm32f1xx-hal is a Rust hardware abstraction layer for STM32F1 microcontrollers. Cargo features select the chip family and density, enabling matching peripheral support while avoiding unnecessary code. section 3.1.2
#![no_std]
#![no_main]
use core::fmt::Write;
use cortex_m_rt::entry;
use panic_halt as _;
use stm32f1xx_hal::{pac, prelude::*, serial::Config};
#[entry]
fn main() -> ! {
// take ownership of the device peripherals; callable only once
let dp = pac::Peripherals::take().unwrap();
// wrap the raw RCC registers in a safer, stateful clock/reset API
let mut rcc = dp.RCC.constrain();
// split GPIOA into individually owned, typed pin handles
let mut gpioa = dp.GPIOA.split(&mut rcc);
// PA9 as USART1 TX (alternate function, push-pull)
let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
// PA10 as USART1 RX; the default floating input mode works fine for RX
let rx = gpioa.pa10;
// 115,200 baud, 8 data bits, no parity, 1 stop bit
let mut serial = dp
.USART1
.serial((tx, rx), Config::default().baudrate(115_200.bps()), &mut rcc);
// blocking write: returns once the whole message has been sent
writeln!(serial, "UART log message").unwrap();
loop {}
}
This UART setup takes ownership of device peripherals, configures the clock and GPIO pins, and initializes USART1 for 115,200-baud serial output. section 6.2.2
