TF 卡检测&初始化通讯命令
一、MMC通讯命令格式如下:
host->mmc:发送命令格式如下:
| 描述 | bit位 | 长度(bit) | 默认值 |
|---|---|---|---|
| 起始位 | 47 | 1 | 0 |
| 传输位 | 46 | 1 | 1 |
| 命令码 | [45-40] | 6 | 如:CMD41=101001b |
| 参数 | [39-8] | 32 | 如:0x51040000 |
| CRC7 | [7-1] | 7 | 校验值 |
| 结束位 | 0 | 1 | 1 |
如日志:mmc1 :mmc_start_request starting CMD41 arg 51040000 flags 000000e1,其中CMD41 是命令码,51040000 为参数
二、mmc->host response分为:R1、R1b、R2、R3、R6、R7
R1 response(48bit):CMD11、CMD13、CMD16、CMD17、CMD18、CMD19、CMD22、CMD23、CMD30、CMD32、CMD33、CMD40、CMD42、CMD55、CMD56
| 描述 | bit位 | 长度(bit) | 默认值 |
|---|---|---|---|
| 起始位 | 47 | 1 | 0 |
| 传输位 | 46 | 1 | 0 |
| 命令码 | [45-40] | 6 | 如:CMD41=101001b |
| 卡状态 | [39-8] | 32 | 见卡状态表 |
| CRC7 | [7-1] | 7 | 校验值 |
| 结束位 | 0 | 1 | 1 |
R2 response(136bit):CMD2(CID)、CMD10(CID)、CMD9(CSD)
| 描述 | bit位 | 长度(bit) | 默认值 |
|---|---|---|---|
| 起始位 | 135 | 1 | 0 |
| 传输位 | 134 | 1 | 0 |
reserved | [133-128] | 6 | 111111b |
CID or CSD register incl. internal CRC7 | [127-1] | 127 | [127-120]:Manufactuer ID(MID) 由jedec组织分配 [119-104]:OEM/Application ID(OID) [103-64]:Product name(PNM) [63-56]:Product revision(PRV) [55-24]:Product serial number(PSN) [23-20]:reserved [19-8]:Manufacturing date(MDT) [7-1]:CRC7 checksum [0]:not used,always 1 |
| 结束位 | 0 | 1 | 1 |
例如:[Mon Dec 8 07:44:51 2025] mmc1: req done (CMD2): 0: ad4c5355 53443030 1035893d b1719700
R3 response(48bit):ACMD41
| 描述 | bit位 | 长度(bit) | 默认值 |
|---|---|---|---|
| 起始位 | 47 | 1 | 0 |
| 传输位 | 46 | 1 | 0 |
| `reserved | [45-40] | 6 | 111111b |
OCR register | [39-8] | 32 | [31]:Busy[30]:CCS 0:SDSC 1:SDHC or SDXC[29]:UHS-II 0:Non UHS-II Card 1:UHS-II Card [28-25]:reserved 默认0[24]:S18A 0:Continues current voltage signaling 1:Ready for switching signal voltage[23-8]:OCR[7-0]:reserved 默认0 |
| 结束位 | 0 | 1 | 1 |
R6 response(48bit):CMD3
| 描述 | bit位 | 长度(bit) | 默认值 |
|---|---|---|---|
| 起始位 | 47 | 1 | 0 |
| 传输位 | 46 | 1 | 0 |
| 命令码 | [45-40] | 6 | |
| 参数 | [39-8] | 32 | [31-16]:new published RCA[15-0]:card status bits(见卡状态表:[23,22,19,12:0]) |
| CRC7 | [7-1] | 7 | 校验值 |
| 结束位 | 0 | 1 | 1 |
R7 response(48bit):CMD8
| 描述 | bit位 | 长度(bit) | 默认值 |
|---|---|---|---|
| 起始位 | 47 | 1 | 0 |
| 传输位 | 46 | 1 | 0 |
| 命令码 | [45-40] | 6 | |
| reserved | [39-22] | 18 | |
| PCLe 1.2V support | 21 | 1 | Card responds whether it support VDD3(1.2V power rail)0:Not supporting 1.2V 1:supporting 1.2V |
| PCLe Response | 20 | 1 | Card responds PCLe acceptance0:not accepted 1:accepted |
| Voltage accepted | [19-16] | 4 | 0000b:not defined0001b:2.7~3.6V0010b:reserved for Low Voltage Range0100b:reserved1000b:reservedothers:not defined |
| Echo-back of check pattern | [15-8] | 8 | |
| CRC7 | [7-1] | 7 | 校验值 |
| 结束位 | 0 | 1 | 1 |
例如:mmc1: req done (CMD8): 0: 000001aa 00000000 00000000 00000000,aa是'Echo-back of check pattern',1代表2.7~3.6V
卡状态:
Type:
E: Error bit
S: Status bit
R: Detected and set for the actual command response
X: Detected and set during command execution. The host can get the status by issuing a command with R1 response.
Clear Condition:
A: According to the card current status:
B: Always related to the previous command. Reception of a valid commhand will clear it (with a delay of one command).
C: Clear by read.
| bit | 标识 | Type | Value | 描述 | Clear Condition |
|---|---|---|---|---|---|
| 31 | OUT_OF_RANGE | E R X | 0:no error 1:error | The command's argument was out of the allowed range for this card. | C |
| 30 | ADDRESS_ERROR | E R X | 0:no error 1:error | A misaligned address which did not match the block length was used in the command. | C |
| 29 | BLOCK_LEN_ERROR | E R X | 0:no error 1:error | the transferred block length is not allowed for this card, or the number of transferred bytes does not match the block length | C |
| 28 | ERASE_SEQ_ERROR | E R | 0:no error 1:error | An error in the sequence of erase commands occurred. | C |
| 27 | ERASE_PARAM | E R X | 0:no error 1:error | An invalid selection of write-blocks<br/>for erase occurred | C |
| 26 | WP_VIOLATION | E R X | 0: not protected 1:protected | Set when the host attempts to write to a protected block or to the temporary write protected card or write protected until power cycle card or permanent write protected card. | |
| 25 | CARD_IS_LOCKED | S X | 0: card unlocked 1 : card locked | When set,signals that the card is locked by the host | |
| 24 | LOCK_UNLOCK_FAILED | E R X | 0:no error 1:error | Set when a sequence or password error has been detected in lock/unlock card command. | |
| 23 | COM_CRC_ERROR | E R | 0:no error 1:error | The CRC check of the previous command failed. | |
| 22 | ILLEGAL_COMMAND | E R | 0:no error 1:error | Command not legal for the card state | |
| 21 | CARD_ECC_FAILED | E R X | 0:success 1:failure | Card internal ECC was applied but failed to correct the data. | |
| 20 | CC_ERROR | E R X | 0:no error 1:error | Internal card controller error | |
| 19 | ERROR | E R X | 0:no error 1:error | A general or an unknown error occurred during the operation. | |
| 18 | reserved | ||||
| 17 | reserved | reserved for DEFERRED_RESPONSE (Refer to eSD Addeendum) | |||
| 16 | CSD_OVERWRITE | E R X | 0:no error 1:error | Can be either one of the following errors:- The read only section of the CSD does not match the card content.- An attempt to reverse the copy (set as original) or permanent WP (unprotected)bits was made. | |
| 15 | WP_ERASE_SKIP | E R X | 0: not protected 1:protected | Set when only partial address space was erased due to existing write protected blocks or the temporary write protected or write protected until power clcle or permanent write protected card was erased. | |
| 14 | CARD_ECC_DISABLED | S R | 0:enable 1:disable | the command has been executed without using the internal ECC | |
| 13 | ERASE_RESET | S R | 0:cleared 1:set | An erase sequence was cleared before executing because an out of erase sequence command was received | |
| [12-9] | CURRENT_STATE | S X | 0 = idle1 = ready2 = ident3 = stby4 = tran5 = data6 = rcv7 = prg8 = dis9-14=reserved15 = reserved for I/O mode | the state of the card when receiveing the command. if the command execution causes a state change,it will be visible to the host in the response to the next command The four bits are interpreted as a binary coded number between 0 and 15 | |
| 8 | READY FOR_DATA | S X | 0:not ready 1:ready | Corresponds to buffer empty signaling on the bus | |
| 7 | reserved | ||||
| 6 | FX_EVENT | S X | 0:No event 1:Event invoked | Extension Functions may set this bit to get host to deal with events. | |
| 5 | APP_CMD | S R | 0:disable 1:enable | The card will expect ACMD,or an indication that the command has been interpreted as ACMD | |
| 4 | reserved for SD I/O Card | ||||
| 3 | AKE_SEQ_ERROR | E R | 0:no error 1:error | Error in the sequence of the authentication process | |
| 2 | reserved | ||||
| [1-0] | reserved for test mode |
三、常见CMD功能介绍
CMD0:
功能:TF卡初始化命令
参数:0x00000000
回复:0x00000000
CMD8:
功能:查询电压是否支持
参数:0x000001aa
[0~7]bit:check pattern 0xaa
[8~11]bit:voltage supplied(VHS) 0000b:not define 0001b:2.7~3.6V ...
[12]bit:PCLe Avail-ability
[13]bit:PCLe 1.2V Support
[14~31]:0
回复:R7
CMD2:
功能:读取卡的唯一标识信息(CID寄存器),获取卡的制造商、产品信息、序列号等,用于后续为卡分配相对地址(RCA)
参数:必须为 0x00000000,广播命令,不需要指定特定卡,所有处于Idle状态的卡都会响应
回复:R2
CMD3:
功能:主机分配地址给卡
参数:通常为0(卡生成RCA)
回复:R6
ACMD41:先发送CMD55再发送CMD41
功能:
参数(bit):
[31]:Busy
[30]:HCS 0: SDSC Only Host 1:SDHC or SDXC Supported
[29]:FB
[28]:XPS 0:power saving 1:Maximum performance
[27-25]:reserved 默认0
[24]:S18R 0:use current signal voltage 1:switch to 1.8V signal voltage
[23-8]:OCR
[7-0]:reserved 默认0
备注:
1.如果[23-0]参数是0,则当前命令仅用于查询OCR,不做初始化动作。查询命令忽略[31-24]字段
2.如果[23-0]参数非0,则表明当前是第一个ACMD41命令,会做初始化动作,并且[31-24]字段生效
3.后续的ACMD41命令参数需要跟第一个ACMD41参数保持一致
回复:R3
CMD7:
功能:选择一张卡并将其切换至传输状态
参数:
[31-16]:RCA
[15-0]:reserved 默认0
备注:RCA是0x0000的话则为广播命令,发送至所有的卡。若是CMD3分配的RCA的话则是指定的卡
回复:R1b(命令格式与R1相同,卡在DAT0线上保持低电平表示忙,主机需要等待DAT0变高才能发送下一条指令)
CMD6:
功能:切换卡的工作模式
参数:
[31]: 0:检查模式,检查支持的functions,1:切换模式
[30-24]: reserved 默认0
[23-20]: reserved for function group 6(0x0 or 0xF)
[19-16]: reserved for function group 5(0x0 or 0xF)
[15-12]:function group 4 for Power Limit
[11-8]:function group 3 for Drive Strength
[7-4]:function group 2 for Command System
[3-0]:function group 1 for Access Mode
回复:R1
CMD42:
功能:解锁卡
参数:
[31-24]:
0x06 // 弹出卡(软件弹出)
0x0B // 用密码解锁(已弃用)
0x08 // 强制擦除
0x05 // 解锁卡 UNLOCK
0x04 // 锁定卡
0x0A // 清除密码 CLR_PWD
0x09 // 设置密码 SET_PWD
对于 SET_PWD/CLR_PWD/UNLOCK:
[23-16]:reserved 默认0
[15-8]:密码长度
[7-0]:reserved 默认0
对于 LOCK/UNLOCK/EJECT:
位[23:0] : 保留(全0)
对于 FORCE_ERASE:
[23:0] : 擦除模式/参数
数据块:512byte
typedef struct {
uint8_t password[32]; // 密码数据(最大32字节)
uint8_t reserved[480]; // 保留,填充为0
} lock_unlock_data_t;
回复:R1b
MMC cmd42
----------------------------------------------------------------
| Byte# | Bit7 | Bit6 | Bit5 | Bit4 | Bit3 | Bit2 | Bit1 | Bit0 |
| 0 | Reserved | COP | ERASE| Lock | CLR | Set |
| | shell be set to 0 | | | Ulock| PWD | PWD |
-----------------------------------------------------------------
| 1 | PWDS_LEN |
-----------------------------------------------------------------
| 2 | |
-----------
| ... | Password data |
-----------
|PWDS_LEN+1| |
-------------------------------------------------------------------
源码定义:include/linux/mmc/sd.h
/* SD commands type argument response */
/* class 0 */
/* This is basically the same command as for MMC with some quirks. */
#define SD_SEND_RELATIVE_ADDR 3 /* bcr R6 */
#define SD_SEND_IF_COND 8 /* bcr [11:0] See below R7 */
#define SD_SWITCH_VOLTAGE 11 /* ac R1 */
/* class 10 */
#define SD_SWITCH 6 /* adtc [31:0] See below R1 */
/* class 5 */
#define SD_ERASE_WR_BLK_START 32 /* ac [31:0] data addr R1 */
#define SD_ERASE_WR_BLK_END 33 /* ac [31:0] data addr R1 */
/* Application commands */
#define SD_APP_SET_BUS_WIDTH 6 /* ac [1:0] bus width R1 */
#define SD_APP_SD_STATUS 13 /* adtc R1 */
#define SD_APP_SEND_NUM_WR_BLKS 22 /* adtc R1 */
#define SD_APP_OP_COND 41 /* bcr [31:0] OCR R3 */
#define SD_APP_SEND_SCR 51 /* adtc R1 */
/* OCR bit definitions */
#define SD_OCR_S18R (1 << 24) /* 1.8V switching request */
#define SD_ROCR_S18A SD_OCR_S18R /* 1.8V switching accepted by card */
#define SD_OCR_XPC (1 << 28) /* SDXC power control */
#define SD_OCR_CCS (1 << 30) /* Card Capacity Status */
/*
* SD_SWITCH argument format:
* [31] Check (0) or switch (1)
* [30:24] Reserved (0)
* [23:20] Function group 6
* [19:16] Function group 5
* [15:12] Function group 4
* [11:8] Function group 3
* [7:4] Function group 2
* [3:0] Function group 1
*/
/*
* SD_SEND_IF_COND argument format:
*
* [31:12] Reserved (0)
* [11:8] Host Voltage Supply Flags
* [7:0] Check Pattern (0xAA)
*/
/*
* SCR field definitions
*/
#define SCR_SPEC_VER_0 0 /* Implements system specification 1.0 - 1.01 */
#define SCR_SPEC_VER_1 1 /* Implements system specification 1.10 */
#define SCR_SPEC_VER_2 2 /* Implements system specification 2.00-3.0X */
/*
* SD bus widths
*/
#define SD_BUS_WIDTH_1 0
#define SD_BUS_WIDTH_4 2
/*
* SD_SWITCH mode
*/
#define SD_SWITCH_CHECK 0
#define SD_SWITCH_SET 1
/*
* SD_SWITCH function groups
*/
#define SD_SWITCH_GRP_ACCESS 0
/*
* SD_SWITCH access modes
*/
#define SD_SWITCH_ACCESS_DEF 0
#define SD_SWITCH_ACCESS_HS 1
1万+

被折叠的 条评论
为什么被折叠?



