Close

3. Accessing SD card using SPI on LPC2148

A project log for USB MicroSD card reader

Reads the data from MicroSD card interfaced to LPC2148 and sends via USB.

rutwik-narendra-jainRutwik Narendra Jain 11/26/2018 at 13:050 Comments

SD card data access can be done in SD mode as well as SPI mode. The latter is more frequently used for embedded systems. SD involves block read/write operations, a single byte can never be read or written. Typical block size is 512 bytes. 

The SPI mode is compliant with the Serial Peripheral Interface (SPI) specification. Its bus
architecture includes the following signals:
1. CS: Host to card Chip Select signal
2. CLK: Host to card clock signal
3. MOSI: (Master Out Slave In) Host to card single bit data signal
4. MISO: (Master In Slave Out) Card to host single bit data signal 

In order to configure SPI interface on the LPC2148 SSP port, the design consideration
includes,


• GPIO setting. The SPI pins, CLK, CS, MOSI, MISO need to be configured through
pin select and GPIO registers, PINSEL1, IODIR0, and IOSET0, before configuring
the SPI interface.
• SPI clock pre-scale and clock rate. Based on the APB clock (PCLK) setting in APB
Divider Control register (APBDIV), the clock pre-scale can be set through SSP Clock
pre-scale Register (SSPCPSR), and the clock rate can be controlled in the SSP
Control 0 Register (SSPCR0). The SPI clock rate in the sample test program is set to
4 MHz.
• SPI frame format and data size. The SPI format and data size can be configured
through setting the proper clock polarity bit (CPOL) and clock phase bit (CPHA) and
data size field (DSS) in the SSP control registers (SSPCR0). The data size is set to
8 bits/frame, and both CPOL and CPHA bits are set to zero.
• SPI enable/disable. The SSP port should be disabled before the GPIO pin setting,
clock pre-scale setting, frame format configuration, and enabled after all the
configuration is done to ensure a clear start. 

The SPI related APIs in the attached sample program include:
• void SPI_Init ( void );
Initializing SPI interface through configuring GPIO, VPBDIV, SSP port registers.
• void SPI_Send ( unsigned char * data_pointer, unsigned int data_length );
Sending a block of data based on the data pointer and the length of the data block.
• void SPI_Receive ( unsigned char *data_pointer, unsigned int data_length ); 

Receiving a block of data based on the data pointer and the length of the data block.

• unsigned char SPI_ReceiveByte( void );
Receiving one byte of data, the return value of the API is the received data. This API
is primarily used to obtain the command response at different phases. 

Basic SD/MMC commands

A command frame is first sent to which the SD card responds on the basis of the command number. 

The standard commands are listed below:

CMD0 GO_IDLE_STATE, reset the card to idle state

CMD1 SEND_OP_COND, ask the card in idle state to send their operation
conditions contents in the response on the MISO line. Any negative response
indicates the media card cannot be initialized correctly.
CMD16 SET_BLOCKLEN, set the block length (in bytes) for all the following block
commands, both read and write. In the sample program, the data length is set to
512 bytes.

CMD17 READ_SINGLE_BLOCK, read a block of data that its size is determined by
the SET_BLOCKLEN command.
CMD24 WRITE_BLOCK, write a block of data that its size is determined by the
SET_BLOCKLEN command. 

Discussions