You are on page 1of 26

ARM7 LPC2148

SPI PROTOCOL

Prepared By
HARISH
SPI ( Synchronous Peripheral Interface)
SPI PROTOCOL:
▪ SPI is a synchronous, full duplex protocol
▪ Also called as 3 wire interface protocol because require 3 lines (SCLK, MISO,
MOSI)
▪ A controller should act as a master and other should act like slaves.
▪ A master can have multiple slaves also both the master and slave can
transmit/receive data.
▪ ARM 7 Microcontrollers possess two SPI controllers:
a) SPI0
b) SPI1
Feature of SPI
▪ 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.
SPI pin description
MOSI – Master Output Slave Input pin serves as output pin for the Master and
input for Slave.

MISO – Master Input Slave Output serves as input pin for Master and output for
the slave.

SCK – This pin generates the required clock for the communication to takes
place. Master generates clock and feed the pulse to the slave device.

SSEL – This pin is used to select the slave device when multiple slave devices are
used along with a single master.
SPI Registers

Register Description
SPCR SPI Control Register
SPSR SPI status Register
SPDR SPI Data Register
SPCCR SPI Clock Counter Register

Pin SPI0 SPI1


SCK0 P0.4 P0.17
MISO0 P0.5 P0.18
MOSI0 P0.6 P0.19
SSEL0 P0.7 P0.20
SPCR (SPI Control Register)

15:12 11:8 7 6 5 4 3 2 1: 0
RESERVED BITS SPIE LSBF MSTR CPOL CPHA BIT ENABLE RESERVED
SPSR: SPI Status Register

7 6 5 4 3 2:0
SPIF WCOL ROVR MODF ABRT RESERVED

Bit 2:0 – RESERVED


Bit 3 – ABRT: Slave abort. When 1: Error Occurred, 0: No Error.
Bit 4 – MODF: Mode fault. When 1: Error Occurred, 0: No Error.
Bit 5 – ROVR: Read overrun. When 1: Error Occurred, 0: No Error.
Bit 6 – WCOL: Write collision. When 1: Error Occurred, 0: No Error.
Bit 7 – SPIF: SPI transfer complete flag. When 1: Error Occurred, 0: No Error.
SPDR (SPI Data Register)

15:8 7: 0
Data High Data Low (8-bit Data)
(More than 8-bit Data)

▪ This bi-directional data register provides the transmit and receive data for the
SPI.
▪ 7:0 Data-Low SPI Bi-directional data port.
▪ 15:8 Data-High If bit 2 of the SPCR is 1 and bits 11:8 are other than 1000
SPCCR: SPI Clock Counter Register
SPI0 Clock counter setting.
7:0
COUNTER

The SPI0 rate may be calculated as:


PCLK / SPCCR0

The PCLK rate is CCLK /APB divider rate as determined by the APBDIV
register contents.

SPI clock rate = CCLK/(APB* SPCCR)


SPI Interrupt register
This register contains the interrupt flag for the SPI0 interface.
SPI Architecture
Master operation

1. Select the functions of the pins MISO, MOSI and SCK using PINSEL
2. Set the SPI clock counter register to the desired clock rate.
3. Set the SPI control register to the desired settings.
4. Write the data to transmitted to the SPI data register. This write starts the SPI
data transfer.
5. Wait for the SPIF bit in the SPI status register to be set to 1. The SPIF bit
will be set after the last cycle of the SPI data transfer.
6. Read the SPI status register.
7. Go to step 4 if more data is required to transmit.
Slave operation
1. Set the SPI control register to the desired settings.
2. Write the data to transmitted to the SPI data register (optional). Note that this
can only
3. be done when a slave SPI transfer is not in progress.
4. Wait for the SPIF bit in the SPI status register to be set to 1. The SPIF bit
will be set
5. after the last sampling clock edge of the SPI data transfer.
6. Read the SPI status register.
7. Read the received data from the SPI data register (optional).
8. Go to step 2 if more data is required to transmit.
MASTER PROGRAM
void spi_tx(unsigned char); void spi_tx(unsigned char data)
void spi_init(); {
int main() S0SPDR=data; //Data transmission
{ while(!(S0SPSR&(1<<7))); //Status
while(1) check
{ }
spi_init();
spi_tx('A');
}
}
void spi_init()
{
unsigned int n=60000;
PINSEL0=(1<<8)|(1<<10)|(1<<12); //Selecting SPI
pins
IODIR0=(1<<7);
IOSET0=(1<<7);
S0SPCCR=42; //SPI frequency
S0SPCR=(0<<2)|(1<<5); //master mode
IOCLR0=(1<<7);
SLAVE PROGRAM
unsigned char spi_rx();
int main()
{
unsigned char i;
PINSEL0=(1<<8)|(1<<10)|(1<<12)|(1<<14); //Selecting SPI pins
S0SPCR=0x00; //SPI Slave mode
i=spi_rx();
IO0DIR=0x0000ff00;;
IO0SET=i<<8;
while(1);
}

unsigned char spi_rx()


{
unsigned char ret;
while(!(S0SPSR&(1<<7))); //Status check
ret=S0SPDR;
return ret;
}

You might also like