Single chip microcomputer SD card reading and writing

Source: Internet
Author: User

1. The most detailed analysis and code HTTP://ELM-CHAN.ORG/DOCS/MMC/MMC_E.HTML2 of the SD card SPI mode have been seen so far. Reprint http://blog.csdn.net/ming1006/ article/details/7281597

Now our cell phone memory is micro SD card, also known as TF card, so micro SD card is more common than SD card. I once also want to write the SD card reading program, but do not want to go to buy an SD card, then think of the phone memory card is not like the SD card? On the internet after the discovery of SD card and micro SD card In fact, the size and pin is not the same, their operation is actually the same, so the online SD card read and write code can actually be used directly. For the PIN definition of SD card and micro SD card and different visible below two tables:

We can find that the Micro SD card only has 8 pins because there is a VSS less than the SD card. Of course, you can also buy a card condom on the Micro SD card, this size is as large as the SD card, when the 9 pins on the card sleeve and SD card, you can completely as an SD card to operate.

The connection of the SPI circuit is simple, connect the power cord vdd and ground VSS, connect the SPI Cs,sclk,di (MOSI) and do (miso), and the other pins can be vented. Note that the SD card power and operating voltage are 2.7-3.6v,5v of the single-chip for level conversion or series resistor current limit. Also remember the SD card Cs,sclkh and di to use 10~100k resistor pull. I was set up a card socket circuit, because the Micro SD card PIN is too dense, not good welding, SD card relative pin good welding. Because there is no card seat, and there is no special PCB I directly welded to the card sleeve, ah, the sacrifice of a card sleeve. Here is the circuit diagram I drew myself:

The above micro SD card hardware circuit is good, below we talk about the Micro SD card software driver and instruction set.

The command format of the SD card is as follows,6 bytes Total 48 bits, transmission highest bit (MSB) transmits first:

SD card Command (commands) accounted for 6 bit, generally called cmdx or acmdx, such as CMD1 is 1,cmd13 is 13,acmd41 is 41, and so on. Command Argument 4 byte, not all commands have parameters, the bit is generally used to set 0 without parameters. The last byte consists of a 7 bit CRC check bit and a 1 bit stop bit. In SPI mode, the CRC is ignored and can be set to 1 or 0. But when sending the CMD0, remember to add the CRC, that is, the last 1 bytes is 0x95 (because the sending CMD0 is not in the SPI mode, Ps:cmd8 also want, but generally everyone sends CMD8 omitted).

The SD card responds after each command is sent. SD card response has several formats, 1 bytes of r1,2 bytes of R2, but generally in the SPI mode we only use R1, the following describes the R1 format:

Note the following points regarding the transmission of the SD card SPI and command:
The SPI bus of the 1.SD card, when reading the data, the SD card SPI is the rising edge of CLK input latch, the output data is also on the rising edge.
2. The process of writing a cmd or acmd instruction to the SD card is as follows: first make CS low level, SD card enable, second in the SD card din write instruction, after write instruction also attach 8 fill clock, is SD card completes internal operation, then accept response on SD card dout The response is accepted so that CS is low and 8 additional fill clocks are attached.

3. When there is no data write on the SD card din, keep the din high. On this point I can thoroughly understand, originally also remember to maintain high level, the result somehow unconsciously 0 pull low. As a result, the program has a variety of strange seemingly accidental errors, such as two consecutive resets will have a failure, single-step debugging successful full-speed operation will fail. Anyway, in this process I have a variety of timing changes, each solve a problem and there will be new problems, how many times I shook the microSD card and SD card operation is the same view. Because this low-level mistake delayed my three or four days, it seems that careful is very important ah! I have been more than once because of not careful waste of time, I hope we also warning.

OK, now the SD Card command and response is clear, we will talk about the SD card reset, initialization and read and write methods.

Reset method:

1. High CS, send at least 74 CLK cycles to enable SD card to reach normal operating voltage and synchronize

2. Select Low CS, send CMD0, need to receive response 0x01 indicates success into idle state

3. High CS, send 8 clocks

Reset Timing Diagram:

Initialization

After the reset is successful, the SD card enters the SPI mode and should be initialized. There are two ways to initialize: (1) Send CMD1, (2) Send cmd55+acmd41. I check the information from the Internet can see this statement: If it is the MMC card CMD1,SD card is sent cmd55+acmd41. But what kind of micro SD card to send but not very clear, the online use of these two methods have been successful, but some are not successful. I myself have encountered this problem, just started to take their own mobile phone on the 2GB Micro SD card (should be a non-brand) initialized two days also did not succeed, is about to give up when remembered why do not change a try it, so find a roommate borrowed his cell phone memory card, is 2GB of Apacer Micro SD card (of course, may also be a no-name, roommate buy that card place is generally selling all kinds of cheap electronic products, we all know is no-name), the result of a try on the success. Then I used to make a method discovery can also initialize, that is to say two methods can initialize successfully. But what about my kind? Not all micro SD cards support SPI mode. I am on the internet Baidu for half a day is not sure whether all Micro SD card support SPI mode. But I think, now micro SD card production company A lot, and you do not guarantee that your micro SD card is not a non-brand. You don't know what the manufacturers have done, because there are some manufacturers of SD cards that streamline some of the commands. So it is recommended to try all two types when initializing, but I remember the second method recommended on the SD card's manual.

Here is the initialization method:

(1) Using CMD1

Send CMD1, receive 0x00 to indicate success

The timing diagram is as follows:

(2) using cmd55+acmd41

1. Send CMD55 (means use ACMDX class command), receive 0x01

2. Send ACMD41, receive 0x00 to indicate success

Remember that the initial speed of the SD card cannot be greater than 400kHz, so the SPI rate should be set low at the start of reset and initialization.

Read Stompbox and multiple blocks:

SD card read Stompbox and multi-block commands are CMD17 and CMD18 respectively, and their parameters are the start address of the area to be read. Because of the general SD card read and write requirements address alignment, so generally we will address to block, and sector (block) (512Byte) for the unit to read and write, such as read sector 0 parameter is 0, read sector 1 parameter is 1<<9 (that is, address 512), read sector 2 parameter is 2< <9 (that is, address 1024), and so on.

Read a single block method:

1. Send CMD17, receive 0x00 to indicate success

2. Read continuously until start byte 0xFE

3. Read 512 bytes

4. Read two CRC bytes

Read a single block timing diagram:

Read more than a block of methods:

1. Send CMD18 read, receive 0x00 to indicate success

2. Read continuously until start byte 0xFE

3. Read 512 bytes

4. Read two CRC bytes

5. If you want to read the next sector, repeat 2-4

6. Send CMD12 to stop read multiple operations

Write a single block and multiple blocks:

SD card with CMD24 and CMD25 to write a single block and multiple blocks, the definition of parameters and read operation is the same.

Write a single block method:

1. Send CMD24, receive 0x00 to indicate success

2. Send several clocks

3. Send Write block start byte 0xFE

4. Send 512 bytes of data

5. Send a 2-byte CRC (can all be 0xff)

6. Continuous reading until read XXX00101 indicates successful data write

7. Continue reading for busy detection (read to 0x00 indicates that the SD card is busy), when read to 0xFF indicates that the write operation is complete

To write a single block sequence diagram:

Write Multiple methods:

1. Send CMD25, receive 0x00 to indicate success

2. Send several clocks

3. Send write multi-block start byte 0xFC

4. Send 512 bytes of data

5. Send two CRC (can all be 0xff)

6. Continuous reading until read XXX00101 indicates successful data write

7. Continue reading for busy detection until the 0xFF indicates that the write operation is complete

8. Repeat 2-7 steps if you want to read the next sector

9. Send write multiple block stop byte 0xFD to stop the write operation

10. Do a busy test until you read 0xFF

The above describes the micro SD card hardware connection, reset, initialization, read and write single block and multi-block implementation method, in fact you can also read the SD card ID, to the SD card name, set the password, change the size of a read and write, here is not more introduction, we can see the official SD card information. In the next article, I will post my own write the Micro SD card program based on the Nios II, the program in the implementation of the above features, but also increased the function of reading Csd,cid register.

SOURCE program for Analog SPI

http://blog.csdn.net/ming1006/article/details/7283689

An open source code with FAT16 file system

Https://github.com/devthrash/picSD

Open source libraries on the Arduino

Https://github.com/infomaniac50/SDuFAT

An embedded file system

FatFs is a generic FAT file system module for small embedded systems

Http://elm-chan.org/fsw/ff/00index_e.html

SPI NOR Flash

Write rate is generally between 365bytek~1500kbyte

Fast reading speed, single line in 6mbyte/s

SD card speed should be fast

Single chip microcomputer SD card reading and writing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.