Skip to content

STM32F401: UART1 does not work send or recieve characters. #827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
saqibjaved977 opened this issue Dec 26, 2024 · 2 comments
Open

STM32F401: UART1 does not work send or recieve characters. #827

saqibjaved977 opened this issue Dec 26, 2024 · 2 comments

Comments

@saqibjaved977
Copy link

saqibjaved977 commented Dec 26, 2024

Hi,

I have following code from the serial example. I am using UART1 on stm32f401 with pins PA9 and PA10. Baudraet is 9600, I have a bluetooth module HC-05 connected.
Rx<->Tx PA9
Tx<->Rx PA10

I connect bluetooth module to my phone and use an app to send and read data, but I do not recieve it and cant read it back as well.

Anyone who can help me with it?

#![no_main]
#![no_std]

use panic_halt as _;

use stm32f4xx_hal as hal;

use crate::hal::{block, pac, prelude::*, serial::config::Config};
use cortex_m_rt::entry;

use core::fmt::Write;

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

    let gpioa = dp.GPIOA.split();
    //let gpiob = dp.GPIOB.split();
    let mut led = gpioa.pa5.into_push_pull_output();

    let rcc = dp.RCC.constrain();

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

    let mut delay = cp.SYST.delay(&clocks);

    // define RX/TX pins
    let tx_pin = gpioa.pa9.into_alternate();
    let rx_pin = gpioa.pa10.into_alternate();

    // configure serial
    let serial = dp
        .USART1
        .serial(
            (tx_pin, rx_pin),
            Config::default().baudrate(9600.bps()),
            &clocks,
        )
        .unwrap();

    let (mut tx, mut rx) = serial.split();

    loop {
        // Receive what we just sent
        let received: u16 = rx.read().unwrap();

        // Update LEDs to display what was received
        if received == 1 {
            led.set_high();
        } else {
            led.set_low();
        }
        tx.write(received).unwrap();

        delay.delay_ms(10);
    }
}
@burrbull
Copy link
Member

Why are you reading u16, not u8?

@saqibjaved977
Copy link
Author

saqibjaved977 commented Dec 27, 2024

I have fixed the Rx and changed configuration to read u16. I have also added block!(rx.read()).unwrap() and block!(tx.write(received).unwrap() .

I am able to receive the what I send from the phone. But the Tx is not working, its not transmitted back.

`//! Blinks an LED

#![no_main]
#![no_std]

use panic_halt as _;

use stm32f4xx_hal as hal;

use crate::hal::{block, pac, prelude::*, serial::config::Config};
use cortex_m_rt::entry;

use core::fmt::Write;
use core::ops::Range;

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

let gpioa = dp.GPIOA.split();
//let gpiob = dp.GPIOB.split();
let mut led = gpioa.pa5.into_push_pull_output();

let rcc = dp.RCC.constrain();

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

let mut delay = cp.SYST.delay(&clocks);

// define RX/TX pins
//let tx_pin = gpiob.pb6.into_alternate();
//let rx_pin = gpiob.pb7.into_alternate();

let tx_pin = gpioa.pa9.into_alternate();
let rx_pin = gpioa.pa10.into_alternate();

let serial = dp
    .USART1
    .serial(
        (tx_pin, rx_pin),
        Config::default().baudrate(9600.bps()),
        &clocks,
    )
    .unwrap()
    // Make this Serial object use u16s instead of u8s
    .with_u16_data();

let (mut tx, mut rx) = serial.split();

loop {
    // Read character and echo it back
    let received: u16 = block!(rx.read()).unwrap();
    if received == 70 {
        led.set_high();
    } else {
        led.set_low();
    }
    block!(tx.write(received)).unwrap();
    delay.delay_ms(10);
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants