TF 卡检测&初始化通讯命令

TF 卡检测&初始化通讯命令

一、MMC通讯命令格式如下:

host->mmc:发送命令格式如下:

描述bit位长度(bit)默认值
起始位4710
传输位4611
命令码[45-40]6如:CMD41=101001b
参数[39-8]32如:0x51040000
CRC7[7-1]7校验值
结束位011

如日志: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)默认值
起始位4710
传输位4610
命令码[45-40]6如:CMD41=101001b
卡状态[39-8]32见卡状态表
CRC7[7-1]7校验值
结束位011

R2 response(136bit):CMD2(CID)、CMD10(CID)、CMD9(CSD)

描述bit位长度(bit)默认值
起始位13510
传输位13410
reserved[133-128]6111111b
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
结束位011

例如:[Mon Dec 8 07:44:51 2025] mmc1: req done (CMD2): 0: ad4c5355 53443030 1035893d b1719700

R3 response(48bit):ACMD41

描述bit位长度(bit)默认值
起始位4710
传输位4610
`reserved[45-40]6111111b
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
结束位011

R6 response(48bit):CMD3

描述bit位长度(bit)默认值
起始位4710
传输位4610
命令码[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校验值
结束位011

R7 response(48bit):CMD8

描述bit位长度(bit)默认值
起始位4710
传输位4610
命令码[45-40]6
reserved[39-22]18
PCLe 1.2V support211Card responds whether it support VDD3(1.2V power rail)
0:Not supporting 1.2V 1:supporting 1.2V
PCLe Response201Card responds PCLe acceptance
0:not accepted 1:accepted
Voltage accepted[19-16]40000b:not defined
0001b:2.7~3.6V
0010b:reserved for Low Voltage Range
0100b:reserved
1000b:reserved
others:not defined
Echo-back of check pattern[15-8]8
CRC7[7-1]7校验值
结束位011

例如: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标识TypeValue描述Clear Condition
31OUT_OF_RANGEE R X0:no error 1:errorThe command's argument was out of the allowed range for this card.C
30ADDRESS_ERRORE R X0:no error 1:errorA misaligned address which did not match the block length was used in the command.C
29BLOCK_LEN_ERRORE R X0:no error 1:errorthe transferred block length is not allowed for this card, or the number of transferred bytes does not match the block lengthC
28ERASE_SEQ_ERRORE R0:no error 1:errorAn error in the sequence of erase commands occurred.C
27ERASE_PARAME R X0:no error 1:errorAn invalid selection of write-blocks<br/>for erase occurredC
26WP_VIOLATIONE R X0: not protected 1:protectedSet 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.
25CARD_IS_LOCKEDS X0: card unlocked 1 : card lockedWhen set,signals that the card is locked by the host
24LOCK_UNLOCK_FAILEDE R X0:no error 1:errorSet when a sequence or password error has been detected in lock/unlock card command.
23COM_CRC_ERRORE R0:no error 1:errorThe CRC check of the previous command failed.
22ILLEGAL_COMMANDE R0:no error 1:errorCommand not legal for the card state
21CARD_ECC_FAILEDE R X0:success 1:failure Card internal ECC was applied but failed to correct the data.
20CC_ERRORE R X0:no error 1:errorInternal card controller error
19ERRORE R X0:no error 1:errorA general or an unknown error occurred during the operation.
18reserved
17reservedreserved for DEFERRED_RESPONSE (Refer to eSD Addeendum)
16CSD_OVERWRITEE R X0:no error 1:errorCan 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.
15WP_ERASE_SKIPE R X0: not protected 1:protectedSet 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.
14CARD_ECC_DISABLEDS R0:enable 1:disablethe command has been executed without using the internal ECC
13ERASE_RESETS R0:cleared 1:setAn erase sequence was cleared before executing because an out of erase sequence command was received
[12-9]CURRENT_STATES X0 = idle
1 = ready
2 = ident
3 = stby
4 = tran
5 = data
6 = rcv
7 = prg
8 = dis
9-14=reserved
15 = 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
8READY FOR_DATAS X0:not ready 1:readyCorresponds to buffer empty signaling on the bus
7reserved
6FX_EVENTS X0:No event 1:Event invokedExtension Functions may set this bit to get host to deal with events.
5APP_CMDS R0:disable 1:enableThe card will expect ACMD,or an indication that the command has been interpreted as ACMD
4reserved for SD I/O Card
3AKE_SEQ_ERRORE R0:no error 1:errorError in the sequence of the authentication process
2reserved
[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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值