SD/MMC SPI模式下命令合集

来源:互联网 发布:c语言函数调用的例子 编辑:程序博客网 时间:2024/04/28 00:36

NOTE:

1) class1,class3,class9:SPI模式不支持!
2) 继SD1.1之后又推出了SD2.0,主要特性是支持更大容量。SD1.1中卡容量存放于CSD寄存器中,
    而由于其规范中相关域的值较小,固最大只能表示2G地址。随着Nand容量的大大提高,
    SD1.1已经不适合潮流,因而推出了SD2.0。
    初始化流程大同小义,只需要CMD0之后再加上CMD8命令的识别。SD1.1不支持CMD8,
    而SD2.0的CMD8能读到卡的接口信息。如果卡响应CMD8为无效命令,则走SD1.1的流程,
    可能是SD1.1或MMC。如CMD8响应正确,则很可能是SD2.0了。

3) SD卡具有安全加密功能,内置128bit加密位,在加密状态下,用户需提供密码才可以访问卡内的数据。

    在卡上电时,若卡包含密码,卡自动进入锁定状态,读写命令均返回错误,以保护卡内容不被读出及修改。

    密码设置功能由CMD42实现,其数据包中包括该命令中所有的信息。

Byte Bit7 Bit6 Bit5 Bit4      Bit3             Bit2             Bit1           Bit0

0                            Erase Card     Lock/UnLock     ClrPwd       SetPwd

1             Psw_Length

2             Pwd Data
.
.
.

PwdLen+1

    擦除:此位置1时,卡的密码和内容会被强制擦除,在遗忘密码时可使用此功能。

    锁定/解锁:此位置1时,表示命令结束后状态为锁定,为0,表示卡解锁。

    清除密码:此位置1,表示清除卡的旧密码,此时数据中必须包含旧密码的正确内容。

    加密:此位置1,表示设置卡的新密码,数据中必须包含新密码内容;更改密码时,新密码紧跟随旧密码内容。
    在任意刻,主机可以通过CMD13命令读取卡的内部状态,判断其锁定状态。bit0置1表示卡处于锁定状态。

1、设置密码

使用CMD16设置Block长度为密码长度为PWD_LEN+2;

发送CMD42命令:0x6A,0x00,0x00,0x00,0x00,0x95;

发送命令数据:0x01,LEN,CMD_DATA0,CMD_DATA1……,0xFF,0xFF;

使用CMD16恢复原Block长度。

2、清除密码

使用CMD16设置Block长度为PWD_LEN+2;

发送CMD42命令:0x6A,0x00,0x00,0x00,0x00,0x95;

发送命令数据:0x02,LEN,CMD_DATA0,CMD_DATA1……,0xFF,0xFF;

使用CMD16恢复原Block长度。

3、卡的锁定、解锁

使用CMD16设置Block长度为PWD_LEN+2;

发送CMD42命令:0x6A,0x00,0x00,0x00,0x00,0x95;

发送命令数据:0x04,LEN,密码[LEN]……,0xFF,0xFF;

使用CMD16恢复原Block长度。

4、修改密码

使用CMD16设置Block长度为OLD_PWD_LEN+NEW_PWD_LEN+2;

发送CMD42命令:0x6A,0x00,0x00,0x00,0x00,0x95;

发送命令数据:0x05,LEN,旧密码[n],新密码[m]……,0xFF,0xFF;

使用CMD16恢复原Block长度。

5、卡擦除

使用CMD16设置Block长度为1;

发送CMD42命令:0x6A,0x00,0x00,0x00,0x00,0x95;

发送命令数据:0x08,0xFF,0xFF;

使用CMD16恢复原Block长度。

/******************************** Class0 : 基本命令集 Basic command set **************************/

/* 复位SD 卡 Reset cards to idle state */
#define CMD0 0
#define CMD0_R R1

/* 读OCR寄存器,不适用于SD卡 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

/***************************** Class2:块读命令集 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

/***************************** Class4:块写命令集 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

/*****************************Class6:写保护 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

/***************************** Class5:擦除命令 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

/***************************** class7:锁卡命令 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使能或禁止 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

/* 启动此卡的初始化过程 Activates the card’s initialization process */
#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

 

原创粉丝点击