embassy-rs / embassy

Modern embedded framework, using Rust and async.
https://embassy.dev
Apache License 2.0
4.78k stars 647 forks source link

Uart write work but not read fn #2913

Open AnthonyYahtec opened 1 month ago

AnthonyYahtec commented 1 month ago

The function uart.read() don't work in usart i use a STM32L431RC.

bind_interrupts!(struct Irqs { USART1 => usart::InterruptHandler; });

[embassy_executor::main]

async fn main(spawner: Spawner) { let device_config = embassy_stm32::Config::default(); let p = embassy_stm32::init(device_config); let config = new_uart_config(9600u32, DataBits::DataBits8, Parity::ParityNone, StopBits::STOP1); let mut uart = Uart::new(p.USART1, p.PA10, p.PA9, Irqs, p.DMA1_CH4, p.DMA1_CH5, config).unwrap();

info!("current usart1 priority: {:?}", Interrupt::USART1.get_priority()); //P0

loop {
    let mut buf = [0u8; 128];
    // uart.blocking_write(&[0x02, 0x01]);

    let _n = uart.write(&mut [0x01, 0x02]).await;

    Timer::after_millis(200).await;
    uart.read_until_idle(&mut buf).await.unwrap();

    //Traitez les données reçues ici
    println!("Received data: {:?}", buf);

    Timer::after_millis(200).await;
}

}

lulf commented 1 month ago

Keep in mind that unless you enable hardware flow-control, the uart read might miss the data, because the basic usart does not do any buffering and doesn't enable RX until the read_until_idle() is called. To increase the likelyhood that it will read the input, try configuring the buffered uart instead. In any case, using hardware flow control is the best way to guarantee not missing any data.