MicroSD card (TF card) SPI mode implementation method

Original: https://www.cnblogs.com/einstein-2014731/p/4885382.html

Now the memory cards of our mobile phones are mostly Micro SD cards, also called TF cards, so Micro SD cards are more common than SD cards. I once wanted to write a reading program for SD cards, but I didn't want to buy another SD card. At this time, I remembered that the memory card of the mobile phone is not very similar to the SD card? After searching on the Internet, I found that SD cards
and Micro SD cards are actually different in size and pins, and their operations are actually the same, so the SD card reading and writing codes on the Internet can actually be used directly. The pin definitions and differences between SD card and Micro SD card can be seen in the following two tables:

  We can find that the Micro SD card has only 8 pins because there is one less Vss than the SD card. Of course, you can also buy a card sleeve to put on the Micro SD card, so that the size is the same as the SD card. At this time, the 9 pins on the card sleeve are the same as the SD card, and you can operate it as an SD card. .

The connection of the circuit under the spi is very simple. Connect the power line Vdd and the ground line Vss, and then connect the CS, SCLK, DI (MOSI) and DO (MISO) of the spi, and other pins can be left empty. Note that the power supply and operating voltage of the SD card are both 2.7-3.6V, and the 5V microcontroller needs to perform level conversion or series resistance current limiting.

Also remember that CS, SCLKh and DI of SD card should be pulled up with 10~100K resistors. I set up a card socket circuit, because the pins of the Micro SD card are too dense and difficult to solder, and the SD card is easy to solder relative to the pins. Because there is no card holder, and there is no special PCB, I directly welded it to the card sleeve, eh, sacrificed a card sleeve.

Here is the circuit diagram I drew myself:

The hardware circuit of the Micro SD card above is fine. Let's talk about the software driver and instruction set of the Micro SD card.

The command format of SD card is as follows, 6 bytes total 48 bits, the most significant bit (MSB) is transmitted first during transmission:

SD card command (command) occupies 6 bits, generally called CMDx or ACMDx, such as CMD1 is 1, CMD13 is 13, ACMD41 is 41, and so on. Command Argument (command parameter) occupies 4 bytes. Not all commands have parameters. If there are no parameters, this bit is generally set to 0.

The last byte consists of 7 bits of CRC check bits and 1 bit of stop bits. In SPI mode, CRC is ignored and can be set to 1 or 0. But remember to add CRC when sending CMD0, that is, the last byte is 0x95 (because the SPI mode has not been entered when sending CMD0, PS: CMD8 also,

But generally everyone omits sending CMD8). After each command is sent, the SD card will respond. The response of the SD card has various formats, 1-byte R1, 2-byte R2, etc., but generally we only use R1 in SPI mode. The format of R1 is described below:

The following points should be noted about the sending of SD card SPI and command:

1. The SPI bus of the SD card, when reading data, the SPI of the SD card is input and latched on the rising edge of CLK, and the output data is also on the rising edge.

2. The process of writing a CMD or ACMD command to the SD card is as follows: First, make CS low to enable the SD card; secondly, write the command to Din of the SD card; add 8 additional commands after the write command The filling clock is the completion of the internal operation of the SD card; then the response is received on the Dout of the SD card; after the response is accepted, CS is set to low level, and 8 filling clocks are added.

3. When Din of SD card has no data to be written, Din should be
kept at high level. I've had a hard time with this, and I remembered to keep it high, but somehow it was set to 0 and pulled low. As a result, the program has all kinds of strange seemingly accidental errors, such as
two consecutive resets will fail once, and single-step debugging will fail when running at full speed. In short, during this process, I made various changes to the timing, and each time a problem was solved, new problems appeared, which shook my
view that the operations of MicroSD cards and SD cards are the same. Because this low-level mistake delayed me for three or four days, it seems that being careful is very important! I have wasted a lot of time because of inattentiveness more than once, and I
hope everyone will take this as a warning.

Well, now that the commands and responses of the SD card are clear, let's talk about how to reset, initialize, and read and write the SD card.

 

Reset method:

1. Pull CS high and send at least 74 clk cycles to bring the SD card to normal operating voltage and synchronization

2. Select low CS, send CMD0, you need to receive a response 0x01 to indicate successful entry into the idle state

3. Pull CS high and send 8 clocks

Reset timing diagram:

initialization:

    After the reset is successful, the SD card enters the SPI mode, and then it should be initialized. There are two methods for initialization: (1) send CMD1, (2) send CMD55+ACMD41.

I can see this statement from the information I checked on the Internet: if it is an MMC card, send CMD1, and SD card send CMD55+ACMD41. However, it is not very clear about which Micro SD card to send. Some people have successfully used these two methods on the Internet, but some have failed.

I also encountered this kind of problem. I just took the 2GB Micro SD card (should be a non-brand) on my mobile phone with Nokia written on it, and it failed to initialize for two days. When I was about to give up, I remembered why I didn’t change it. I tried it, so I asked my roommate to borrow his mobile phone memory card,

It is a 2GB Apacer Micro SD card (of course it may be a non-brand, the place where my roommate buys the card is usually a variety of cheap electronic products, everyone knows it is a non-brand), and the result was a success. Later, I used a method to find that it can also be initialized,

也就是说两种方法都可以初始化成功。但我的那种怎么就不行呢?难道不是所有Micro SD卡都支持SPI模式。我在网上百度了半天也不能确定是不是所有Micro SD卡都支持SPI模式。

但我想,现在Micro SD卡的生产公司很多,而且你也并不能保证你的Micro SD卡不是杂牌的。你并不知道生产厂家进行了那些改变,因为确实有些厂家生产的SD卡精简了一些命令。所以初始化的时候建议两种都试一下,

不过我记得SD 卡的说明书上推荐使用第二种方法。

 

下面是初始化方法:

(1)使用CMD1

发送CMD1,收到0x00表示成功

时序图如下:

 

读多块方法:

    1.发送CMD18读,收到0x00表示成功

    2.连续读直到读到开始字节0xFE

    3.读512字节

    4.读两个CRC字节

    5.如果还想读下一扇区,重复2-4

    6.发送CMD12来停止读多块操作

 

    写单块和多块:

    SD卡用CMD24和CMD25来写单块和多块,参数的定义和读操作是一样的。

    写单块方法:

    1.发送CMD24,收到0x00表示成功

    2.发送若干时钟

    3.发送写单块开始字节0xFE

    4.发送512个字节数据

    5.发送2字节CRC(可以均为0xff)

    6.连续读直到读到XXX00101表示数据写入成功

    7.继续读进行忙检测(读到0x00表示SD卡正忙),当读到0xff表示写操作完成

    写单块时序图:

写多块方法:

    1.发送CMD25,收到0x00表示成功

    2.发送若干时钟

    3.发送写多块开始字节0xFC

    4.发送512字节数据

    5.发送两个CRC(可以均为0xff)

    6.连续读直到读到XXX00101表示数据写入成功

    7.继续读进行忙检测,直到读到0xFF表示写操作完成

    8.如果想读下一扇区重复2-7步骤

    9.发送写多块停止字节0xFD来停止写操作

    10.进行忙检测直到读到0xFF


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325601325&siteId=291194637