Skip to content

Commit aa0d608

Browse files
committed
Don't consume gpiob and make frequency a parameter
1 parent 525293b commit aa0d608

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

examples/sdcard_test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use panic_halt as _;
88

99
use riscv_rt::entry;
1010
use longan_nano::hal::{pac, prelude::*};
11-
use longan_nano::{sdcard, sprint, sprintln};
11+
use longan_nano::{sdcard, sdcard_pins, sprint, sprintln};
1212

1313
#[entry]
1414
fn main() -> ! {
@@ -26,7 +26,8 @@ fn main() -> ! {
2626
longan_nano::stdout::configure(dp.USART0, gpioa.pa9, gpioa.pa10, 115_200.bps(), &mut afio, &mut rcu);
2727

2828
let gpiob = dp.GPIOB.split(&mut rcu);
29-
let mut sdcard = sdcard::configure(dp.SPI1, gpiob, &mut rcu);
29+
let sdcard_pins = sdcard_pins!(gpiob);
30+
let mut sdcard = sdcard::configure(dp.SPI1, sdcard_pins, sdcard::SdCardFreq::Safe, &mut rcu);
3031

3132
sprint!("Initializing SD card ... ");
3233
if let Err(_) = sdcard.device().init() {

src/sdcard.rs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
//! On-board SD Card Slot
22
33
use 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};
55
use gd32vf103xx_hal::gpio::{Alternate, Floating, Input, Output, PushPull};
66
use gd32vf103xx_hal::pac::{SPI1};
77
use gd32vf103xx_hal::rcu::Rcu;
88
use gd32vf103xx_hal::spi::{Spi, MODE_0};
9-
use gd32vf103xx_hal::time::U32Ext;
9+
use gd32vf103xx_hal::time::{Hertz, U32Ext};
1010
use 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+
1230
type SckPin = PB13<Alternate<PushPull>>;
1331
type MisoPin = PB14<Input<Floating>>;
1432
type MosiPin = PB15<Alternate<PushPull>>;
@@ -23,21 +41,39 @@ pub type SdCardSpi = SdMmcSpi<Spi1, CsPin>;
2341
/// A type based on embedded_sdmmc::Controller.
2442
pub 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

Comments
 (0)