4.9 SPI System (Serial Peripheral Interface)
In addition to UART system, the AT89S8253 has also another system for serial communication which doesn’t fall into the 8051 Standard. It is SPI system which provides a high-speed synchronous data transfer between the microcontroller and one or more peripheral devices or between multiple microcontrollers. Here, one microcontroller is always considered main and is called master therefore. It defines rate, transfer direction (whether data is to be transferred or received) and data format. The other is slave device which is in subordinated position, which further means that it cannot start data transfer, but has to adjust to conditions set by the master device.
The data are transferred via full duplex
connection using 3 conductors connected to pins MISO (P1.6), MOSI (P1.5) and SCK (P1.7). The forth pin-control pin SS- is not used on the master side and may be used as a general-purpose input/output therefore, while on the slave side it must have voltage level 0. When the SS pin on the slave side is set, its SPI system is deactivated and the MOSI pin can be used as a general-purpose input.
As shown on the schematic, pins MISO and MOSI are configured differently in the master and slave device (as inputs or outputs), which is determined by the MSTR bit of the SPCR register.
Knowing abbraviations makes connection easier:
MISO - master in, slave out; MOSI - master out, slave in; SCK - serial clock; SS - slave select;
Similar to many other circuits within the microcontroller, the SPI system can also be configured to operate in several modes.
Normal SPI mode (buffer out of use)
Data written to the SPI data register SPDR is automatically transferred to an 8- bit shift register. SPI clock generator is enabled and serial data appears on the MOSI pin. An initial delay may occur for the sake of synchronization with the main oscillator.
After sending one byte, the SPI clock generator stops, the SPIF bit (flag) is set, the received byte is transferred to the SPDR register and, if enabled, an interrupt is generated.
Any attempt to write another byte to the SPDR register while byte transmit is in progress will cause the WCOL bit to be set. It indicates that an error has occured. However, the byte will be succesfully transmitted, while the new byte will be ignored, i.e. it will not be transmitted.
Enhanced SPI mode (buffer in use)
Enhanced mode is similar to normal except that this time data goes through one more register while being transmitted. It makes no sense at first sight, but communication is really faster. Look at the figure below...
Data written to the SPI data register SPDR is automatically transferred to the capture register (buffer), which causes the WCOL bit to be set. It means that the buffer is full and any further write will cause an overflow. Control electronics (hardware) cleares this bit after transmitting data from buffer to the shift register and after commencing serial data transmit. If the byte sent is the first, the data is immediately transmitted to the shift register (still empty), thus clearing the WCOL bit (buffer is empty).
While one byte transmit is in progress, the next byte to transmit may be written to the SPDR register. It will be immediately moved to buffer. In order to check whether data transmit is in progress, it is sufficient to check the logic state of the LDEN bit of the SPSR register. If this bit is set (Load Enable
) and the WCOL bit is cleared, data transmit is in progress and buffer is empty so the next byte can be written to the SPDR register.
How to select the right mode? If individual bytes are sent occasionally then there is no need to complicate- the best solution is the normal mode. If it is necessary to send a great amounts of data, it is better to use enhanced mode in which the clock oscillator is enabled as far as buffer is regularly loaded and the WCOL bit is set. In addition, no time is needed for synchronization and data is easily and efficiently transferred.
The SPI system is under control of 3 special function registers. These are SPDR, SPSR and SPCR.
SPDR (SPI Data Register)
The SPDR register is used for storing data to be transferred via SPI (in serial format). It is also used for storing received data.
SPSR (SPI Status Register)
Interrupt flag. Upon data transfer, this bit is automatically set and an interrupt is generated if SPIE=1 and ES=1. The SPIF bit is cleared by reading SPSR followed by reading/writing SPDR register.
This bit is set in normal mode (ENH=0) if the SPDR register is written during data transfer is in progress. The write is premature and has no effect. It is called Write Collision
. This bit is cleared in the same manner as the SPIF bit.
The bit is set in enhanced mode (ENH=1) when buffer is full. It is indication that a new data is ready to be transmitted to the shift register.
In enhanced mode, a new data can be written to buffer when the WCOL bit is set. In addition, the WCOL bit must be cleared.
When set, this bit causes the MISO pin to float, thus enabling several slave
microcontrollers to share the same interface. Normally, the first byte, called address byte, is received by all of them, but only one should clear its DISSO bit.
0 SPI system operates in normal mode (without buffer).
1 SPI system operates in enhanced mode.
SPCR (SPI Control Register)
When this bit is set, the SPI system can generate an interrupt.
This bit enables SPI communication. When set, pins SS, MOSI, MISO and SCK are connected to the microcontroller pins P1.4, P1.5, P1.6 and P1.7.
Bit determines which bytes in serial communication are to be sent first:
- 0 - MSB bit is sent first.
- 1 - LSB bit is sent first.
Bit determines whether the microcontroller is to operate as master
- 0 - Operate as slave.
- 1 - Operate as master.
Bit controls the SCK pin logic state when the SPI communication is not in progress:
- 0 - Pin SCK is cleared.
- 1 - Pin SCK is set.
This bit along with the CPOL bit controls relation between clock and data in serial format. Refer to the figure below.
When SPI system operates as master
, these two bits determine boud rate, i.e. clock signal frequency of the master
device. When operates as slave
, these bits have no effect and SPI system operates at a rate imposed by the master
Serial data format if CPHA=0
* not defined. It is usually MSB of previously received byte.
Serial data format if CPHA=1
* not defined. It is usually LSB of previously received byte.
Two things are important to remember when configuring SPI system:
- Master should be configured before slave.
- When writing bits to the SPCR register, the SPE bit enabling SPI should be set last, i.e. after setting all other parameters.