Module stm32f1xx_hal::serial

source ·
Expand description

Serial Communication (USART)

This module contains the functions to utilize the USART (Universal synchronous asynchronous receiver transmitter)

Note

When transmitting with the parity enabled, the value written in the MSB (bit 7 or bit 8 depending on the word length) has no effect because it is replaced by the parity. When receiving with the parity enabled, the value read in the MSB is the received parity bit.

Frame formatWord LengthParity
7 data bits + 1 parity bit8 bitsV
8 data bits8 bits
8 data bits + 1 parity bit9 bitsV
9 data bits9 bits

Example usage:

let dp = stm32f1xx_hal::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut afio = dp.AFIO.constrain();
let mut gpioa = dp.GPIOA.split();

// USART1 on Pins A9 and A10
let pin_tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
let pin_rx = gpioa.pa10;
// Create an interface struct for USART1 with 9600 Baud
let serial = Serial::new(
   dp.USART1,
   (pin_tx, pin_rx),
   &mut afio.mapr,
   Config::default()
       .baudrate(9600.bps())
       .wordlength_9bits()
       .parity_none(),
   &clocks,
);

// Separate into tx and rx channels
let (mut tx, mut rx) = serial.split();

// Write data (9 bits) to the USART.
// Depending on the configuration, only the lower 7, 8, or 9 bits are used.
block!(tx.write_u16(0x1FF)).unwrap_infallible();

// Write 'R' (8 bits) to the USART
block!(tx.write(b'R')).unwrap_infallible();

// Receive a data (9 bits) from the USART and store it in "received"
let received = block!(rx.read_u16()).unwrap();

// Receive a data (8 bits) from the USART and store it in "received"
let received = block!(rx.read()).unwrap();

Structs

Stores data for release
Serial receiver
Serial abstraction
Serial transmitter

Enums

Serial error
Interrupt event

Traits

Functions

Reconfigure the USART instance.

Type Definitions