SPI & I2C Protocol
SPI & I2C Protocol
• The SPI Master initiates the communication cycle when pulling low
the Slave Select SS pin of the desired Slave.
• When this is done, writing a byte to the SPI Data Register starts the SPI
clock generator, and the hardware shifts the eight bits into the Slave.
• After shifting one byte, the SPI clock generator stops, setting the end
of Transmission Flag (SPIF).
• The Master may continue to shift the next byte by writing it into SPDR,
or signal the end of packet by pulling high the Slave Select, SS line.
• When configured as a Slave, the SPI interface will remain sleeping
with MISO tri-stated as long as the SS pin is driven high.
• In this state, software may update the contents of the SPI Data
Register, SPDR, but the data will not be shifted out by incoming
clock pulses on the SCK pin until the SS pin is driven low.
• The Slave may continue to place new data to be sent into SPDR
before reading the incoming data. The last incoming byte will be
kept in the Buffer Register for later use.
• The system is single buffered in the transmit direction and double
buffered in the receive direction.
• In SPI Slave mode, the control logic will sample the incoming signal
of the SCK pin.
– When SS is held low, the SPI is activated, and MISO becomes an output if
configured so by the user. All other pins are inputs.
– When SS is driven high, all pins are inputs except MISO which can be user
configured as an output, and the SPI is passive, which means that it will not
receive incoming data.
– Note that the SPI logic will be reset once the SS pin is driven high.
– The SS pin is useful for packet / byte synchronization to keep the slave bit
counter synchronous with the master clock generator.
– When the SS pin is driven high, the SPI Slave will immediately reset the send
and receive logic, and drop any partially received data in the Shift Register.
• Master Mode
– When the SPI is configured as a Master (MSTR in SPCR is set), the
user can determine the direction of the SS pin.
– If the MSTR bit has been cleared by a slave select, it must be set
by the user to re-enable SPI master mode.
SPI Control Register – SPCR
– The SPI Data Register is a read/write register used for data transfer
between the Register File and the SPI Shift Register.
– Writing to the register initiates data transmission.
– Reading the register causes the Shift Register Receive buffer to be
read.
• WAP to initialize the SPI in master, mode 0, with a
clock freq. = fosc/16 and transmit ‘G’ via SPI
repeatedly. The data should be displayed on Port A.
WAP to initialize the SPI in slave, mode 0, with a
clock freq. = fosc/16 and transmit ‘G’ via SPI
repeatedly. The received data should be
displayed on Port A.
I2C (TWI) Protocol
• I2C or IIC : Inter-Integrated Circuit
• TWI : Two Wire Serial Interface
Data Transfer and Frame Format
• If for some reason two masters initiate I2C command at the same
time, the arbitration procedure determines which master wins
and can continue with the command.