5
5
use stm32_i2s_v12x:: { Instance , RegisterBlock } ;
6
6
7
7
use crate :: pac:: RCC ;
8
- use crate :: rcc:: { Enable , Reset } ;
8
+ use crate :: rcc;
9
9
use crate :: time:: Hertz ;
10
10
use crate :: { rcc:: Clocks , spi} ;
11
11
@@ -271,6 +271,10 @@ mod ws_pins {
271
271
}
272
272
}
273
273
274
+ pub trait I2sFreq {
275
+ fn i2s_freq ( clocks : & Clocks ) -> Hertz ;
276
+ }
277
+
274
278
/// Implements Instance for I2s<$SPIX, _> and creates an I2s::$spix function to create and enable
275
279
/// the peripheral
276
280
///
@@ -281,49 +285,63 @@ mod ws_pins {
281
285
/// to this SPI peripheral (i2s_cl, i2s_apb1_clk, or i2s2_apb_clk)
282
286
macro_rules! i2s {
283
287
( $SPIX: ty, $i2sx: ident, $clock: ident) => {
288
+ impl I2sFreq for $SPIX {
289
+ fn i2s_freq( clocks: & Clocks ) -> Hertz {
290
+ clocks
291
+ . $clock( )
292
+ . expect( "I2S clock input for SPI not enabled" )
293
+ }
294
+ }
295
+
284
296
impl <PINS > I2s <$SPIX, PINS >
285
297
where
286
298
PINS : Pins <$SPIX>,
287
299
{
288
- /// Creates an I2s object around an SPI peripheral and pins
289
- ///
290
- /// This function enables and resets the SPI peripheral, but does not configure it.
291
- ///
292
- /// The returned I2s object implements [stm32_i2s_v12x::Instance], so it can be used
293
- /// to configure the peripheral and communicate.
294
- ///
295
- /// # Panics
296
- ///
297
- /// This function panics if the I2S clock input (from the I2S PLL or similar)
298
- /// is not configured.
300
+ #[ deprecated( since = "0.10.0" , note = "Please use new instead" ) ]
299
301
pub fn $i2sx( spi: $SPIX, pins: PINS , clocks: Clocks ) -> Self {
300
- let input_clock = clocks
301
- . $clock( )
302
- . expect( "I2S clock input for SPI not enabled" ) ;
303
- unsafe {
304
- // NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
305
- let rcc = & ( * RCC :: ptr( ) ) ;
306
- // Enable clock, enable reset, clear, reset
307
- <$SPIX>:: enable( rcc) ;
308
- <$SPIX>:: reset( rcc) ;
309
- }
310
- I2s {
311
- _spi: spi,
312
- _pins: pins,
313
- input_clock,
314
- }
302
+ Self :: new( spi, pins, clocks)
315
303
}
316
304
}
317
305
impl PinMck <$SPIX> for NoMasterClock { }
318
- unsafe impl <PINS > Instance for I2s <$SPIX, PINS >
319
- where
320
- PINS : Pins <$SPIX>,
321
- {
306
+ unsafe impl <PINS > Instance for I2s <$SPIX, PINS > {
322
307
const REGISTERS : * mut RegisterBlock = <$SPIX>:: ptr( ) as * mut _;
323
308
}
324
309
} ;
325
310
}
326
311
312
+ impl < SPI , PINS > I2s < SPI , PINS >
313
+ where
314
+ SPI : I2sFreq + rcc:: Enable + rcc:: Reset ,
315
+ PINS : Pins < SPI > ,
316
+ {
317
+ /// Creates an I2s object around an SPI peripheral and pins
318
+ ///
319
+ /// This function enables and resets the SPI peripheral, but does not configure it.
320
+ ///
321
+ /// The returned I2s object implements [stm32_i2s_v12x::Instance], so it can be used
322
+ /// to configure the peripheral and communicate.
323
+ ///
324
+ /// # Panics
325
+ ///
326
+ /// This function panics if the I2S clock input (from the I2S PLL or similar)
327
+ /// is not configured.
328
+ pub fn new ( spi : SPI , pins : PINS , clocks : Clocks ) -> Self {
329
+ let input_clock = SPI :: i2s_freq ( & clocks) ;
330
+ unsafe {
331
+ // NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
332
+ let rcc = & ( * RCC :: ptr ( ) ) ;
333
+ // Enable clock, enable reset, clear, reset
334
+ SPI :: enable ( rcc) ;
335
+ SPI :: reset ( rcc) ;
336
+ }
337
+ I2s {
338
+ _spi : spi,
339
+ _pins : pins,
340
+ input_clock,
341
+ }
342
+ }
343
+ }
344
+
327
345
// Actually define the SPI instances that can be used for I2S
328
346
// Each one has to be split into two declarations because the F412, F413, F423, and F446
329
347
// have two different I2S clocks while other models have only one.
@@ -396,10 +414,7 @@ pub struct I2s<I, PINS> {
396
414
input_clock : Hertz ,
397
415
}
398
416
399
- impl < I , PINS > I2s < I , PINS >
400
- where
401
- PINS : Pins < I > ,
402
- {
417
+ impl < I , PINS > I2s < I , PINS > {
403
418
/// Returns the frequency of the clock signal that the SPI peripheral is receiving from the
404
419
/// I2S PLL or similar source
405
420
pub fn input_clock ( & self ) -> Hertz {
@@ -435,7 +450,6 @@ mod dma {
435
450
for stm32_i2s_v12x:: I2s < I2s < SPI , PINS > , MODE >
436
451
where
437
452
SPI : DMASet < STREAM , CHANNEL , DIR > ,
438
- PINS : Pins < SPI > ,
439
453
{
440
454
}
441
455
}
0 commit comments