The secure digital card (SD) is a low cost, non-volatile memory card format developed past the SD Carte du jour Clan. Since its inception back at the beginning of the century, the demand for this medium-sized, free energy and infinite-efficient, the memory storage device has been growing at a fast rate. Therefore, to run across the market requirements, the SDA was set upwards every bit a non-profit organization to promote and create SD Card standards. There are various topics related to the SD carte du jour such as the dissimilar device families, speed classes, smart cards, carte du jour security and so on and it is used in various markets like digital cameras, personal computers, and embedded systems. Some of the standard variations include SD, SDHC, SDXC, SD-ultra high speed etc. The microSD is the miniaturized SD retentivity card format with a small form gene and is widely used in various electronic devices.
What we are going to learn is the use of SD cards in an embedded system. To exist specific, we will be dealing with the use of SD cards in small embedded systems.

Circuit and Interfacing

SD card has a native host interface apart from the SPI style for communicating with master devices. The native interface uses 4 lines for data transfer where the microcontroller has SD card controller module and it needs separate license to use it. Since the SPI is a widely used protocol and it is available in most low-price microcontrollers, the SPI mode is the widely used interface in low cost embedded systems. The working voltage range of SD family is 2.7V to 3.6V and this is indicated in the operation condition register (OCR).

2000px-sd_pins-svg

sd-card

SD Card pinout

sd-card-2

MicroSD Card pinout

Most micro-controllers use the SPI advice protocol to interface with the SD cards. The SD cards have a microcontroller that shows their availability to the master controller(microcontroller). The micro-controller sees the SD card equally an addressable sector on which read/write functions are possible. Once the microcontroller is in the SPI mode, communication between the main and the slave is done via 4 pins viz. clock, scrap select, data in and data out. Information technology should exist kept in mind that throughout the communication between the two devices, the micro-controller will exist sending out the clock.
Nearly development boards accept a defended SD card slot. But to understand the connections, allow us analyze this fairly simple excursion.

Circuit

sd-card-circuit

If the logic level of the microcontroller is different than the SD bill of fare, level shifter needs to be used for converting the line voltages.
The MISO (master in serial out) pin should exist connected to the SDI (series data in) pin on the microcontroller.
The MOSI (master out serial in) pin should be connected to the SDO (serial data out) pin on the microcontroller.
The SCK (serial clock) pivot should be connected to the SCK (serial clock) pivot on the microcontroller.
The CS (chip select) pin should be continued to the respective CS pin on the microcontroller or on any digital I/O pivot on the microcontroller. A common footing is provided.

Important Annotation: While connecting the ability supply, make certain that the supply is drawn from a 3.3V supply as a 5V supply would see your carte get up in smoke.

Interfacing

Once the connections are gear up, we are ready to interface our hardware with the software.
Set the directions of each of the four pins correctly. While activating the SPI style, an ideal configuration should be, mode(0,0) and the phase with input sampled at the middle of information out. Besides, the clock frequency should be set in the range of 100 kHz and 400 kHz prior to initializing the card. Once the initialization is done, the clock tin be set to a more desired frequency.

SD Commands

Next comes the catchy part, initializing the SD carte and performing the raw data communication. A systematic approach to programming the software would brand the chore pretty easy.
But first, it is important to learn how the micro-controller activates the SD menu. There are a stock-still fix of commands and responses, which must be followed to create a command to response structure in our program. The data is transmitted in a byte-oriented format with a definite length.
The following table shows the necessary commands to the menu and the corresponding response from the card.

sd-card-3

Every command has a constant length of 6 bytes.
The offset byte is the add-on of the command number and the number 64.
Example:
For CMD0: command number 0 + 64 = 64 = 0x40 in hexadecimal.
For CMD1: command number 1 + 64 = 65 = 0x41 in hexadecimal.
And so on.
This is followed by a ready of four bytes which are known every bit the arguments. These arguments commonly contain the accost of a data or the length of a cake.
The last byte is the CRC (Cyclic Redundancy Check) Byte. Nearly commands in the SPI mode does non require a check byte if the CRC feature isn't enabled. For some commands like CMD0, the CRC is 0x95 and in nearly cases, a 0xFF is sent. Enabling the CRC requires you to send the correct cheque byte from the micro-controller. So, ensure whether the CRC feature is enabled or disabled.
A control frame looks like this-

Interfacing Microcontrollers with SD Card - Command frame

The bill of fare will receive a command when the DO pivot (Information Out) is driven high as shown above. The CS pin (Chip Select) must be driven loftier to low before sending the control and must exist kept low during the process. The fourth dimension between a command and its response is known as the command response time (NCR). Every bit mentioned earlier, regarding the clock pulses from the micro-controller, the corresponding response byte sent dorsum from the card to the microcontroller should be driven past the serial clock pulses of the micro-controller. In that location is a chance that the response byte from the card might get skipped by the microcontroller due to the absence of a driving clock pulse. Hence information technology is necessary to make sure that an 8-Chip clock pulse is sent to the Card soon after the Command Frame is sent (Refer the TECHNICAL NOTE below). Also while receiving a Byte from the card, the DI pin (Information In) must be driven loftier.

Furthermore, let us analyze the different types of responses that we get from the card and what they mean.

response_color

An R1 response, 0x01 ways that the control sent prior to the response has resulted in the card going into an idle land.
A response byte 0x00 means that the command has been accepted and the card will be waiting for the proposed event to accept place. If any other bits in an R1 response is set, it is the upshot of an error and it'll be down to the factor mentioned in each R1 response scrap in the figure.

Initializing the SD Menu

Now, as far equally sending the commands are concerned, there is an order in which they must exist sent. Just the commands, CMD0, CMD1, ACMD41, CMD58 and CMD59 will be accepted when the card is in its idle state. Sending whatsoever other command volition likely to yield an illegal response.

TECHNICAL Notation: After interfacing the card, the micro-controller must ever send a fix of bytes, which nosotros will refer to as dummy bytes. I dummy byte is 0xFF. These dummy bytes have a simple even so significant purpose. Prior to the initialization, the card must know the frequency at which the data is being sent. Past sending around 75 dummy bits approximately (dummy byte * 10 times = 80 bits), the card will be set for communication. Also even subsequently every command is sent, it is a skilful practice to send at least i dummy byte. A logical caption for this is that communication is driven by the clock pulse of the micro-controller. The clock pulse is sent only when the information buffer is filled. Subsequently every response is sent and prior to the side by side command or between command and response, the SCK will end generating pulses due to an empty data buffer. To ensure the continual manual of clock pulses between every command, fill the data buffer with a junk value such as a dummy byte. To create a dummy byte part with the number of loops as the argument.

The outset command to be sent to the carte is the CMD0 command. The construction of every other command-response should be based on this model.

  • Drive the DO pin high.
  • Drive the CS pin from loftier to low.
  • Send a dummy byte.
  • Then send the post-obit 6-byte command continuously
    Starting time byte: 0x40
    Side by side 4 bytes: 0x00000000
    CRC byte: 0x95
  • Transport some other dummy byte or equally many every bit required, to generate the SCK giving time for the SD card to respond to the command request.
  • Wait for the receive flag scrap to set and then read the response from the SD card or create a loop to read the response a few number of times. The response should attain dorsum within the command to response (Ncr) time. Or else, something must exist incorrect.
  • The desired response is 0x01, meaning the card is in the idle state and we are good to go.

Transport the CMD8 command adjacent to bank check the version of your SD carte.
The difference in the 6-byte commands are
Showtime byte: 0x48
Next four bytes: 0x000001AA
CRC byte: 0x87

We are looking for two possibilities in our response byte. Either 0x01 or 0x05.

A 0x01 response means that you have a version 2 SD card. The 0x01 response is followed past the 4 bytes 0x00, 0x00, 0x01, 0xAA in the order of their transmission from the SD card which is, in fact, the argument you send in your command.

If the response is 0x05, it means the card is a version 1 or an MMC card. If the card is really a version 2 SD card, and so this response is the result of an illegal control. Also, the menu is now in the idle state.

Once the above two commands (CMD0 and CMD8) are done, it is safe to say that our SD Card is working in good condition and prepare for data Read/Write.
Additionally, just to ensure whether the SD Card is functioning in the correct working voltage, ship the CMD58 Control.

Next, we must initiate the initialization procedure. For this send a CMD1 command and wait for response 0x00, meaning the idle state bit is cleared.
If you are using an SDC or for the purpose of creating a general code, information technology is advisable to send an ACMD41 command offset to brainstorm the initialization process so send the CMD1 command if the ACMD41 is rejected.
Either manner, the initialization process might take a few hundred milliseconds, so iterating a few number of times to check for the response byte would be advisable.

Suppose the response of CMD8 resulted in 0x05, a few considerations have to be made regarding the initialization procedure. Version 1 cards may not back up the ACMD41 control. And even if they do support the ACMD41 command, send the CMD55 command and check for the response, which volition be 0x05 instead of 0x01 and and then instead of sending the CMD41 control, ship a CMD1 command to complete the initialization process.

Read/Write SD Card

Now, we are set up for the raw information read/write.

The first thing to do here is to fix the cake size of the read/write block.
For this, send CMD16 command. The arguments for this command should specify the cake length.
Case: To set the block length to 512 bytes, send the four-byte argument as 0x00000200.
Ship a 0xFF every bit the CRC byte and look for a 0x00 response byte.

Regarding the commands for the read/write, the general structure for sending the commands is the same, but with a few additional details also.

The 4-byte statement of every command will contain the specified address of the sector from where the information is being read or written to.

Transport the CMD17 command for a single read block and wait for the expected response 0x00. In one case that is washed, wait for the SD card to send some other response, 0xFE. This response, transmitted past the SD card, is an indication for the start of continuous transmission of a block of data. At the end of this, transport a dummy byte or two and the process is complete.

Single Block Read Timing Diagram

cmd17_color

Multi-Cake Read Timing Diagram

This content is exclusive for our registered users. Create a free account to access the content.

SignUp/SignIn

In the case of CMD18 for a multi-read block, follow the procedure up until the 0xFE response. Later on this, the carte du jour will be transmitting blocks of data till it receives a CMD12 command, which is the indication from the microcontroller that the block read is over.

data_tok_clr

In case of performing a unmarried block write or a multi-block write, ship CMD24 or CMD25 respectively and wait for the response 0x00. Every block write requires a start token 0xFE in example of a single block write and 0xFC in case of a multi-cake write to enable the card to outset writing a unmarried block of data onto the specified sector. Ensure 0xFC is sent for every block-write during a multi-block write. Afterwards every block writes, a terminate token 0xFD must be issued (not required in the case of single block write). After the completion of all the data block write, a CMD13 command is mandatory.
Do not forget to transport dummy bytes between responses besides equally prior to sending start tokens.

Single Block Write Timing Diagram

This content is exclusive for our registered users. Create a free account to access the content.

SignUp/SignIn

Multi-Cake Write Timing Diagram

This content is exclusive for our registered users. Create a gratuitous business relationship to access the content.

SignUp/SignIn

Interfacing Microcontrollers with SD Card – Flow diagram

Creating a role for each of the read/write sections is the platonic way to code the software. This is the basic structure to exist followed during read/write operations performed on raw data on an SD card.

This content is exclusive for our registered users. Create a free account to access the content.

SignUp/SignIn

Farther reading

  • Raw SD Read/Write Using PIC18F4550
  • SD Card Using Petit File Arrangement
Spread the love, share this