How to Use MMC/SDC

本文详细介绍了使用SPI模式操作SD卡的基本知识和技术细节,包括SPI模式下的数据传输、命令响应格式、初始化流程等内容,并提供了提高读写性能的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导读:
  
  Now SD Memory Card(Secure Digital Memory Card) is the most popular memory card for mobile equipments. The SD Memory Card (SDC below) was developped as upper-compatible to Multi Media Card(MMC below) so that the SDC compleant equipments can also use an MMC with a few considerations. There are also reduced size versions, such as RS-MMC, miniSDand microSD, with same function. The MMC/SDC has a microcontroller in it, the flash memory controls (erase, read, write and error control) are completed at inside of the memory card. The data is transferred between memory card and host controller in unit of 512 bytes per block in default, so that it can be seen like a generic hard disk drive from view point of application programs. The currentry defined file system is only FAT12/16 with FDISK patitioning rule. The FAT32 is defined for only high capacity (>= 4G) cards.
  This page describes the basic knowledge and miscellaneous things that I become aware, on using MMC/SDC with small embedded system. I believe that this information must be a useful getting started notes for people who is going to begin to enjoy MMC/SDC.
   Contact Surface
  
  
  Right photo shows the contact surface of the SDC/MMC. The MMC has seven contact pads and the SDC has nine contact pads that two pads added to MMC. Three of the contacts for each occupy as power supply pins so that the effective signal numbers are four and six. Ofcourse the data transfer between the host and the card is done in clocked serial data transfer.
  The working supply voltage range is indicated in OCR register and it should be read to comfirm the operating voltage range. However, the supply voltage can be fixed to a proper value because the MMC/SDC works at supply voltage of 2.7 to 3.6 volts. The current consumption can reach up to several ten milliamperes, so that the host system should able to supply 100 miliamperes at least.
   SPI Mode
  SPI modeis an alternative operating mode that defined to use MMC/SDC without its specific host interface. The communication protocol for the SPI mode is very simple compared to MMC/SDC native mode, the MMC/SDC can be attached via a generic SPI port or a GPIO port built in most microcontrollers. Therefore the SPI mode is suitable for low cost embedded applications. Especialy, there is no reason to use native mode for electronic handiwork as a hobby. For SDC, the 'SPI mode 0' is defined for its SPI mode. But for MMC, it is not the SPI timing, both latch and shift actions are defined with rising edge of SCLK, but it seems work in SPI mode 0 at SPI mode. Thus SPI Mode 0(CPHA=0, CPOL=0) is the proper setting for MMC/SDC interface, but SPI mode 3 also works as well in most case.
   Command and Response
  In SPI mode, the data direction on the signal line is fixed and the data is transferred in byte oriented serial communication. The command frame from host to card is a fixed length (six bytes) packet that shown below. When a command frame is transmitted to the card, a response to the command (R1, R2 or R3) will be sent back to the host. Because data transfer is driven by serial clock generated by host, the host must continue to read bytes until receive any valid response. The command response time (NCR) is 0 to 8 bytes for SDC, 1 to 8 bytes for MMC. The CS signal must be held low during a transaction (command, response and data transfer if exist). The CRC field is optional in SPI mode, but it is required as a bit field to compose a command frame. The DI signal must be kept high during read transfer.
   SPI Command Set
  Each command is expressed in abbreviation like GO_IDLE_STATE or CMD , is the number of the command index and the value can be 0 to 63. Following table describes only commands that to be usually used for generic read/write and card initialization. For details on all commands, please refer to spec sheets from MMCA and SDCA.
  













































































































































Command
Index
ArgumentResponseDataAbbreviationDescription
CMD0None(0)R1NoGO_IDLE_STATESoftware reset.
CMD1None(0)R1NoSEND_OP_CONDInitiate initialization process.
ACMD41(*1)*2R1NoAPP_SEND_OP_CONDFor only SDC. Initiate initialization process.
CMD8*3R7NoSEND_IF_CONDFor only SDC V2. Check voltage range.
CMD9None(0)R1YesSEND_CSDRead CSD register.
CMD10None(0)R1YesSEND_CIDRead CID register.
CMD12None(0)R1bNoSTOP_TRANSMISSIONStop to read data.
CMD16Block
length[31:0]
R1NoSET_BLOCKLENChange R/W block size.
CMD17Address[31:0]R1YesREAD_SINGLE_BLOCKRead a block.
CMD18Address[31:0]R1YesREAD_MULTIPLE_BLOCKRead multiple blocks.
CMD23Number of
blocks[15:0]
R1NoSET_BLOCK_COUNTFor only MMC. Define number of blocks to transfer
with next multi-block read/write command.
ACMD23(*1)Number of
blocks[22:0]
R1NoSET_WR_BLOCK_ERASE_COUNTFor only SDC. Define number of blocks to pre-erase
with next multi-block write command.
CMD24Address[31:0]R1YesWRITE_BLOCKWrite a block.
CMD25Address[31:0]R1YesWRITE_MULTIPLE_BLOCKWrite multiple blocks.
CMD55(*1)None(0)R1NoAPP_CMDApplication specific command.
CMD58None(0)R3NoREAD_OCRRead OCR.
*1:ACMD means a command sequense of CMD55-CMD .

*2: Rsv(0)[31], HCS[30], Rsv(0)[29:0]

*3: Rsv(0)[31:12], Supply Voltage(1)[11:8], Check Pattern(0xAA)[7:0]

   SPI Response
  There are three command response formats, R1, R2and R3, depends on each command. A byte of response R1 is returned for most commands. The bit field of R1 response is shown in right image, the value 0x00 means successful. When any error occured, corresponding bit in the response will be set. The R3 response is for only CMD58, its first byte is same as R1 and it is trailing the content of OCR.
  Some commands take a time longer than NCRand it responds R1b. It is an R1 response followed by busy flag (DO is held low as long as internal process is being executed). The host controller should wait for end of the process until 0xFF is received.
   Initialization Procedure for SPI Mode
  After power on reset, MMC/SDC enters its native operating mode. To put it SPI mode, follwing procedure must be performed.
   Power ON (Insersion)
  After supply voltage reached 2.2 volts, wait for a millisecond at least. Set DI and CS high and apply more than 74 clock pulses to SCLKand the card will go ready to accept native commands.
   Software Reset
  Set SPI clock rate between 100kHz and 400kHz and then send a CMD0with CS low to reset the card. The card samples CS signal when a CMD0 is received. If the CS signal is low, the card enters SPI mode. Since the CMD0 must be sent as a native command, the CRC field must have a valid value. When once the card enters SPI mode, the CRC feature is disabled and the CRC is not checked, so that command transmission routine can be written with the hardcorded CRC value that valid for only CMD0 and CMD8. When the CMD0 is accepted, the card will enter idle state and respond R1 response with In Idle State bit (0x01). The CRC feature can also be switched with CMD59.
   Initialization
  In idle state, the card accepts only CMD0, CMD1 and CMD58. Any other commands will be rejected. In this time, check working voltage range indicated in the OCR. In case of the system sypply voltage is out of working voltage range, the card must be rejected. The card initiates initialization when a CMD1is detected. To poll end of the initialization, the host controller must send CMD1 and check the response until end of the initialization. When the card is initialized successfuly, In Idle State bit in the R1 response is cleared (R1 resp changes 0x01 to 0x00). The initialization process can take several hundred milliseconds(large cards tend to longer), so that this is a consideration to determin the time out value. After the card has initialized, generic read/write commands will able to be accepted.
  Because ACMD41instead of CMD1 is recommended for SDC, send ACMD41 first and when it is rejected, retry with CMD1, is ideal, to support both type of the card.
  The SPI clock rate should be changed to fast as possible to optimize the read/write performance. The TRAN_SPEED field in the CSD indicates the maximum clock rate of the card. The maximum clock rate is 20MHz for MMC, 25MHz for SDC in most case. Note that the clock rate can also be fixed to 20/25MHz in SPI mode because there is no open-drain condition that restricts the clock rate.
  The initial block length can be set larger than 512 at 2GB card, so that the block size should be re-initialized with CMD16 if needed.
   How to support SDC Ver2 and high capacity cards
  After the card enters idle state with a CMD0, send a CMD8with 0x1AA and correct CRC before initiate initialization. When the CMD8 is rejected with an illigal command error, the card is SDC V1 or MMC. When the CMD8 is accepted, R7 response (R1 + 32 bit return value) will be returned. The lower 12 bits in the return value 0x1AA means that the card is SDC V2 and can work at voltage range of 2.7 to 3.6 volts. If not the case, the card must be rejected. And then initiate initialization with ACMD41 with HCS bit. After the initialization completed, read OCR and check CCS bit in the OCR. When it is set, subsequent data read/write operations that described below are commanded in block address insted of byte address. The block size is fixed to 512 bytes.
   Data Transfer
   Data Packet and Data Response
  
  
  In a transaction with data transfer, one or more data blocks will be sent/received after command response. The data block is transferred as a data packet that consist of Token, Data Block and CRC. The format of the data packet is showin in right image and there are three data tokens. As for Stop Tran token that means end of multiple block write, it is used in single byte without data block and CRC.
   Single Block Read
  The argument specifies the location to start to read in unit of byte or block. The sector address specified by upper layer must be scaled properly. When a CMD17 is accepted, a read operation is initiated and the read data block will be sent to the host. After a valid data token is detected, the host controller receives following data field and two byte CRC. The CRC bytes must be flushed even if it is not needed. If any error occured during the read operation, an error token will be returned instead of data packet.
   Multiple Block Read
  The Multiple Block Read command reads multiple blocks in sequense from the specified address. When number of transfer blocks has not been sepecified before this command, the transaction will be initiated as an open-ended multiple block read, the read operation will continue until stopped with a CMD12. The received byte immediataly following CMD12 is a stuff byte, it should be discarded before receive the response of the CMD12.
   Single Block Write
  When a write command is accepted, the host controller sends a data packet to the card after a byte space. The packet format is same as Block Read command. The CRC field can have any invalid value unless the CRC function is enabled. When a data packet has been sent, the card responds a Data Response immediataly following the data packet. The data response trails a busy flag to process the write operation. Most cards cannot change write block size and it is fixed to 512.
  In principle of the SPI mode, the CS signal must be asserted during a transaction, however there is an exception to this rule. When the card is busy, the host controller can deassert CS to release SPI bus for any other SPI devices. The card will drive DO signal low again when reselect it during internal process is in progress. Therefore a preceding busy check (wait ready immediataly before command and data packet) instead of post wait can eliminate waste wait time. In addition the internal process is initiated a byte after the data response, this means eight clocks are required to initiate internal write operation. The state of CS signal during the eight clocks is negligible so that it can done by bus release process described below.
   Multiple Block Write
  The Multiple Block Read command writes multiple blocks in sequense from the specified address. When number of transfer blocks has not been sepecified before this command, the transaction will be initiated as an open-ended multiple block write, the write operation will continue until terminated with a Stop Tran token. The busy flag will appear a byte after the Stop Tran token. As for SDC, the multiple block write transaction must be terminated with a Stop Tran token independent of pre-defined or open-ended.
   Reading CSD and CID
  These are same as Single Block Read except for the data block length. The CSD and CID are sent to the host as 16 byte data blocks. For details of the CMD, CID and OCR, please refer to the MMC/SDC specs.
   Cosideration to Bus Floating and Hot Insertion
  
  
  Any signal that can float should be pulled low or high properly via a resister. This is a generic design rule on MOS devices. Because DI and DO are normally high, they should be pulled-up. According to SDC/MMC specs, from 50k to 100k ohms is recommended to the value of pull-up registers. However the clock signal is not mentioned in the SDC/MMC specs because it is always driven by host controller. When there is a possibility of float, it should be pulled to the normal state, low.
  The MMC/SDC can hot insertion/removal but some consideration to the host circuit are needed to avoid an incorrect operation. For example, if the system power supply (Vcc) is tied to the card socket directly, the supply voltage will dip at the instant of contact closed due to charge current to the capacitor that built in the card. 'A' in the right image is the scope view and it shows that occureing a voltage dip of 600 millivolts. This is a sufficient level to trigger brown out detector. 'B' in the right image shows that an inductor is inserted to block pulse current, the voltage dip is improved to 200 millivoits. A large OS-CON can improve the voltage dip dratiscally. However the OS-CON can cause an oscillation of LDO regulator.
   Cosideration on Multi-slave Configuration
  
  
  In SPI, each slave device is selected with separated CS signals, and plural devices can be attached to an SPI bus. Generic SPI slave device drives/releases its DO signal by CS signal asynchronously to share an SPI bus. However MMC/SDC drives/releases DO signal in synchronising to SCLK. There is a posibility of bus conflict when attach MMC/SDC and any other SPI slaves to an SPI bus. Right image shows the drive/release timing of MMC/SDC (DO is pulled to 1/2 vcc to see the bus state). Therefore to make MMC/SDC release DO signal, the master device must send a byte after deasserted CS signal.
   Optimization of Write Performance
  Most MMC/SDC employs NAND Flash Memoryas a memory array. The NAND flash memory is cost effective and it can read/write largedata fast, but on the other hand, there is a disadvantage that rewriting a partof data is inefficient. Generally the flash memory requires to erase existing data before write a new data, and minimum unit of erase operation (called erase block) is larger than write block size. The typical NAND flash memory has a block size of 512/16K bytes for write/erase operation, and recent monster card employs large block chip (2K/128K). This means that rewriting entire data in the erase block is done in the card even if write only a sector (512 bytes).
   Benchmark
  I examined the read/write performance of some MMC/SDCwith a cheap 8 bit MCU (ATmega64 @9.2MHz) on the assumption that an embedded system with limited memory size. For reason of memory size, write()and read()ware performed in 2048 bytes at a time. The result is: Write: 77kB/sec, Read: 328kB/sec on the 128MB SDC, Write: 28kB/sec, Read: 234kB/sec on the 512MB SDCand Write: 182kB/sec, Read: 312kB/sec on the 128MB MMC.
  Therefor the write performance of the 512MB SDC was very poor that one third value of 128MB SDC. Generally the read/write performance of the mass storage device increases proportional to its recording density, however it sometimes appears a tendency of opposite on the memory card. As for the MMC, it seems to be several times faster than SDC, it is not bad performance. After that time, I examined some SDCs supplied from different makers, and I found that PQI's SDC was as fast as Hitachi's MMC but Panasonic's and Toshiba's one was very poor performances.
   Erase Block Size
  To analys detail of write operation, busy time (number of polling cycles) after sent a write data is typed out to console in the low level disk write function. Multiple numbers on a line indicates data blocks and a Stop Tran token that issued by a multiple block write transaction.
  In resulut of the analysis, there is a different of internal process between 128MB SDC and 512MB SDC. The 128MB SDC rewrites erase block at end of the mutiple block write transaction. The 512MB SDC seems have 4K bytes data buffer and it rewrites erase block every 4K bytes boundary. Therefor it cannot compared directly but the processing time of rewriting an erase block can be read 3800 for 128MB SDC and the 512MB SDC taeks 30000 that 8 times longer than 128MB SDC. Judging from this resulut, it seems the 128MB SDC uses a small block chip and the 512MB SDC uses a large block or MLC chip. Ofcourse the larger block size decreases the performance on pertial block rewriting. In 512MB SDC, only an area that 512K bytes from top of the memory is relatively fast. This can be read from write time in close(). It might any special processing is applied to this area for fast FAT accsess.
   Improving Write Performance
  To avoid this bottleneck and rise write performance of SDC/MMC, writing large number of blocsks as possible (aligned to erase block is ideal) at a time will do. In other words, allocate large buffer memory and pass it to fwrite()will do. For low level disk write function, it must pre-inform number of write sectors to the card for efficient write processing. This called `pre-defined multiple block write'. However the pre-definition command is not the same between MMC (CMD23) and SDC (ACMD23).
  Well, it might a vain efforts that to rise write performance of SDC on the cheap MCUs that have only several kilobytes of RAM. CompactFlash have a good performance that ten times faster than SDC. When you require a write performance to the memory card, a CompactFlash or an MMC will be suitable better than SDC.
  The memory cards are initially patitioned and formatted to align the allocation unit to the erase block. When re-patition or re-format the memory card with a system that is not compliant to MMC/SDC (this is just a PC) with no care, the optimization will be broken and the write performance might be lost. I tried to re-format 512MB SDC in FAT32 with a PC, the write performance measured in file copy was lowerd to one several. Therefore the re-formatting the card should be done with MMC/SDC compliant equipments rather than PC.

本文转自
http://elm-chan.org/docs/mmc/mmc_e.html
<think>我们面对的问题:磁盘分区时出现“Error: /dev/sdb: unrecognised disk label”错误 根据引用[1]和[2]的描述,该错误通常表示磁盘没有有效的磁盘标签(即分区表)。在Linux中,磁盘标签可以是MSDOS(MBR)或GPT等格式。 原因分析:新磁盘或者没有初始化分区表的磁盘会出现此错误。因此,我们需要为磁盘创建分区表(即磁盘标签)。 解决步骤: 1. 首先,我们需要确认磁盘设备。用户提到的是/dev/sdc(在问题中),但引用中示例是/dev/sdb。这里以用户问题中的/dev/sdc为准。 2. 使用parted工具为磁盘创建新的分区表(磁盘标签)。常见的分区表类型有msdos(MBR)和gpt。 注意:创建新的分区表会清除磁盘上所有现有数据!请确保磁盘上没有重要数据或已经备份。 具体操作: 步骤1:启动parted工具,选择磁盘(例如:/dev/sdc) 步骤2:创建新的分区表(选择msdos或gpt) 步骤3:创建分区并设置文件系统类型 步骤4:格式化分区 详细步骤: 1. 创建分区表(以GPT为例,如果磁盘大于2TB,必须使用GPT;如果小于2TB,可以选择msdos或gpt) ``` # parted /dev/sdc (parted) mklabel gpt # 或者使用 mklabel msdos ``` 2. 创建分区(例如,创建一个占用整个磁盘的分区): ``` (parted) mkpart primary 0% 100% ``` 3. 退出parted(输入quit) 4. 格式化分区(例如,格式化为ext4文件系统): 注意:分区创建后,设备名通常是在磁盘设备名后面加上分区号,例如/dev/sdc1。 ``` # mkfs.ext4 /dev/sdc1 ``` 5. 挂载使用。 另外,也可以使用fdisk工具(对于MBR分区表)或gdisk(对于GPT分区表)来创建分区表。 使用fdisk(针对MBR): ``` # fdisk /dev/sdc 命令(输入 m 获取帮助):o # 创建新的DOS分区表(即MBR) 命令(输入 m 获取帮助):n # 新建分区 然后按照提示操作,最后写入:w ``` 使用gdisk(针对GPT): ``` # gdisk /dev/sdc 命令(输入 ? 显示帮助):o # 创建新的GPT分区表 命令(输入 ? 显示帮助):n # 新建分区 然后按照提示操作,最后写入:w ``` 注意:在创建分区表之前,请确保选择了正确的磁盘设备,因为操作会清除磁盘上的所有数据。 引用说明: [^1] 和 [^2] 描述了问题现象,即磁盘标签未被识别,通常是因为磁盘没有初始化分区表。 [^3] 提供了关于设备文件的基本知识,磁盘设备在Linux中通常以/dev/sdX的形式出现。 因此,解决该问题的关键步骤是创建磁盘标签(分区表),然后创建分区并格式化。 相关问题: 1. 如何选择分区表类型(MBR vs GPT)? 2. 如何为多个磁盘分区? 3. 如何挂载新格式化的磁盘分区?</think>### 解决 /dev/sdc unrecognised disk label 错误 当磁盘显示 `unrecognised disk label` 错误时,表示系统无法识别磁盘的分区表结构。以下是完整的解决步骤: #### 📝 原因分析 1. **新磁盘未初始化**:新添加的磁盘未创建分区表(常见原因)[^1] 2. **分区表损坏**:磁盘原有分区表损坏或被清除 3. **不兼容分区格式**:使用了系统不支持的格式(如 Windows 动态磁盘) 4. **设备识别错误**:磁盘本身存在物理故障或连接问题[^2] #### 🛠️ 解决方案(以 /dev/sdc 为例) ```bash # 1. 创建新的分区表 (选择 GPT 或 MBR) sudo parted /dev/sdc mklabel gpt # 推荐 GPT(适用于 >2TB 磁盘) 或 sudo parted /dev/sdc mklabel msdos # MBR(适用于 <2TB 磁盘) # 2. 创建新分区(示例:创建占用整个磁盘的分区) sudo parted /dev/sdc mkpart primary 0% 100% # 3. 格式化分区(EXT4 示例) sudo mkfs.ext4 /dev/sdc1 # 注意分区号变为 sdc1 # 4. 挂载使用 sudo mkdir /mnt/newdisk sudo mount /dev/sdc1 /mnt/newdisk ``` #### ⚠️ 重要注意事项 1. **数据丢失警告**:此操作会**永久清除磁盘所有数据**,操作前务必确认磁盘无重要数据 2. **确认设备标识**:通过 `lsblk` 或 `sudo fdisk -l` 确认设备路径正确 ```bash $ lsblk sdc 8:32 0 2T 0 disk # 确认目标磁盘容量和标识 ``` 3. **GPT vs MBR 选择**: - 磁盘 > 2TB → **必须使用 GPT** - 旧系统兼容 → 可选 MBR - UEFI 启动 → **必须使用 GPT** #### 🔍 高级处理(当基础方案无效时) 1. **检查磁盘健康状态**: ```bash sudo smartctl -a /dev/sdc ``` 2. **尝试低级格式化**(修复损坏扇区): ```bash sudo badblocks -sv -b 4096 /dev/sdc ``` 3. **验证设备识别**: ```bash ls -l /dev/disk/by-id/ | grep sdc ``` > **技术原理**:磁盘标签(Disk Label)是存储在磁盘起始扇区的分区表元数据,Linux 通过 `/dev/sdX` 设备文件访问磁盘时需依赖此结构[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhxlx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值