| 1 | use core::marker::PhantomData; |
| 2 | use core::ops::{BitAnd, BitOr, BitOrAssign}; |
| 3 | |
| 4 | use super::pin_roles; |
| 5 | use super::types::Group; |
| 6 | |
| 7 | /// Pin defines |
| 8 | #[allow (missing_docs)] |
| 9 | #[cfg_attr (feature = "defmt" , derive(defmt::Format))] |
| 10 | #[derive (PartialEq, Clone, Copy, Debug)] |
| 11 | pub enum IOPin { |
| 12 | Group1Io1, |
| 13 | Group1Io2, |
| 14 | Group1Io3, |
| 15 | Group1Io4, |
| 16 | Group2Io1, |
| 17 | Group2Io2, |
| 18 | Group2Io3, |
| 19 | Group2Io4, |
| 20 | Group3Io1, |
| 21 | Group3Io2, |
| 22 | Group3Io3, |
| 23 | Group3Io4, |
| 24 | Group4Io1, |
| 25 | Group4Io2, |
| 26 | Group4Io3, |
| 27 | Group4Io4, |
| 28 | Group5Io1, |
| 29 | Group5Io2, |
| 30 | Group5Io3, |
| 31 | Group5Io4, |
| 32 | Group6Io1, |
| 33 | Group6Io2, |
| 34 | Group6Io3, |
| 35 | Group6Io4, |
| 36 | #[cfg (any(tsc_v2, tsc_v3))] |
| 37 | Group7Io1, |
| 38 | #[cfg (any(tsc_v2, tsc_v3))] |
| 39 | Group7Io2, |
| 40 | #[cfg (any(tsc_v2, tsc_v3))] |
| 41 | Group7Io3, |
| 42 | #[cfg (any(tsc_v2, tsc_v3))] |
| 43 | Group7Io4, |
| 44 | #[cfg (tsc_v3)] |
| 45 | Group8Io1, |
| 46 | #[cfg (tsc_v3)] |
| 47 | Group8Io2, |
| 48 | #[cfg (tsc_v3)] |
| 49 | Group8Io3, |
| 50 | #[cfg (tsc_v3)] |
| 51 | Group8Io4, |
| 52 | } |
| 53 | |
| 54 | /// Represents a TSC I/O pin with associated group and role information. |
| 55 | /// |
| 56 | /// This type combines an `tsc::IOPin` with phantom type parameters to statically |
| 57 | /// encode the pin's group and role. This allows for type-safe operations |
| 58 | /// on TSC pins within their specific contexts. |
| 59 | /// |
| 60 | /// - `Group`: A type parameter representing the TSC group (e.g., `G1`, `G2`). |
| 61 | /// - `Role`: A type parameter representing the pin's role (e.g., `Channel`, `Sample`). |
| 62 | #[derive (Clone, Copy, Debug)] |
| 63 | pub struct IOPinWithRole<Group, Role: pin_roles::Role> { |
| 64 | /// The underlying TSC I/O pin. |
| 65 | pub pin: IOPin, |
| 66 | pub(super) phantom: PhantomData<(Group, Role)>, |
| 67 | } |
| 68 | |
| 69 | impl<G, R: pin_roles::Role> IOPinWithRole<G, R> { |
| 70 | pub(super) fn get_pin(wrapped_pin: IOPinWithRole<G, R>) -> IOPin { |
| 71 | wrapped_pin.pin |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl IOPin { |
| 76 | /// Maps this IOPin to the Group it belongs to. |
| 77 | /// |
| 78 | /// This method provides a convenient way to determine which Group |
| 79 | /// a specific TSC I/O pin is associated with. |
| 80 | pub const fn group(&self) -> Group { |
| 81 | match self { |
| 82 | IOPin::Group1Io1 | IOPin::Group1Io2 | IOPin::Group1Io3 | IOPin::Group1Io4 => Group::One, |
| 83 | IOPin::Group2Io1 | IOPin::Group2Io2 | IOPin::Group2Io3 | IOPin::Group2Io4 => Group::Two, |
| 84 | IOPin::Group3Io1 | IOPin::Group3Io2 | IOPin::Group3Io3 | IOPin::Group3Io4 => Group::Three, |
| 85 | IOPin::Group4Io1 | IOPin::Group4Io2 | IOPin::Group4Io3 | IOPin::Group4Io4 => Group::Four, |
| 86 | IOPin::Group5Io1 | IOPin::Group5Io2 | IOPin::Group5Io3 | IOPin::Group5Io4 => Group::Five, |
| 87 | IOPin::Group6Io1 | IOPin::Group6Io2 | IOPin::Group6Io3 | IOPin::Group6Io4 => Group::Six, |
| 88 | #[cfg (any(tsc_v2, tsc_v3))] |
| 89 | IOPin::Group7Io1 | IOPin::Group7Io2 | IOPin::Group7Io3 | IOPin::Group7Io4 => Group::Seven, |
| 90 | #[cfg (tsc_v3)] |
| 91 | IOPin::Group8Io1 | IOPin::Group8Io2 | IOPin::Group8Io3 | IOPin::Group8Io4 => Group::Eight, |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /// Returns the `Group` associated with the given `IOPin`. |
| 96 | pub fn get_group(pin: IOPin) -> Group { |
| 97 | pin.group() |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | impl BitOr<IOPin> for u32 { |
| 102 | type Output = u32; |
| 103 | fn bitor(self, rhs: IOPin) -> Self::Output { |
| 104 | let rhs: u32 = rhs.into(); |
| 105 | self | rhs |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | impl BitOr<u32> for IOPin { |
| 110 | type Output = u32; |
| 111 | fn bitor(self, rhs: u32) -> Self::Output { |
| 112 | let val: u32 = self.into(); |
| 113 | val | rhs |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | impl BitOr for IOPin { |
| 118 | type Output = u32; |
| 119 | fn bitor(self, rhs: Self) -> Self::Output { |
| 120 | let val: u32 = self.into(); |
| 121 | let rhs: u32 = rhs.into(); |
| 122 | val | rhs |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | impl BitOrAssign<IOPin> for u32 { |
| 127 | fn bitor_assign(&mut self, rhs: IOPin) { |
| 128 | let rhs: u32 = rhs.into(); |
| 129 | *self |= rhs; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | impl BitAnd<IOPin> for u32 { |
| 134 | type Output = u32; |
| 135 | fn bitand(self, rhs: IOPin) -> Self::Output { |
| 136 | let rhs: u32 = rhs.into(); |
| 137 | self & rhs |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | impl BitAnd<u32> for IOPin { |
| 142 | type Output = u32; |
| 143 | fn bitand(self, rhs: u32) -> Self::Output { |
| 144 | let val: u32 = self.into(); |
| 145 | val & rhs |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | impl IOPin { |
| 150 | const fn to_u32(self) -> u32 { |
| 151 | match self { |
| 152 | IOPin::Group1Io1 => 0x00000001, |
| 153 | IOPin::Group1Io2 => 0x00000002, |
| 154 | IOPin::Group1Io3 => 0x00000004, |
| 155 | IOPin::Group1Io4 => 0x00000008, |
| 156 | IOPin::Group2Io1 => 0x00000010, |
| 157 | IOPin::Group2Io2 => 0x00000020, |
| 158 | IOPin::Group2Io3 => 0x00000040, |
| 159 | IOPin::Group2Io4 => 0x00000080, |
| 160 | IOPin::Group3Io1 => 0x00000100, |
| 161 | IOPin::Group3Io2 => 0x00000200, |
| 162 | IOPin::Group3Io3 => 0x00000400, |
| 163 | IOPin::Group3Io4 => 0x00000800, |
| 164 | IOPin::Group4Io1 => 0x00001000, |
| 165 | IOPin::Group4Io2 => 0x00002000, |
| 166 | IOPin::Group4Io3 => 0x00004000, |
| 167 | IOPin::Group4Io4 => 0x00008000, |
| 168 | IOPin::Group5Io1 => 0x00010000, |
| 169 | IOPin::Group5Io2 => 0x00020000, |
| 170 | IOPin::Group5Io3 => 0x00040000, |
| 171 | IOPin::Group5Io4 => 0x00080000, |
| 172 | IOPin::Group6Io1 => 0x00100000, |
| 173 | IOPin::Group6Io2 => 0x00200000, |
| 174 | IOPin::Group6Io3 => 0x00400000, |
| 175 | IOPin::Group6Io4 => 0x00800000, |
| 176 | #[cfg (any(tsc_v2, tsc_v3))] |
| 177 | IOPin::Group7Io1 => 0x01000000, |
| 178 | #[cfg (any(tsc_v2, tsc_v3))] |
| 179 | IOPin::Group7Io2 => 0x02000000, |
| 180 | #[cfg (any(tsc_v2, tsc_v3))] |
| 181 | IOPin::Group7Io3 => 0x04000000, |
| 182 | #[cfg (any(tsc_v2, tsc_v3))] |
| 183 | IOPin::Group7Io4 => 0x08000000, |
| 184 | #[cfg (tsc_v3)] |
| 185 | IOPin::Group8Io1 => 0x10000000, |
| 186 | #[cfg (tsc_v3)] |
| 187 | IOPin::Group8Io2 => 0x20000000, |
| 188 | #[cfg (tsc_v3)] |
| 189 | IOPin::Group8Io3 => 0x40000000, |
| 190 | #[cfg (tsc_v3)] |
| 191 | IOPin::Group8Io4 => 0x80000000, |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | impl Into<u32> for IOPin { |
| 197 | fn into(self) -> u32 { |
| 198 | self.to_u32() |
| 199 | } |
| 200 | } |
| 201 | |