Skip to content

Commit

Permalink
Update uart.rs, add rs485 half duplex (#456)
Browse files Browse the repository at this point in the history
* Update uart.rs, add rs485 half duplex

* Update uart.rs, fmt
  • Loading branch information
xiaguangbo authored Jul 14, 2024
1 parent a61e24b commit 7d4edb9
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
//! ```
//!
//! # TODO
//! - Add all extra features esp32 supports (eg rs485, etc. etc.)
//! - Add all extra features esp32 supports
//! - Free APB lock when TX is idle (and no RX used)
//! - Address errata 3.17: UART fifo_cnt is inconsistent with FIFO pointer
Expand Down Expand Up @@ -68,6 +68,35 @@ pub mod config {
use enumset::{enum_set, EnumSet, EnumSetType};
use esp_idf_sys::*;

/// Mode
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Mode {
/// regular UART mode
UART,
/// half duplex RS485 UART mode control by RTS pin
RS485HalfDuplex,
}

impl From<Mode> for uart_mode_t {
fn from(mode: Mode) -> Self {
match mode {
Mode::UART => uart_mode_t_UART_MODE_UART,
Mode::RS485HalfDuplex => uart_mode_t_UART_MODE_RS485_HALF_DUPLEX,
}
}
}

impl From<uart_mode_t> for Mode {
#[allow(non_upper_case_globals)]
fn from(uart_mode: uart_mode_t) -> Self {
match uart_mode {
uart_mode_t_UART_MODE_UART => Mode::UART,
uart_mode_t_UART_MODE_RS485_HALF_DUPLEX => Mode::RS485HalfDuplex,
_ => unreachable!(),
}
}
}

/// Number of data bits
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum DataBits {
Expand Down Expand Up @@ -101,7 +130,7 @@ pub mod config {
}
}

/// Number of data bits
/// Flow control
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum FlowControl {
None,
Expand Down Expand Up @@ -450,6 +479,7 @@ pub mod config {
/// UART configuration
#[derive(Debug, Clone)]
pub struct Config {
pub mode: Mode,
pub baudrate: Hertz,
pub data_bits: DataBits,
pub parity: Parity,
Expand Down Expand Up @@ -484,6 +514,7 @@ pub mod config {
impl Config {
pub const fn new() -> Config {
Config {
mode: Mode::UART,
baudrate: Hertz(19_200),
data_bits: DataBits::DataBits8,
parity: Parity::ParityNone,
Expand All @@ -500,6 +531,12 @@ pub mod config {
}
}

#[must_use]
pub fn mode(mut self, mode: Mode) -> Self {
self.mode = mode;
self
}

#[must_use]
pub fn baudrate(mut self, baudrate: Hertz) -> Self {
self.baudrate = baudrate;
Expand Down Expand Up @@ -1914,6 +1951,8 @@ fn new_common<UART: Uart>(
)
})?;

esp!(unsafe { uart_set_mode(UART::port(), config.mode.into()) })?;

// Configure interrupts after installing the driver
// so it won't get overwritten.
let usr_intrs = config.event_config.clone().into();
Expand Down

0 comments on commit 7d4edb9

Please sign in to comment.