diff options
| author | Karun <[email protected]> | 2024-04-19 13:33:10 -0400 |
|---|---|---|
| committer | Karun <[email protected]> | 2024-05-02 17:17:39 -0400 |
| commit | 87d2c66ef4be5396663f6f1724f3a15da31d0482 (patch) | |
| tree | 84115410ed4f7d815a7a6b71bb4541d1019335a1 | |
| parent | 0b606b57f101b4c8081e76de86c7224763deb9e1 (diff) | |
Add io pin masking
| -rw-r--r-- | embassy-stm32/src/tsc/enums.rs | 92 | ||||
| -rw-r--r-- | embassy-stm32/src/tsc/mod.rs | 116 |
2 files changed, 159 insertions, 49 deletions
diff --git a/embassy-stm32/src/tsc/enums.rs b/embassy-stm32/src/tsc/enums.rs index 6dfc8709c..56df4173a 100644 --- a/embassy-stm32/src/tsc/enums.rs +++ b/embassy-stm32/src/tsc/enums.rs | |||
| @@ -1,3 +1,95 @@ | |||
| 1 | use core::ops::BitOr; | ||
| 2 | |||
| 3 | /// Pin defines | ||
| 4 | #[allow(missing_docs)] | ||
| 5 | pub enum TscIOPin { | ||
| 6 | Group1Io1, | ||
| 7 | Group1Io2, | ||
| 8 | Group1Io3, | ||
| 9 | Group1Io4, | ||
| 10 | Group2Io1, | ||
| 11 | Group2Io2, | ||
| 12 | Group2Io3, | ||
| 13 | Group2Io4, | ||
| 14 | Group3Io1, | ||
| 15 | Group3Io2, | ||
| 16 | Group3Io3, | ||
| 17 | Group3Io4, | ||
| 18 | Group4Io1, | ||
| 19 | Group4Io2, | ||
| 20 | Group4Io3, | ||
| 21 | Group4Io4, | ||
| 22 | Group5Io1, | ||
| 23 | Group5Io2, | ||
| 24 | Group5Io3, | ||
| 25 | Group5Io4, | ||
| 26 | Group6Io1, | ||
| 27 | Group6Io2, | ||
| 28 | Group6Io3, | ||
| 29 | Group6Io4, | ||
| 30 | Group7Io1, | ||
| 31 | Group7Io2, | ||
| 32 | Group7Io3, | ||
| 33 | Group7Io4, | ||
| 34 | Group8Io1, | ||
| 35 | Group8Io2, | ||
| 36 | Group8Io3, | ||
| 37 | Group8Io4, | ||
| 38 | } | ||
| 39 | |||
| 40 | impl BitOr<TscIOPin> for u32 { | ||
| 41 | type Output = u32; | ||
| 42 | fn bitor(self, rhs: TscIOPin) -> Self::Output { | ||
| 43 | self | rhs as u32 | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | impl BitOr for TscIOPin { | ||
| 48 | type Output = u32; | ||
| 49 | fn bitor(self, rhs: Self) -> Self::Output { | ||
| 50 | self as u32 | rhs as u32 | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | impl Into<u32> for TscIOPin { | ||
| 55 | fn into(self) -> u32 { | ||
| 56 | match self { | ||
| 57 | TscIOPin::Group1Io1 => 0x00000001, | ||
| 58 | TscIOPin::Group1Io2 => 0x00000002, | ||
| 59 | TscIOPin::Group1Io3 => 0x00000004, | ||
| 60 | TscIOPin::Group1Io4 => 0x00000008, | ||
| 61 | TscIOPin::Group2Io1 => 0x00000010, | ||
| 62 | TscIOPin::Group2Io2 => 0x00000020, | ||
| 63 | TscIOPin::Group2Io3 => 0x00000040, | ||
| 64 | TscIOPin::Group2Io4 => 0x00000080, | ||
| 65 | TscIOPin::Group3Io1 => 0x00000100, | ||
| 66 | TscIOPin::Group3Io2 => 0x00000200, | ||
| 67 | TscIOPin::Group3Io3 => 0x00000400, | ||
| 68 | TscIOPin::Group3Io4 => 0x00000800, | ||
| 69 | TscIOPin::Group4Io1 => 0x00001000, | ||
| 70 | TscIOPin::Group4Io2 => 0x00002000, | ||
| 71 | TscIOPin::Group4Io3 => 0x00004000, | ||
| 72 | TscIOPin::Group4Io4 => 0x00008000, | ||
| 73 | TscIOPin::Group5Io1 => 0x00010000, | ||
| 74 | TscIOPin::Group5Io2 => 0x00020000, | ||
| 75 | TscIOPin::Group5Io3 => 0x00040000, | ||
| 76 | TscIOPin::Group5Io4 => 0x00080000, | ||
| 77 | TscIOPin::Group6Io1 => 0x00100000, | ||
| 78 | TscIOPin::Group6Io2 => 0x00200000, | ||
| 79 | TscIOPin::Group6Io3 => 0x00400000, | ||
| 80 | TscIOPin::Group6Io4 => 0x00800000, | ||
| 81 | TscIOPin::Group7Io1 => 0x01000000, | ||
| 82 | TscIOPin::Group7Io2 => 0x02000000, | ||
| 83 | TscIOPin::Group7Io3 => 0x04000000, | ||
| 84 | TscIOPin::Group7Io4 => 0x08000000, | ||
| 85 | TscIOPin::Group8Io1 => 0x10000000, | ||
| 86 | TscIOPin::Group8Io2 => 0x20000000, | ||
| 87 | TscIOPin::Group8Io3 => 0x40000000, | ||
| 88 | TscIOPin::Group8Io4 => 0x80000000, | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 1 | /// Charge transfer pulse cycles | 93 | /// Charge transfer pulse cycles |
| 2 | #[allow(missing_docs)] | 94 | #[allow(missing_docs)] |
| 3 | #[derive(Copy, Clone)] | 95 | #[derive(Copy, Clone)] |
diff --git a/embassy-stm32/src/tsc/mod.rs b/embassy-stm32/src/tsc/mod.rs index 60dd734c4..91cf7187a 100644 --- a/embassy-stm32/src/tsc/mod.rs +++ b/embassy-stm32/src/tsc/mod.rs | |||
| @@ -6,12 +6,15 @@ | |||
| 6 | pub mod enums; | 6 | pub mod enums; |
| 7 | 7 | ||
| 8 | use crate::gpio::AnyPin; | 8 | use crate::gpio::AnyPin; |
| 9 | use crate::pac::tsc::regs; | ||
| 9 | use crate::{pac::tsc::Tsc as Regs, rcc::RccPeripheral}; | 10 | use crate::{pac::tsc::Tsc as Regs, rcc::RccPeripheral}; |
| 10 | use crate::{peripherals, Peripheral}; | 11 | use crate::{peripherals, Peripheral}; |
| 11 | use embassy_hal_internal::{into_ref, PeripheralRef}; | 12 | use embassy_hal_internal::{into_ref, PeripheralRef}; |
| 12 | 13 | ||
| 13 | pub use enums::*; | 14 | pub use enums::*; |
| 14 | 15 | ||
| 16 | const TSC_NUM_GROUPS: u32 = 8; | ||
| 17 | |||
| 15 | /// Error type defined for TSC | 18 | /// Error type defined for TSC |
| 16 | #[derive(Debug)] | 19 | #[derive(Debug)] |
| 17 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 20 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -110,6 +113,12 @@ pub struct Config { | |||
| 110 | pub acquisition_mode: bool, | 113 | pub acquisition_mode: bool, |
| 111 | /// Enable max count interrupt | 114 | /// Enable max count interrupt |
| 112 | pub max_count_interrupt: bool, | 115 | pub max_count_interrupt: bool, |
| 116 | /// Channel IO mask | ||
| 117 | pub channel_ios: u32, | ||
| 118 | /// Shield IO mask | ||
| 119 | pub shield_ios: u32, | ||
| 120 | /// Sampling IO mask | ||
| 121 | pub sampling_ios: u32, | ||
| 113 | } | 122 | } |
| 114 | 123 | ||
| 115 | impl Default for Config { | 124 | impl Default for Config { |
| @@ -126,6 +135,9 @@ impl Default for Config { | |||
| 126 | synchro_pin_polarity: false, | 135 | synchro_pin_polarity: false, |
| 127 | acquisition_mode: false, | 136 | acquisition_mode: false, |
| 128 | max_count_interrupt: false, | 137 | max_count_interrupt: false, |
| 138 | channel_ios: 0, | ||
| 139 | shield_ios: 0, | ||
| 140 | sampling_ios: 0, | ||
| 129 | } | 141 | } |
| 130 | } | 142 | } |
| 131 | } | 143 | } |
| @@ -175,45 +187,44 @@ impl<'d, T: Instance> Tsc<'d, T> { | |||
| 175 | pub fn new( | 187 | pub fn new( |
| 176 | peri: impl Peripheral<P = T> + 'd, | 188 | peri: impl Peripheral<P = T> + 'd, |
| 177 | // g1_d1: Option<PeriPin<impl Peripheral<P = impl G1IO1Pin<T>> + 'd>>, | 189 | // g1_d1: Option<PeriPin<impl Peripheral<P = impl G1IO1Pin<T>> + 'd>>, |
| 178 | g1_d2: Option<PeriPin<impl Peripheral<P = impl G1IO2Pin<T>> + 'd>>, | 190 | // g1_d2: Option<PeriPin<impl Peripheral<P = impl G1IO2Pin<T>> + 'd>>, |
| 179 | g1_d3: Option<PeriPin<impl Peripheral<P = impl G1IO3Pin<T>> + 'd>>, | 191 | // g1_d3: Option<PeriPin<impl Peripheral<P = impl G1IO3Pin<T>> + 'd>>, |
| 180 | g1_d4: Option<impl Peripheral<P = impl G1IO4Pin<T>> + 'd>, | 192 | // g1_d4: Option<PeriPin<impl Peripheral<P = impl G1IO4Pin<T>> + 'd>>, |
| 181 | 193 | ||
| 182 | g2_d1: Option<impl Peripheral<P = impl G2IO1Pin<T>> + 'd>, | 194 | // g2_d1: Option<impl Peripheral<P = impl G2IO1Pin<T>> + 'd>, |
| 183 | g2_d2: Option<impl Peripheral<P = impl G2IO2Pin<T>> + 'd>, | 195 | // g2_d2: Option<impl Peripheral<P = impl G2IO2Pin<T>> + 'd>, |
| 184 | g2_d3: Option<impl Peripheral<P = impl G2IO3Pin<T>> + 'd>, | 196 | // g2_d3: Option<impl Peripheral<P = impl G2IO3Pin<T>> + 'd>, |
| 185 | g2_d4: Option<impl Peripheral<P = impl G2IO4Pin<T>> + 'd>, | 197 | // g2_d4: Option<impl Peripheral<P = impl G2IO4Pin<T>> + 'd>, |
| 186 | 198 | ||
| 187 | g3_d1: Option<impl Peripheral<P = impl G3IO1Pin<T>> + 'd>, | 199 | // g3_d1: Option<impl Peripheral<P = impl G3IO1Pin<T>> + 'd>, |
| 188 | g3_d2: Option<impl Peripheral<P = impl G3IO2Pin<T>> + 'd>, | 200 | // g3_d2: Option<impl Peripheral<P = impl G3IO2Pin<T>> + 'd>, |
| 189 | g3_d3: Option<impl Peripheral<P = impl G3IO3Pin<T>> + 'd>, | 201 | // g3_d3: Option<impl Peripheral<P = impl G3IO3Pin<T>> + 'd>, |
| 190 | g3_d4: Option<impl Peripheral<P = impl G3IO4Pin<T>> + 'd>, | 202 | // g3_d4: Option<impl Peripheral<P = impl G3IO4Pin<T>> + 'd>, |
| 191 | 203 | ||
| 192 | g4_d1: Option<impl Peripheral<P = impl G4IO1Pin<T>> + 'd>, | 204 | // g4_d1: Option<impl Peripheral<P = impl G4IO1Pin<T>> + 'd>, |
| 193 | g4_d2: Option<impl Peripheral<P = impl G4IO2Pin<T>> + 'd>, | 205 | // g4_d2: Option<impl Peripheral<P = impl G4IO2Pin<T>> + 'd>, |
| 194 | g4_d3: Option<impl Peripheral<P = impl G4IO3Pin<T>> + 'd>, | 206 | // g4_d3: Option<impl Peripheral<P = impl G4IO3Pin<T>> + 'd>, |
| 195 | g4_d4: Option<impl Peripheral<P = impl G4IO4Pin<T>> + 'd>, | 207 | // g4_d4: Option<impl Peripheral<P = impl G4IO4Pin<T>> + 'd>, |
| 196 | 208 | ||
| 197 | g5_d1: Option<impl Peripheral<P = impl G5IO1Pin<T>> + 'd>, | 209 | // g5_d1: Option<impl Peripheral<P = impl G5IO1Pin<T>> + 'd>, |
| 198 | g5_d2: Option<impl Peripheral<P = impl G5IO2Pin<T>> + 'd>, | 210 | // g5_d2: Option<impl Peripheral<P = impl G5IO2Pin<T>> + 'd>, |
| 199 | g5_d3: Option<impl Peripheral<P = impl G5IO3Pin<T>> + 'd>, | 211 | // g5_d3: Option<impl Peripheral<P = impl G5IO3Pin<T>> + 'd>, |
| 200 | g5_d4: Option<impl Peripheral<P = impl G5IO4Pin<T>> + 'd>, | 212 | // g5_d4: Option<impl Peripheral<P = impl G5IO4Pin<T>> + 'd>, |
| 201 | 213 | ||
| 202 | g6_d1: Option<impl Peripheral<P = impl G6IO1Pin<T>> + 'd>, | 214 | // g6_d1: Option<impl Peripheral<P = impl G6IO1Pin<T>> + 'd>, |
| 203 | g6_d2: Option<impl Peripheral<P = impl G6IO2Pin<T>> + 'd>, | 215 | // g6_d2: Option<impl Peripheral<P = impl G6IO2Pin<T>> + 'd>, |
| 204 | g6_d3: Option<impl Peripheral<P = impl G6IO3Pin<T>> + 'd>, | 216 | // g6_d3: Option<impl Peripheral<P = impl G6IO3Pin<T>> + 'd>, |
| 205 | g6_d4: Option<impl Peripheral<P = impl G6IO4Pin<T>> + 'd>, | 217 | // g6_d4: Option<impl Peripheral<P = impl G6IO4Pin<T>> + 'd>, |
| 206 | 218 | ||
| 207 | g7_d1: Option<impl Peripheral<P = impl G7IO1Pin<T>> + 'd>, | 219 | // g7_d1: Option<impl Peripheral<P = impl G7IO1Pin<T>> + 'd>, |
| 208 | g7_d2: Option<impl Peripheral<P = impl G7IO2Pin<T>> + 'd>, | 220 | // g7_d2: Option<impl Peripheral<P = impl G7IO2Pin<T>> + 'd>, |
| 209 | g7_d3: Option<impl Peripheral<P = impl G7IO3Pin<T>> + 'd>, | 221 | // g7_d3: Option<impl Peripheral<P = impl G7IO3Pin<T>> + 'd>, |
| 210 | g7_d4: Option<impl Peripheral<P = impl G7IO4Pin<T>> + 'd>, | 222 | // g7_d4: Option<impl Peripheral<P = impl G7IO4Pin<T>> + 'd>, |
| 211 | 223 | ||
| 212 | g8_d1: Option<impl Peripheral<P = impl G8IO1Pin<T>> + 'd>, | 224 | // g8_d1: Option<impl Peripheral<P = impl G8IO1Pin<T>> + 'd>, |
| 213 | g8_d2: Option<impl Peripheral<P = impl G8IO2Pin<T>> + 'd>, | 225 | // g8_d2: Option<impl Peripheral<P = impl G8IO2Pin<T>> + 'd>, |
| 214 | g8_d3: Option<impl Peripheral<P = impl G8IO3Pin<T>> + 'd>, | 226 | // g8_d3: Option<impl Peripheral<P = impl G8IO3Pin<T>> + 'd>, |
| 215 | g8_d4: Option<impl Peripheral<P = impl G8IO4Pin<T>> + 'd>, | 227 | // g8_d4: Option<impl Peripheral<P = impl G8IO4Pin<T>> + 'd>, |
| 216 | |||
| 217 | config: Config, | 228 | config: Config, |
| 218 | ) -> Self { | 229 | ) -> Self { |
| 219 | into_ref!(peri); | 230 | into_ref!(peri); |
| @@ -224,6 +235,15 @@ impl<'d, T: Instance> Tsc<'d, T> { | |||
| 224 | } | 235 | } |
| 225 | 236 | ||
| 226 | // fn filter_group() -> Option<PinGroup<'d>> {} | 237 | // fn filter_group() -> Option<PinGroup<'d>> {} |
| 238 | fn extract_groups(io_mask: u32) -> u32 { | ||
| 239 | let mut groups: u32 = 0; | ||
| 240 | for idx in 0..TSC_NUM_GROUPS { | ||
| 241 | if io_mask & (0x0F << idx * 4) != 0 { | ||
| 242 | groups |= 1 << idx | ||
| 243 | } | ||
| 244 | } | ||
| 245 | groups | ||
| 246 | } | ||
| 227 | 247 | ||
| 228 | fn new_inner(peri: impl Peripheral<P = T> + 'd, config: Config) -> Self { | 248 | fn new_inner(peri: impl Peripheral<P = T> + 'd, config: Config) -> Self { |
| 229 | into_ref!(peri); | 249 | into_ref!(peri); |
| @@ -245,22 +265,20 @@ impl<'d, T: Instance> Tsc<'d, T> { | |||
| 245 | 265 | ||
| 246 | // Set IO configuration | 266 | // Set IO configuration |
| 247 | // Disable Schmitt trigger hysteresis on all used TSC IOs | 267 | // Disable Schmitt trigger hysteresis on all used TSC IOs |
| 248 | // T::REGS.iohcr().modify(|w| { | 268 | T::REGS |
| 249 | // w. | 269 | .iohcr() |
| 250 | // }); | 270 | .write(|w| w.0 = config.channel_ios | config.shield_ios | config.sampling_ios); |
| 251 | 271 | ||
| 252 | // Set channel and shield IOs | 272 | // Set channel and shield IOs |
| 253 | // T::REGS.ioccr().modify(|w| {}); | 273 | T::REGS.ioccr().write(|w| w.0 = config.channel_ios | config.shield_ios); |
| 254 | 274 | ||
| 255 | // Set sampling IOs | 275 | // Set sampling IOs |
| 256 | // T::REGS.ioscr().modify(|w| { | 276 | T::REGS.ioscr().write(|w| w.0 = config.sampling_ios); |
| 257 | // w.set_g1_io1(val) | ||
| 258 | // }); | ||
| 259 | 277 | ||
| 260 | // Set the groups to be acquired | 278 | // Set the groups to be acquired |
| 261 | // T::REGS.iogcsr().modify(|w| { | 279 | T::REGS |
| 262 | // w.set_g1e(val); | 280 | .iogcsr() |
| 263 | // }); | 281 | .write(|w| w.0 = Self::extract_groups(config.channel_ios)); |
| 264 | 282 | ||
| 265 | // Disable interrupts | 283 | // Disable interrupts |
| 266 | T::REGS.ier().modify(|w| { | 284 | T::REGS.ier().modify(|w| { |
