博客首页 | 排行榜 |

设计我最赞的博客

个人档案
博文分类
性价比好的移动数据存储(Cost-Effective Mobile Data Storage)  2008-11-25 11:53
Interface an SD Memory Card with an MCU

When it comes to designing an embedded application, memory matters. In this article,Mandar explains how to expand a microcontroller‘s nonvolatile data storage capacity with an SD card. It's an intelligent way to handle the excessive memory requirements of typical embedded applications.

In the last couple of years, the Secure Digital (SD) memory card has become a widely-accepted memory solution for a variety of portable devices and equipment, including cameras, PDAs, mobile phones, GPS navigation modules, gaming consoles,MP3 players, and more. Another useful technology is the high-performance 8-bit RISC Atmel AVR microcontroller,which is suitable for a variety of applications. The ATmega32 has 32 KB of flash memory that can be used to store code (e.g., Hex files)。 But what if your embedded system is memory hungry for applications, such as a data logger for a weather station or an industrial automation application to log alarms and trends? Both systems require more nonvolatile memory storage than the ATmega32 has to offer (flash memory/EEPROM)。

In this article, I will describe how to interface an SD card to expand an AVR microcontroller‘s nonvolatile data storage capacity, which should fulfill the excessive memory requirements of a typical embedded application. In this project, an ATmega32L is interfaced with 256- and 512-MB SD cards from Kingston Technology and a 1-GB SD card from Transcend Information and SanDisk (see Photo 1)。

DESIGN CONSIDERATIONS
The first design considerations for this type of application are the supply voltage, the current requirement, and I/O lines and form factor. SD cards typically operate at 3.3 V, with a modest current requirement of 75 mA (maximum)。Next, consider that the number of microcontroller I/O lines used to connect external storage must be kept to a minimum to interface. SD cards support the SPI protocol,so just four I/O lines are required to interface. The form factor of an SD card is 24 mm ×32 mm × 2.1 mm. (Refer to the file labeled pinout details.doc posted on the Circuit Cellar FTP site.) The previous design considerations are the main advantages of interfacing an SD card in an embedded system.

Compared with other available flash memories, an SD card is the better option. For instance, the 512-KB AT29LV512 flash chip requires 15 address lines, eight data lines, and three control lines, when interfaced with a microcontroller.

SPI and I2C-based external memory devices require four or two interface lines, but the maximum memory capacity of these devices can go up to 64 MB (i.e, the AT45DB642D, which costs $6 at Mouser Electronics)。 Cost is an important factor. A 1-GB Transcend SD card costs $6.15 (Source:Amazon.com)。

The on-chip integrated SPI available in AVR microcontrollers is an advantage of interfacing and accessing the SD memory card (see Figure 1)。 Compared with other popular detachable memory cards (i.e., CompactFlash), the SD card supersedes it in terms of form factor, cost, and ease of interfacing(number of connections)。

HISTORY
The SD card is specifically designed to meet the security, capacity, performance,and environmental requirements inherent in newly emerging audio and video consumer electronic devices. In August of 1999, Matsushita Electric Industrial, SanDisk, and Toshiba announced an agreement to jointly develop, specify, and widely promote a next-generation secure memory card called the SD memory card. At the 2000 Consumer Electronics Show, the three companies announced that a new industry-wide association would be created to set industry standards for the proprietary SD memory card and promote its wide acceptance in digital applications.

The new organization, named the SD Card Association (SDA), is based in California and its executive membership includes about 30 world-leading high-technology companies and major content companies. Sampling of the SD card began in the first quarter of 2000 and production shipments commenced in the second quarter of 2000.

HARDWARE
The SD card has a controller in it(see Figure 2)。 The flash memory control enables you to perform basic functions including erase, read, write, and error control inside the memory card. Data is transferred between an SD card and a host-interfaced microcontroller in units of 512 bytes per block.

The first piece of hardware in the project is the SD memory card header. To enable you to easily connect to the SD card, I used a 3M card connector SD (Mouser Electronics, SDRSMT- 2-MQ)。

The second piece of hardware is the Transcend 1-GB TS1GSDC SD memory card. It is fully compatible with SD card specification V1.1. To test the setup, 256- and 512-MB SD cards(from Kingston) and a 1-GB SD card(from SanDisk) were also procured and tested.

The third piece is the ATmega32L microcontroller. The ATmega32L- 8PU is the best choice for this setup because the operating voltage is from 2.7 to 5.5 V and it operates at 8 MHz. The 1-KB internal SRAM is advantageous because you can declare a buffer size of 512 bytes to exchange data with the SD card. A SPI is an integral part of the microcontroller. The full SPI details are covered in the ATmega32‘s datasheet. The microcontroller and SD card are powered up at 3.3 V. The fourth piece is the serial interface.

The setup has a MAX232 interfaced to a microcontroller, by which the microcontroller can transmit the block (512 bytes) read from the SD card and the response (R1) to HyperTerminal(see Photo 2)。

FIRMWARE DESIGN DETAILS
The SD card has a dual personality. The first one is SD Protocol (1 bit or 4 bits) Native mode. The second,which is supported, is the SPI mode of the SD card protocol. It is distinct from the 1- and 4-bit protocols in that the protocol operates over a generic and well-known bus interface, SPI (see Figure 3)。

A SPI is a synchronous serial protocol that is extremely popular for interfacing peripheral devices with microcontrollers,where a microcontroller is configured as a master and an SD card as a slave. The initialization is covered in the firmware routine. The microcontroller development board communicates with a PC via an RS-232 interface. The status and read data is transmitted to HyperTerminal.

SD COMMAND FORMAT
As you can see in Figure 3, the command that is transmitted by the microcontroller to the SD card is in a 6-byte packet. The first byte is the command. The next 4 bytes are the argument. The sixth byte is the CRC value. To construct the first byte, you have to OR the SD card command with 0x40.

Note that for this application and the sake of simplicity in explanation,I‘ve ignored CRC checksum implementation. Refer to Table 1 for important SD commands.

COMMAND RESPONSE
The three command response formats:R1, R2, and R3 depend on each SD command. A byte of response R1 is returned for most of the commands.

The card sends the R1 response token after every command with the exception of SEND_STATUS commands. It is 1 byte long and the most significant bit (MSB) is always set to zero. The other bits are error indications. An error is signaled by a 1. The structure of the R1 format can be seen in Figure 4.

The idle state flag means the card is in the idle state and running the initializing process. Erase reset means an erase sequence was cleared before executing because an out-of-erase sequence command was received. In illegal command, an illegal command code was detected. In communication CRC error, the CRC checks of the last command failed. In erase sequence error,an error in the sequence of erase commands occurred. In address error, a misaligned address that did not match the block length is used in the command. In parameter error, the command's argument(e.g., address, block length) was outside the allowed range for this card.

INITIALIZE THE CARD IN SPI MODE
During powerup, the SD card defaults to the proprietary SD bus protocol. To switch the card to SPI mode, the following procedure must be followed: After power on, wait a few milliseconds for the power supply to stabilize. Then, set the MOSI and Slave Select (SS) lines to logic high and apply more than 74 pulses to the SCLK line and the card will be able to accept a native command. Set the SS line to logic low.

This enables/selects the SD card. Now send CMD0. Reset the SD card (i.e., 0x40 0x00 0x00 0x00 0x00 0x95)。 The first byte is the command and the last byte is CRC. This switches the card to SPI mode,but it is still in the IDLE state. Send 0xFF and check the response. If you get 0x01, go to the next step or repeat sending 0xFF for eight to 10 times. If you don‘t see a 0x01 (R1) response in eight to 10 retries, something is wrong with the setup or the SD card. So let's talk about interfacing with an SD card in SPI mode!

Now that the SD card is in SPI mode,initialize the card by sending command CMD 1 (starts card initialization for low-capacity cards like 256- or 512-MB SD cards or the 1-GB SD card from SanDisk)。 Set the SS to logic low. This enables/selects the SD card. Send the following 6 bytes: 0x41 0x00 0x00 0x00 0x00 0xFF. This is the format of transmitting the CMD1 command to the SD card. This leads to the SD card‘s initialization. Send 0xFF and check the response. If the return value is "0xFF,"

ignore it and repeat sending byte"0xFF" eight to 10 times. If you see 0xFF as the response, something is wrong with the setup or the SD card. If you get 0x00 as the response, continue with the next step. If you get 0x01, the card is still in the idle state. Set the SS to logic high, send 0xFF to the SCLK,and ignore the return value. This is the end of command clocking. At this point, the card is initialized and ready to go for single-block write and singleblock read operations.

INITIALIZATION PROCEDURE
While interfacing an SD card with an AVR microcontroller, SD cards of different makes and capacities were procured:a Transcend (1 GB), a SanDisk(1 GB), a Kingston (512 MB), and a Kingston (256 MB)。 To support backward compatibility with the MultiMediaCard(MMC), lower capacity 256-MB and 512-MB SD cards from Kingston support the command CMD1 for card initialization by default.

A 1-GB card from SanDisk would not accept the CMD1 command because the card fully complies with the SD card standard "Physical Layer Specification" (Version 2.00)。 According to these specifications, CMD1 is reserved. It should not be used for SD card initialization.
 
There is an initialization sequence for a SanDisk 1-GB SD card. A different set of commands needs to be transmitted after CMD0 (i.e., the SD card is in "idle state")。 The R1 response is 0x01. Now the initialization sequence is different. Transmit CMD55 is the prefix command for application-specific command ACMDxx. The command format is(0x40 | 0x37), 0x00, 0x00, 0x00, 0x00,0xFF. Check for the R1 response by transmitting 0xFF eight times (the maximum Ncr response time)。 The R1 response for CMD55 can be ignored.

Now transmit the application-specific command to start card initialization(ACMD41)。 The command format is(0x40 | 0x29), 0x00, 0x00, 0x00, 0x00,0xFF. Check for the R1 response by transmitting 0xFF eight times. The R1 response for ACMD41 should be 0x00. This indicates that the card is initialized and ready to perform a read/write operation.

COMMAND RESPONSE TIME
Some SD cards took a longer time to respond for a command than a specific NCR time. The command response time can vary from 0 to 8 bytes after NCR. So the best option is to transmit 0xFF, after 6-byte command packets and wait for maximum NCR time by transmitting 0xFF eight times (see Figure 5)。


SD CARD REGISTERS
Six registers are defined within the card interface: OCR, CID, CSD, RCA,DSR, and SCR. They can be accessed only by corresponding commands. If the application demands, you can read these registers at this stage. For instance, to read the operating conditions register (OCR), the 32-bit operation conditions register stores the VDD voltage profile of the card. The SD card is capable of executing the voltage recognition procedure with any standard SD card host using operating voltages from 2 to 3.6 V.

Accessing the data in the memory array, however, requires 2.7 to 3.6 V. The OCR shows the voltage range in which the card data can be accessed. Send CMD58. Read OCR. Send byte "0xFF"five times after transmitting CMD58 to read the R3 response. You get an R1 response for every CMD58 command transmitted to the SD card (see Figure 6)。

The other two important SD card registers are card identification (CID)register and card specific data (CSD)register. Please refer to the secure digital card product manual from SanDisk for more information.

One or more data blocks will be sent in a write operation from the microcontroller to the SD card and vice-versa in a read operation. A data block is transferred because a data packet consists of Data Token, Data Block, and CRC (see Figure 7)。

SINGLE BLOCK WRITE
To write a single block (512 bytes) of data to the SD card, the microcontroller transmits the command CMD24 (0x40 | 0x18) to the SD card. Once the command is accepted by the SD card, it sends an R1 response as 0x00 to the microcontroller (see Figure 8)。 A microcontroller sends a Data Token 0xFE,followed by a Data Packet of 512 bytes. A card responds with Data Response 0xE5 (i.e., data accepted)。

SINGLE BLOCK READ
To read a single block (512 bytes) of data from the SD card, the microcontroller transmits the command CMD17(0x40 | 0x11) to the SD card. Once the command is accepted by the SD card, it sends an R1 response as 0x00 to the microcontroller. The command is transmitted along with an argument that specifies the location to start to read in bytes. A read operation is initiated and the read data block will be sent to the microcontroller (see Figure 9)。 After a valid data token (0xFE) is detected, the microcontroller receives the data block followed by 2 bytes of CRC.

At this point, you have completed a single block write task and a single block read. If you are interested in a multiple-block write and read, refer to the Resources section.

COMMERCIAL APPLICATION
Markets such as industrial automation,medical, automotive, data collection,and portable computing devices demand more memory in less space. In those markets, the need for reliable and high-performance data storage in a small form factor is increasingly critical.

In industrial automation, a large amount of data (e.g., alarms, trends, and different parameters) is logged for analysis for preventive maintenance or to optimize process operation. One option is a network of field devices. Or if the field device is stand-alone or even if the application is demanding for redundancy in case of network failure, then the field device will have an interface for detachable nonvolatile memory storage(SD card)。 Consider a particular example:a human machine interface (HMI)works as a stand-alone device and is interfaced with a logic controller. It has to log alarms, trends, and different parameters at a regular interval onto the SD card. The logged data will be used for predictive analysis and preventative maintenance or process optimization.

FINDINGS
An SD memory card enables you to implement robust, reliable, high-performance,and cost-effective memory storage for today‘s most demanding industrial applications and portable computing devices. SD cards offer a cost-effective way to store a large amount of data in a removable memory storage device. Its interfacing flexibility and small form factor make it ideal for use with microcontrollers in embedded applications. Multivendor product support and a wide range of applications incorporating SD cards is another plus point.

Combined with the low-cost, lowpower ATmega32 and its advanced features(including an on-chip SPI), you can quickly implement an affordable data-logging solution.

Author‘s note: I want to thank Yashomani Kolhatkar (Ph.D.) for proofreading this article. He provided a lot of valuable advice.

Mandar Bagul (mandar.bagul01@gmail. com) is a lead engineer in the Department of Industrial Electronics at General Electric‘s India Innovation Center in Hyderabad, India. During his six years at GE, he has worked extensively in the field of embedded product design and development, which includes microcontrollers and programmable logic controllers. Mandar earned a Bachelor‘s degree in electronics engineering at Shivaji University in Maharashtra,India.

PROJECT FILES
To download code, go to ftp://ftp.circuitcellar.com/pub/Circuit_Cellar/2008/221.

RESOURCES
Atmel Corp., "ATmega32 Datasheet:8-bit Microcontroller with 32K Bytes In-System Programmable Flash,"2503N, 2008.

AVR freaks, www.avrfreaks.net.

SanDisk Corp., "Secure Digital Card Product Manual," version 1.9, 80-13-00169, 2003.

Santa Clara University, Engineering department, Embedded Systems Resources, www.mil.ufl.edu/~chrisarnold/components/microcontrollerBoard/AVR/avrlib/docs/html/index.html.

SD Card Association, www.sdcard.org.

P. Shinde and T. Love, "Interfacing SD/MMC Cards With TMS320F28xxx DSCs," Texas Instruments, Inc.,Application report SPRAAO7, 2007,http://focus.tij.co.jp/jp/lit/an/spraao7/spraao7.pdf.

SOURCES
AT29LV512 Flash memory,AT45DB642D DataFlash, ATmega16L-8PU microcontroller, ATmega32L microcontroller, and AVR Studio
Atmel Corp.
www.atmel.com

256- and 512-MB SD Cards
Kingston Technology Corp.
www.kingston.com

Card connector SD
3M
www.3m.com

TS1GSDC SD Memory card
Transcend Information, Inc.
www.transcendusa.com

WinAVR
WinAVR
http://winavr.sourceforge.net
类别:可编程逻辑 |
上一篇:电子ID系统(Electronic ID System) | 下一篇:一种动态牌的展示(A Dynamic Tile Display)
以下网友评论只代表其个人观点,不代表本网站的观点或立场