Skip to content

Commit 8e61457

Browse files
committed
STM32: USART: Add de_assertion_time and de_deassertion_time configs
1 parent 224ebef commit 8e61457

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

embassy-stm32/src/usart/mod.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ pub enum ConfigError {
185185
RxOrTxNotEnabled,
186186
/// Data bits and parity combination not supported
187187
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,
188194
}
189195

190196
#[non_exhaustive]
@@ -251,6 +257,14 @@ pub struct Config {
251257
/// Set the pin configuration for the DE pin.
252258
pub de_config: OutputConfig,
253259

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+
254268
// private: set by new_half_duplex, not by the user.
255269
duplex: Duplex,
256270
}
@@ -296,6 +310,8 @@ impl Default for Config {
296310
tx_config: OutputConfig::PushPull,
297311
rts_config: OutputConfig::PushPull,
298312
de_config: OutputConfig::PushPull,
313+
de_assertion_time: 0,
314+
de_deassertion_time: 0,
299315
duplex: Duplex::Full,
300316
}
301317
}
@@ -1706,6 +1722,16 @@ fn configure(
17061722
return Err(ConfigError::RxOrTxNotEnabled);
17071723
}
17081724

1725+
#[cfg(not(any(usart_v1, usart_v2)))]
1726+
let dem = r.cr3().read().dem();
1727+
1728+
#[cfg(not(any(usart_v1, usart_v2)))]
1729+
if config.de_assertion_time > 31 {
1730+
return Err(ConfigError::DeAssertionTimeTooHigh);
1731+
} else if config.de_deassertion_time > 31 {
1732+
return Err(ConfigError::DeDeassertionTimeTooHigh);
1733+
}
1734+
17091735
// UART must be disabled during configuration.
17101736
r.cr1().modify(|w| {
17111737
w.set_ue(false);
@@ -1754,6 +1780,20 @@ fn configure(
17541780
w.set_re(enable_rx);
17551781
}
17561782

1783+
#[cfg(not(any(usart_v1, usart_v2)))]
1784+
if dem {
1785+
w.set_deat(if over8 {
1786+
config.de_assertion_time / 2
1787+
} else {
1788+
config.de_assertion_time
1789+
});
1790+
w.set_dedt(if over8 {
1791+
config.de_assertion_time / 2
1792+
} else {
1793+
config.de_assertion_time
1794+
});
1795+
}
1796+
17571797
// configure word size and parity, since the parity bit is inserted into the MSB position,
17581798
// it increases the effective word size
17591799
match (config.parity, config.data_bits) {

0 commit comments

Comments
 (0)