diff options
| author | Jacob Davis-Hansson <[email protected]> | 2023-04-14 21:05:03 +0200 |
|---|---|---|
| committer | Jacob Davis-Hansson <[email protected]> | 2023-04-14 21:08:24 +0200 |
| commit | b9fc2a6b33d8af88d95d750965f3fb8b1e9032d4 (patch) | |
| tree | 4d832c5f4e4383a7f43f91cf4ebe4888a303c27a | |
| parent | a3ecf5caf614e17b2e1cb3d1f79d8313f2e71a63 (diff) | |
Add ability to invert UART pins
This is useful in some cases where the surrounding circuit
for some reason inverts the UART signal, for instance if you're talking
to a device via an optocoupler.
| -rw-r--r-- | embassy-rp/src/uart/mod.rs | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/embassy-rp/src/uart/mod.rs b/embassy-rp/src/uart/mod.rs index a945f2295..dfa56a842 100644 --- a/embassy-rp/src/uart/mod.rs +++ b/embassy-rp/src/uart/mod.rs | |||
| @@ -6,6 +6,7 @@ use crate::dma::{AnyChannel, Channel}; | |||
| 6 | use crate::gpio::sealed::Pin; | 6 | use crate::gpio::sealed::Pin; |
| 7 | use crate::gpio::AnyPin; | 7 | use crate::gpio::AnyPin; |
| 8 | use crate::{pac, peripherals, Peripheral}; | 8 | use crate::{pac, peripherals, Peripheral}; |
| 9 | use crate::pac::io::vals::Inover; | ||
| 9 | 10 | ||
| 10 | #[cfg(feature = "nightly")] | 11 | #[cfg(feature = "nightly")] |
| 11 | mod buffered; | 12 | mod buffered; |
| @@ -53,6 +54,14 @@ pub struct Config { | |||
| 53 | pub data_bits: DataBits, | 54 | pub data_bits: DataBits, |
| 54 | pub stop_bits: StopBits, | 55 | pub stop_bits: StopBits, |
| 55 | pub parity: Parity, | 56 | pub parity: Parity, |
| 57 | /// Invert the tx pin output | ||
| 58 | pub invert_tx: bool, | ||
| 59 | /// Invert the rx pin input | ||
| 60 | pub invert_rx: bool, | ||
| 61 | // Invert the rts pin | ||
| 62 | pub invert_rts: bool, | ||
| 63 | // Invert the cts pin | ||
| 64 | pub invert_cts: bool, | ||
| 56 | } | 65 | } |
| 57 | 66 | ||
| 58 | impl Default for Config { | 67 | impl Default for Config { |
| @@ -62,6 +71,10 @@ impl Default for Config { | |||
| 62 | data_bits: DataBits::DataBits8, | 71 | data_bits: DataBits::DataBits8, |
| 63 | stop_bits: StopBits::STOP1, | 72 | stop_bits: StopBits::STOP1, |
| 64 | parity: Parity::ParityNone, | 73 | parity: Parity::ParityNone, |
| 74 | invert_rx: false, | ||
| 75 | invert_tx: false, | ||
| 76 | invert_rts: false, | ||
| 77 | invert_cts: false, | ||
| 65 | } | 78 | } |
| 66 | } | 79 | } |
| 67 | } | 80 | } |
| @@ -381,19 +394,31 @@ impl<'d, T: Instance + 'd, M: Mode> Uart<'d, T, M> { | |||
| 381 | let r = T::regs(); | 394 | let r = T::regs(); |
| 382 | unsafe { | 395 | unsafe { |
| 383 | if let Some(pin) = &tx { | 396 | if let Some(pin) = &tx { |
| 384 | pin.io().ctrl().write(|w| w.set_funcsel(2)); | 397 | pin.io().ctrl().write(|w| { |
| 398 | w.set_funcsel(2); | ||
| 399 | w.set_inover(if config.invert_tx { Inover::INVERT } else { Inover::NORMAL }); | ||
| 400 | }); | ||
| 385 | pin.pad_ctrl().write(|w| w.set_ie(true)); | 401 | pin.pad_ctrl().write(|w| w.set_ie(true)); |
| 386 | } | 402 | } |
| 387 | if let Some(pin) = &rx { | 403 | if let Some(pin) = &rx { |
| 388 | pin.io().ctrl().write(|w| w.set_funcsel(2)); | 404 | pin.io().ctrl().write(|w| { |
| 405 | w.set_funcsel(2); | ||
| 406 | w.set_inover(if config.invert_rx { Inover::INVERT } else { Inover::NORMAL }); | ||
| 407 | }); | ||
| 389 | pin.pad_ctrl().write(|w| w.set_ie(true)); | 408 | pin.pad_ctrl().write(|w| w.set_ie(true)); |
| 390 | } | 409 | } |
| 391 | if let Some(pin) = &cts { | 410 | if let Some(pin) = &cts { |
| 392 | pin.io().ctrl().write(|w| w.set_funcsel(2)); | 411 | pin.io().ctrl().write(|w| { |
| 412 | w.set_funcsel(2); | ||
| 413 | w.set_inover(if config.invert_cts { Inover::INVERT } else { Inover::NORMAL }); | ||
| 414 | }); | ||
| 393 | pin.pad_ctrl().write(|w| w.set_ie(true)); | 415 | pin.pad_ctrl().write(|w| w.set_ie(true)); |
| 394 | } | 416 | } |
| 395 | if let Some(pin) = &rts { | 417 | if let Some(pin) = &rts { |
| 396 | pin.io().ctrl().write(|w| w.set_funcsel(2)); | 418 | pin.io().ctrl().write(|w| { |
| 419 | w.set_funcsel(2); | ||
| 420 | w.set_inover(if config.invert_rts { Inover::INVERT } else { Inover::NORMAL }); | ||
| 421 | }); | ||
| 397 | pin.pad_ctrl().write(|w| w.set_ie(true)); | 422 | pin.pad_ctrl().write(|w| w.set_ie(true)); |
| 398 | } | 423 | } |
| 399 | 424 | ||
