Thursday, 28 March 2019

SD card interfacing with LPC2148 using SPI proocol


  • SPI Interface uses four wires for communication. Hence it is also known as four wire serial communication protocol.
  • SPI is a full duplex master-slave communication protocol. This means that only a single master and a single slave can communicate on the interface bus at the same time.
  • SPI enabled devices work in two basic modes of SPI operation i.e. SPI Master Mode and SPI Slave Mode.
  • Master Device is responsible for initiation of communication.
  • Master Device generates Serial Clock for synchronous data transfer.
  • Master Device can handle multiple slave devices on the bus by selecting them one by one.

SPI Module:

Features of SPI in LPC2148:
  • Single complete and independent SPI controller.
  •  Compliant with Serial Peripheral Interface (SPI) specification.
  •  Synchronous, Serial, Full Duplex Communication.
  •  Combined SPI master and slave.
  •  Maximum data bit rate of one eighth of the input clock rate.
  •  8 to 16 bits per transfer
Pin Description:


SPI Registers:


SD Card:
Digital (SD) cards are removable flash-based storage device SD means ‘secure digital’ and MMC means ‘multimedia card.’ You can insert these cards in your media player, PDA or digital camera. Their small size, relative simplicity, low power consumption and low cost make them an ideal solution for many applications.

  • SD/MMC cards have their own architecture and signals.
  • These are universal low-cost, high-speed data storage cards. 
  • MMCs work at 20 MHz, while SD cards work at up to 25 MHz's, 
  • The two memories work in two different modes: SD mode and serial peripheral interface (SPI).

Interfacing diagram:

Explanation:
SD Memory interfaces to the host point-to-point (in Fig. an ARM microcontroller is the host). This type of interfacing is very popular in the industry. In serial peripheral interface (SPI) mode, you can use following signals of the host:
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
Now many companies are manufacturing suitable hosts for the SD bus interface.
For example, Philips is manufacturing LPC2148 microcontroller with MOSI and MISO

•Master-slave mode of communication is used for multiple slave devices in the SD architecture. 
• MOSI is a unidirectional signal used to transfer serial data from the master to the slave. When the host is master, data can move from the host to the SD card. That’s why MOSI is connected to data input (DI) of the SD/MMC card. 
•The MISO signal transfers serial data from the slave to the master. When the SD is a slave, serial data is output on MISO signal. When the SD is a master, it clocks in serial data from this signal. 
•SD memory cards use 1- or 4-bit bus width and star topology to connect multiple cards, while MMC cards use 1-bit bus width and bus topology for reading multiple cards.
Steps for Master Mode:

Steps for slave Mode:

Flow Chart:
Code: SPI Initialization 


/**********************************************************
Expt. ***: SPI interfacing with to LPC2148
Platform:  LPC2148 Development Board.
College: PICT
**********************************************************/       
/********************************/
           Function for initializing SPI
/********************************/
void spi_init()
{
 PINSEL0=0X00001505;// Select MOSI=P0.6, MISO=P0.5, SCK=P0.4,SSEL=P0.7
S0SPCCR=0X08; // clock is divided by 8 ( SPI Clock freq = PCLK / S0SPCCR Value)
S0SPCR=0X0020; // select as master mode
}
/********************************/
           Function for sending a char 
/********************************/
void spi_master(char a)
{
S0SPDR=a;   //write character “a” to be transmitted in S0SPDR
while(!(S0SPSR & 0X80)); // wait till SPIF=1 i.e., complete transfer of data
}
/********************************/
           Function for receiving a char 
/********************************/
char spi_slave(void)
{
while(!(S0SPSR & 0X80)); // wait till SPIF=1 i.e., complete reception of data

return S0SPDR; //pick-up received character which is arrived in S0SPDR

}

No comments:

Post a Comment