11//! On-board SD Card Slot
22
33use embedded_hal:: digital:: v2:: OutputPin ;
4- use gd32vf103xx_hal:: gpio:: gpiob:: { PB12 , PB13 , PB14 , PB15 , Parts } ;
4+ use gd32vf103xx_hal:: gpio:: gpiob:: { PB12 , PB13 , PB14 , PB15 } ;
55use gd32vf103xx_hal:: gpio:: { Alternate , Floating , Input , Output , PushPull } ;
66use gd32vf103xx_hal:: pac:: { SPI1 } ;
77use gd32vf103xx_hal:: rcu:: Rcu ;
88use gd32vf103xx_hal:: spi:: { Spi , MODE_0 } ;
9- use gd32vf103xx_hal:: time:: U32Ext ;
9+ use gd32vf103xx_hal:: time:: { Hertz , U32Ext } ;
1010use embedded_sdmmc:: { Controller , SdMmcSpi , TimeSource , Timestamp } ;
1111
12+ /// Sets up all the needed GPIO pins for the sdcard
13+ ///
14+ /// ```
15+ /// let gpiob = dp.GPIOB.split(&mut rcu);
16+ /// let sdcard_pins = sdcard_pins!(gpiob);
17+ /// ```
18+ #[ macro_export]
19+ macro_rules! sdcard_pins {
20+ ( $gpiob: ident) => { {
21+ $crate:: sdcard:: SdCardPins {
22+ miso: $gpiob. pb14. into_floating_input( ) ,
23+ mosi: $gpiob. pb15. into_alternate_push_pull( ) ,
24+ sck: $gpiob. pb13. into_alternate_push_pull( ) ,
25+ cs: $gpiob. pb12. into_push_pull_output( ) ,
26+ }
27+ } } ;
28+ }
29+
1230type SckPin = PB13 < Alternate < PushPull > > ;
1331type MisoPin = PB14 < Input < Floating > > ;
1432type MosiPin = PB15 < Alternate < PushPull > > ;
@@ -23,21 +41,39 @@ pub type SdCardSpi = SdMmcSpi<Spi1, CsPin>;
2341/// A type based on embedded_sdmmc::Controller.
2442pub type SdCard = Controller < SdCardSpi , FakeTimeSource > ;
2543
44+ pub struct SdCardPins {
45+ pub miso : MisoPin ,
46+ pub mosi : MosiPin ,
47+ pub sck : SckPin ,
48+ pub cs : CsPin ,
49+ }
50+
51+ pub enum SdCardFreq {
52+ /// Should work for all cards
53+ Safe ,
54+ /// May not work for some cards
55+ Fast ,
56+ /// Specify SPI frequency
57+ Custom ( Hertz )
58+ }
59+
2660/// Constructs SD Card driver from the required components.
27- pub fn configure ( spi : SPI1 , gpiob : Parts , rcu : & mut Rcu ) -> SdCard {
28- let miso = gpiob. pb14 . into_floating_input ( ) ;
29- let mosi = gpiob. pb15 . into_alternate_push_pull ( ) ;
30- let sck = gpiob. pb13 . into_alternate_push_pull ( ) ;
31- let mut cs = gpiob. pb12 . into_push_pull_output ( ) ;
61+ pub fn configure ( spi : SPI1 , pins : SdCardPins , freq : SdCardFreq , rcu : & mut Rcu ) -> SdCard {
62+ let freq = match freq {
63+ SdCardFreq :: Safe => 200 . khz ( ) . into ( ) , // using 300 kHz here because the sdcard init needs 100 to 400 kHz (see SdMmcSpi.init)
64+ SdCardFreq :: Fast => 27 . mhz ( ) . into ( ) , // this is the max SPI frequency according to datasheet
65+ SdCardFreq :: Custom ( val) => val,
66+ } ;
3267
3368 let spi1 = Spi :: spi1 (
3469 spi,
35- ( sck, miso, mosi) ,
70+ ( pins . sck , pins . miso , pins . mosi ) ,
3671 MODE_0 ,
37- 300 . khz ( ) , // using 300 kHz here because the sdcard init needs 100 to 400 kHz (see SdMmcSpi.init)
72+ freq ,
3873 rcu,
3974 ) ;
4075
76+ let mut cs = pins. cs ;
4177 cs. set_high ( ) . unwrap ( ) ;
4278
4379 let sdmmcspi = SdMmcSpi :: new ( spi1, cs) ;
0 commit comments