|
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+//! SAI interface with DMA data transfer.
|
|
|
2
|
+
|
|
|
3
|
+// TODO: Unify capitalization of template parameters.
|
|
|
4
|
+
|
|
|
5
|
+use core::marker::PhantomData;
|
|
|
6
|
+use core::ops::Deref;
|
|
|
7
|
+
|
|
|
8
|
+use stm32f4xx_hal::gpio::gpiod::PD6;
|
|
|
9
|
+use stm32f4xx_hal::gpio::gpioe::{PE2, PE3, PE4, PE5, PE6};
|
|
|
10
|
+use stm32f4xx_hal::gpio::gpiof::{PF6, PF7, PF8, PF9};
|
|
|
11
|
+use stm32f4xx_hal::gpio::{Alternate, AF6};
|
|
|
12
|
+use stm32f4xx_hal::stm32::sai::CH;
|
|
|
13
|
+use stm32f4xx_hal::stm32::{RCC, SAI};
|
|
|
14
|
+
|
|
|
15
|
+use super::i2s::I2SClocks;
|
|
|
16
|
+
|
|
|
17
|
+struct NoMck;
|
|
|
18
|
+
|
|
|
19
|
+/// Trait for master clock pins.
|
|
|
20
|
+pub trait PinMck<Block> {}
|
|
|
21
|
+impl PinMck<SAIA> for PE2<Alternate<AF6>> {}
|
|
|
22
|
+impl PinMck<SAIB> for PF7<Alternate<AF6>> {}
|
|
|
23
|
+impl<Block> PinMck<Block> for NoMck {}
|
|
|
24
|
+
|
|
|
25
|
+/// Trait for frame select pins.
|
|
|
26
|
+pub trait PinFs<Block> {}
|
|
|
27
|
+impl PinFs<SAIA> for PE4<Alternate<AF6>> {}
|
|
|
28
|
+impl PinFs<SAIB> for PF9<Alternate<AF6>> {}
|
|
|
29
|
+
|
|
|
30
|
+/// Trait for bit clock pins.
|
|
|
31
|
+pub trait PinSck<Block> {}
|
|
|
32
|
+impl PinSck<SAIA> for PE5<Alternate<AF6>> {}
|
|
|
33
|
+impl PinSck<SAIB> for PF8<Alternate<AF6>> {}
|
|
|
34
|
+
|
|
|
35
|
+/// Trait for data pins.
|
|
|
36
|
+pub trait PinSd<Block> {}
|
|
|
37
|
+impl PinSd<SAIA> for PD6<Alternate<AF6>> {}
|
|
|
38
|
+impl PinSd<SAIA> for PE6<Alternate<AF6>> {}
|
|
|
39
|
+impl PinSd<SAIB> for PE3<Alternate<AF6>> {}
|
|
|
40
|
+impl PinSd<SAIB> for PF6<Alternate<AF6>> {}
|
|
|
41
|
+
|
|
|
42
|
+/// Pins required for an asynchronous SAI master channel.
|
|
|
43
|
+pub trait MasterPins<Block> {}
|
|
|
44
|
+
|
|
|
45
|
+impl<Block, MCK, FS, SCK, SD> MasterPins<Block> for (MCK, FS, SCK, SD)
|
|
|
46
|
+where
|
|
|
47
|
+ MCK: PinMck<Block>,
|
|
|
48
|
+ FS: PinFs<Block>,
|
|
|
49
|
+ SCK: PinSck<Block>,
|
|
|
50
|
+ SD: PinSd<Block>,
|
|
|
51
|
+{
|
|
|
52
|
+}
|
|
|
53
|
+
|
|
|
54
|
+/// Pins required for an asynchronous SAI slave channel.
|
|
|
55
|
+pub trait SlavePins<Block> {}
|
|
|
56
|
+
|
|
|
57
|
+impl<Block, MCK, FS, SCK, SD> SlavePins<Block> for (MCK, FS, SCK, SD)
|
|
|
58
|
+where
|
|
|
59
|
+ MCK: PinMck<Block>,
|
|
|
60
|
+ FS: PinFs<Block>,
|
|
|
61
|
+ SCK: PinSck<Block>,
|
|
|
62
|
+ SD: PinSd<Block>,
|
|
|
63
|
+{
|
|
|
64
|
+}
|
|
|
65
|
+
|
|
|
66
|
+// TODO: SAIA is on Channel 0, Stream 1 and 3.
|
|
|
67
|
+// TODO: SAIB is on Channel 0, Stream 5 and Channel 1, Stream 4.
|
|
|
68
|
+
|
|
|
69
|
+/// Asynchronous SAI sub-block which has not yet been configured.
|
|
|
70
|
+///
|
|
|
71
|
+/// Asynchronous means that the sub-block has its own set of clock pins.
|
|
|
72
|
+pub struct Asynchronous;
|
|
|
73
|
+
|
|
|
74
|
+/// Asynchronous SAI sub-block which as been configured as a master.
|
|
|
75
|
+pub struct AsyncMaster<Pins> {
|
|
|
76
|
+ pins: Pins,
|
|
|
77
|
+}
|
|
|
78
|
+
|
|
|
79
|
+/// Asynchronous SAI sub-block which as been configured as a slave.
|
|
|
80
|
+pub struct AsyncSlave<Pins> {
|
|
|
81
|
+ pins: Pins,
|
|
|
82
|
+}
|
|
|
83
|
+
|
|
|
84
|
+/// Synchronous SAI sub-block.
|
|
|
85
|
+///
|
|
|
86
|
+/// Synchronous sub-blocks are always configured as slaves.
|
|
|
87
|
+pub struct Synchronous;
|
|
|
88
|
+
|
|
|
89
|
+/// SAI sub-block which has neither been configured as a receiver nor as a transmitter.
|
|
|
90
|
+pub struct NoDir;
|
|
|
91
|
+
|
|
|
92
|
+/// SAI sub-block which has been configured as a receiver.
|
|
|
93
|
+pub struct Receive;
|
|
|
94
|
+
|
|
|
95
|
+/// SAI sub-block which has been configured as a transmitter.
|
|
|
96
|
+pub struct Transmit;
|
|
|
97
|
+
|
|
|
98
|
+/// "A" channel of the SAI.
|
|
|
99
|
+pub struct SAIA;
|
|
|
100
|
+
|
|
|
101
|
+impl Deref for SAIA {
|
|
|
102
|
+ type Target = CH;
|
|
|
103
|
+
|
|
|
104
|
+ fn deref(&self) -> &Self::Target {
|
|
|
105
|
+ unsafe { &(*SAI::ptr()).cha }
|
|
|
106
|
+ }
|
|
|
107
|
+}
|
|
|
108
|
+
|
|
|
109
|
+/// "B" channel of the SAI.
|
|
|
110
|
+pub struct SAIB;
|
|
|
111
|
+
|
|
|
112
|
+impl Deref for SAIB {
|
|
|
113
|
+ type Target = CH;
|
|
|
114
|
+
|
|
|
115
|
+ fn deref(&self) -> &Self::Target {
|
|
|
116
|
+ unsafe { &(*SAI::ptr()).chb }
|
|
|
117
|
+ }
|
|
|
118
|
+}
|
|
|
119
|
+
|
|
|
120
|
+/// Wrapper for a single channel of the SAI and its configuration.
|
|
|
121
|
+pub struct SubBlock<Channel, Config, Direction> {
|
|
|
122
|
+ channel: Channel,
|
|
|
123
|
+ config: Config,
|
|
|
124
|
+ direction: Direction,
|
|
|
125
|
+}
|
|
|
126
|
+
|
|
|
127
|
+/// Functions to configure the two sub-blocks of an SAI instance.
|
|
|
128
|
+///
|
|
|
129
|
+/// For the two sub-blocks of a single SAI instance, only specific combinations of modes are valid.
|
|
|
130
|
+/// This trait has one method for each such combination.
|
|
|
131
|
+pub trait SAIExt {
|
|
|
132
|
+ /// Splits the SAI instance into two asynchronous sub-blocks.
|
|
|
133
|
+ fn split(
|
|
|
134
|
+ self,
|
|
|
135
|
+ ) -> (
|
|
|
136
|
+ SubBlock<SAIA, Asynchronous, NoDir>,
|
|
|
137
|
+ SubBlock<SAIB, Asynchronous, NoDir>,
|
|
|
138
|
+ )
|
|
|
139
|
+ where
|
|
|
140
|
+ Self: Sized;
|
|
|
141
|
+
|
|
|
142
|
+ /// Splits the SAI instance so that the A block uses the synchronization signals of the B
|
|
|
143
|
+ /// block.
|
|
|
144
|
+ fn split_sync_a(
|
|
|
145
|
+ self,
|
|
|
146
|
+ ) -> (
|
|
|
147
|
+ SubBlock<SAIA, Synchronous, NoDir>,
|
|
|
148
|
+ SubBlock<SAIB, Asynchronous, NoDir>,
|
|
|
149
|
+ )
|
|
|
150
|
+ where
|
|
|
151
|
+ Self: Sized;
|
|
|
152
|
+
|
|
|
153
|
+ /// Splits the SAI instance so that the B block uses the synchronization signals of the A
|
|
|
154
|
+ /// block.
|
|
|
155
|
+ fn split_sync_b(
|
|
|
156
|
+ self,
|
|
|
157
|
+ ) -> (
|
|
|
158
|
+ SubBlock<SAIA, Asynchronous, NoDir>,
|
|
|
159
|
+ SubBlock<SAIB, Synchronous, NoDir>,
|
|
|
160
|
+ )
|
|
|
161
|
+ where
|
|
|
162
|
+ Self: Sized;
|
|
|
163
|
+
|
|
|
164
|
+ /*/// Un-splits the two sub-blocks and resets the SAI.
|
|
|
165
|
+ fn uninit<ConfigA, ConfigB>(a: SubBlock<SAIA, ConfigA>, b: SubBlock<SAIB, ConfigB>) -> Self
|
|
|
166
|
+ where
|
|
|
167
|
+ Self: Sized;*/
|
|
|
168
|
+
|
|
|
169
|
+ /// Enables and resets the SAI instance.
|
|
|
170
|
+ fn reset(&mut self);
|
|
|
171
|
+}
|
|
|
172
|
+
|
|
|
173
|
+impl SAIExt for SAI {
|
|
|
174
|
+ fn split(
|
|
|
175
|
+ mut self,
|
|
|
176
|
+ ) -> (
|
|
|
177
|
+ SubBlock<SAIA, Asynchronous, NoDir>,
|
|
|
178
|
+ SubBlock<SAIB, Asynchronous, NoDir>,
|
|
|
179
|
+ )
|
|
|
180
|
+ where
|
|
|
181
|
+ Self: Sized,
|
|
|
182
|
+ {
|
|
|
183
|
+ self.reset();
|
|
|
184
|
+ (
|
|
|
185
|
+ SubBlock {
|
|
|
186
|
+ channel: SAIA,
|
|
|
187
|
+ config: Asynchronous,
|
|
|
188
|
+ direction: NoDir,
|
|
|
189
|
+ },
|
|
|
190
|
+ SubBlock {
|
|
|
191
|
+ channel: SAIB,
|
|
|
192
|
+ config: Asynchronous,
|
|
|
193
|
+ direction: NoDir,
|
|
|
194
|
+ },
|
|
|
195
|
+ )
|
|
|
196
|
+ }
|
|
|
197
|
+
|
|
|
198
|
+ fn split_sync_a(
|
|
|
199
|
+ mut self,
|
|
|
200
|
+ ) -> (
|
|
|
201
|
+ SubBlock<SAIA, Synchronous, NoDir>,
|
|
|
202
|
+ SubBlock<SAIB, Asynchronous, NoDir>,
|
|
|
203
|
+ )
|
|
|
204
|
+ where
|
|
|
205
|
+ Self: Sized,
|
|
|
206
|
+ {
|
|
|
207
|
+ self.reset();
|
|
|
208
|
+ (
|
|
|
209
|
+ SubBlock {
|
|
|
210
|
+ channel: SAIA,
|
|
|
211
|
+ config: Synchronous,
|
|
|
212
|
+ direction: NoDir,
|
|
|
213
|
+ },
|
|
|
214
|
+ SubBlock {
|
|
|
215
|
+ channel: SAIB,
|
|
|
216
|
+ config: Asynchronous,
|
|
|
217
|
+ direction: NoDir,
|
|
|
218
|
+ },
|
|
|
219
|
+ )
|
|
|
220
|
+ }
|
|
|
221
|
+
|
|
|
222
|
+ fn split_sync_b(
|
|
|
223
|
+ mut self,
|
|
|
224
|
+ ) -> (
|
|
|
225
|
+ SubBlock<SAIA, Asynchronous, NoDir>,
|
|
|
226
|
+ SubBlock<SAIB, Synchronous, NoDir>,
|
|
|
227
|
+ )
|
|
|
228
|
+ where
|
|
|
229
|
+ Self: Sized,
|
|
|
230
|
+ {
|
|
|
231
|
+ self.reset();
|
|
|
232
|
+ (
|
|
|
233
|
+ SubBlock {
|
|
|
234
|
+ channel: SAIA,
|
|
|
235
|
+ config: Asynchronous,
|
|
|
236
|
+ direction: NoDir,
|
|
|
237
|
+ },
|
|
|
238
|
+ SubBlock {
|
|
|
239
|
+ channel: SAIB,
|
|
|
240
|
+ config: Synchronous,
|
|
|
241
|
+ direction: NoDir,
|
|
|
242
|
+ },
|
|
|
243
|
+ )
|
|
|
244
|
+ }
|
|
|
245
|
+
|
|
|
246
|
+ /*fn uninit<ConfigA, ConfigB>(a: SubBlock<SAIA, ConfigA>, b: SubBlock<SAIB, ConfigB>) -> Self {
|
|
|
247
|
+ // TODO
|
|
|
248
|
+ }*/
|
|
|
249
|
+
|
|
|
250
|
+ fn reset(&mut self) {
|
|
|
251
|
+ let rcc = unsafe { &*RCC::ptr() };
|
|
|
252
|
+ rcc.apb2enr.modify(|_, w| w.sai1en().set_bit());
|
|
|
253
|
+ rcc.apb2rstr.modify(|_, w| w.sai1rst().set_bit());
|
|
|
254
|
+ rcc.apb2rstr.modify(|_, w| w.sai1rst().clear_bit());
|
|
|
255
|
+ }
|
|
|
256
|
+}
|
|
|
257
|
+
|
|
|
258
|
+impl<Channel> SubBlock<Channel, Asynchronous, NoDir>
|
|
|
259
|
+where
|
|
|
260
|
+ Channel: Deref<Target = CH>,
|
|
|
261
|
+{
|
|
|
262
|
+ /// Configures the channel as a master and a receiver.
|
|
|
263
|
+ pub fn master_rx<Pins>(self, pins: Pins) -> SubBlock<Channel, AsyncMaster<Pins>, Receive>
|
|
|
264
|
+ where
|
|
|
265
|
+ Pins: MasterPins<Channel>,
|
|
|
266
|
+ {
|
|
|
267
|
+ // TODO: Clock/data type configuration?
|
|
|
268
|
+ SubBlock {
|
|
|
269
|
+ channel: self.channel,
|
|
|
270
|
+ config: AsyncMaster { pins },
|
|
|
271
|
+ direction: Receive,
|
|
|
272
|
+ }
|
|
|
273
|
+ }
|
|
|
274
|
+
|
|
|
275
|
+ /// Configures the channel as a master and a receiver.
|
|
|
276
|
+ pub fn master_tx<Pins>(self, pins: Pins) -> SubBlock<Channel, AsyncMaster<Pins>, Transmit>
|
|
|
277
|
+ where
|
|
|
278
|
+ Pins: MasterPins<Channel>,
|
|
|
279
|
+ {
|
|
|
280
|
+ // TODO: Clock/data type configuration?
|
|
|
281
|
+ SubBlock {
|
|
|
282
|
+ channel: self.channel,
|
|
|
283
|
+ config: AsyncMaster { pins },
|
|
|
284
|
+ direction: Transmit,
|
|
|
285
|
+ }
|
|
|
286
|
+ }
|
|
|
287
|
+
|
|
|
288
|
+ /// Configures the channel as a slave and a receiver.
|
|
|
289
|
+ pub fn slave_rx<Pins>(self, pins: Pins) -> SubBlock<Channel, AsyncSlave<Pins>, Receive>
|
|
|
290
|
+ where
|
|
|
291
|
+ Pins: SlavePins<Channel>,
|
|
|
292
|
+ {
|
|
|
293
|
+ // TODO: Data type configuration?
|
|
|
294
|
+ SubBlock {
|
|
|
295
|
+ channel: self.channel,
|
|
|
296
|
+ config: AsyncSlave { pins },
|
|
|
297
|
+ direction: Receive,
|
|
|
298
|
+ }
|
|
|
299
|
+ }
|
|
|
300
|
+
|
|
|
301
|
+ /// Configures the channel as a master and a receiver.
|
|
|
302
|
+ pub fn slave_tx<Pins>(self, pins: Pins) -> SubBlock<Channel, AsyncSlave<Pins>, Transmit>
|
|
|
303
|
+ where
|
|
|
304
|
+ Pins: SlavePins<Channel>,
|
|
|
305
|
+ {
|
|
|
306
|
+ // TODO: Data type configuration?
|
|
|
307
|
+ SubBlock {
|
|
|
308
|
+ channel: self.channel,
|
|
|
309
|
+ config: AsyncSlave { pins },
|
|
|
310
|
+ direction: Transmit,
|
|
|
311
|
+ }
|
|
|
312
|
+ }
|
|
|
313
|
+}
|
|
|
314
|
+
|
|
|
315
|
+impl<Channel, Config> SubBlock<Channel, Config, Receive>
|
|
|
316
|
+where
|
|
|
317
|
+ Channel: Deref<Target = CH>,
|
|
|
318
|
+{
|
|
|
319
|
+ // TODO
|
|
|
320
|
+}
|
|
|
321
|
+
|
|
|
322
|
+impl<Channel, Config> SubBlock<Channel, Config, Transmit>
|
|
|
323
|
+where
|
|
|
324
|
+ Channel: Deref<Target = CH>,
|
|
|
325
|
+{
|
|
|
326
|
+ // TODO
|
|
|
327
|
+}
|
|
|
328
|
+
|
|
|
329
|
+/// Wrapper around `Receive` and `Transmit` blocks to provide full-duplex I2S transfers.
|
|
|
330
|
+pub struct Duplex<Channel1, Config1, Channel2, Config2> {
|
|
|
331
|
+ rx: SubBlock<Channel1, Config1, Receive>,
|
|
|
332
|
+ tx: SubBlock<Channel2, Config2, Transmit>,
|
|
|
333
|
+}
|
|
|
334
|
+
|
|
|
335
|
+impl<Channel1, Config1, Channel2, Config2> Duplex<Channel1, Config1, Channel2, Config2>
|
|
|
336
|
+where
|
|
|
337
|
+ Channel1: Deref<Target = CH>,
|
|
|
338
|
+ Channel2: Deref<Target = CH>,
|
|
|
339
|
+{
|
|
|
340
|
+ /// Wraps the specified receiver/transmitter objects.
|
|
|
341
|
+ pub fn new(
|
|
|
342
|
+ rx: SubBlock<Channel1, Config1, Receive>,
|
|
|
343
|
+ tx: SubBlock<Channel2, Config2, Transmit>,
|
|
|
344
|
+ ) -> Self {
|
|
|
345
|
+ // TODO
|
|
|
346
|
+ Self { rx, tx }
|
|
|
347
|
+ }
|
|
|
348
|
+
|
|
|
349
|
+ pub fn try_read(&mut self) -> nb::Result<(u32, u32), Self::Error> {
|
|
|
350
|
+ // TODO
|
|
|
351
|
+ }
|
|
|
352
|
+
|
|
|
353
|
+ pub fn try_send(&mut self) -> nb::Result<(u32, u32), Self::Error> {
|
|
|
354
|
+ // TODO
|
|
|
355
|
+ }
|
|
|
356
|
+}
|
|
|
357
|
+
|
|
|
358
|
+/*impl<
|
|
|
359
|
+ SAIX: SAIExt<SAIX>,
|
|
|
360
|
+ RxBlockInner: SubBlock<SAI = SAIX>,
|
|
|
361
|
+ RxBlock: SubBlockConfig<SubBlock = RxBlockInner>,
|
|
|
362
|
+ TxBlockInner: SubBlock<SAI = SAIX>,
|
|
|
363
|
+ TxBlock: SubBlockConfig<SubBlock = TxBlockInner>,
|
|
|
364
|
+ > Duplex<SAIX, RxBlockInner, RxBlock, TxBlockInner, TxBlock>
|
|
|
365
|
+{
|
|
|
366
|
+ /// Wraps the specified receiver/transmitter objects.
|
|
|
367
|
+ pub fn new(rx: Receive<RxBlock>, tx: Transmit<TxBlock>) -> Self {
|
|
|
368
|
+ // TODO
|
|
|
369
|
+ Self {
|
|
|
370
|
+ rx,
|
|
|
371
|
+ tx,
|
|
|
372
|
+ _rx_inner: PhantomData,
|
|
|
373
|
+ _tx_inner: PhantomData,
|
|
|
374
|
+ }
|
|
|
375
|
+ }
|
|
|
376
|
+
|
|
|
377
|
+ /// Starts transmitting and receiving data.
|
|
|
378
|
+ ///
|
|
|
379
|
+ /// For I2S masters, this function starts the clock signals.
|
|
|
380
|
+ pub fn start(&mut self) {
|
|
|
381
|
+ // TODO
|
|
|
382
|
+ }
|
|
|
383
|
+
|
|
|
384
|
+ /// Stops transmitting and receiving data.
|
|
|
385
|
+ pub fn stop(&mut self) {
|
|
|
386
|
+ // TODO
|
|
|
387
|
+ }
|
|
|
388
|
+}*/
|
|
|
389
|
+
|
|
|
390
|
+/*// Use cases:
|
|
|
391
|
+// - SAI should be usable as two simplex I2S interfaces, both either slave or master.
|
|
|
392
|
+// - SAI should be usable as one duplex I2S interface, either as slave or master.
|
|
|
393
|
+// - SAI should be usable as two simple I2S interfaces with synchronization
|
|
|
394
|
+
|
|
|
395
|
+/// Mode of an SAI sub-block (sync/async, master/slave).
|
|
|
396
|
+pub trait SubBlockConfig {
|
|
|
397
|
+ type SubBlock: SubBlock;
|
|
|
398
|
+ // TODO
|
|
|
399
|
+}
|
|
|
400
|
+
|
|
|
401
|
+/// SAI block configured to use the synchronization signals of its asynchronous sibling block.
|
|
|
402
|
+pub struct Synchronous<Block> {
|
|
|
403
|
+ block: Block,
|
|
|
404
|
+}
|
|
|
405
|
+
|
|
|
406
|
+impl<Block> Synchronous<Block>
|
|
|
407
|
+where
|
|
|
408
|
+ Block: SubBlock,
|
|
|
409
|
+{
|
|
|
410
|
+ /// Configures this sub-block as a slave.
|
|
|
411
|
+ pub fn slave<SDPin: SD<Block>>(mut self, sd: SDPin) -> SyncSlave<Block, SDPin> {
|
|
|
412
|
+ self.block.set_slave();
|
|
|
413
|
+ SyncSlave {
|
|
|
414
|
+ block: self.block,
|
|
|
415
|
+ sd,
|
|
|
416
|
+ }
|
|
|
417
|
+ }
|
|
|
418
|
+}
|
|
|
419
|
+
|
|
|
420
|
+/// SAI sub-block configured as a synchronous slave.
|
|
|
421
|
+pub struct SyncSlave<Block, SDPin> {
|
|
|
422
|
+ block: Block,
|
|
|
423
|
+ sd: SDPin,
|
|
|
424
|
+ // TODO
|
|
|
425
|
+}
|
|
|
426
|
+
|
|
|
427
|
+impl<Block, SDPin> SyncSlave<Block, SDPin> {
|
|
|
428
|
+ /// Uninitializes the sub-block, releasing the pins.
|
|
|
429
|
+ pub fn destroy(self) -> (Asynchronous<Block>, SDPin) {
|
|
|
430
|
+ // TODO: Disable?
|
|
|
431
|
+ (Asynchronous { block: self.block }, self.sd)
|
|
|
432
|
+ }
|
|
|
433
|
+}
|
|
|
434
|
+
|
|
|
435
|
+impl<Block, SDPin> SubBlockConfig for SyncSlave<Block, SDPin>
|
|
|
436
|
+where
|
|
|
437
|
+ Block: SubBlock,
|
|
|
438
|
+{
|
|
|
439
|
+ type SubBlock = Block;
|
|
|
440
|
+ // TODO
|
|
|
441
|
+}
|
|
|
442
|
+
|
|
|
443
|
+/// SAI block configured to provide its own synchronization signals (either as inputs in the case
|
|
|
444
|
+/// o a slave or via a clock generator in the case of a master).
|
|
|
445
|
+pub struct Asynchronous<Block> {
|
|
|
446
|
+ block: Block,
|
|
|
447
|
+}
|
|
|
448
|
+
|
|
|
449
|
+impl<Block> Asynchronous<Block>
|
|
|
450
|
+where
|
|
|
451
|
+ Block: SubBlock,
|
|
|
452
|
+{
|
|
|
453
|
+ /// Configures this sub-block as a master.
|
|
|
454
|
+ pub fn master<MCKPin, FSPin, SCKPin, SDPin>(
|
|
|
455
|
+ mut self,
|
|
|
456
|
+ mck: MCKPin,
|
|
|
457
|
+ fs: FSPin,
|
|
|
458
|
+ sck: SCKPin,
|
|
|
459
|
+ sd: SDPin,
|
|
|
460
|
+ _clocks: I2SClocks,
|
|
|
461
|
+ ) -> AsyncMaster<Block, MCKPin, FSPin, SCKPin, SDPin>
|
|
|
462
|
+ where
|
|
|
463
|
+ MCKPin: MCK<Block>,
|
|
|
464
|
+ FSPin: FS<Block>,
|
|
|
465
|
+ SCKPin: SCK<Block>,
|
|
|
466
|
+ SDPin: SD<Block>,
|
|
|
467
|
+ {
|
|
|
468
|
+ self.block.set_master();
|
|
|
469
|
+ AsyncMaster {
|
|
|
470
|
+ block: self.block,
|
|
|
471
|
+ mck,
|
|
|
472
|
+ fs,
|
|
|
473
|
+ sck,
|
|
|
474
|
+ sd,
|
|
|
475
|
+ }
|
|
|
476
|
+ }
|
|
|
477
|
+
|
|
|
478
|
+ /// Configures this sub-block as a slave.
|
|
|
479
|
+ pub fn slave<FSPin, SCKPin, SDPin>(
|
|
|
480
|
+ mut self,
|
|
|
481
|
+ fs: FSPin,
|
|
|
482
|
+ sck: SCKPin,
|
|
|
483
|
+ sd: SDPin,
|
|
|
484
|
+ ) -> AsyncSlave<Block, FSPin, SCKPin, SDPin>
|
|
|
485
|
+ where
|
|
|
486
|
+ FSPin: FS<Block>,
|
|
|
487
|
+ SCKPin: SCK<Block>,
|
|
|
488
|
+ SDPin: SD<Block>,
|
|
|
489
|
+ {
|
|
|
490
|
+ self.block.set_slave();
|
|
|
491
|
+ AsyncSlave {
|
|
|
492
|
+ block: self.block,
|
|
|
493
|
+ fs,
|
|
|
494
|
+ sck,
|
|
|
495
|
+ sd,
|
|
|
496
|
+ }
|
|
|
497
|
+ }
|
|
|
498
|
+}
|
|
|
499
|
+
|
|
|
500
|
+/// SAI sub-block configured as a master.
|
|
|
501
|
+pub struct AsyncMaster<Block, MCKPin, FSPin, SCKPin, SDPin> {
|
|
|
502
|
+ block: Block,
|
|
|
503
|
+ mck: MCKPin,
|
|
|
504
|
+ fs: FSPin,
|
|
|
505
|
+ sck: SCKPin,
|
|
|
506
|
+ sd: SDPin,
|
|
|
507
|
+}
|
|
|
508
|
+
|
|
|
509
|
+impl<Block, MCKPin, FSPin, SCKPin, SDPin> AsyncMaster<Block, MCKPin, FSPin, SCKPin, SDPin> {
|
|
|
510
|
+ /// Uninitializes the sub-block, releasing the pins.
|
|
|
511
|
+ pub fn destroy(self) -> (Asynchronous<Block>, MCKPin, FSPin, SCKPin, SDPin) {
|
|
|
512
|
+ // TODO: Disable?
|
|
|
513
|
+ (
|
|
|
514
|
+ Asynchronous { block: self.block },
|
|
|
515
|
+ self.mck,
|
|
|
516
|
+ self.fs,
|
|
|
517
|
+ self.sck,
|
|
|
518
|
+ self.sd,
|
|
|
519
|
+ )
|
|
|
520
|
+ }
|
|
|
521
|
+}
|
|
|
522
|
+
|
|
|
523
|
+impl<Block, MCKPin, FSPin, SCKPin, SDPin> SubBlockConfig
|
|
|
524
|
+ for AsyncMaster<Block, MCKPin, FSPin, SCKPin, SDPin>
|
|
|
525
|
+where
|
|
|
526
|
+ Block: SubBlock,
|
|
|
527
|
+{
|
|
|
528
|
+ type SubBlock = Block;
|
|
|
529
|
+ // TODO
|
|
|
530
|
+}
|
|
|
531
|
+
|
|
|
532
|
+/// SAI sub-block configured as an asynchronous slave.
|
|
|
533
|
+pub struct AsyncSlave<Block, FSPin, SCKPin, SDPin> {
|
|
|
534
|
+ block: Block,
|
|
|
535
|
+ fs: FSPin,
|
|
|
536
|
+ sck: SCKPin,
|
|
|
537
|
+ sd: SDPin,
|
|
|
538
|
+}
|
|
|
539
|
+
|
|
|
540
|
+impl<Block, FSPin, SCKPin, SDPin> AsyncSlave<Block, FSPin, SCKPin, SDPin> {
|
|
|
541
|
+ /// Uninitializes the sub-block, releasing the pins.
|
|
|
542
|
+ pub fn destroy(self) -> (Asynchronous<Block>, FSPin, SCKPin, SDPin) {
|
|
|
543
|
+ // TODO: Disable?
|
|
|
544
|
+ (
|
|
|
545
|
+ Asynchronous { block: self.block },
|
|
|
546
|
+ self.fs,
|
|
|
547
|
+ self.sck,
|
|
|
548
|
+ self.sd,
|
|
|
549
|
+ )
|
|
|
550
|
+ }
|
|
|
551
|
+}
|
|
|
552
|
+
|
|
|
553
|
+impl<Block, FSPin, SCKPin, SDPin> SubBlockConfig for AsyncSlave<Block, FSPin, SCKPin, SDPin>
|
|
|
554
|
+where
|
|
|
555
|
+ Block: SubBlock,
|
|
|
556
|
+{
|
|
|
557
|
+ type SubBlock = Block;
|
|
|
558
|
+ // TODO
|
|
|
559
|
+}
|
|
|
560
|
+
|
|
|
561
|
+/// SAI sub-block configured as a transmitter.
|
|
|
562
|
+pub struct Transmit<Block: SubBlockConfig> {
|
|
|
563
|
+ block: Block,
|
|
|
564
|
+}
|
|
|
565
|
+
|
|
|
566
|
+impl<Block: SubBlockConfig> Transmit<Block> {
|
|
|
567
|
+ /// Initializes the sub-block as a transmitter.
|
|
|
568
|
+ pub fn new(block: Block) -> Self {
|
|
|
569
|
+ // TODO: Configuration.
|
|
|
570
|
+ Self { block }
|
|
|
571
|
+ }
|
|
|
572
|
+
|
|
|
573
|
+ /// Starts transmitting data.
|
|
|
574
|
+ ///
|
|
|
575
|
+ /// For I2S masters, this function starts the clock signals.
|
|
|
576
|
+ pub fn start(&mut self) {
|
|
|
577
|
+ // TODO
|
|
|
578
|
+ }
|
|
|
579
|
+
|
|
|
580
|
+ /// Stops transmitting data.
|
|
|
581
|
+ pub fn stop(&mut self) {
|
|
|
582
|
+ // TODO
|
|
|
583
|
+ }
|
|
|
584
|
+}
|
|
|
585
|
+
|
|
|
586
|
+/// SAI sub-block configured as a receiver.
|
|
|
587
|
+pub struct Receive<Block: SubBlockConfig> {
|
|
|
588
|
+ block: Block,
|
|
|
589
|
+}
|
|
|
590
|
+
|
|
|
591
|
+impl<Block: SubBlockConfig> Receive<Block> {
|
|
|
592
|
+ /// Initializes the sub-block as a receiver.
|
|
|
593
|
+ pub fn new(block: Block) -> Self {
|
|
|
594
|
+ // TODO: Configuration.
|
|
|
595
|
+ Self { block }
|
|
|
596
|
+ }
|
|
|
597
|
+
|
|
|
598
|
+ /// Starts receiving data.
|
|
|
599
|
+ ///
|
|
|
600
|
+ /// For I2S masters, this function starts the clock signals.
|
|
|
601
|
+ pub fn start(&mut self) {
|
|
|
602
|
+ // TODO
|
|
|
603
|
+ }
|
|
|
604
|
+
|
|
|
605
|
+ /// Stops receiving data.
|
|
|
606
|
+ pub fn stop(&mut self) {
|
|
|
607
|
+ // TODO
|
|
|
608
|
+ }
|
|
|
609
|
+}*/
|