S12SPIV4
S12SPIV4
SPI
Block Guide
V04.01
Freescale Semiconductor, Inc...
Motorola, Inc.
Motorola reserves the right to make changes without further notice to any products herein. Motorola makes no warranty, representation or guarantee regarding the suitability of its
products for any particular purpose, nor does Motorola assume any liability arising out of the application or use of any product or circuit, and specifically disclaims any and all liability,
including without limitation consequential or incidental damages. “Typical” parameters which may be provided in Motorola data sheets and/or specifications can and do vary in
different applications and actual performance may vary over time. All operating parameters, including “Typicals” must be validated for each customer application by customer’s
technical experts. Motorola does not convey any license under its patent rights nor the rights of others. Motorola products are not designed, intended, or authorized for use as
components in systems intended for surgical implant into the body, or other applications intended to support or sustain life, or for any other application in which the failure of the
Motorola product could create a situation where personal injury or death may occur. Should Buyer purchase or use Motorola products for any such unintended or unauthorized
application, Buyer shall indemnify and hold Motorola and its officers, employees, subsidiaries, affiliates, and distributors harmless against all claims, costs, damages, and expenses,
and reasonable attorney fees arising out of, directly or indirectly, any claim of personal injury or death associated with such unintended or unauthorized use, even if such claim alleges
that Motorola was negligent regarding the design or manufacture of the part. Motorola and are registered trademarks of Motorola, Inc. Motorola, Inc. is an Equal
Opportunity/Affirmative Action Employer.
Revision History
- Line is added with respect to SPTEF bit to make spec more clear.
6 July 6 July
0.7 - Landscape pages have been removed from pdf.
2001 2001
- Extra blank pages have been removed.
19 July 19 July - Line is added with respect to SPE bit to make spec more clear.
0.8
2001 2001
-Added Document Names
26 July
V02.02 -variable definitions and Names have been hidden
2001
-Changed chapter 3.9 Errata to Note
Based on the BUG version V02.02 an improved version was
created. The specification counter has to be increased, because
27 Sep 27 Sep there is a difference in the behavior in SPI master mode from this
V03.00
2001 2001 specification to its predecessor. In SPI Master Mode, the change of
a config bit during a transmission in progress, will abort the
transmission and force the SPI into idle state.
Section 4.4.2
- Changed description of transfer format CPHA=0 in slave mode
Section 4.4.3
- Changed description of transfer format CPHA=1 in master mode
- Changed Figure 4-3
14 Dec 14 Dec
V03.01 Section 4.6.2
2001 2001
- Added note for mode fault in bidirectional master mode
Section 4.7.1
- Changed description of bidirectional mode with mode fault
Section 4.8.3
- Changed last sentence in stop mode description
Section 3.3.4
07 Jan 07 Jan - Changed description of SPTEF flag
V03.02
2002 2002 Section 4.1
- Changed description of SPTEF flag and SPIDR behaviour
02 Jun 02 Jun
V04.00 - modified functionality of data reception
2004 2004
Section 4.3
- updated note regarding change of config bits for modified
functionality of data reception
Section 4.8.1
- modified note regarding spi slave in wait/stop mode
14 Jul 14 Jul Section 3.1.5
V04.01
2004 2004 - minor rewording.
Table of Contents
Section 1 Introduction
1.1 Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.2 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.3 Modes of Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.1 MOSI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2.2 MISO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2.3 SS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2.4 SCK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
List of Figures
List of Tables
10
Preface
Terminology
11
12
Section 1 Introduction
Figure 1-1 gives an overview on the SPI architecture. The main parts of the SPI are status,control and
data registers, shifter logic, baud rate generator, master/slave control logic and port control logic.
SPI
2
SPI Control Register 1
BIDIROE
2
SPI Control Register 2
Freescale Semiconductor, Inc...
SPC0
8 LSBFE=1
SPI Data Register 8 MSB LSB
LSBFE=0
1.1 Overview
The SPI module allows a duplex, synchronous, serial communication between the MCU and peripheral
devices. Software can poll the SPI status flags or the SPI operation can be interrupt driven.
13
1.2 Features
The SPI includes these distinctive features:
• Master mode and slave mode
• Bi-directional mode
• Slave select output
• Mode fault error flag with CPU interrupt capability
• Double-buffered data register
• Serial clock with programmable polarity and phase
• Control of SPI operation during wait mode
Freescale Semiconductor, Inc...
14
2.1 Overview
This section lists the name and description of all ports including inputs and outputs that do, or may, connect
off chip. The SPI module has a total of 4 external pins.
This pin is used to transmit data out of the SPI module when it is configured as a Master and receive data
when it is configured as Slave.
2.2.2 MISO
Freescale Semiconductor, Inc...
This pin is used to transmit data out of the SPI module when it is configured as a Slave and receive data
when it is configured as Master.
2.2.3 SS
This pin is used to output the select signal from the SPI module to another peripheral with which a data
transfer is to take place when its configured as a Masterand its used as an input to receive the slave select
signal when the SPI is configured as Slave.
2.2.4 SCK
This pin is used to output the clock with respect to which the SPI transfers data or receive clock in case of
Slave.
15
This section consists of register descriptions in address order. Each description includes a standard register
diagram with an associated figure number. Details of register bit and field function follow the register
diagrams, in bit order.
Read:anytime
Write:anytime
16
17
= Reserved
Read:anytime
Freescale Semiconductor, Inc...
18
= Reserved
Read:anytime
( SPR + 1 )
BaudRateDivisor = ( SPPR + 1 ) • 2
NOTE: For max. allowed baud rates, please refer to the SPI Electrical Specification in
the according SoC-Guide.
19
Table 3-4 Example SPI Baud Rate Selection (25 MHz Bus Clock)
BaudRate
SPPR2 SPPR1 SPPR0 SPR2 SPR1 SPR0 Baud Rate
Divisor
0 0 0 0 0 0 2 12.5 MHz
0 0 0 0 0 1 4 6.25 MHz
0 0 0 0 1 0 8 3.125 MHz
0 0 0 0 1 1 16 1.5625 MHz
0 0 0 1 0 0 32 781.25 kHz
0 0 0 1 0 1 64 390.63 kHz
0 0 0 1 1 0 128 195.31 kHz
0 0 0 1 1 1 256 97.66 kHz
0 0 1 0 0 0 4 6.25 MHz
0 0 1 0 0 1 8 3.125 MHz
0 0 1 0 1 0 16 1.5625 MHz
Freescale Semiconductor, Inc...
0 0 1 0 1 1 32 781.25 kHz
0 0 1 1 0 0 64 390.63 kHz
0 0 1 1 0 1 128 195.31 kHz
0 0 1 1 1 0 256 97.66 kHz
0 0 1 1 1 1 512 48.83 kHz
0 1 0 0 0 0 6 4.16667 MHz
0 1 0 0 0 1 12 2.08333 MHz
0 1 0 0 1 0 24 1.04167 MHz
0 1 0 0 1 1 48 520.83 kHz
0 1 0 1 0 0 96 260.42 kHz
0 1 0 1 0 1 192 130.21 kHz
0 1 0 1 1 0 384 65.10 kHz
0 1 0 1 1 1 768 32.55 kHz
0 1 1 0 0 0 8 3.125 MHz
0 1 1 0 0 1 16 1.5625 MHz
0 1 1 0 1 0 32 781.25 kHz
0 1 1 0 1 1 64 390.63 kHz
0 1 1 1 0 0 128 195.31 kHz
0 1 1 1 0 1 256 97.66 kHz
0 1 1 1 1 0 512 48.83 kHz
0 1 1 1 1 1 1024 24.41 kHz
1 0 0 0 0 0 10 2.5 MHz
1 0 0 0 0 1 20 1.25 MHz
1 0 0 0 1 0 40 625 kHz
1 0 0 0 1 1 80 312.5 kHz
1 0 0 1 0 0 160 156.25 kHz
1 0 0 1 0 1 320 78.13 kHz
1 0 0 1 1 0 640 39.06 kHz
1 0 0 1 1 1 1280 19.53 kHz
1 0 1 0 0 0 12 2.08333 MHz
1 0 1 0 0 1 24 1.04167 MHz
1 0 1 0 1 0 48 520.83 kHz
20
Table 3-4 Example SPI Baud Rate Selection (25 MHz Bus Clock)
BaudRate
SPPR2 SPPR1 SPPR0 SPR2 SPR1 SPR0 Baud Rate
Divisor
1 0 1 0 1 1 96 260.42 kHz
1 0 1 1 0 0 192 130.21 kHz
1 0 1 1 0 1 384 65.10 kHz
1 0 1 1 1 0 768 32.55 kHz
1 0 1 1 1 1 1536 16.28 kHz
1 1 0 0 0 0 14 1.78571 MHz
1 1 0 0 0 1 28 892.86 kHz
1 1 0 0 1 0 56 446.43 kHz
1 1 0 0 1 1 112 223.21 kHz
1 1 0 1 0 0 224 111.61 kHz
1 1 0 1 0 1 448 55.80 kHz
Freescale Semiconductor, Inc...
= Reserved
Read:anytime
Write:has no effect
21
This bit is set after a received data byte has been transferred into the SPI Data Register. This bit is
cleared by reading the SPISR register (with SPIF set) followed by a read access to the SPI Data
Register.
1 = New data copied to SPIDR
0 = Transfer not yet complete
SPTEF — SPI Transmit Empty Interrupt Flag
If set, this bit indicates that the transmit data register is empty. To clear this bit and place data into the
transmit data register, SPISR has to be read with SPTEF=1, followed by a write to SPIDR. Any write
to the SPI Data Register without reading SPTEF=1, is effectively ignored.
1 = SPI Data register empty
0 = SPI Data register not empty
This bit is set if the SS input becomes low while the SPI is configured as a master and mode fault
detection is enabled, MODFEN bit of SPICR2 register is set. Refer to MODFEN bit description in
3.1.2 SPI Control Register 2. The flag is cleared automatically by a read of the SPI Status Register
(with MODF set) followed by a write to the SPI Control Register 1.
1 = Mode fault has occurred.
0 = Mode fault has not occurred.
Write:anytime
The SPI Data Register is both the input and output register for SPI data. A write to this register allows
a data byte to be queued and transmitted. For a SPI configured as a master, a queued data byte is
transmitted immediately after the previous transmission has completed. The SPI Transmitter Empty
Flag SPTEF in the SPISR register indicates when the SPI Data Register is ready to accept new data.
Received data in the SPIDR is valid when SPIF is set.
If SPIF is cleared and a byte has been received, the received byte is transferred from the receive shift
register to the SPIDR and SPIF is set.
If SPIF is set and not serviced, and a second byte has been received, the second received byte is kept
as valid byte in the receive shift register until the start of another transmission. The byte in the SPIDR
does not change.
22
If SPIF is set and a valid byte is in the receive shift register, and SPIF is serviced before the start of a
third transmission, the byte in the receive shift register is transferred into the SPIDR and SPIF remains
set (see Figure 3-6 Reception with SPIF serviced in time).
If SPIF is set and a valid byte is in the receive shift register, and SPIF is serviced after the start of a
third transmission, the byte in the receive shift register has become invalid and is not transferred into
the SPIDR (see Figure 3-7 Reception with SPIF serviced too late)
SPIF
SPIF
23
4.1 General
The SPI module allows a duplex, synchronous, serial communication between the MCU and peripheral
devices. Software can poll the SPI status flags or SPI operation can be interrupt driven.
The SPI system is enabled by setting the SPI enable (SPE) bit in SPI Control Register 1. While SPE bit is
set, the four associated SPI port pins are dedicated to the SPI function as:
• Slave select (SS)
• Serial clock (SCK)
• Master out/slave in (MOSI)
Freescale Semiconductor, Inc...
NOTE: A change of CPOL or MSTR bit while there is a received byte pending in the
receive shift register will destroy the received byte and must be avoided.
24
• S-clock
The SPR2, SPR1, and SPR0 baud rate selection bits in conjunction with the SPPR2, SPPR1, and SPPR0
baud rate preselection bits in the SPI Baud Rate register control the baud rate generator and determine the
speed of the transmission. The SCK pin is the SPI clock output. Through the SCK pin, the baud rate
generator of the master controls the shift register of the slave peripheral.
• MOSI, MISO pin
In master mode, the function of the serial data output pin (MOSI) and the serial data input pin (MISO) is
determined by the SPC0 and BIDIROE control bits.
• SS pin
If MODFEN and SSOE bit are set, the SS pin is configured as slave select output. The SS output becomes
low during each transmission and is high when the SPI is in idle state.
Freescale Semiconductor, Inc...
If MODFEN is set and SSOE is cleared, the SS pin is configured as input for detecting mode fault error.
If the SS input becomes low this indicates a mode fault error where another master tries to drive the MOSI
and SCK lines. In this case, the SPI immediately switches to slave mode, by clearing the MSTR bit and
also disables the slave output buffer MISO (or SISO in bidirectional mode). So the result is that all outputs
are disabled and SCK, MOSI and MISO are inputs. If a transmission is in progress when the mode fault
occurs, the transmission is aborted and the SPI is forced into idle state.
This mode fault error also sets the mode fault (MODF) flag in the SPI Status Register (SPISR). If the SPI
interrupt enable bit (SPIE) is set when the MODF flag gets set, then an SPI interrupt sequence is also
requested.
When a write to the SPI Data Register in the master occurs, there is a half SCK-cycle delay. After the
delay, SCK is started within the master. The rest of the transfer operation differs slightly, depending on
the clock format specified by the SPI clock phase bit, CPHA, in SPI Control Register 1 (see 4.4
Transmission Formats).
NOTE: A change of the bits CPOL, CPHA, SSOE, LSBFE, MODFEN, SPC0, BIDIROE
with SPC0 set, SPPR2-SPPR0 and SPR2-SPR0 in master mode will abort a
transmission in progress and force the SPI into idle state. The remote slave cannot
detect this, therefore the master has to ensure that the remote slave is set back to
idle state.
25
• SS pin
The SS pin is the slave select input. Before a data transmission occurs, the SS pin of the slave SPI must
be low. SS must remain low until the transmission is complete. If SS goes high, the SPI is forced into idle
state.
The SS input also controls the serial data output pin, if SS is high (not selected), the serial data output pin
is high impedance, and, if SS is low the first bit in the SPI Data Register is driven out of the serial data
output pin. Also, if the slave is not selected (SS is high), then the SCK input is ignored and no internal
shifting of the SPI shift register takes place.
Although the SPI is capable of duplex operation, some SPI peripherals are capable of only receiving SPI
data in a slave mode. For these simpler devices, there is no serial data out pin.
NOTE: When peripherals with duplex capability are used, take care not to simultaneously
enable two receivers whose serial outputs drive the same system slave’s serial data
Freescale Semiconductor, Inc...
output line.
As long as no more than one slave device drives the system slave’s serial data output line, it is possible for
several slaves to receive the same transmission from a master, although the master would not receive
return information from all of the receiving slaves.
If the CPHA bit in SPI Control Register 1 is clear, odd numbered edges on the SCK input cause the data
at the serial data input pin to be latched. Even numbered edges cause the value previously latched from the
serial data input pin to shift into the LSB or MSB of the SPI shift register, depending on the LSBFE bit.
If the CPHA bit is set, even numbered edges on the SCK input cause the data at the serial data input pin
to be latched. Odd numbered edges cause the value previously latched from the serial data input pin to shift
into the LSB or MSB of the SPI shift register, depending on the LSBFE bit.
When CPHA is set, the first edge is used to get the first data bit onto the serial data output pin. When CPHA
is clear and the SS input is low (slave selected), the first bit of the SPI data is driven out of the serial data
output pin. After the eighth shift, the transfer is considered complete and the received data is transferred
into the SPI Data Register. To indicate transfer is complete, the SPIF flag in the SPI Status Register is set.
NOTE: A change of the bits CPOL, CPHA, SSOE, LSBFE, MODFEN, SPC0 and BIDIROE
with SPC0 set in slave mode will corrupt a transmission in progress and has to be
avoided.
26
MISO MISO
SHIFT REGISTER
MOSI MOSI
SHIFT REGISTER
SCK SCK
BAUD RATE
GENERATOR SS SS
VDD
Using two bits in the SPI Control Register1, software selects one of four combinations of serial clock phase
and polarity.
The CPOL clock polarity control bit specifies an active high or low clock and has no significant effect on
the transmission format.
The CPHA clock phase control bit selects one of two fundamentally different transmission formats.
Clock phase and polarity should be identical for the master SPI device and the communicating slave
device. In some cases, the phase and polarity are changed between transmissions to allow a master device
to communicate with peripheral slaves having different requirements.
The first edge on the SCK line is used to clock the first data bit of the slave into the master and the first
data bit of the master into the slave. In some peripherals, the first bit of the slave’s data is available at the
slave’s data out pin as soon as the slave is selected. In this format, the first SCK edge is issued a half cycle
after SS has become low.
A half SCK cycle later, the second edge appears on the SCK line. When this second edge occurs, the value
previously latched from the serial data input pin is shifted into the LSB or MSB of the shift register,
depending on LSBFE bit.
After this second edge, the next bit of the SPI master data is transmitted out of the serial data output pin
of the master to the serial input pin on the slave. This process continues for a total of 16 edges on the SCK
line, with data being latched on odd numbered edges and shifted on even numbered edges.
Data reception is double buffered. Data is shifted serially into the SPI shift register during the transfer and
is transferred to the parallel SPI Data Register after the last bit is shifted in.
After the 16th (last) SCK edge:
27
• Data that was previously in the master SPI Data Register should now be in the slave data register
and the data that was in the slave data register should be in the master.
• The SPIF flag in the SPI Status Register is set indicating that the transfer is complete.
Figure 4-2 is a timing diagram of an SPI transfer where CPHA = 0. SCK waveforms are shown for CPOL
= 0 and CPOL = 1. The diagram may be interpreted as a master or slave timing diagram since the SCK,
MISO, and MOSI pins are connected directly between the master and the slave. The MISO signal is the
output from the slave and the MOSI signal is the output from the master. The SS pin of the master must
be either high or reconfigured as a general-purpose output not affecting the SPI.
SCK (CPOL = 0)
SCK (CPOL = 1)
SAMPLE I
MOSI/MISO
CHANGE O
MISO pin
SEL SS (O)
Master only
SEL SS (I)
tL tT tI tL
MSB first (LSBFE = 0): MSB Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 LSB Minimum 1/2 SCK
LSB first (LSBFE = 1): LSB Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 MSB for tT, tl, tL
tL = Minimum leading time before the first SCK edge
tT = Minimum trailing time after the last SCK edge
tI = Minimum idling time between transfers (minimum SS high time)
tL, tT, and tI are guaranteed for the master mode and required for the slave mode.
In slave mode, if the SS line is not deasserted between the successive transmissions then the content of the
SPI Data Register is not transmitted, instead the last received byte is transmitted. If the SS line is
deasserted for at least minimum idle time ( half SCK cycle) between successive transmissions then the
content of the SPI Data Register is transmitted.
In master mode, with slave select output enabled the SS line is always deasserted and reasserted between
successive transfers for at least minimum idle time.
28
Some peripherals require the first SCK edge before the first data bit becomes available at the data out pin,
the second edge clocks data into the system. In this format, the first SCK edge is issued by setting the
CPHA bit at the beginning of the 8-cycle transfer operation.
The first edge of SCK occurs immediately after the half SCK clock cycle synchronization delay. This first
edge commands the slave to transfer its first data bit to the serial data input pin of the master.
A half SCK cycle later, the second edge appears on the SCK pin. This is the latching edge for both the
master and slave.
When the third edge occurs, the value previously latched from the serial data input pin is shifted into the
LSB or MSB of the SPI shift register, depending on LSBFE bit. After this edge, the next bit of the master
data is coupled out of the serial data output pin of the master to the serial input pin on the slave.
Freescale Semiconductor, Inc...
This process continues for a total of 16 edges on the SCK line with data being latched on even numbered
edges and shifting taking place on odd numbered edges.
Data reception is double buffered, data is serially shifted into the SPI shift register during the transfer and
is transferred to the parallel SPI Data Register after the last bit is shifted in.
After the 16th SCK edge:
• Data that was previously in the SPI Data Register of the master is now in the data register of the
slave, and data that was in the data register of the slave is in the master.
• The SPIF flag bit in SPISR is set indicating that the transfer is complete.
Figure 4-3 shows two clocking variations for CPHA = 1. The diagram may be interpreted as a master or
slave timing diagram since the SCK, MISO, and MOSI pins are connected directly between the master and
the slave. The MISO signal is the output from the slave, and the MOSI signal is the output from the master.
The SS line is the slave select input to the slave. The SS pin of the master must be either high or
reconfigured as a general-purpose output not affecting the SPI.
29
SCK (CPOL = 0)
SCK (CPOL = 1)
SAMPLE I
MOSI/MISO
CHANGE O
Freescale Semiconductor, Inc...
MISO pin
SEL SS (O)
Master only
SEL SS (I)
tL tT tI tL
MSB first (LSBFE = 0): MSB Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 LSB Minimum 1/2 SCK
LSB first (LSBFE = 1): LSB Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 MSB for tT, tl, tL
tL = Minimum leading time before the first SCK edge, not required for back to back transfers
tT = Minimum trailing time after the last SCK edge
tI = Minimum idling time between transfers (minimum SS high time), not required for back to back transfers
The SS line can remain active low between successive transfers (can be tied low at all times). This format
is sometimes preferred in systems having a single fixed master and a single slave that drive the MISO data
line.
• Back to Back transfers in master mode
In master mode, if a transmission has completed and a new data byte is available in the SPI Data Register,
this byte is send out immediately without a trailing and minimum idle time.
The SPI interrupt request flag (SPIF) is common to both the master and slave modes. SPIF gets set one
half SCK cycle after the last SCK edge.
30
The SPI clock rate is determined by the product of the value in the baud rate preselection bits
(SPPR2–SPPR0) and the value in the baud rate selection bits (SPR2–SPR0). The module clock divisor
equation is shown in Figure 4-4.
When all bits are clear (the default condition), the SPI module clock is divided by 2. When the selection
bits (SPR2–SPR0) are 001 and the preselection bits (SPPR2–SPPR0) are 000, the module clock divisor
becomes 4. When the selection bits are 010, the module clock divisor becomes 8 etc.
When the preselection bits are 001, the divisor determined by the selection bits is multiplied by 2. When
the preselection bits are 010, the divisor is multiplied by 3, etc. See Table 3-4 for baud rate calculations
for all bit conditions, based on a 25 MHz Bus Clock. The two sets of selects allows the clock to be divided
by a non-power of two to achieve other baud rates such as divide by 6, divide by 10, etc.
The baud rate generator is activated only when the SPI is in the master mode and a serial transfer is taking
place. In the other cases, the divider is disabled to decrease IDD current.
Freescale Semiconductor, Inc...
( SPR + 1 )
BaudRateDivisor = ( SPPR + 1 ) • 2
NOTE: For max. allowed baud rates, please refer to the SPI Electrical Specification in
the according SoC-Guide.
The SS output feature automatically drives the SS pin low during transmission to select external devices
and drives it high during idle to deselect external devices. When SS output is selected, the SS output pin
is connected to the SS input pin of the external device.
The SS output is available only in master mode during normal SPI operation by asserting SSOE and
MODFEN bit as shown in Table 3-2.
The mode fault feature is disabled while SS output is enabled.
NOTE: Care must be taken when using the SS output feature in a multimaster system since
the mode fault feature is not available for detecting system errors between masters.
The bidirectional mode is selected when the SPC0 bit is set in SPI Control Register 2 (see Table 4-1
Normal Mode and Bidirectional Mode). In this mode, the SPI uses only one serial data pin for the
interface with external device(s). The MSTR bit decides which pin to use. The MOSI pin becomes the
serial data I/O (MOMI) pin for the master mode, and the MISO pin becomes serial data I/O (SISO) pin for
the slave mode. The MISO pin in master mode and MOSI pin in slave mode are not used by the SPI.
31
BIDIROE
Bidirectional Mode SPI
BIDIROE SPI
SPC0 = 1
Serial In
Serial Out SISO
The direction of each serial I/O pin depends on the BIDIROE bit. If the pin is configured as an output,
serial data from the shift register is driven out on the pin. The same pin is also the serial input to the shift
register.
The SCK is output for the master mode and input for the slave mode.
The SS is the input or output for the master mode, and it is always the input for the slave mode.
The bidirectional mode does not affect SCK and SS functions.
NOTE: In bidirectional master mode, with mode fault enabled, both data pins MISO and
MOSI can be occupied by the SPI, though MOSI is normally used for transmissions
in bidirectional mode and MISO is not used by the SPI. If a mode fault occurs, the
SPI is automatically switched to slave mode, in this case MISO becomes occupied
by the SPI and MOSI is not used. This has to be considered, if the MISO pin is used
for other purpose.
If the SS input becomes low while the SPI is configured as a master, it indicates a system error where more
than one master may be trying to drive the MOSI and SCK lines simultaneously. This condition is not
32
permitted in normal operation, the MODF bit in the SPI Status Register is set automatically provided the
MODFEN bit is set.
In the special case where the SPI is in master mode and MODFEN bit is cleared, the SS pin is not used by
the SPI. In this special case, the mode fault error function is inhibited and MODF remains cleared. In case
the SPI system is configured as a slave, the SS pin is a dedicated input pin. Mode fault error doesn’t occur
in slave mode.
If a mode fault error occurs the SPI is switched to slave mode, with the exception that the slave output
buffer is disabled. So SCK, MISO and MOSI pins are forced to be high impedance inputs to avoid any
possibility of conflict with another output driver. A transmission in progress is aborted and the SPI is
forced into idle state.
If the mode fault error occurs in the bidirectional mode for a SPI system configured in master mode, output
enable of the MOMI (MOSI in bidirectional mode) is cleared if it was set. No mode fault error occurs in
Freescale Semiconductor, Inc...
NOTE: If a mode fault error occurs and a received data byte is pending in the receive shift
register, this data byte will be lost.
In run mode with the SPI system enable (SPE) bit in the SPI control register clear, the SPI system is in a
low-power, disabled state. SPI registers can still be accessed, but clocks to the core of this module are
disabled.
SPI operation in wait mode depends upon the state of the SPISWAI bit in SPI Control Register 2.
• If SPISWAI is clear, the SPI operates normally when the CPU is in wait mode
• If SPISWAI is set, SPI clock generation ceases and the SPI module enters a power conservation
state when the CPU is in wait mode.
– If SPISWAI is set and the SPI is configured for master, any transmission and reception in
progress stops at wait mode entry. The transmission and reception resumes when the SPI exits
wait mode.
– If SPISWAI is set and the SPI is configured as a slave, any transmission and reception in
progress continues if the SCK continues to be driven from the master. This keeps the slave
synchronized to the master and the SCK.
33
If the master transmits several bytes while the slave is in wait mode, the slave will continue to
send out bytes consistent with the operation mode at the start of wait mode (i.e. If the slave is
currently sending its SPIDR to the master, it will continue to send the same byte. Else if the
slave is currently sending the last received byte from the master, it will continue to send each
previous master byte).
NOTE: Care must be taken when expecting data from a master while the slave is in wait or
stop mode. Even though the shift register will continue to operate, the rest of the
SPI is shut down (i.e. a SPIF interrupt will not be generated until exiting stop or
wait mode). Also, the byte from the shift register will not be copied into the SPIDR
register until after the slave SPI has exited wait or stop mode. In slave mode, a
received byte pending in the receive shift register will be lost when entering wait or
stop mode. A SPIF flag and SPIDR copy is only generated if wait mode is entered
or exited during a tranmission. If the slave enters wait mode in idle mode and exits
Freescale Semiconductor, Inc...
wait mode in idle mode, neither a SPIF nor a SPIDR copy will occur.
Stop mode is dependent on the system. The SPI enters stop mode when the module clock is disabled (held
high or low). If the SPI is in master mode and exchanging data when the CPU enters stop mode, the
transmission is frozen until the CPU exits stop mode. After stop, data to and from the external SPI is
exchanged correctly. In slave mode, the SPI will stay synchronized with the master.
The stop mode is not dependent on the SPISWAI bit.
4.8.4 Reset
The reset values of registers and signals are described in the Memory Map and Registers section (see
Section 3 Memory Map/Register Definition) which details the registers and their bit-fields.
• If a data transmission occurs in slave mode after reset without a write to SPIDR, it will transmit
garbage, or the byte last received from the master before the reset.
• Reading from the SPIDR after reset will always read a byte of zeros.
4.8.5 Interrupts
The SPI only originates interrupt requests when SPI is enabled (SPE bit in SPICR1 set). The following is
a description of how the SPI makes a request and how the MCU should acknowledge that request. The
interrupt vector offset and interrupt priority are chip dependent.
The interrupt flags MODF, SPIF and SPTEF are logically ORed to generate an interrupt request.
4.8.5.1 MODF
MODF occurs when the master detects an error on the SS pin. The master SPI must be configured for the
MODF feature (see Table 3-2 SS Input / Output Selection). Once MODF is set, the current transfer is
aborted and the following bit is changed:
34
4.8.5.2 SPIF
SPIF occurs when new data has been received and copied to the SPI Data Register. Once SPIF is set, it
does not clear until it is serviced. SPIF has an automatic clearing process which is described in 3.1.4 SPI
Status Register.
4.8.5.3 SPTEF
SPTEF occurs when the SPI Data Register is ready to accept new dataOnce SPTEF is set, it does not clear
Freescale Semiconductor, Inc...
until it is serviced. SPTEF has an automatic clearing process which is described in 3.1.4 SPI Status
Register.
35
36
Index
–B–
Block diagram 13
–C–
Cross reference 13
–D–
Diagram
block 13
Freescale Semiconductor, Inc...
–F–
Figure
cross-reference style 13
–I–
Initialization/application information 35
–S–
SPI clock 17
37
38
39
FINAL PAGE OF
40
PAGES
40