diff options
| author | Adam Greig <[email protected]> | 2025-09-18 01:50:48 +0100 |
|---|---|---|
| committer | Adam Greig <[email protected]> | 2025-10-01 21:11:08 +0100 |
| commit | c87051eecb50b80114c69cedc71ed9e3fcd7bb45 (patch) | |
| tree | e492c513cccc47fb7f5d8c5034111141ee225e70 /embassy-stm32/src/usart | |
| parent | 2e9f3a815d440f33126d47cdcbf3bf1c9eab0ee1 (diff) | |
STM32: USART: Add de_assertion_time and de_deassertion_time configs
Diffstat (limited to 'embassy-stm32/src/usart')
| -rw-r--r-- | embassy-stm32/src/usart/mod.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index e439f2cee..c8012a9b4 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs | |||
| @@ -185,6 +185,12 @@ pub enum ConfigError { | |||
| 185 | RxOrTxNotEnabled, | 185 | RxOrTxNotEnabled, |
| 186 | /// Data bits and parity combination not supported | 186 | /// Data bits and parity combination not supported |
| 187 | DataParityNotSupported, | 187 | DataParityNotSupported, |
| 188 | /// DE assertion time too high | ||
| 189 | #[cfg(not(any(usart_v1, usart_v2)))] | ||
| 190 | DeAssertionTimeTooHigh, | ||
| 191 | /// DE deassertion time too high | ||
| 192 | #[cfg(not(any(usart_v1, usart_v2)))] | ||
| 193 | DeDeassertionTimeTooHigh, | ||
| 188 | } | 194 | } |
| 189 | 195 | ||
| 190 | #[non_exhaustive] | 196 | #[non_exhaustive] |
| @@ -251,6 +257,14 @@ pub struct Config { | |||
| 251 | /// Set the pin configuration for the DE pin. | 257 | /// Set the pin configuration for the DE pin. |
| 252 | pub de_config: OutputConfig, | 258 | pub de_config: OutputConfig, |
| 253 | 259 | ||
| 260 | /// Set DE assertion time before the first start bit, 0-31 16ths of a bit period. | ||
| 261 | #[cfg(not(any(usart_v1, usart_v2)))] | ||
| 262 | pub de_assertion_time: u8, | ||
| 263 | |||
| 264 | /// Set DE deassertion time after the last stop bit, 0-31 16ths of a bit period. | ||
| 265 | #[cfg(not(any(usart_v1, usart_v2)))] | ||
| 266 | pub de_deassertion_time: u8, | ||
| 267 | |||
| 254 | // private: set by new_half_duplex, not by the user. | 268 | // private: set by new_half_duplex, not by the user. |
| 255 | duplex: Duplex, | 269 | duplex: Duplex, |
| 256 | } | 270 | } |
| @@ -296,6 +310,10 @@ impl Default for Config { | |||
| 296 | tx_config: OutputConfig::PushPull, | 310 | tx_config: OutputConfig::PushPull, |
| 297 | rts_config: OutputConfig::PushPull, | 311 | rts_config: OutputConfig::PushPull, |
| 298 | de_config: OutputConfig::PushPull, | 312 | de_config: OutputConfig::PushPull, |
| 313 | #[cfg(not(any(usart_v1, usart_v2)))] | ||
| 314 | de_assertion_time: 0, | ||
| 315 | #[cfg(not(any(usart_v1, usart_v2)))] | ||
| 316 | de_deassertion_time: 0, | ||
| 299 | duplex: Duplex::Full, | 317 | duplex: Duplex::Full, |
| 300 | } | 318 | } |
| 301 | } | 319 | } |
| @@ -1706,6 +1724,16 @@ fn configure( | |||
| 1706 | return Err(ConfigError::RxOrTxNotEnabled); | 1724 | return Err(ConfigError::RxOrTxNotEnabled); |
| 1707 | } | 1725 | } |
| 1708 | 1726 | ||
| 1727 | #[cfg(not(any(usart_v1, usart_v2)))] | ||
| 1728 | let dem = r.cr3().read().dem(); | ||
| 1729 | |||
| 1730 | #[cfg(not(any(usart_v1, usart_v2)))] | ||
| 1731 | if config.de_assertion_time > 31 { | ||
| 1732 | return Err(ConfigError::DeAssertionTimeTooHigh); | ||
| 1733 | } else if config.de_deassertion_time > 31 { | ||
| 1734 | return Err(ConfigError::DeDeassertionTimeTooHigh); | ||
| 1735 | } | ||
| 1736 | |||
| 1709 | // UART must be disabled during configuration. | 1737 | // UART must be disabled during configuration. |
| 1710 | r.cr1().modify(|w| { | 1738 | r.cr1().modify(|w| { |
| 1711 | w.set_ue(false); | 1739 | w.set_ue(false); |
| @@ -1754,6 +1782,20 @@ fn configure( | |||
| 1754 | w.set_re(enable_rx); | 1782 | w.set_re(enable_rx); |
| 1755 | } | 1783 | } |
| 1756 | 1784 | ||
| 1785 | #[cfg(not(any(usart_v1, usart_v2)))] | ||
| 1786 | if dem { | ||
| 1787 | w.set_deat(if over8 { | ||
| 1788 | config.de_assertion_time / 2 | ||
| 1789 | } else { | ||
| 1790 | config.de_assertion_time | ||
| 1791 | }); | ||
| 1792 | w.set_dedt(if over8 { | ||
| 1793 | config.de_assertion_time / 2 | ||
| 1794 | } else { | ||
| 1795 | config.de_assertion_time | ||
| 1796 | }); | ||
| 1797 | } | ||
| 1798 | |||
| 1757 | // configure word size and parity, since the parity bit is inserted into the MSB position, | 1799 | // configure word size and parity, since the parity bit is inserted into the MSB position, |
| 1758 | // it increases the effective word size | 1800 | // it increases the effective word size |
| 1759 | match (config.parity, config.data_bits) { | 1801 | match (config.parity, config.data_bits) { |
