SD卡学习(SDIO和SPI模式)

SD卡学习(SDIO和SPI模式)

** SD卡的操作模式:SD卡模式(SDIO)、SPI模式(默认为SD模式)。

其中SD卡模式的信号线有:CLK、CMD、DAT0-DAT3,6根线。

SPI模式的信号线有:CS、CLK、MISO(DATAOUT)、MOSI(DATAIN),4根线。

![](data:image/*;base64,iVBORw0KGgoAAAANSUhEUgAAAgEAAABCCAIAAABB15LdAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOxAAADsQBlSsOGwAAC0pJREFUeF7tnetyIzkIhSf7ZHn0vNlup6hhKXRDNJLo9vGPKcfR5fCBQJLHzp9/xePn50f++JrnD7IrudTM8qCtv2Cz8Tmu56CAg1OXCf+fP3iAAAiAAAh8KoGvqyyQ7V9fX58KAXaDAAiAwCcSuPL/13UqIdO/v78/kQFsBgEQAIFPJXDlf30O4GPBa5jQ+eYRdiWXmlketPUXbDY+x/UcFHBwahkkLAPvB7ym2MEQEAABEJgmgBowjQwdQAAEQOA1BJbUgOrby8P3nIcNXgMdhoAACIBAEgJLaoDRtivp00O159dRFYwk0QwEQAAEfARW1YBWflcq1f9M5aRPH2TwmYReIBBCAFuQEIwYJDmB+BpwrRz5UTSj/ZTxH5T38ycIYxk2Oii8WXJ54fa6B5TH4uNRl+GMfhbIcQLhAuJrwFSsH4/pKbXUuHp/5RhnaRdZiRNCTi6PvLzUQVODOzZVU+MbG0uvGbssasZAFo3fGlZtcPcHyQoBwTWAoMgtXus5UaaNP2fVR6RX3FNtXnj7p6OVtn/ezDMqJhn4wE0hARNcAzg/qid81VNNoGWvENs+eZAMS7TDX9b+bFITZpY892YZlBwMmN9vVvh7QEwYJ76MF1wDZkXsP0zNKnx6+8yRSrUfMTCMMb76OMsq1Q3ewcBOcl0RFQzBNUDd6gxVyv8XdLC8DxfhQxscXCcgFkUgz7rIoySK7ew4K67jpzTQnokeUe4IrgHVKyC7kfKcZe+FllUCgVESTni4OQif0TigXGP0TpWx47pmGTSssw4jzxIIfz88uAZIe2ZjV7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1Wq5Ui5d72pr9Wy65yOwWz6miL9+9T8s3i63VfbSqv8a6RqwK/ElUGSmKnyLOSKAGeGtDKL1OBxYO0zgHkzpzlgRdACIopbo7GZ0Xy7MMStV9nVduwZO6pT5ZZorJA9SjAe7V+tO/3mnLQuiyvsPQttfjLsXjtxroFeGrAJYtwKPqcrztqVCLoJ3oqDzSd28Kb3IfdqyiGvUIa0NQWMgdFtiytSkqoM8RTKwbZlvvKNKSmXuG1R8f2CnevG9NZAzgvy2jgHb1druzi6G6faGlLUn5kTdrtSiiyKimhTjvknS1PgUrotVModrp73Vz+GuDTJDet6p7Hcu2TP9X6sKAXCIBAfgLH888KAZ4aELXhVYcAPluUlxtcHlSURCkZBp9Cz/NuEzBUyBd01FLeER0XWZ7rq5KO6FTaqo5ued/iFHsbyywlohVJgTRn9hpT7QMx3pTafSRbWvzlG9nYK0qA5/MB6v6HMg7HjTSgvDesZihO+tXMVS0J9KLFx6ShHMQIumqOFMwN7k9hl8otiYDsWA5S+ku6rOq4Khy7vLK78tRQ0izMQG2teG4F+TCQprSpWfo5TmaBPrEpDTLDqoN7Ge1VV95h0ortajJpzT60d9igs/CNaaoFYWpqTphynd4UIMf8DSGVzmbX3tDZrWyiMpcq7EMZdo72lj5bAnsll5pZHrT14zAbn+N6Dgo4OHVZ2K5M67kLup/1KMXTXZAarfpiOaOx2X2pGAEEQAAEXkzgTA14MVCYBgIgAAIPIqDvgh4kHVJBAARAAATuEPi9k8ffE75DEH1BAARA4LkEfvM/XazT4/pZ/via5w+yK7nUzPKgrb9gs/E5rueggINTlwkf7wc8t4RDOQiAAAjcJfAftLp0jBngFzEAAAAASUVORK5CYII=)

SD卡只能使用3.3V的I/O电平。SPI模式下信号线要加10-100K的上拉电阻。

SD卡有五个寄存器:

名称 宽度 描述
CID 128 卡标识寄存器
RCA 16 相对地址寄存器(Relative Card Address):本地系统中卡的地址,动态变化,在主机初始化的时候确定,SPI模式中没有。
CSD 128 卡描述数据:卡操作条件相关的信息数据
SCR 64 SD配置寄存器:SD卡特定信息数据
OCR 32 操作条件寄存器

SD卡的命令格式:

命令CMD0就是0,CMD16就是16,以此类推。

SPI命令格式为6字节构成,高位在前(MSB)。

字节1 字节2-5 字节6
7 6 5-0
0 1 command

字节1的最高2位固定是01,低6位为命令号(CMD0就是0,CMD16就是16,以此类推),字节2-5为命令参数,有些命令是没有参数的;字节6的高7位为CRC,最低位恒为1。

SD卡的命令总共有12类,下表为几个比较重要的命令:
SD卡的主要相关各种命令解析
CMD0: SD卡进入IDLE和复位SD卡
CMD1: 读OCR,是否为SD卡
CMD2: 获取卡CID信息
CMD3: 获取SD卡所分配的相对地址
CMD9: 获取SD卡的存储信息(容量、块大小等)
CMD12:停止传输操作
CMD13:获取卡的状态
CMD16:设置SD卡块大小
CMD17:使SD卡进入传输状态、读取单个块
CMD18: 使SD卡进入传输状态、读取多个块,直到收到CMD12为止
CMD24:使SD卡进入传输状态、写入单个块
CMD25: 使SD卡进入传输状态、写入多个块

命令 参数 回应 描述
CMD0(0X00) NONE R1 复位SD卡
CMD8(0X08) VHS+Check Pattern R7 发送接口状态命令
CMD9(0X09) NONE R1 读取卡特定数据寄存器
CMD10(0X0A) NONE R1 读取卡标志数据寄存器
CMD16(0X10) 块大小 R1 设置块大小(字节数)
CMD17(0X11) 地址 R1 读取一个块的数据
CMD24(0X18) 地址 R1 写入一个块的数据
CMD41(0X29) NONE R3 发送给主机容量支持信息和激活卡初始化过程
CMD55(0X37) NONE R1 告诉SD卡,下一个是特定应用命令
CMD58(0X3A) NONE R3 读取OCR寄存器

SD卡和单片机的通信采用发送应答机制:

![](data:image/*;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCIfIiEmKzcvJik0KSEiMEExNDk7Pj4+JS5ESUM8SDc9Pjv/wAALCACTAVoBAREA/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwU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=)

R1响应格式
含义

初始化SD卡过程:

SPI操作模式下:在SD卡收到复位命令时,CS为有效电平(低电平),则SPI模式被启用,在发送CMD之前要先发送74个时钟,64个为内部供电上升时间,10个用于SD卡同步;之后才能开始CMD操作,在初始化时CLK时钟不能超过400KHz。

接着我们看看SD卡的初始化,** SD卡的典型初始化过程如下:**

1、初始化与SD卡连接的硬件条件(MCU的SPI配置,IO口配置);

2、上电延时(>74个CLK);

3、复位卡(CMD0),进入IDLE状态;

4、发送CMD8,检查是否支持2.0协议;

5、根据不同协议检查SD卡(命令包括:CMD55、CMD41、CMD58和CMD1等);

6、取消片选,发多8个CLK,结束初始化

这样我们就完成了对SD卡的初始化,注意末尾发送的8个CLK是提供SD卡额外的时钟,完成某些操作。通过SD卡初始化,我们可以知道SD卡的类型(V1、V2、V2HC或者MMC),在完成了初始化之后,就可以开始读写数据了。

SD卡读取数据,这里通过CMD17来实现,具体过程如下:

1、发送CMD17;

2、接收卡响应R1;

3、接收数据起始令牌0XFE;

4、接收数据;

5、接收2个字节的CRC,如果不使用CRC,这两个字节在读取后可以丢掉。

6、禁止片选之后,发多8个CLK;

以上就是一个典型的读取SD卡数据过程,SD卡的写于读数据差不多,写数据通过CMD24来实现,具体过程如下:

1、发送CMD24;

2、接收卡响应R1;

3、发送写数据起始令牌0XFE;

4、发送数据;

5、发送2字节的伪CRC;

6、禁止片选之后,发多8个CLK;

以上就是一个典型的写SD卡过程。关于SD卡的介绍,我们就介绍到这里,更详细的介绍请参考光盘SD卡的参考资料(SD卡2.0协议)。

这里写图片描述

你可能感兴趣的:(STM32)