源:SPI模式下MCU对SD卡的控制及操作命令

一、前言

SD 卡有两个可选的通讯协议:SD 模式和 SPI模式 SD 模式是SD 卡标准的读写方式,但是在选用SD 模式时,往往需要选择带有SD 卡控制器接口的 MCU,或者必须加入额外的SD卡控制单元以支持SD 卡的读写 然而,大多数MCU都没有集成SD 卡控制器接口,若选用SD 模式通讯就无形中增加了产品的硬件成本。在SD卡数据读写时间要求不是很严格的情况下, 选用 SPI模式可以说是一种最佳的解决方案 因为在 SPI模式下,通过四条线就可以完成所有的数据交换,并且目前市场上很多MCU都集成 有现成的SPI接口电路,采用 SPI模式对 SD卡进行读写操作可大大简化硬件电路的设计

二、硬件电路实现

以NXP的LPC2210 ARM7MCU为例,下图是周立功开发的实现板电路

这里,将LPC2210MCU的SPI0用于SD卡的控制和数据读写。对SPI0的两个数据线加了上拉电阻以便于MMC卡兼容。

卡供电采用了可控方式,通过GPIO口控制MOS管对其进行供电。

卡检测电路也使用GPIO口实现。通过读GPIO口数据,检查卡是否写保护和完全插入。

具体内容可以参考周立功的说明书,百度文库里边有

三、SD卡物理接口

我们看到的SD卡一包如下所示,包含9个引脚和一个写保护开关:

其引脚定义如下:

注:1.  S:电源;I:输入;O:推挽输出;PP:推挽I/O。         2.  扩展的DAT线(DAT1 ~ DAT3)在上电后处于输入状态。它们在执行SET_BUS_WIDTH命令后作为DAT线操作。当不使用DAT1 ~ DAT3 线时,主机应使自己的DAT1~DAT3线处于输入模式。这样定义是为了与MMC卡保持兼容。

3.  上电后,这条线为带 50KΩ上拉电阻的输入线(可以用于检测卡是否存在或选择 SPI 模式) 。用户可以在正常的数据传输中用 SET_CLR_CARD_DETECT(ACMD42)命令断开上拉电阻的连接。MMC卡的该引脚在SD模式下为保留引脚,在SD模式下无任何作用。  4.  MMC卡在SD模式下为:I/O/PP/OD。  5.  MMC卡在SPI模式下为:I/PP。

四、对SD卡的控制流程

1、SD卡的SPI工作模式

SD 卡在上电初期自动进入SD 总线模式,在此模式下向 SD 卡发送复位命令CMD0 。如果SD卡在接收复位命令过程中CS低电平有效,则进入SPI模式,否则工作在SD 总线模式。

下边是插入SD卡,并初始化为SPI模式的流程图:(至于CMD××究竟是什么样的命令,本文最后会附上

在复位成功之后可以通过CMD55和ACMD41 判断当前电压是否在工作范围内 主机还可以继续通过CMD10读取SD 卡的CID寄存器,通过CMD16 设置数据 Block长度,通过CMD9 读取卡的 CSD寄存器 从CSD 寄存器中,主机可获知卡容量,支持的命令集等重要参数。

2、数据块的读写

完成SD 卡的初始化之后即可进行它的读写操作 SD卡的读写操作都是通过发送 SD 卡命令完成的SPI总线模式支持单块(CMD24)和多块(CMD25)写操作,多块操作是指从指定位置开始写下去,直到SD 卡收到一个停止命令CMD12才停止 单块写操作的数据块长度只能是512 字节 单块写入时,命令为CMD24,当应答为0时说明可以写入数据,大小为512 字节 SD 卡对每个发送给自己的数据块都通过一个应答命令确认,它为1个字节长,当低 5位为00101 时,表明数据块被正确写入SD 卡     在需要读取SD 卡中数据的时候,读SD卡的命令字为CMD17,接收正确的第一个响应命令字节为0xFE,随后是512 个字节的用户数据块,最后为2 个字节的CRC验证码    可见,读写SD 卡的操作都是在初始化后基于 SD 卡命令和响应完成操作的,写、读 SD 卡的程序流程图如下所示  :

(1)写SD卡流程

(2)读SD卡流程

五、SD卡的操作命令集合

对SD卡的操作就靠这些命令来实现的。一下命令来自周立功的SD/MMC中间件。我查了好多地方都只显示CMD0、CMD1之类的东西,而没人说这些东西是什么。就贴到这里供参考,这也是我写这个博客的目的,因为这些命令我找了好久的。关于SD,我一句操作代码都没贴。。。

/* 命令响应定义 define command's response */
#define R1 1
#define R1B 2
#define R2 3
#define R3 4 /********************************************** SD卡SPI模式下命令集 **********************************************/ /******************************** 基本命令集 Basic command set **************************/
/* 复位SD 卡 Reset cards to idle state */
#define CMD0 0
#define CMD0_R R1 /* 读OCR寄存器 Read the OCR (MMC mode, do not use for SD cards) */
#define CMD1 1
#define CMD1_R R1 /* 读CSD寄存器 Card sends the CSD */
#define CMD9 9
#define CMD9_R R1 /* 读CID寄存器 Card sends CID */
#define CMD10 10
#define CMD10_R R1 /* 停止读多块时的数据传输 Stop a multiple block (stream) read/write operation */
#define CMD12 12
#define CMD12_R R1B /* 读 Card_Status 寄存器 Get the addressed card's status register */
#define CMD13 13
#define CMD13_R R2 /***************************** 块读命令集 Block read commands **************************/ /* 设置块的长度 Set the block length */
#define CMD16 16
#define CMD16_R R1 /* 读单块 Read a single block */
#define CMD17 17
#define CMD17_R R1 /* 读多块,直至主机发送CMD12为止 Read multiple blocks until a CMD12 */
#define CMD18 18
#define CMD18_R R1 /***************************** 块写命令集 Block write commands *************************/
/* 写单块 Write a block of the size selected with CMD16 */
#define CMD24 24
#define CMD24_R R1 /* 写多块 Multiple block write until a CMD12 */
#define CMD25 25
#define CMD25_R R1 /* 写CSD寄存器 Program the programmable bits of the CSD */
#define CMD27 27
#define CMD27_R R1 /***************************** 写保护 Write protection *****************************/
/* Set the write protection bit of the addressed group */
#define CMD28 28
#define CMD28_R R1B /* Clear the write protection bit of the addressed group */
#define CMD29 29
#define CMD29_R R1B /* Ask the card for the status of the write protection bits */
#define CMD30 30
#define CMD30_R R1 /***************************** 擦除命令 Erase commands *******************************/
/* 设置擦除块的起始地址(只用于SD卡) Set the address of the first write block to be erased(only for SD) */
#define CMD32 32
#define CMD32_R R1 /* 设置擦除块的终止地址(只用于SD卡) Set the address of the last write block to be erased(only for SD) */
#define CMD33 33
#define CMD33_R R1 /* 设置擦除块的起始地址(只用于MMC卡) Set the address of the first write block to be erased(only for MMC) */
#define CMD35 35
#define CMD35_R R1 /* 设置擦除块的终止地址(只用于MMC卡) Set the address of the last write block to be erased(only for MMC) */
#define CMD36 36
#define CMD36_R R1 /* 擦除所选择的块 Erase the selected write blocks */
#define CMD38 38
#define CMD38_R R1B /***************************** 锁卡命令 Lock Card commands ***************************/
/* 设置/复位密码或上锁/解锁卡 Set/reset the password or lock/unlock the card */
#define CMD42 42
#define CMD42_R R1B
/* Commands from 42 to 54, not defined here */ /***************************** 应用命令 Application-specific commands ****************/
/* 禁止下一个命令为应用命令 Flag that the next command is application-specific */
#define CMD55 55
#define CMD55_R R1 /* 应用命令的通用I/O General purpose I/O for application-specific commands */
#define CMD56 56
#define CMD56_R R1 /* 读OCR寄存器 Read the OCR (SPI mode only) */
#define CMD58 58
#define CMD58_R R3 /* 使能或禁止 CRC Turn CRC on or off */
#define CMD59 59
#define CMD59_R R1 /***************************** 应用命令 Application-specific commands ***************/
/* 获取 SD Status寄存器 Get the SD card's status */
#define ACMD13 13
#define ACMD13_R R2 /* 得到已写入卡中的块的个数 Get the number of written write blocks (Minus errors ) */
#define ACMD22 22
#define ACMD22_R R1 /* 在写之前,设置预先擦除的块的个数 Set the number of write blocks to be pre-erased before writing */
#define ACMD23 23
#define ACMD23_R R1 /* 读取OCR寄存器 Get the card's OCR (SD mode) */
#define ACMD41 41
#define ACMD41_R R1 /* 连接/断开CD/DATA[3]引脚上的上拉电阻 Connect or disconnect the 50kOhm internal pull-up on CD/DAT[3] */
#define ACMD42 42
#define ACMD42_R R1 /* 读取SCR寄存器 Get the SD configuration register */
#define ACMD51 51
#define ACMD51_R R1

SPI模式下MCU对SD卡的控制及操作命令(转)的更多相关文章

  1. SPI模式下MCU对SD卡的控制及操作命令

    一.前言 SD 卡有两个可选的通讯协议:SD 模式和 SPI模式 SD 模式是SD 卡标准的读写方式,但是在选用SD 模式时,往往需要选择带有SD 卡控制器接口的 MCU,或者必须加入额外的SD卡控制 ...

  2. 如何在Ubuntu下使用TF/SD 卡制作Exynos 4412 u-boot启动盘

    /** ****************************************************************************** * @author    Maox ...

  3. 如何在Mac下使用TF/SD 卡制作Exynos 4412 u-boot启动盘

    /** ****************************************************************************** * @author    Maox ...

  4. 模拟器下的虚拟sd卡添加文件

    1.若出现mkdir failed for myData Read-only file system,在执行 adb shell 命令后,执行mount -o remount ,rw / (去除文件的 ...

  5. linux下如何获取sd卡中的mbr

    答:使用dd命令,示例如下: dd if=/dev/mmcblk0 of=mbr.bin bs=512 count=1 解析: bs表示指定输入输出的块大小为512个字节 count表示指定读取输入的 ...

  6. [FatFs 学习] SD卡总结-SPI模式

    SD卡为移动设备提供了安全的,大容量存储解决方法.它本身可以通过两种总线模式和MCU进行数据传输,一种是称为SD BUS的4位串行数据模式,另一种就是大家熟知的4线SPI Bus模式.一些廉价,低端的 ...

  7. SD卡 模拟SPI总线控制流程

    SD卡为移动设备提供了安全的,大容量存储解决方法.它本身可以通过两种总线模式和MCU进行数据传输,一种是称为SD BUS的4位串行数据模式,另一种就是大家熟知的4线SPI Bus模式.一些廉价,低端的 ...

  8. MicroSD卡(TF卡)SPI模式实现方法

    现在我们手机的内存卡多为Micro SD卡,又叫TF卡,所以Micro SD卡比SD卡常见.自己曾经也想写写SD卡的读取程序,但又不想特地再去买个SD卡,这时想起手机内存卡不是和SD卡很像吗?在网上查 ...

  9. CE6.0 下获得 SD 卡序列号的方法

    经常在坛子里看到讨论软件加密的帖子,纯软件加密与读取硬件序列号加密是经常讨论到的. 两种方法各有优缺点. 在通过读取硬件序列号的方法来加密的方法,受硬件的限制. 一般来说,CPU和T-Flash可能存 ...

随机推荐

  1. java 时间

    package com.grace.test; import java.text.DateFormat; import java.text.ParseException; import java.te ...

  2. sql第二天

    --基本格式 select * from tblclass --对于列进行限制 --格式一:取指定列 select cid,cname from TblClass select cname from ...

  3. [帖子收集]通用Windows平台(UWP)

    通用Windows平台,universal windows platform,UWP 什么是通用 Windows 平台 (UWP) 应用?(微软MSDN) 如何在通用 Windows 平台应用中使用现 ...

  4. FileReader和FileInputStream的区别

    1.FileReader和FileInputStream的区别: FileReader.FileWriter处理的是文本文件. FileInputStream/FileOutputStream是继承I ...

  5. codefirst mvc Self referencing loop detected for property

    登录时,json序列化用户类时提示错误"Self referencing loop detected for property--",经过5个小时的查找,发现原因可能是,用户类包含 ...

  6. rsync 断点续传

    # rsync -avzP file root@172.20.7.219:/root/tmp           (我这里file指要传送的文件)

  7. jquery正则常用的

    jQuery.validator.addMethod("mobilePhone",function(value,element){ return this.optional(ele ...

  8. php分类

    <?php /* * PHP分页类 * @package Page * @Created 2013-03-27 * @Modify 2013-03-27 * @link http://www.6 ...

  9. OvS: ovs-ofctl adding parameters analysis

    if using dpdk, then OvS' datapath folder is ignored. Only OvS' userspace code are in use. According ...

  10. hdfs格式化hadoop namenode -format错误

    在对HDFS格式化,执行hadoop namenode -format命令时,出现未知的主机名的问题,异常信息如下所示: [shirdrn@localhost bin]$ hadoop namenod ...