Skip to content

Commit

Permalink
Introduce a uart_peripheral_reset function
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernQ committed Feb 28, 2024
1 parent 6bd0c9e commit e4b2549
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions esp-hal/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P>(
Expand Down Expand Up @@ -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();
}
Expand All @@ -944,41 +929,46 @@ 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());

// 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
Expand Down

0 comments on commit e4b2549

Please sign in to comment.