Advanced PIC18 Projects—SD Card Projects:The SD Card

In this and the remaining chapters we will look at the design of more complex PIC18 microcontroller–based projects. This chapter discusses the design of Secure Digital (SD) memory card–based projects. The remaining chapters of the book describe the basic theory and design of projects based on the popular USB bus and CAN bus protocols.

The SD Card

Before going into the design details of SD card–based projects, we should take a look at the basic principles and operation of SD card memory devices. Figure 7.1 shows a typical SD card.

The SD card is a flash memory storage device designed to provide high-capacity, nonvolatile, and rewritable storage in a small size. These devices are frequently used in many electronic consumer goods, such as cameras, computers, GPS systems, mobile phones, and PDAs. The memory capacity of the SD cards is increasing all the time. Currently they are available at capacities from 256MB to 8GB. The SD cards come in three sizes: standard, mini, and micro. Table 7.1 lists the main specifications of the most common standard SD and miniSD cards.

SD card specifications are maintained by the SD Card Association, which has over six hundred members. MiniSD and microSD cards are electrically compatible with the standard SD cards and can be inserted in special adapters and used as standard SD cards in standard card slots.

Advanced PIC18 Projects—SD Card Projects-0093

SD card speeds are measured three different ways: in KB/s (kilobytes per second), in MB/s (megabytes per second), in an “x” rating similar to that of CD-ROMS where “x” is the speed corresponding to 150KB/s. The various “x” based speeds are:

• 4x: 600KB/s

• 16x: 2.4MB/s

• 40x: 6.0MB/s

• 66x: 10MB/s

In this chapter we are using the standard SD card only. The specifications of the smaller SD cards are the same and are not described further in this chapter.

SD cards can be interfaced to microcontrollers using two different protocols: SD card protocol and the SPI (Serial Peripheral Interface) protocol. The SPI protocol, being more widely used, is the one used in this chapter. The standard SD card has 9 pins with the pin layout shown in Figure 7.2. The pins have different functions depending on the interface protocol. Table 7.2 gives the function of each pin in both the SD and SPI modes of operation.

Advanced PIC18 Projects—SD Card Projects-0094

Since the SD card projects described in this chapter are based on the SPI bus protocol, it is worth looking at the specifications of this bus before proceeding to the projects themselves.

The SPI Bus

The SPI (Serial Peripheral Interface) bus is a synchronous serial bus standard named by Motorola that operates in full duplex mode. Devices on a SPI bus operate in master- slave mode, where the master device initiates the data transfer, selects a slave, and provides a clock for the slaves. The selected slave responds and sends its data to the

Advanced PIC18 Projects—SD Card Projects-0095

master at each clock pulse. The SPI bus can operate with a single master device and one or more slave devices. This simple interface is also called a “four-wire” interface.

The signals in the SPI bus are named as follows:

• MOSI—master output, slave input

• MISO—master input, slave output

• SCLK—serial clock

• SS—slave select

These signals are also named as:

• DO—data out

• DI—data in

• CLK—clock

• CD—chip select

Figure 7.3 shows the basic connection between a master device and a slave device in SPI bus. The master sends out data on line MOSI and receives data on line MISO. The slave must be selected before data transfer can take place.

Advanced PIC18 Projects—SD Card Projects-0096

Figure 7.4 shows an instance where more than one slave device is connected to the SPI bus. Here, each slave is selected individually by the master, and although all the slaves receive the clock pulses, only the selected slave device responds. If an SPI device is not selected, its data output goes into a high-impedance state so it does not interfere with the currently selected device on the bus.

Advanced PIC18 Projects—SD Card Projects-0097

Data transmission normally occurs in and out of the master and slave devices as the master supplies clock pulses. To begin a communication, the master first pulls the slave select line low for the desired slave device. Then the master issues clock pulses, and during each SPI clock cycle, a full duplex data transmission occurs.

When there are no more data to be transmitted, the master stops toggling its clock output.

The SPI bus is currently used by microcontroller interface circuits to talk to a variety of devices such as:

• Memory devices (SD cards)

• Sensors

• Real-time clocks

• Communications devices

• Displays

The advantages of the SPI bus are:

• Simple communication protocol

• Full duplex communication

• Very simple hardware interface Its disadvantages are:

• Requires four pins

• No hardware flow control

• No slave acknowledgment

It is worth remarking that there are no SPI standards governed by an international committee, so there are several versions of SPI bus implementation. In some applications, the MOSI and MISO lines are combined into a single data line, thereby reducing the line requirements to three. Some implementations have two clocks, one to capture (or display) data and the other to clock it into the device. Also, in some implementations the chip select line may be active-high rather than active-low.

Operation of the SD Card in SPI Mode

When the SD card is operated in SPI mode, only seven pins are used. Three (pins 3, 4, and 6) are used for the power supply, leaving four pins (pins 1, 2, 5, and 7) for the SPI mode of operation:

• Two power supply ground (pins 3 and 6)

• Power supply (pin 4)

• Chip select (pin 1)

• Data out (pin 7)

• Data in (pin 2)

• CLK (pin 5)

At power-up, the SD card defaults to the SD bus protocol. The card is switched to SPI mode if the Chip Select (CS) signal is asserted during reception of the reset command. When the card is in SPI mode, it only responds to SPI commands. The host may reset a card by switching the power supply off and then on again.

The mikroC compiler provides a library of commands for initializing, reading, and writing to SD cards. It is not necessary to know the internal structure of an SD card in detail before using one, since the library functions are available. However, a basic understanding of the internal structure of an SD card is helpful in making the best use of the card. In this section we will look briefly at the internal architecture and operation of SD cards.

An SD card has a set of registers that provide information about the status of the card. When the card is operated in SPI mode these are:

• Card identification register (CID)

• Card specific data register (CSD)

• SD configuration register (SCR)

• Operation control register (OCR)

The CID register consists of 16 bytes and contains the manufacturer ID, product name, product revision, card serial number, manufacturer date code, and a checksum byte. Table 7.3 shows the structure of the CID register.

Advanced PIC18 Projects—SD Card Projects-0098

The CSD register consists of 16 bytes and contains card-specific data such as the card data transfer rate, read/write block lengths, read/write currents, erase sector size, file format, write protection flags, and checksum. Table 7.4 shows the structure of the CSD register.

The SCR register is 8 bytes long and contains information about the SD card’s special features and capabilities, such as security support and data bus widths supported.

The OCR register is only 4 bytes long and stores the VDD voltage profile of the card. The OCR shows the voltage range in which the card data can be accessed.

All SD-card SPI commands are 6 bytes long, with the MSB transmitted first. Figure 7.5 shows the command format. The first byte is known as the command byte, and the remaining five bytes are called command arguments. Bit 6 of the command byte is set to 1 and the MSB bit is always 0. With the remaining six bits we have sixty-four possible commands, named CMD0 to CMD63. Some of the important commands are:

• CMD0 GO_IDLE_STATE (resets the SD card)

• CMD1 SEND_OP_COND (initializes the card)

• CMD9 SEND_CSD (gets CSD register data)

Advanced PIC18 Projects—SD Card Projects-0099

Advanced PIC18 Projects—SD Card Projects-0100

• CMD10 SEND_CID (gets CID register data)

• CMD16 SET_BLOCKLEN (selects a block length in bytes)

• CMD17 READ_SINGLE_BLOCK (reads a block of data)

• CMD24 WRITE_BLOCK (writes a block of data)

• CMD32 ERASE_WR_BLK_START_ADDR (sets the address of the first write block to be erased)

• CMD33 ERASE_WR_BLK_END_ADDR (sets the address of the last write block to be erased)

• CMD38 ERASE (erases all previously selected blocks)

In response to a command, the card sends a status byte known as R1. The MSB bit of this byte is always 0 and the other bits indicate the following error conditions:

• Card in idle state

• Erase reset

• Illegal command

• Communication CRC error

• Erase sequence error

• Address error

• Parameter error

Reading Data

The SD card in SPI mode supports single-block and multiple-block read operations. The host should set the block length. After a valid read command the card responds with a response token, followed by a data block and a CRC check. The block length can be between 1 and 512 bytes. The starting address can be any valid address in the address range of the card.

In multiple-block read operations, the card sends data blocks with each block having its own CRC check attached to the end of the block.

Writing Data

The SD card in SPI mode supports single- or multiple-block write operations. After receiving a valid write command from the host, the card responds with a response token and waits to receive a data block. A one-byte “start block” token is added to the beginning of every data block. After receiving the data block the card responds with a “data response” token, and the card is programmed as long as the data block is received with no errors.

In multiple-block write operations the host sends the data blocks one after the other, each preceded by a “start block” token. The card sends a response byte after receiving each data block.

Card Size Parameters SD cards are available in various sizes. At the time of writing, SanDisk Corporation (www.sandisk.com) offered the models and capacities shown in Table 7.5. The company may now be offering models with 4GB or even greater capacity.

In addition to the normal storage area on the card, there is also a protected area pertaining to the secured copyright management. This area can be used by applications to save security-related data and can be accessed by the host using secured read/write commands. The card write protection mechanism does not affect this area. Table 7.6 shows the size of the protected area and the data area available to the user for reading and writing data. For example, a 1GB card has 20,480 blocks (one block is 512 bytes) of protected area and 1,983,744 blocks of user data area.

Advanced PIC18 Projects—SD Card Projects-0101

Advanced PIC18 Projects—SD Card Projects-0102

Data can be written to or read from any sector of the card using raw sector access methods. In general, SD card data is structured as a file system and two DOS-formatted partitions are placed on the card: the user area and the security protected area. The size of each area is shown in Table 7.7. For example, in a 1GB card, the size of the security protected area is 519 sectors (1 sector is 512 bytes), and the size of the user data area is 1,982,976 sectors.

Advanced PIC18 Projects—SD Card Projects-0103

A card can be inserted and removed from the bus without any damage. This is because all data transfer operations are protected by cyclic redundancy check (CRC) codes, and any bit changes caused by inserting or removing a card can easily be detected. SD cards typically operate with a supply voltage of 2.7V. The maximum allowed power supply voltage is 3.6V. If the card is to be operated from a standard 5.0V supply, a voltage regulator should be used to drop the voltage to 2.7V.

Using an SD card requires the card to be inserted into a special card holder with external contacts (see Figure 7.6) so connections are easily made to the required card pins.

Advanced PIC18 Projects—SD Card Projects-0104

Incoming search terms:

Leave a comment

Your email address will not be published. Required fields are marked *