diff options
| author | Karun <[email protected]> | 2024-08-06 11:52:16 -0400 |
|---|---|---|
| committer | Karun <[email protected]> | 2024-08-06 11:52:16 -0400 |
| commit | 446169b2c1c85ced844e1a888d7fec75047e11c1 (patch) | |
| tree | 0b794956a63055529ff4b13f5da6e164fbcade1e | |
| parent | a7258927209827e27f4190af57020c2a8017dbaa (diff) | |
Add gpio version dependency
Add configurable output type for half-duplex
| -rw-r--r-- | embassy-stm32/src/usart/mod.rs | 81 |
1 files changed, 77 insertions, 4 deletions
diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index 3283fb6b7..0c5bbf491 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs | |||
| @@ -211,6 +211,19 @@ impl Default for Config { | |||
| 211 | } | 211 | } |
| 212 | } | 212 | } |
| 213 | 213 | ||
| 214 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] | ||
| 215 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 216 | /// Half duplex IO mode | ||
| 217 | pub enum HalfDuplexConfig { | ||
| 218 | /// Push pull allows for faster baudrates, may require series resistor | ||
| 219 | PushPull, | ||
| 220 | /// Open drain output using external pull up resistor | ||
| 221 | OpenDrainExternal, | ||
| 222 | #[cfg(not(gpio_v1))] | ||
| 223 | /// Open drain output using internal pull up resistor | ||
| 224 | OpenDrainInternal, | ||
| 225 | } | ||
| 226 | |||
| 214 | /// Serial error | 227 | /// Serial error |
| 215 | #[derive(Debug, Eq, PartialEq, Copy, Clone)] | 228 | #[derive(Debug, Eq, PartialEq, Copy, Clone)] |
| 216 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 229 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -1042,6 +1055,7 @@ impl<'d> Uart<'d, Async> { | |||
| 1042 | tx_dma: impl Peripheral<P = impl TxDma<T>> + 'd, | 1055 | tx_dma: impl Peripheral<P = impl TxDma<T>> + 'd, |
| 1043 | rx_dma: impl Peripheral<P = impl RxDma<T>> + 'd, | 1056 | rx_dma: impl Peripheral<P = impl RxDma<T>> + 'd, |
| 1044 | mut config: Config, | 1057 | mut config: Config, |
| 1058 | half_duplex: HalfDuplexConfig, | ||
| 1045 | ) -> Result<Self, ConfigError> { | 1059 | ) -> Result<Self, ConfigError> { |
| 1046 | #[cfg(not(any(usart_v1, usart_v2)))] | 1060 | #[cfg(not(any(usart_v1, usart_v2)))] |
| 1047 | { | 1061 | { |
| @@ -1052,7 +1066,21 @@ impl<'d> Uart<'d, Async> { | |||
| 1052 | Self::new_inner( | 1066 | Self::new_inner( |
| 1053 | peri, | 1067 | peri, |
| 1054 | None, | 1068 | None, |
| 1055 | new_pin!(tx, AfType::output_pull(OutputType::OpenDrain, Speed::Medium, Pull::Up)), | 1069 | new_pin!( |
| 1070 | tx, | ||
| 1071 | match half_duplex { | ||
| 1072 | HalfDuplexConfig::PushPull => { | ||
| 1073 | AfType::output(OutputType::PushPull, Speed::Medium) | ||
| 1074 | } | ||
| 1075 | HalfDuplexConfig::OpenDrainExternal => { | ||
| 1076 | AfType::output(OutputType::OpenDrain, Speed::Medium) | ||
| 1077 | } | ||
| 1078 | #[cfg(not(gpio_v1))] | ||
| 1079 | HalfDuplexConfig::OpenDrainInternal => { | ||
| 1080 | AfType::output_pull(OutputType::OpenDrain, Speed::Medium, Pull::Up) | ||
| 1081 | } | ||
| 1082 | } | ||
| 1083 | ), | ||
| 1056 | None, | 1084 | None, |
| 1057 | None, | 1085 | None, |
| 1058 | None, | 1086 | None, |
| @@ -1080,6 +1108,7 @@ impl<'d> Uart<'d, Async> { | |||
| 1080 | tx_dma: impl Peripheral<P = impl TxDma<T>> + 'd, | 1108 | tx_dma: impl Peripheral<P = impl TxDma<T>> + 'd, |
| 1081 | rx_dma: impl Peripheral<P = impl RxDma<T>> + 'd, | 1109 | rx_dma: impl Peripheral<P = impl RxDma<T>> + 'd, |
| 1082 | mut config: Config, | 1110 | mut config: Config, |
| 1111 | half_duplex: HalfDuplexConfig, | ||
| 1083 | ) -> Result<Self, ConfigError> { | 1112 | ) -> Result<Self, ConfigError> { |
| 1084 | config.swap_rx_tx = true; | 1113 | config.swap_rx_tx = true; |
| 1085 | config.half_duplex = true; | 1114 | config.half_duplex = true; |
| @@ -1088,7 +1117,21 @@ impl<'d> Uart<'d, Async> { | |||
| 1088 | peri, | 1117 | peri, |
| 1089 | None, | 1118 | None, |
| 1090 | None, | 1119 | None, |
| 1091 | new_pin!(rx, AfType::output(OutputType::PushPull, Speed::Medium)), | 1120 | new_pin!( |
| 1121 | rx, | ||
| 1122 | match half_duplex { | ||
| 1123 | HalfDuplexConfig::PushPull => { | ||
| 1124 | AfType::output(OutputType::PushPull, Speed::Medium) | ||
| 1125 | } | ||
| 1126 | HalfDuplexConfig::OpenDrainExternal => { | ||
| 1127 | AfType::output(OutputType::OpenDrain, Speed::Medium) | ||
| 1128 | } | ||
| 1129 | #[cfg(not(gpio_v1))] | ||
| 1130 | HalfDuplexConfig::OpenDrainInternal => { | ||
| 1131 | AfType::output_pull(OutputType::OpenDrain, Speed::Medium, Pull::Up) | ||
| 1132 | } | ||
| 1133 | } | ||
| 1134 | ), | ||
| 1092 | None, | 1135 | None, |
| 1093 | None, | 1136 | None, |
| 1094 | new_dma!(tx_dma), | 1137 | new_dma!(tx_dma), |
| @@ -1193,6 +1236,7 @@ impl<'d> Uart<'d, Blocking> { | |||
| 1193 | peri: impl Peripheral<P = T> + 'd, | 1236 | peri: impl Peripheral<P = T> + 'd, |
| 1194 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, | 1237 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, |
| 1195 | mut config: Config, | 1238 | mut config: Config, |
| 1239 | half_duplex: HalfDuplexConfig, | ||
| 1196 | ) -> Result<Self, ConfigError> { | 1240 | ) -> Result<Self, ConfigError> { |
| 1197 | #[cfg(not(any(usart_v1, usart_v2)))] | 1241 | #[cfg(not(any(usart_v1, usart_v2)))] |
| 1198 | { | 1242 | { |
| @@ -1203,7 +1247,21 @@ impl<'d> Uart<'d, Blocking> { | |||
| 1203 | Self::new_inner( | 1247 | Self::new_inner( |
| 1204 | peri, | 1248 | peri, |
| 1205 | None, | 1249 | None, |
| 1206 | new_pin!(tx, AfType::output(OutputType::PushPull, Speed::Medium)), | 1250 | new_pin!( |
| 1251 | tx, | ||
| 1252 | match half_duplex { | ||
| 1253 | HalfDuplexConfig::PushPull => { | ||
| 1254 | AfType::output(OutputType::PushPull, Speed::Medium) | ||
| 1255 | } | ||
| 1256 | HalfDuplexConfig::OpenDrainExternal => { | ||
| 1257 | AfType::output(OutputType::OpenDrain, Speed::Medium) | ||
| 1258 | } | ||
| 1259 | #[cfg(not(gpio_v1))] | ||
| 1260 | HalfDuplexConfig::OpenDrainInternal => { | ||
| 1261 | AfType::output_pull(OutputType::OpenDrain, Speed::Medium, Pull::Up) | ||
| 1262 | } | ||
| 1263 | } | ||
| 1264 | ), | ||
| 1207 | None, | 1265 | None, |
| 1208 | None, | 1266 | None, |
| 1209 | None, | 1267 | None, |
| @@ -1228,6 +1286,7 @@ impl<'d> Uart<'d, Blocking> { | |||
| 1228 | peri: impl Peripheral<P = T> + 'd, | 1286 | peri: impl Peripheral<P = T> + 'd, |
| 1229 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 1287 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, |
| 1230 | mut config: Config, | 1288 | mut config: Config, |
| 1289 | half_duplex: HalfDuplexConfig, | ||
| 1231 | ) -> Result<Self, ConfigError> { | 1290 | ) -> Result<Self, ConfigError> { |
| 1232 | config.swap_rx_tx = true; | 1291 | config.swap_rx_tx = true; |
| 1233 | config.half_duplex = true; | 1292 | config.half_duplex = true; |
| @@ -1236,7 +1295,21 @@ impl<'d> Uart<'d, Blocking> { | |||
| 1236 | peri, | 1295 | peri, |
| 1237 | None, | 1296 | None, |
| 1238 | None, | 1297 | None, |
| 1239 | new_pin!(rx, AfType::output(OutputType::PushPull, Speed::Medium)), | 1298 | new_pin!( |
| 1299 | rx, | ||
| 1300 | match half_duplex { | ||
| 1301 | HalfDuplexConfig::PushPull => { | ||
| 1302 | AfType::output(OutputType::PushPull, Speed::Medium) | ||
| 1303 | } | ||
| 1304 | HalfDuplexConfig::OpenDrainExternal => { | ||
| 1305 | AfType::output(OutputType::OpenDrain, Speed::Medium) | ||
| 1306 | } | ||
| 1307 | #[cfg(not(gpio_v1))] | ||
| 1308 | HalfDuplexConfig::OpenDrainInternal => { | ||
| 1309 | AfType::output_pull(OutputType::OpenDrain, Speed::Medium, Pull::Up) | ||
| 1310 | } | ||
| 1311 | } | ||
| 1312 | ), | ||
| 1240 | None, | 1313 | None, |
| 1241 | None, | 1314 | None, |
| 1242 | None, | 1315 | None, |
