Stm32f303 Usart gibberish


I'm trying to set up a serial connection using usart on the stm32f3Discovery, which has an stm32f303vc cpu on it. When sending text to it using Minicom, I get gibberish back. For instance, when typing abcdefghijklmnopqrstuvwxyz, I get yz{|}~hijklmnoxyz{|}~xyz. So, for some chatacters the value is correct, for some not. And the results are consistent.

This is my code:


use cortex_m_rt::entry;

use stm32f3xx_hal::prelude::*;
use stm32f3xx_hal::stm32;

unsafe fn panic_handler(info: &core::panic::PanicInfo) -> ! {
    cortex_m_semihosting::hprintln!("ERROR! {:?}", info).unwrap();
    loop {}

fn main() -> ! {
    let peripherals = stm32f3xx_hal::stm32::Peripherals::take().unwrap();
    let mut rcc = peripherals.RCC.constrain();
    let mut flash = peripherals.FLASH.constrain();
    let clocks = rcc.cfgr.freeze(&mut flash.acr);

    let mut gpioa = peripherals.GPIOA.split(&mut rcc.ahb);

    let usart1_txd = gpioa.pa9.into_af7(&mut gpioa.moder, &mut gpioa.afrh);
    let usart1_rxd = gpioa.pa10.into_af7(&mut gpioa.moder, &mut gpioa.afrh);

    let usart1 = stm32f3xx_hal::serial::Serial::usart1(
        (usart1_txd, usart1_rxd),
        &mut rcc.apb2,

    let (mut usart1_tx, mut usart1_rx) = usart1.split();

    loop {        
        match {
            Ok(b) => nb::block!(usart1_tx.write(b)).unwrap(),
            Err(nb::Error::Other(_)) => {
                let usart1 = unsafe { &*stm32::USART1::ptr() };
                usart1.icr.write(|w| unsafe { w.bits(0xFF) });
            _ => (),

Here's my .minirc.dfl:

pu baudrate 9600
pu bits 8
pu parity N
pu stopbits 1
pu rtscts No
pu xonxoff No

The command I use to start minicom:

$ minicom -D /dev/ttyUSB0 -b 9600 -o

Anybody know what causes this behaviour?

Without anything else to go by, I'd guess the baud rate is slightly out of whack. Maybe the clocks on the stm are not exactly what rust thinks they are?


I don'g know how STM works, but maybe your code uses the DMA.. Then you should try b ocking also the read, or not using DMA

1 Like

Sounds like a clock issue. I think you are using the HSI clock. Maybe using the HSE would help?

let clocks = rcc.cfgr.use_hse(8.mhz()).freeze(&mut flash.acr);

That's the solution! Thanks a bunch.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.