esp-hal icon indicating copy to clipboard operation
esp-hal copied to clipboard

esp_hal: Add read_bytes() to Uart

Open JurajSadel opened this issue 1 year ago • 0 comments

Thank you for your contribution!

We appreciate the time and effort you've put into this pull request. To help us review it efficiently, please ensure you've gone through the following checklist:

Submission Checklist 📝

  • [ ] I have updated existing examples or added new ones (if applicable).
  • [ ] I have used cargo xtask fmt-packages command to ensure that all changed code is formatted correctly.
  • [ ] My changes were added to the CHANGELOG.md in the proper section.
  • [ ] My changes are in accordance to the esp-rs API guidelines

Extra:

Pull Request Details 📖

Description

Please provide a clear and concise description of your changes, including the motivation behind these changes. The context is crucial for the reviewers.

Testing

//! This shows how to configure UART
//! You can short the TX and RX pin and see it reads what was written.
//! Additionally you can connect a logic analyzer to TX and see how the changes
//! of the configuration change the output signal.
//!
//! The following wiring is assumed:
//! - TX => GPIO4
//! - RX => GPIO5

//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3

#![no_std]
#![no_main]

use esp_backtrace as _;
use esp_hal::{
    clock::ClockControl,
    delay::Delay,
    gpio::Io,
    peripherals::Peripherals,
    prelude::*,
    system::SystemControl,
    uart::Uart,
};
use esp_println::println;

#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let system = SystemControl::new(peripherals.SYSTEM);
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

    let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);

    let mut serial1 = Uart::new(peripherals.UART1, &clocks, io.pins.gpio4, io.pins.gpio5).unwrap();

    let delay = Delay::new(&clocks);

    let mut buf = &mut [0; 32];

    println!("Start");
    delay.delay_millis(10);
    loop {
        let written = serial1.write_bytes(&[5; 30]).ok().unwrap();
        // delay.delay_millis(1);
        let mut buf = &mut [0; 32];
        let read = serial1.read_bytes(buf, written);

        println!("read: {}", read);

        delay.delay_millis(500);
    }
}

Opening as a draft because I'm not sure if this is the correct approach (definitely not the best). The get_rx_fifo_count() is pretty slow for "bigger" transfers and I'm not sure how to block until all bytes are written. Other option would be to "play" a bit with delay before calling read_bytes but that seems hacky as well.

Any idea/input here would be appreciated.

closes #1567

JurajSadel avatar Jun 19 '24 15:06 JurajSadel