embassy-rs / embassy

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

Question: BufferedUart + half duplex #3278

Open BKSalman opened 3 weeks ago

BKSalman commented 3 weeks ago

Hello,

I'm using stm32f446re, and I wanted to have uart with embedded_io_async::Write & embedded_io_async::Read implemented, but also be half duplex

so I'm wondering if that is possible, and just hasn't been implemented yet, or not

thank you for the great project :)

badrbouslikhin commented 2 weeks ago

Hi @BKSalman, It's already possible. Take a look at UartRx in embassy_stm32::usart - Rust and into_ring_buffered - UartRx in embassy_stm32::usart - Rust.

Here's a snippet of how to achieve this:

    const DMA_BUF_SIZE: usize = 256;

    let mut usart = Uart::new_half_duplex(
        p.UART4,
        p.PA0,
        Irqs,
        p.DMA1_CH0,
        p.DMA1_CH1,
        config,
        HalfDuplexConfig::PushPull,
    )
    .unwrap();

    let (mut tx, rx) = usart.split();
    static mut DMA_BUF: [u8; DMA_BUF_SIZE] = [0; DMA_BUF_SIZE];
    let dma_buf = unsafe { DMA_BUF.as_mut() };
    let rx = rx.into_ring_buffered(dma_buf);

In this case, tx implements embedded_io_async::Write and rx implements embedded_io_async::Read.

BKSalman commented 2 weeks ago

thank you for the help :), this might work for the time being

however I feel like this should be part of the BufferedUart API that it just handles the half duplex configuration the same way Uart does (using BufferedUart::new_half_duplex()), and the user doesn't have to split tx and rx