From e4b2549523e3e1a6e2a2e3d91fdaaa9107ea3680 Mon Sep 17 00:00:00 2001 From: bjoernQ Date: Wed, 28 Feb 2024 14:10:11 +0100 Subject: [PATCH] Introduce a `uart_peripheral_reset` function --- esp-hal/src/uart.rs | 66 +++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/esp-hal/src/uart.rs b/esp-hal/src/uart.rs index ae0da3f59b4..08bd24cbe90 100644 --- a/esp-hal/src/uart.rs +++ b/esp-hal/src/uart.rs @@ -448,7 +448,7 @@ where impl<'d, T> Uart<'d, T> where - T: Instance, + T: Instance + 'd, { /// Create a new UART instance with defaults pub fn new_with_config

( @@ -915,22 +915,7 @@ where // initialize peripheral by setting clk_enable and clearing uart_reset bits T::enable_peripheral(); - - // don't reset the console UART - this will cause trouble at least on ESP32-S3 - // ideally this should be configurable once we have a solution for https://github.com/esp-rs/esp-hal/issues/1111 - // see https://github.com/espressif/esp-idf/blob/5f4249357372f209fdd57288265741aaba21a2b1/components/esp_driver_uart/src/uart.c#L179 - if T::uart_number() != CONSOLE_UART_NUM { - T::register_block() - .clk_conf() - .modify(|_, w| w.rst_core().set_bit()); - - // reset peripheral - T::reset_peripheral(); - - T::register_block() - .clk_conf() - .modify(|_, w| w.rst_core().clear_bit()); - } + Self::uart_peripheral_reset(); T::disable_rx_interrupts(); T::disable_tx_interrupts(); } @@ -944,11 +929,34 @@ where // initialize peripheral by setting clk_enable and clearing uart_reset bits T::enable_peripheral(); + Self::uart_peripheral_reset(); + T::disable_rx_interrupts(); + T::disable_tx_interrupts(); + } + + #[cfg(any(esp32, esp32s2))] + #[inline(always)] + fn init() { + T::enable_peripheral(); + Self::uart_peripheral_reset(); + T::disable_rx_interrupts(); + T::disable_tx_interrupts(); + } - // don't reset the console UART - this will cause trouble at least on ESP32-S3 - // ideally this should be configurable once we have a solution for https://github.com/esp-rs/esp-hal/issues/1111 + #[inline(always)] + fn uart_peripheral_reset() { + // don't reset the console UART - this will cause trouble (i.e. the UART will + // start to transmit garbage) + // + // We should only reset the console UART if it was absolutely unused before. + // Apparently the bootloader (and maybe the ROM code) writing to the UART is + // already enough to make this a no-go. (i.e. one needs to mute the ROM + // code via efuse / strapping pin AND use a silent bootloader) + // + // Ideally this should be configurable once we have a solution for https://github.com/esp-rs/esp-hal/issues/1111 // see https://github.com/espressif/esp-idf/blob/5f4249357372f209fdd57288265741aaba21a2b1/components/esp_driver_uart/src/uart.c#L179 if T::uart_number() != CONSOLE_UART_NUM { + #[cfg(not(any(esp32, esp32s2)))] T::register_block() .clk_conf() .modify(|_, w| w.rst_core().set_bit()); @@ -956,29 +964,11 @@ where // reset peripheral T::reset_peripheral(); + #[cfg(not(any(esp32, esp32s2)))] T::register_block() .clk_conf() .modify(|_, w| w.rst_core().clear_bit()); } - - T::disable_rx_interrupts(); - T::disable_tx_interrupts(); - } - - #[cfg(any(esp32, esp32s2))] - #[inline(always)] - fn init() { - T::enable_peripheral(); - - // don't reset the console UART - this will cause trouble at least on ESP32-S3 - // ideally this should be configurable once we have a solution for https://github.com/esp-rs/esp-hal/issues/1111 - // see https://github.com/espressif/esp-idf/blob/5f4249357372f209fdd57288265741aaba21a2b1/components/esp_driver_uart/src/uart.c#L179 - if T::uart_number() != CONSOLE_UART_NUM { - T::reset_peripheral(); - } - - T::disable_rx_interrupts(); - T::disable_tx_interrupts(); } #[cfg(any(esp32c3, esp32c6, esp32h2, esp32s3))] // TODO introduce a cfg symbol for this