Skip to content

Commit f1e2924

Browse files
committed
Add TX and RX logic invert options to USART config
1 parent c38187d commit f1e2924

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/serial/config.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ pub struct LowPowerConfig {
6161
pub(crate) parity: Parity,
6262
pub(crate) stopbits: StopBits,
6363
pub(crate) swap: bool,
64+
pub(crate) tx_invert: bool,
65+
pub(crate) rx_invert: bool,
6466
pub(crate) fifo_enable: bool,
6567
pub(crate) tx_fifo_threshold: FifoThreshold,
6668
pub(crate) rx_fifo_threshold: FifoThreshold,
@@ -75,6 +77,8 @@ pub struct FullConfig {
7577
pub(crate) parity: Parity,
7678
pub(crate) stopbits: StopBits,
7779
pub(crate) swap: bool,
80+
pub(crate) tx_invert: bool,
81+
pub(crate) rx_invert: bool,
7882
pub(crate) fifo_enable: bool,
7983
pub(crate) tx_fifo_threshold: FifoThreshold,
8084
pub(crate) rx_fifo_threshold: FifoThreshold,
@@ -128,6 +132,24 @@ impl LowPowerConfig {
128132
self
129133
}
130134

135+
/// Invert the polarity of the Tx pin
136+
///
137+
/// The peripheral will treat VDD as a "low" (mark) line value and Gnd as a "high" (idle)
138+
/// value for the Tx pin.
139+
pub fn tx_invert(mut self) -> Self {
140+
self.tx_invert = true;
141+
self
142+
}
143+
144+
/// Invert the polarity of the Rx pin
145+
///
146+
/// The peripheral will treat VDD as a "low" (mark) line value and Gnd as a "high" (idle)
147+
/// value for the Rx pin.
148+
pub fn rx_invert(mut self) -> Self {
149+
self.rx_invert = true;
150+
self
151+
}
152+
131153
pub fn fifo_enable(mut self) -> Self {
132154
self.fifo_enable = true;
133155
self
@@ -198,6 +220,24 @@ impl FullConfig {
198220
self
199221
}
200222

223+
/// Invert the polarity of the Tx pin
224+
///
225+
/// The peripheral will treat VDD as a "low" (mark) line value and Gnd as a "high" (idle)
226+
/// value for the Tx pin.
227+
pub fn tx_invert(mut self) -> Self {
228+
self.tx_invert = true;
229+
self
230+
}
231+
232+
/// Invert the polarity of the Rx pin
233+
///
234+
/// The peripheral will treat VDD as a "low" (mark) line value and Gnd as a "high" (idle)
235+
/// value for the Rx pin.
236+
pub fn rx_invert(mut self) -> Self {
237+
self.rx_invert = true;
238+
self
239+
}
240+
201241
pub fn fifo_enable(mut self) -> Self {
202242
self.fifo_enable = true;
203243
self
@@ -243,6 +283,8 @@ impl Default for LowPowerConfig {
243283
parity: Parity::ParityNone,
244284
stopbits: StopBits::STOP1,
245285
swap: false,
286+
tx_invert: false,
287+
rx_invert: false,
246288
fifo_enable: false,
247289
tx_fifo_threshold: FifoThreshold::FIFO_8_BYTES,
248290
rx_fifo_threshold: FifoThreshold::FIFO_8_BYTES,
@@ -261,6 +303,8 @@ impl Default for FullConfig {
261303
parity: Parity::ParityNone,
262304
stopbits: StopBits::STOP1,
263305
swap: false,
306+
tx_invert: false,
307+
rx_invert: false,
264308
fifo_enable: false,
265309
tx_fifo_threshold: FifoThreshold::FIFO_8_BYTES,
266310
rx_fifo_threshold: FifoThreshold::FIFO_8_BYTES,

src/serial/usart.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,10 @@ macro_rules! uart_lp {
581581
.bits(config.stopbits.bits())
582582
.swap()
583583
.bit(config.swap)
584+
.txinv()
585+
.bit(config.tx_invert)
586+
.rxinv()
587+
.bit(config.rx_invert)
584588
});
585589

586590
usart.cr3().write(|w| unsafe {
@@ -726,6 +730,10 @@ macro_rules! uart_full {
726730
.bits(config.stopbits.bits())
727731
.swap()
728732
.bit(config.swap)
733+
.txinv()
734+
.bit(config.tx_invert)
735+
.rxinv()
736+
.bit(config.rx_invert)
729737
});
730738

731739
if let Some(timeout) = config.receiver_timeout {

0 commit comments

Comments
 (0)