Skip to content

Commit

Permalink
Multiple API fixes in UART driver (#2851)
Browse files Browse the repository at this point in the history
* Close a number of issues

doctest update

fmt

* changelog entry

* dumb

* addressing reviews

* Add an entry to migration guide
  • Loading branch information
playfulFence authored Dec 20, 2024
1 parent 2862197 commit 62c7294
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 32 deletions.
3 changes: 3 additions & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `crate::Mode` was renamed to `crate::DriverMode` (#2828)
- Renamed some I2C error variants (#2844)
- I2C: Replaced potential panics with errors. (#2831)
- UART: Make `AtCmdConfig` and `ConfigError` non-exhaustive (#2851)
- UART: Make `AtCmdConfig` use builder-lite pattern (#2851)

### Fixed

- Xtensa devices now correctly enable the `esp-hal-procmacros/rtc-slow` feature (#2594)
- User-bound GPIO interrupt handlers should no longer interfere with async pins. (#2625)
- `spi::master::Spi::{into_async, into_blocking}` are now correctly available on the typed driver, to. (#2674)
- It is no longer possible to safely conjure `GpioPin` instances (#2688)
- UART: Public API follows `C-WORD_ORDER` Rust API standard (`VerbObject` order) (#2851)

### Removed

Expand Down
7 changes: 7 additions & 0 deletions esp-hal/MIGRATING-0.22.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,10 @@ The reexports that were previously part of the prelude are available through oth
- `esp_hal::timer::Timer`
- `esp_hal::interrupt::InterruptConfigurable`
- The `entry` macro can be imported as `esp_hal::entry`, while other macros are found under `esp_hal::macros`

## `AtCmdConfig` now uses builder-lite pattern

```diff
- uart0.set_at_cmd(AtCmdConfig::new(None, None, None, b'#', None));
+ uart0.set_at_cmd(AtCmdConfig::default().with_cmd_char(b'#'));
```
50 changes: 22 additions & 28 deletions esp-hal/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
//! uart0.set_interrupt_handler(interrupt_handler);
//!
//! critical_section::with(|cs| {
//! uart0.set_at_cmd(AtCmdConfig::new(None, None, None, b'#', None));
//! uart0.set_at_cmd(AtCmdConfig::default().with_cmd_char(b'#'));
//! uart0.listen(UartInterrupt::AtCmd | UartInterrupt::RxFifoFull);
//!
//! SERIAL.borrow_ref_mut(cs).replace(uart0);
Expand Down Expand Up @@ -250,6 +250,7 @@ use crate::{
};

const UART_FIFO_SIZE: u16 = 128;
const CMD_CHAR_DEFAULT: u8 = 0x2b;

/// UART Error
#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -510,6 +511,9 @@ impl Default for Config {
}
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, procmacros::BuilderLite)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
/// Configuration for the AT-CMD detection functionality
pub struct AtCmdConfig {
/// Optional idle time before the AT command detection begins, in clock
Expand All @@ -524,28 +528,17 @@ pub struct AtCmdConfig {
/// The character that triggers the AT command detection.
pub cmd_char: u8,
/// Optional number of characters to detect as part of the AT command.
pub char_num: Option<u8>,
pub char_num: u8,
}

impl AtCmdConfig {
/// Creates a new `AtCmdConfig` with the specified configuration.
///
/// This function sets up the AT command detection parameters, including
/// pre- and post-idle times, a gap timeout, the triggering command
/// character, and the number of characters to detect.
pub fn new(
pre_idle_count: Option<u16>,
post_idle_count: Option<u16>,
gap_timeout: Option<u16>,
cmd_char: u8,
char_num: Option<u8>,
) -> AtCmdConfig {
impl Default for AtCmdConfig {
fn default() -> Self {
Self {
pre_idle_count,
post_idle_count,
gap_timeout,
cmd_char,
char_num,
pre_idle_count: None,
post_idle_count: None,
gap_timeout: None,
cmd_char: CMD_CHAR_DEFAULT,
char_num: 1,
}
}
}
Expand Down Expand Up @@ -631,6 +624,7 @@ pub struct UartRx<'d, Dm, T = AnyUart> {
/// A configuration error.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum ConfigError {
/// The requested timeout is not supported.
UnsupportedTimeout,
Expand Down Expand Up @@ -752,7 +746,7 @@ where
}

/// Flush the transmit buffer of the UART
pub fn flush_tx(&mut self) -> nb::Result<(), Error> {
pub fn flush(&mut self) -> nb::Result<(), Error> {
if self.is_tx_idle() {
Ok(())
} else {
Expand Down Expand Up @@ -1219,7 +1213,7 @@ where

register_block.at_cmd_char().write(|w| unsafe {
w.at_cmd_char().bits(config.cmd_char);
w.char_num().bits(config.char_num.unwrap_or(1))
w.char_num().bits(config.char_num)
});

if let Some(pre_idle_count) = config.pre_idle_count {
Expand Down Expand Up @@ -1254,8 +1248,8 @@ where
}

/// Flush the transmit buffer of the UART
pub fn flush_tx(&mut self) -> nb::Result<(), Error> {
self.tx.flush_tx()
pub fn flush(&mut self) -> nb::Result<(), Error> {
self.tx.flush()
}

/// Read a byte from the UART
Expand Down Expand Up @@ -1477,7 +1471,7 @@ where
}

fn flush(&mut self) -> nb::Result<(), Self::Error> {
self.flush_tx()
self.flush()
}
}

Expand All @@ -1491,7 +1485,7 @@ where
}

fn flush(&mut self) -> nb::Result<(), Self::Error> {
self.flush_tx()
self.flush()
}
}

Expand Down Expand Up @@ -1581,7 +1575,7 @@ where
}

fn flush(&mut self) -> Result<(), Self::Error> {
self.tx.flush()
embedded_io::Write::flush(&mut self.tx)
}
}

Expand All @@ -1598,7 +1592,7 @@ where

fn flush(&mut self) -> Result<(), Self::Error> {
loop {
match self.flush_tx() {
match self.flush() {
Ok(_) => break,
Err(nb::Error::WouldBlock) => { /* Wait */ }
Err(nb::Error::Other(e)) => return Err(e),
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async fn main(spawner: Spawner) {
let mut uart0 = Uart::new(peripherals.UART0, config, rx_pin, tx_pin)
.unwrap()
.into_async();
uart0.set_at_cmd(AtCmdConfig::new(None, None, None, AT_CMD, None));
uart0.set_at_cmd(AtCmdConfig::default().with_cmd_char(AT_CMD));

let (rx, tx) = uart0.split();

Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/uart_regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod tests {
// set up TX and send a byte
let mut tx = UartTx::new(peripherals.UART0, uart::Config::default(), tx).unwrap();

tx.flush_tx().unwrap();
tx.flush().unwrap();
tx.write_bytes(&[0x42]).unwrap();
let read = block!(rx.read_byte());

Expand Down
4 changes: 2 additions & 2 deletions hil-test/tests/uart_tx_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod tests {
fn test_send_receive(mut ctx: Context) {
let byte = [0x42];

ctx.tx.flush_tx().unwrap();
ctx.tx.flush().unwrap();
ctx.tx.write_bytes(&byte).unwrap();
let read = block!(ctx.rx.read_byte());

Expand All @@ -50,7 +50,7 @@ mod tests {
let bytes = [0x42, 0x43, 0x44];
let mut buf = [0u8; 3];

ctx.tx.flush_tx().unwrap();
ctx.tx.flush().unwrap();
ctx.tx.write_bytes(&bytes).unwrap();

ctx.rx.read_bytes(&mut buf).unwrap();
Expand Down

0 comments on commit 62c7294

Please sign in to comment.