diff options
| author | pennae <[email protected]> | 2023-04-25 19:28:01 +0200 |
|---|---|---|
| committer | pennae <[email protected]> | 2023-05-01 12:58:57 +0200 |
| commit | 4618b79b22bee36ef9485a2b0e8562121b84c54b (patch) | |
| tree | 4fbfc9b3c57a91137175e2202ed83463e63f4a51 | |
| parent | db16b6ff3f94e48f50c4212c164d018741b61b42 (diff) | |
rp/pio: seal PioInstance, SmInstance
seems prudent to hide access to the internals.
| -rw-r--r-- | embassy-rp/src/pio.rs | 61 |
1 files changed, 41 insertions, 20 deletions
diff --git a/embassy-rp/src/pio.rs b/embassy-rp/src/pio.rs index 3775060f0..756f0e928 100644 --- a/embassy-rp/src/pio.rs +++ b/embassy-rp/src/pio.rs | |||
| @@ -12,6 +12,7 @@ use crate::dma::{Channel, Transfer}; | |||
| 12 | use crate::gpio::sealed::Pin as SealedPin; | 12 | use crate::gpio::sealed::Pin as SealedPin; |
| 13 | use crate::gpio::{Drive, Pin, Pull, SlewRate}; | 13 | use crate::gpio::{Drive, Pin, Pull, SlewRate}; |
| 14 | use crate::pac::dma::vals::{DataSize, TreqSel}; | 14 | use crate::pac::dma::vals::{DataSize, TreqSel}; |
| 15 | use crate::pio::sealed::{PioInstance as _, SmInstance as _}; | ||
| 15 | use crate::{interrupt, pac, peripherals, RegExt}; | 16 | use crate::{interrupt, pac, peripherals, RegExt}; |
| 16 | 17 | ||
| 17 | struct Wakers([AtomicWaker; 12]); | 18 | struct Wakers([AtomicWaker; 12]); |
| @@ -320,14 +321,13 @@ pub struct PioStateMachineInstance<PIO: PioInstance, SM: SmInstance> { | |||
| 320 | sm: PhantomData<SM>, | 321 | sm: PhantomData<SM>, |
| 321 | } | 322 | } |
| 322 | 323 | ||
| 323 | impl<PIO: PioInstance, SM: SmInstance> PioStateMachine for PioStateMachineInstance<PIO, SM> { | 324 | impl<PIO: PioInstance, SM: SmInstance> sealed::PioStateMachine for PioStateMachineInstance<PIO, SM> { |
| 324 | type Pio = PIO; | 325 | type Pio = PIO; |
| 325 | type Sm = SM; | 326 | type Sm = SM; |
| 326 | } | 327 | } |
| 328 | impl<PIO: PioInstance, SM: SmInstance> PioStateMachine for PioStateMachineInstance<PIO, SM> {} | ||
| 327 | 329 | ||
| 328 | pub trait PioStateMachine: Sized + Unpin { | 330 | pub trait PioStateMachine: sealed::PioStateMachine + Sized + Unpin { |
| 329 | type Pio: PioInstance; | ||
| 330 | type Sm: SmInstance; | ||
| 331 | fn pio_no(&self) -> u8 { | 331 | fn pio_no(&self) -> u8 { |
| 332 | let _ = self; | 332 | let _ = self; |
| 333 | Self::Pio::PIO_NO | 333 | Self::Pio::PIO_NO |
| @@ -1027,9 +1027,10 @@ pub struct PioCommonInstance<PIO: PioInstance> { | |||
| 1027 | pio: PhantomData<PIO>, | 1027 | pio: PhantomData<PIO>, |
| 1028 | } | 1028 | } |
| 1029 | 1029 | ||
| 1030 | impl<PIO: PioInstance> PioCommon for PioCommonInstance<PIO> { | 1030 | impl<PIO: PioInstance> sealed::PioCommon for PioCommonInstance<PIO> { |
| 1031 | type Pio = PIO; | 1031 | type Pio = PIO; |
| 1032 | } | 1032 | } |
| 1033 | impl<PIO: PioInstance> PioCommon for PioCommonInstance<PIO> {} | ||
| 1033 | 1034 | ||
| 1034 | fn write_instr<I>(pio_no: u8, start: usize, instrs: I, mem_user: u32) | 1035 | fn write_instr<I>(pio_no: u8, start: usize, instrs: I, mem_user: u32) |
| 1035 | where | 1036 | where |
| @@ -1051,9 +1052,7 @@ where | |||
| 1051 | } | 1052 | } |
| 1052 | } | 1053 | } |
| 1053 | 1054 | ||
| 1054 | pub trait PioCommon: Sized { | 1055 | pub trait PioCommon: sealed::PioCommon + Sized { |
| 1055 | type Pio: PioInstance; | ||
| 1056 | |||
| 1057 | fn write_instr<I>(&mut self, start: usize, instrs: I) | 1056 | fn write_instr<I>(&mut self, start: usize, instrs: I) |
| 1058 | where | 1057 | where |
| 1059 | I: Iterator<Item = u16>, | 1058 | I: Iterator<Item = u16>, |
| @@ -1096,16 +1095,14 @@ pub trait PioCommon: Sized { | |||
| 1096 | // Identifies a specific state machine inside a PIO device | 1095 | // Identifies a specific state machine inside a PIO device |
| 1097 | pub struct SmInstanceBase<const SM_NO: u8> {} | 1096 | pub struct SmInstanceBase<const SM_NO: u8> {} |
| 1098 | 1097 | ||
| 1099 | pub trait SmInstance: Unpin { | 1098 | pub trait SmInstance: sealed::SmInstance + Unpin {} |
| 1100 | const SM_NO: u8; | ||
| 1101 | } | ||
| 1102 | 1099 | ||
| 1103 | impl<const SM_NO: u8> SmInstance for SmInstanceBase<SM_NO> { | 1100 | impl<const SM_NO: u8> sealed::SmInstance for SmInstanceBase<SM_NO> { |
| 1104 | const SM_NO: u8 = SM_NO; | 1101 | const SM_NO: u8 = SM_NO; |
| 1105 | } | 1102 | } |
| 1103 | impl<const SM_NO: u8> SmInstance for SmInstanceBase<SM_NO> {} | ||
| 1106 | 1104 | ||
| 1107 | pub trait PioPeripheral: Sized { | 1105 | pub trait PioPeripheral: sealed::PioPeripheral + Sized { |
| 1108 | type Pio: PioInstance; | ||
| 1109 | fn pio(&self) -> u8 { | 1106 | fn pio(&self) -> u8 { |
| 1110 | let _ = self; | 1107 | let _ = self; |
| 1111 | Self::Pio::PIO_NO | 1108 | Self::Pio::PIO_NO |
| @@ -1145,20 +1142,43 @@ pub trait PioPeripheral: Sized { | |||
| 1145 | } | 1142 | } |
| 1146 | } | 1143 | } |
| 1147 | 1144 | ||
| 1145 | mod sealed { | ||
| 1146 | pub trait PioInstance { | ||
| 1147 | const PIO_NO: u8; | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | pub trait PioCommon { | ||
| 1151 | type Pio: super::PioInstance; | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | pub trait PioStateMachine { | ||
| 1155 | type Pio: super::PioInstance; | ||
| 1156 | type Sm: super::SmInstance; | ||
| 1157 | } | ||
| 1158 | |||
| 1159 | pub trait SmInstance { | ||
| 1160 | const SM_NO: u8; | ||
| 1161 | } | ||
| 1162 | |||
| 1163 | pub trait PioPeripheral { | ||
| 1164 | type Pio: super::PioInstance; | ||
| 1165 | } | ||
| 1166 | } | ||
| 1167 | |||
| 1148 | // Identifies a specific PIO device | 1168 | // Identifies a specific PIO device |
| 1149 | pub struct PioInstanceBase<const PIO_NO: u8> {} | 1169 | pub struct PioInstanceBase<const PIO_NO: u8> {} |
| 1150 | 1170 | ||
| 1151 | pub trait PioInstance: Unpin { | 1171 | pub trait PioInstance: sealed::PioInstance + Unpin {} |
| 1152 | const PIO_NO: u8; | ||
| 1153 | } | ||
| 1154 | 1172 | ||
| 1155 | impl PioInstance for PioInstanceBase<0> { | 1173 | impl sealed::PioInstance for PioInstanceBase<0> { |
| 1156 | const PIO_NO: u8 = 0; | 1174 | const PIO_NO: u8 = 0; |
| 1157 | } | 1175 | } |
| 1176 | impl PioInstance for PioInstanceBase<0> {} | ||
| 1158 | 1177 | ||
| 1159 | impl PioInstance for PioInstanceBase<1> { | 1178 | impl sealed::PioInstance for PioInstanceBase<1> { |
| 1160 | const PIO_NO: u8 = 1; | 1179 | const PIO_NO: u8 = 1; |
| 1161 | } | 1180 | } |
| 1181 | impl PioInstance for PioInstanceBase<1> {} | ||
| 1162 | 1182 | ||
| 1163 | pub type Pio0 = PioInstanceBase<0>; | 1183 | pub type Pio0 = PioInstanceBase<0>; |
| 1164 | pub type Pio1 = PioInstanceBase<1>; | 1184 | pub type Pio1 = PioInstanceBase<1>; |
| @@ -1170,9 +1190,10 @@ pub type Sm3 = SmInstanceBase<3>; | |||
| 1170 | 1190 | ||
| 1171 | macro_rules! impl_pio_sm { | 1191 | macro_rules! impl_pio_sm { |
| 1172 | ($name:ident, $pio:expr) => { | 1192 | ($name:ident, $pio:expr) => { |
| 1173 | impl PioPeripheral for peripherals::$name { | 1193 | impl sealed::PioPeripheral for peripherals::$name { |
| 1174 | type Pio = PioInstanceBase<$pio>; | 1194 | type Pio = PioInstanceBase<$pio>; |
| 1175 | } | 1195 | } |
| 1196 | impl PioPeripheral for peripherals::$name {} | ||
| 1176 | }; | 1197 | }; |
| 1177 | } | 1198 | } |
| 1178 | 1199 | ||
