#sdcard #sdio #sdhc #stm32f407

no-std sdio_sdhc

一个简单的驱动程序,用于通过SDIO驱动sdhc,没有文件系统,仅测试stm32f407板,其他stm32f4xx板未进行测试

5个版本

0.2.3 2020年10月16日
0.2.2 2020年9月6日
0.2.1 2020年7月9日
0.2.0 2020年6月3日
0.1.2 2020年5月16日

#651嵌入式开发

MIT 许可证

26KB
410

SDIO_SDHC

您可以在stm32f407板上驱动sdhc卡,其他stm32f4xx板未进行测试。如果您想测试其他板,可以编辑库和功能

stm32fxxx-hal = { version = "xxx", features = ["xxx"] }

使用该crate

首先您需要初始化一些GPIO,如下

pub fn gpio_init(
    rcc: &mut stm32::RCC,
    gpioc: &mut stm32::GPIOC,
    gpiod: &mut stm32::GPIOD,
) {
    // gpioc gpiod enable
    rcc.ahb1enr.modify(|_r, w| w.gpiocen().set_bit().gpioden().set_bit());

    gpioc.afrh.modify(|_r, w|
        w.afrh8().af12()
            .afrh9().af12()
            .afrh10().af12()
            .afrh11().af12()
            .afrh12().af12());
    gpiod.afrl.modify(|_r, w| w.afrl2().af12());

    gpioc.moder.modify(|_r, w|
        w.moder8().alternate()
            .moder9().alternate()
            .moder10().alternate()
            .moder11().alternate()
            .moder12().alternate());
    gpiod.moder.modify(|_r, w| w.moder2().alternate());

    gpioc.ospeedr.modify(|_r, w|
        w.ospeedr8().high_speed()
            .ospeedr9().high_speed()
            .ospeedr10().high_speed()
            .ospeedr11().high_speed()
            .ospeedr12().high_speed());
    gpiod.ospeedr.modify(|_r, w| w.ospeedr2().high_speed());

    gpioc.otyper.modify(|_r, w|
        w.ot8().push_pull()
            .ot9().push_pull()
            .ot10().push_pull()
            .ot11().push_pull()
            .ot12().push_pull());
    gpiod.otyper.modify(|_r, w| w.ot2().push_pull());

    gpioc.pupdr.modify(|_r, w|
        w.pupdr8().pull_up()
            .pupdr9().pull_up()
            .pupdr10().pull_up()
            .pupdr11().pull_up()
            .pupdr12().pull_up());
    gpiod.pupdr.modify(|_r, w| w.pupdr2().pull_up());
}

然后您就可以驱动您的sdhc卡,并进行一些测试

let card = Card::init().unwrap();
writeln!(USART1, "{:#?}", card).unwrap();
card.erase(0, card.capacity).unwrap();

let buf = [1; 512 * 2];
card.write_multi_blocks(&buf, 0, 2).unwrap();

let mut buf = [0; 512 * 2];
card.read_multi_blocks(&mut buf, 0, 2).unwrap();
writeln!(USART1, "{:?}", &buf[0..buf.len()]).unwrap();

let buf = [2; 512];
card.write_block(&buf, 512).unwrap();

let mut buf = [0; 512];
card.read_block(&mut buf, 512).unwrap();
writeln!(USART1, "{:?}", &buf[0..buf.len()]).unwrap();

将在您的ttl上打印如下

Card {
    capacity: 3963617280,
    block_size: 512,
    rca: 1,
}
[1, 1, 1, .......]
[2, 2, 2, .......]

如何支持fat32文件系统

您可以添加如下功能。访问fat32查看详细用法

sdio_sdhc = { version = "0.2", features = ["filesystem"] }

依赖

~53MB
~1.5M SLoC