Skip to content

Commit

Permalink
Added a few more bits to show the overall idea
Browse files Browse the repository at this point in the history
  • Loading branch information
rtvd committed Oct 26, 2023
1 parent c160ab5 commit 6eac0a2
Showing 1 changed file with 106 additions and 21 deletions.
127 changes: 106 additions & 21 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,37 @@ impl PinRts<USART6> for gpio::PG12<Alternate<8>> {}
impl PinRts<UART7> for gpio::PE9<Alternate<8>> {}
impl PinRts<UART7> for gpio::PF8<Alternate<8>> {}

pub enum IrDAPower {
Normal,
Low,
}

pub enum Rs485Polarity {
High,
Low,
}

enum AsyncFlowControl {
Rs232None,
Rs232CtsRts,
Rs232Cts,
Rs232Rts,
// TODO add Rs485
Rs485(Rs485Polarity),
}

enum SerialMode {
Async(AsyncFlowControl),
// TODO add SingleWire, IrDA, ModbusCommunication, LIN, SnartCard, etc.
// These options are for any UART, including USART
Asynchronous(AsyncFlowControl),
SingleWire,

Check warning on line 163 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f722, stable)

multiple variants are never constructed

Check warning on line 163 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f777, stable)

multiple variants are never constructed

Check warning on line 163 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f756, stable)

multiple variants are never constructed

Check warning on line 163 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (nightly, stm32f746, true)

multiple variants are never constructed

Check warning on line 163 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f732, stable)

multiple variants are never constructed

Check warning on line 163 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f733, stable)

multiple variants are never constructed

Check warning on line 163 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f769, stable)

multiple variants are never constructed

Check warning on line 163 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f778, stable)

multiple variants are never constructed

Check warning on line 163 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f767, stable)

multiple variants are never constructed

Check warning on line 163 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f723, stable)

multiple variants are never constructed
MultiprocessorCommunication,
IrDA(IrDAPower),
ModbusCommunication,
LIN,

// The following options are for USART only
Synchronous,
SmartCard,
SmartCardWithCardLock,
}

/// Serial abstraction
Expand All @@ -163,7 +182,8 @@ pub trait UART<U: Instance, PINS: Pins<U>> {
fn new_async_no_flwctl<>(
uart: U, pins: PINS, clocks: &Clocks, config: Config,
) -> Serial<U, PINS> {
Serial::new(uart, pins, clocks, config, SerialMode::Async(AsyncFlowControl::Rs232None))
Serial::new(uart, pins, clocks, config,
SerialMode::Asynchronous(AsyncFlowControl::Rs232None))
}

fn new_async_rs232_cts_rts<CTS: PinCts<U>, RTS: PinRts<U>>(
Expand All @@ -172,19 +192,36 @@ pub trait UART<U: Instance, PINS: Pins<U>> {
// TODO Clarify if we can borrow cts and rts and keep them borrowed.
// TODO Note that at the moment any CTS and RTS pin of this U(S)ART would be accepted
// This may be too flexible.
Serial::new(uart, pins, clocks, config, SerialMode::Async(AsyncFlowControl::Rs232CtsRts))
Serial::new(uart, pins, clocks, config,
SerialMode::Asynchronous(AsyncFlowControl::Rs232CtsRts))
}

fn new_async_rs232_cts<CTS: PinCts<U>>(
uart: U, pins: PINS, clocks: &Clocks, config: Config, cts: CTS,

Check warning on line 200 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f722, stable)

unused variable: `cts`

Check warning on line 200 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f777, stable)

unused variable: `cts`

Check warning on line 200 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f756, stable)

unused variable: `cts`

Check warning on line 200 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (nightly, stm32f746, true)

unused variable: `cts`

Check warning on line 200 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f732, stable)

unused variable: `cts`

Check warning on line 200 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f733, stable)

unused variable: `cts`

Check warning on line 200 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f769, stable)

unused variable: `cts`

Check warning on line 200 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f778, stable)

unused variable: `cts`

Check warning on line 200 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f765, stable)

unused variable: `cts`

Check warning on line 200 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f767, stable)

unused variable: `cts`

Check warning on line 200 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f723, stable)

unused variable: `cts`
) -> Serial<U, PINS> {
Serial::new(uart, pins, clocks, config, SerialMode::Async(AsyncFlowControl::Rs232Cts))
Serial::new(uart, pins, clocks, config,
SerialMode::Asynchronous(AsyncFlowControl::Rs232Cts))
}

fn new_async_rs232_rts<RTS: PinRts<U>>(
uart: U, pins: PINS, clocks: &Clocks, config: Config, rts: RTS,

Check warning on line 207 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f722, stable)

unused variable: `rts`

Check warning on line 207 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f777, stable)

unused variable: `rts`

Check warning on line 207 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f756, stable)

unused variable: `rts`

Check warning on line 207 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (nightly, stm32f746, true)

unused variable: `rts`

Check warning on line 207 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f732, stable)

unused variable: `rts`

Check warning on line 207 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f733, stable)

unused variable: `rts`

Check warning on line 207 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f769, stable)

unused variable: `rts`

Check warning on line 207 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f778, stable)

unused variable: `rts`

Check warning on line 207 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f765, stable)

unused variable: `rts`

Check warning on line 207 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f767, stable)

unused variable: `rts`

Check warning on line 207 in src/serial.rs

View workflow job for this annotation

GitHub Actions / ci (stm32f723, stable)

unused variable: `rts`
) -> Serial<U, PINS> {
Serial::new(uart, pins, clocks, config, SerialMode::Async(AsyncFlowControl::Rs232Rts))
Serial::new(uart, pins, clocks, config,
SerialMode::Asynchronous(AsyncFlowControl::Rs232Rts))
}

fn new_async_rs458<RTS: PinRts<U>>(
uart: U, pins: PINS, clocks: &Clocks, config: Config, polarity: Rs485Polarity,
) -> Serial<U, PINS> {
Serial::new(uart, pins, clocks, config,
SerialMode::Asynchronous(AsyncFlowControl::Rs485(polarity)))
}

fn new_irda<RTS: PinRts<U>>(
uart: U, pins: PINS, clocks: &Clocks, config: Config, irda_power: IrDAPower,
) -> Serial<U, PINS> {
Serial::new(uart, pins, clocks, config,
SerialMode::IrDA(irda_power))
}

// TODO Add constructors for other modes of operation
Expand Down Expand Up @@ -280,20 +317,68 @@ where
usart.cr3.write(|w| w.dmat().enabled().dmar().enabled());

match mode {
SerialMode::Async(flow_control) => match flow_control {
AsyncFlowControl::Rs232None => {
usart.cr3.write(|w| w.ctse().disabled().rtse().disabled());
},
AsyncFlowControl::Rs232CtsRts => {
usart.cr3.write(|w| w.ctse().enabled().rtse().enabled());
},
AsyncFlowControl::Rs232Cts => {
usart.cr3.write(|w| w.ctse().enabled().rtse().disabled());
},
AsyncFlowControl::Rs232Rts => {
usart.cr3.write(|w| w.ctse().disabled().rtse().enabled());
},
}
SerialMode::Asynchronous(flow_control) => {
usart.cr2.write(|w| w.linen().clear_bit().clken().clear_bit());
usart.cr3.write(|w| w.scen().clear_bit().iren().clear_bit().hdsel().clear_bit());
match flow_control {
AsyncFlowControl::Rs232None => {
usart.cr3.write(|w| w.ctse().disabled().rtse().disabled());
},
AsyncFlowControl::Rs232CtsRts => {
usart.cr3.write(|w| w.ctse().enabled().rtse().enabled());
},
AsyncFlowControl::Rs232Cts => {
usart.cr3.write(|w| w.ctse().enabled().rtse().disabled());
},
AsyncFlowControl::Rs232Rts => {
usart.cr3.write(|w| w.ctse().disabled().rtse().enabled());
},
AsyncFlowControl::Rs485(polarity) => {
usart.cr3.write(|w| w.dem().enabled()); // drive-enabled mode ON
usart.cr3.write(|w| w.dep().bit(match polarity {
Rs485Polarity::High => false,
Rs485Polarity::Low => true,
}));
usart.cr1.write(|w| w.deat().bits(0)); // setting the assertion time
usart.cr1.write(|w| w.dedt().bits(0)); // setting the de-assertion time
// TODO ^ make the times be configurable?
},
}
},
SerialMode::SingleWire => {
usart.cr2.write(|w| w.linen().clear_bit().clken().clear_bit());
usart.cr3.write(|w| w.scen().clear_bit().iren().clear_bit());
usart.cr3.write(|w| w.hdsel().set_bit());
},
SerialMode::MultiprocessorCommunication => {
// TODO implement this
},
SerialMode::IrDA(power) => {
usart.cr3.write(|w| w.irlp().bit(match power {
IrDAPower::Normal => false,
IrDAPower::Low => true,
}));
usart.gtpr.write(|w| w.gt().bits(1)); // setting the prescaler

usart.cr2.write(|w| w.linen().clear_bit().clken().clear_bit().stop().bits(0));
usart.cr3.write(|w| w.scen().clear_bit().hdsel().clear_bit());
usart.cr3.write(|w| w.iren().set_bit());
},
SerialMode::ModbusCommunication => {
// TODO implement this
},
SerialMode::LIN => {
// TODO implement this
},
SerialMode::Synchronous => {
// TODO implement this
},
SerialMode::SmartCard => {
// TODO implement this
},
SerialMode::SmartCardWithCardLock => {
// TODO implement this
},
}

Serial { usart, pins }
Expand Down

0 comments on commit 6eac0a2

Please sign in to comment.