@@ -186,59 +186,74 @@ pub struct Sai<SAI, INTERFACE> {
186
186
187
187
#[ cfg( feature = "rm0455" ) ]
188
188
#[ allow( clippy:: upper_case_acronyms) ]
189
- pub ( crate ) type SAI < const A : usize > = stm32h7:: Periph < crate :: stm32:: sai1:: RegisterBlock , A > ;
189
+ pub ( crate ) type SAI < const A : usize > =
190
+ stm32h7:: Periph < crate :: stm32:: sai1:: RegisterBlock , A > ;
190
191
191
192
#[ cfg( not( feature = "rm0455" ) ) ]
192
193
#[ allow( clippy:: upper_case_acronyms) ]
193
- pub ( crate ) type SAI < const A : usize > = stm32h7:: Periph < crate :: stm32:: sai4:: RegisterBlock , A > ;
194
+ pub ( crate ) type SAI < const A : usize > =
195
+ stm32h7:: Periph < crate :: stm32:: sai4:: RegisterBlock , A > ;
194
196
195
197
// Common to all interfaces
196
- impl < INTERFACE , const A : usize > Sai < SAI < A > , INTERFACE >
197
- where SAI < A > : GetClkSAI
198
+ impl < INTERFACE , const A : usize > Sai < SAI < A > , INTERFACE >
199
+ where
200
+ SAI < A > : GetClkSAI ,
198
201
{
199
202
/// Low level RCC initialisation
200
- fn sai_rcc_init ( & mut self , prec : <SAI < A > as GetClkSAI >:: Rec )
201
- {
203
+ fn sai_rcc_init ( & mut self , prec : <SAI < A > as GetClkSAI >:: Rec ) {
202
204
let _ = prec. enable ( ) . reset ( ) ; // drop, can be recreated by free method
203
205
}
204
206
205
207
/// Access to the current master channel
206
208
fn master_channel < F , T > ( & self , func : F ) -> T
207
- where F : FnOnce ( & CH ) -> T ,
209
+ where
210
+ F : FnOnce ( & CH ) -> T ,
208
211
{
209
212
func ( self . rb . ch ( self . master_channel as usize ) )
210
213
}
211
214
212
215
/// Access to the current slave channel, if set
213
216
fn slave_channel < F , T > ( & self , func : F ) -> Option < T >
214
- where F : FnOnce ( & CH ) -> T ,
217
+ where
218
+ F : FnOnce ( & CH ) -> T ,
215
219
{
216
- self . slave_channel . map ( |channel| func ( self . rb . ch ( channel as usize ) ) )
220
+ self . slave_channel
221
+ . map ( |channel| func ( self . rb . ch ( channel as usize ) ) )
217
222
}
218
223
219
224
/// Start listening for `event` on a given `channel`
220
225
pub fn listen ( & mut self , channel : SaiChannel , event : Event ) {
221
226
let ch = & self . rb . ch ( channel as usize ) ;
222
227
match event {
223
- Event :: Overdue => ch. im ( ) . modify ( |_, w| w. ovrudrie ( ) . set_bit ( ) ) ,
224
- Event :: Muted => ch. im ( ) . modify ( |_, w| w. mutedetie ( ) . set_bit ( ) ) ,
225
- Event :: WrongClock => ch. im ( ) . modify ( |_, w| w. wckcfgie ( ) . set_bit ( ) ) ,
226
- Event :: Data => ch. im ( ) . modify ( |_, w| w. freqie ( ) . set_bit ( ) ) ,
227
- Event :: AnticipatedFrameSync => ch. im ( ) . modify ( |_, w| w. afsdetie ( ) . set_bit ( ) ) ,
228
- Event :: LateFrameSync => ch. im ( ) . modify ( |_, w| w. lfsdetie ( ) . set_bit ( ) ) ,
228
+ Event :: Overdue => ch. im ( ) . modify ( |_, w| w. ovrudrie ( ) . set_bit ( ) ) ,
229
+ Event :: Muted => ch. im ( ) . modify ( |_, w| w. mutedetie ( ) . set_bit ( ) ) ,
230
+ Event :: WrongClock => ch. im ( ) . modify ( |_, w| w. wckcfgie ( ) . set_bit ( ) ) ,
231
+ Event :: Data => ch. im ( ) . modify ( |_, w| w. freqie ( ) . set_bit ( ) ) ,
232
+ Event :: AnticipatedFrameSync => {
233
+ ch. im ( ) . modify ( |_, w| w. afsdetie ( ) . set_bit ( ) )
234
+ }
235
+ Event :: LateFrameSync => {
236
+ ch. im ( ) . modify ( |_, w| w. lfsdetie ( ) . set_bit ( ) )
237
+ }
229
238
} ;
230
239
}
231
240
232
241
/// Stop listening for `event` on a given `channel`
233
242
pub fn unlisten ( & mut self , channel : SaiChannel , event : Event ) {
234
243
let ch = & self . rb . ch ( channel as usize ) ;
235
244
match event {
236
- Event :: Overdue => ch. im ( ) . modify ( |_, w| w. ovrudrie ( ) . clear_bit ( ) ) ,
237
- Event :: Muted => ch. im ( ) . modify ( |_, w| w. mutedetie ( ) . clear_bit ( ) ) ,
238
- Event :: WrongClock => ch. im ( ) . modify ( |_, w| w. wckcfgie ( ) . clear_bit ( ) ) ,
239
- Event :: Data => ch. im ( ) . modify ( |_, w| w. freqie ( ) . clear_bit ( ) ) ,
240
- Event :: AnticipatedFrameSync => ch. im ( ) . modify ( |_, w| w. afsdetie ( ) . clear_bit ( ) ) ,
241
- Event :: LateFrameSync => ch. im ( ) . modify ( |_, w| w. lfsdetie ( ) . clear_bit ( ) ) ,
245
+ Event :: Overdue => ch. im ( ) . modify ( |_, w| w. ovrudrie ( ) . clear_bit ( ) ) ,
246
+ Event :: Muted => ch. im ( ) . modify ( |_, w| w. mutedetie ( ) . clear_bit ( ) ) ,
247
+ Event :: WrongClock => {
248
+ ch. im ( ) . modify ( |_, w| w. wckcfgie ( ) . clear_bit ( ) )
249
+ }
250
+ Event :: Data => ch. im ( ) . modify ( |_, w| w. freqie ( ) . clear_bit ( ) ) ,
251
+ Event :: AnticipatedFrameSync => {
252
+ ch. im ( ) . modify ( |_, w| w. afsdetie ( ) . clear_bit ( ) )
253
+ }
254
+ Event :: LateFrameSync => {
255
+ ch. im ( ) . modify ( |_, w| w. lfsdetie ( ) . clear_bit ( ) )
256
+ }
242
257
} ;
243
258
let _ = ch. im ( ) . read ( ) ;
244
259
let _ = ch. im ( ) . read ( ) ; // Delay 2 peripheral clocks
@@ -250,12 +265,14 @@ impl<INTERFACE, const A: usize> Sai<SAI<A>, INTERFACE>
250
265
pub fn clear_irq ( & mut self , channel : SaiChannel , event : Event ) {
251
266
let ch = & self . rb . ch ( channel as usize ) ;
252
267
match event {
253
- Event :: Overdue => ch. clrfr ( ) . write ( |w| w. covrudr ( ) . set_bit ( ) ) ,
254
- Event :: Muted => ch. clrfr ( ) . write ( |w| w. cmutedet ( ) . set_bit ( ) ) ,
255
- Event :: WrongClock => ch. clrfr ( ) . write ( |w| w. cwckcfg ( ) . set_bit ( ) ) ,
256
- Event :: Data => 0 , // Cleared by reading/writing data
257
- Event :: AnticipatedFrameSync => ch. clrfr ( ) . write ( |w| w. cafsdet ( ) . set_bit ( ) ) ,
258
- Event :: LateFrameSync => ch. clrfr ( ) . write ( |w| w. clfsdet ( ) . set_bit ( ) ) ,
268
+ Event :: Overdue => ch. clrfr ( ) . write ( |w| w. covrudr ( ) . set_bit ( ) ) ,
269
+ Event :: Muted => ch. clrfr ( ) . write ( |w| w. cmutedet ( ) . set_bit ( ) ) ,
270
+ Event :: WrongClock => ch. clrfr ( ) . write ( |w| w. cwckcfg ( ) . set_bit ( ) ) ,
271
+ Event :: Data => 0 , // Cleared by reading/writing data
272
+ Event :: AnticipatedFrameSync => {
273
+ ch. clrfr ( ) . write ( |w| w. cafsdet ( ) . set_bit ( ) )
274
+ }
275
+ Event :: LateFrameSync => ch. clrfr ( ) . write ( |w| w. clfsdet ( ) . set_bit ( ) ) ,
259
276
} ;
260
277
let _ = ch. sr ( ) . read ( ) ;
261
278
let _ = ch. sr ( ) . read ( ) ; // Delay 2 peripheral clocks
@@ -274,13 +291,19 @@ impl<INTERFACE, const A: usize> Sai<SAI<A>, INTERFACE>
274
291
/// Mute `channel`, this is checked at the start of each frame
275
292
/// Meaningful only in Tx mode
276
293
pub fn mute ( & mut self , channel : SaiChannel ) {
277
- self . rb . ch ( channel as usize ) . cr2 ( ) . modify ( |_, w| w. mute ( ) . enabled ( ) ) ;
294
+ self . rb
295
+ . ch ( channel as usize )
296
+ . cr2 ( )
297
+ . modify ( |_, w| w. mute ( ) . enabled ( ) ) ;
278
298
}
279
299
280
300
/// Unmute `channel`, this is checked at the start of each frame
281
301
/// Meaningful only in Tx mode
282
302
pub fn unmute ( & mut self , channel : SaiChannel ) {
283
- self . rb . ch ( channel as usize ) . cr2 ( ) . modify ( |_, w| w. mute ( ) . disabled ( ) ) ;
303
+ self . rb
304
+ . ch ( channel as usize )
305
+ . cr2 ( )
306
+ . modify ( |_, w| w. mute ( ) . disabled ( ) ) ;
284
307
}
285
308
286
309
/// Used to operate the audio block(s) with an external SAI for synchronization
@@ -296,42 +319,46 @@ impl<INTERFACE, const A: usize> Sai<SAI<A>, INTERFACE>
296
319
/// Synchronization output for other SAI blocks
297
320
pub fn set_sync_output ( & mut self , channel : Option < SaiChannel > ) {
298
321
match channel {
299
- Some ( SaiChannel :: ChannelA ) => unsafe { & self . rb . gcr ( ) . modify ( |_, w| w. syncout ( ) . bits ( 0b01 ) ) } ,
300
- Some ( SaiChannel :: ChannelB ) => unsafe { & self . rb . gcr ( ) . modify ( |_, w| w. syncout ( ) . bits ( 0b10 ) ) } ,
301
- None => unsafe { & self . rb . gcr ( ) . modify ( |_, w| w. syncout ( ) . bits ( 0b00 ) ) } ,
322
+ Some ( SaiChannel :: ChannelA ) => unsafe {
323
+ & self . rb . gcr ( ) . modify ( |_, w| w. syncout ( ) . bits ( 0b01 ) )
324
+ } ,
325
+ Some ( SaiChannel :: ChannelB ) => unsafe {
326
+ & self . rb . gcr ( ) . modify ( |_, w| w. syncout ( ) . bits ( 0b10 ) )
327
+ } ,
328
+ None => unsafe {
329
+ & self . rb . gcr ( ) . modify ( |_, w| w. syncout ( ) . bits ( 0b00 ) )
330
+ } ,
302
331
} ;
303
332
}
304
333
305
334
/// Enable DMA for the SAI peripheral.
306
335
pub fn enable_dma ( & mut self , channel : SaiChannel ) {
307
- self . rb . ch ( channel as usize ) . cr1 ( ) . modify ( |_, w| w. dmaen ( ) . enabled ( ) ) ;
336
+ self . rb
337
+ . ch ( channel as usize )
338
+ . cr1 ( )
339
+ . modify ( |_, w| w. dmaen ( ) . enabled ( ) ) ;
308
340
}
309
341
310
342
/// Releases the SAI peripheral
311
343
pub fn free ( self ) -> ( SAI < A > , <SAI < A > as GetClkSAI >:: Rec ) {
312
344
// Refer to RM0433 Rev 7 51.4.15 Disabling the SAI
313
345
314
346
// Master: Clear SAIEN
315
- self . master_channel ( |ch| {
316
- ch. cr1 ( ) . modify ( |_, w| w. saien ( ) . disabled ( ) )
317
- } ) ;
347
+ self . master_channel ( |ch| ch. cr1 ( ) . modify ( |_, w| w. saien ( ) . disabled ( ) ) ) ;
318
348
319
349
// Master: Wait for SAI to clear at the end of the
320
350
// frame
321
- while self . master_channel ( |ch| {
322
- ch. cr1 ( ) . read ( ) . saien ( ) . bit_is_set ( )
323
- } ) { }
351
+ while self . master_channel ( |ch| ch. cr1 ( ) . read ( ) . saien ( ) . bit_is_set ( ) ) { }
324
352
325
353
// Slave: Clear SAIEN
326
- self . slave_channel ( |ch| {
327
- ch. cr1 ( ) . modify ( |_, w| w. saien ( ) . disabled ( ) )
328
- } ) ;
354
+ self . slave_channel ( |ch| ch. cr1 ( ) . modify ( |_, w| w. saien ( ) . disabled ( ) ) ) ;
329
355
330
356
// Slave: Wait for SAI to clear
331
- while self . slave_channel ( |ch| {
332
- ch. cr1 ( ) . read ( ) . saien ( ) . bit_is_set ( )
333
- } ) . unwrap_or ( false ) { }
357
+ while self
358
+ . slave_channel ( |ch| ch. cr1 ( ) . read ( ) . saien ( ) . bit_is_set ( ) )
359
+ . unwrap_or ( false )
360
+ { }
334
361
335
362
( self . rb , unsafe { <SAI < A > as GetClkSAI >:: Rec :: new ( ) } )
336
363
}
337
- }
364
+ }
0 commit comments