USART using stm32f4xx_hal

Hi

I am trying to print something on putty using USART to learn how serial communication works. I need to use it to write adc voltage values from rtic-adc-dma.rs later.

I am using serial.rs stm32f4xx_hal example on stm32f439 board as a starter. I tried to configure PA9 as TX and PA10 as RX and had set up a port on PUTTY. After cargo run I see that the program is flashed successfully but I don't see anything on PUTTY. I don't know what I am missing. Appreciate any help!

#![no_main]
#![no_std]
use defmt_rtt as _; // global logger
use panic_halt as _;

use cortex_m_rt::entry;
use stm32f4xx_hal as hal;

use crate::hal::{pac, prelude::*, serial::config::Config, serial::Serial};

use core::fmt::Write; // for pretty formatting of the serial output

#[entry]
fn main() -> ! {
    let dp = pac::Peripherals::take().unwrap();

    let gpioa = dp.GPIOA.split();

    let rcc = dp.RCC.constrain();

    let clocks = rcc.cfgr.use_hse(25.MHz()).freeze();

    let mut delay = dp.TIM1.delay_ms(&clocks);

    // define RX/TX pins
    let tx_pin = gpioa.pa9;
    let _rx_pin=gpioa.pa10;

    // configure serial
    // let mut tx = Serial::tx(dp.USART1, tx_pin, 9600.bps(), &clocks).unwrap();
    // or
   // let mut tx = dp.USART1.tx(tx_pin, 9600.bps(), &clocks).unwrap();
    let serial_config = Config::default().baudrate(9600.bps());
    let mut serial = Serial::new(dp.USART1, (tx_pin, _rx_pin), serial_config, &clocks).unwrap();

    let mut value: u8 = 0;

    loop {

        //Read the data received on PA10 (USART1_RX)
        if let Ok(byte) = nb::block!(serial.read()){
            // echo the received byte back to the sender
            nb::block!(serial.write(byte)).ok();
        }

        // print some value every 500 ms, value will overflow after 255
        writeln!(serial, "value: {value:02}\r").ok();
        value = value.wrapping_add(1);
        delay.delay(2.secs());
    }
}

image

image

Thank you!

what board are you using? is the board configuration compatible with the example code? for example, what clock source are you using? how the serial port is routed to the usb converter? details like these may give a clue.

1 Like

How do you do the port configuration for putty?

Use mode on command promt to see status for device COMx. Use this in Serial mode of putty.

Thanks for the reponse @nerditation !

I am using stm32f439zi board with stm32f4xx_hal example. I checked configurations, connections but still stuck at the same thing :frowning:

that's the microcontroller chip name, not the board. you must know your board configuration. for example, on the Nucleo-144 board, PA9 and PA10 are connected to USB OTG pins, and if you are using the virtual COM port of the on-board ST-Link probe, it's connected to USART3 through PD8 and PD9. on the other hand if you want to communicate via USART1, you must connect the right pins (PA9 and PA10 in this case) to your PC, presumably going though a USB-Serial bridging chip as these days no many PCs have serial ports.

doing embedded development, it is the very basics to know your hardware well, because you write software for a specific hardware, you can't copy-paste some code from the internet and expects it to be working, unless you are absolutely sure the code is targeting the exact same hardware configuration.

1 Like

Thank you again @nerditation!

I went through the datasheets and understand it better now. I am pretty new to embedded development and just get lost.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.