Skip to content
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

Rework Uart constructors, add UartTx and UartRx constuctors. #1592

Merged
merged 26 commits into from
Jun 11, 2024

Conversation

SergioGasquez
Copy link
Member

@SergioGasquez SergioGasquez commented May 24, 2024

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

  • Uart constructors now take tx and rx pins.
  • Added new_with_default_pins constructor which uses the defaults Serial/UART gpios.
    • This made some examples/tests a bit noisy with some cfgs arrounds.
  • Added constructor for UartTx and UartRx`.
  • Introduced the following methods: with_cts, with_rts
  • Removed the configure_pins methods.
  • Fix Double check handling of pins used by drivers #1544

Testing

Reproducer example
//! Issue 1544

//% 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, Level, Output},
    peripherals::Peripherals,
    prelude::*,
    system::SystemControl,
    uart::{config::Config, Uart},
};
use esp_println::println;
use nb::block;

#[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);

    // Issue - This built before
    // let pins = TxRxPins::new_tx_rx(&mut io.pins.gpio4, &mut io.pins.gpio2);
    // let mut serial1 = Uart::new_with_config(
    //     peripherals.UART1,
    //     Config::default(),
    //     Some(pins),
    //     &clocks,
    //     None,
    // );
    // let _led = Output::new(io.pins.gpio4, Level::High);

    // Solution
    let mut serial1 = Uart::new(peripherals.UART1, &clocks, io.pins.gpio4, io.pins.gpio2);

    // let _led = Output::new(io.pins.gpio4, Level::High); // This fails to build

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

    println!("Start");
    loop {
        serial1.write_byte(0x42).ok();
        let read = block!(serial1.read_byte());

        match read {
            Ok(read) => println!("Read 0x{:02x}", read),
            Err(err) => println!("Error {:?}", err),
        }

        delay.delay_millis(250);
    }
}

@SergioGasquez SergioGasquez linked an issue May 24, 2024 that may be closed by this pull request
@SergioGasquez SergioGasquez marked this pull request as draft May 24, 2024 12:55
@SergioGasquez SergioGasquez force-pushed the fix/check-pins branch 2 times, most recently from ba1bb23 to b540ad0 Compare May 24, 2024 13:30
@MabezDev
Copy link
Member

In light of #1544 I wonder if we want to take the time now to always take the tx/rx pins and only make the flow control pins optional?

@bjoernQ
Copy link
Contributor

bjoernQ commented May 24, 2024

In light of #1544 I wonder if we want to take the time now to always take the tx/rx pins and only make the flow control pins optional?

I guess having only TX or only RX is a valid use-case?

@MabezDev
Copy link
Member

I guess having only TX or only RX is a valid use-case?

True, but if you only passed TX for example, you can still call read_byte() which will use the default RX pin. I wonder how other HALs handle this 🤔

@bjoernQ
Copy link
Contributor

bjoernQ commented May 24, 2024

I guess having only TX or only RX is a valid use-case?

True, but if you only passed TX for example, you can still call read_byte() which will use the default RX pin. I wonder how other HALs handle this 🤔

Maybe adding type-state but that gets annoying later

@MabezDev
Copy link
Member

I guess having only TX or only RX is a valid use-case?

So I think we should add constructors to UartTx and UartRx to take the uart instance + the relevant pin. We should also optionally add the ability to add the rts/cts pin respectively on each struct. We should then change Uart to always take the tx and rx pins.

@SergioGasquez could you make those changes?

@MabezDev
Copy link
Member

One final note, we could also offer a Uart::new_with_default_pins, where you still need to pass the pins in, but the pins are the correct default to print out to the uart serial converter. Not sure if this is a good idea though?

@bjoernQ
Copy link
Contributor

bjoernQ commented May 28, 2024

One final note, we could also offer a Uart::new_with_default_pins, where you still need to pass the pins in, but the pins are the correct default to print out to the uart serial converter. Not sure if this is a good idea though?

That would be convenient in a couple of situations - especially since the pins are different for different targets

esp-hal/src/uart.rs Outdated Show resolved Hide resolved
esp-hal/src/uart.rs Outdated Show resolved Hide resolved
esp-hal/src/uart.rs Outdated Show resolved Hide resolved
@SergioGasquez SergioGasquez force-pushed the fix/check-pins branch 11 times, most recently from a6886b2 to c3a4680 Compare May 30, 2024 09:34
@SergioGasquez SergioGasquez marked this pull request as ready for review May 30, 2024 10:07
Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think this is pretty close, just a couple more things :)

esp-hal/src/uart.rs Outdated Show resolved Hide resolved
esp-hal/src/uart.rs Outdated Show resolved Hide resolved
Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need UartTx/Rx::new_async methods I think looking at the generated documentation (sorry I missed that earlier!).

esp-hal/src/uart.rs Show resolved Hide resolved
@SergioGasquez SergioGasquez force-pushed the fix/check-pins branch 3 times, most recently from 895624b to c5553da Compare June 11, 2024 12:10
Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for working on this!

@MabezDev MabezDev added this pull request to the merge queue Jun 11, 2024
Merged via the queue into esp-rs:main with commit a33159a Jun 11, 2024
29 checks passed
@SergioGasquez SergioGasquez deleted the fix/check-pins branch June 11, 2024 13:41
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

Successfully merging this pull request may close these issues.

Double check handling of pins used by drivers
5 participants