liveBook · Manning

Stm32f1xx-hal

As explained in Embedded Software with Rust

stm32f1xx-hal is a Rust hardware abstraction layer crate for STM32F1 microcontrollers.

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

How Embedded Software with Rust covers it

How deep it goes
Connects chip and density features to peripheral selection, then demonstrates GPIO pin setup and serial configuration.
Examples
Uses an STM32F103 Blue Pill with PA9 and PA10 configured for USART1 at 115,200 baud.
What you can do after
Apply stm32f1xx-hal to configure STM32F103 peripherals and initialize USART1 for UART output.
How it connects
Links Cargo features with Peripheral Access Crate selection, runtime integration, GPIO pins, and UART.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.