spi协议
spi协议
HIGHLIGHTS
This section of the manual contains the following major topics:
20
Serial Peripheral
Interface (SPI)
20.1 Introduction
The Serial Peripheral Interface (SPI) module is a synchronous serial interface useful for
communicating with other peripheral or microcontroller devices. These peripheral devices may
be Serial EEPROMs, shift registers, display drivers, A/D converters, etc. The SPI module is
compatible with Motorola’s SPI and SIOP interfaces.
Depending on the variant, the dsPIC30F family offers one or two SPI modules on a single device.
SPI1 and SPI2 are functionally identical. The SPI2 module is available in many of the higher pin
count packages (64-pin and higher), while the SPI1 module is available on all devices.
Note: In this section, the SPI modules are referred together as SPIx or separately as SPI1
and SPI2. Special Function registers will follow a similar notation. For example,
SPIxCON refers to the control register for the SPI1 or SPI2 module.
The SPI serial port consists of the following Special Function Registers (SFR):
• SPIxBUF: Address in SFR space that is used to buffer data to be transmitted and data that
is received. This address is shared by the SPIxTXB and SPIxRXB registers.
• SPIxCON: A control register that configures the module for various modes of operation.
• SPIxSTAT: A status register that indicates various status conditions.
In addition, there is a 16-bit shift register, SPIxSR, that is not memory mapped. It is used for
shifting data in and out of the SPI port.
The memory mapped SFR, SPIxBUF, is the SPI Data Receive/Transmit register. Internally, the
SPIxBUF register actually comprises of two separate registers - SPIxTXB and SPIxRXB. The
Receive Buffer register, SPIxRXB, and the Transmit Buffer register, SPIxTXB, are two unidirec-
tional 16-bit registers. These registers share the SFR address named SPIxBUF. If a user writes
data to be transmitted to the SPIxBUF address, internally the data gets written to the SPIxTXB
register. Similarly, when the user reads the received data from SPIxBUF, internally the data is
read from the SPIxRXB register. This double-buffering of transmit and receive operations allows
continuous data transfers in the background. Transmission and reception occur simultaneously.
Note: The user cannot write to the SPIxTXB register or read from the SPIxRXB register
directly. All reads and writes are performed on the SPIxBUF register.
Note: The SPI module can be configured to operate using 3 or 4 pins. In the 3-pin mode,
the SSx pin is not used.
Internal
Data Bus
SPIxBUF
SPIxRXB SPIxTXB
Transmit
Receive
SPIxSR
SDIx bit0
SDOx Shift
Control
Slave Select Clock Edge
and Frame Control Select
SSx Sync Control
Secondary Primary
Prescaler Prescaler FCY
1,2,3,...8 1, 4, 16, 64
SCKx
Note: The SPIxTXB and SPIxRXB registers are memory mapped to the SPIxBUF register.
20
Serial Peripheral
Interface (SPI)
Lower Byte:
U-0 R/W-0 U-0 U-0 U-0 U-0 R-0 R-0
HS
— SPIROV — — — — SPITBF SPIRBF
bit 7 bit 0
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
HC = Cleared by Hardware HS = Set by Hardware
-n = Value at RESET ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Lower Byte:
R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0
SSEN CKP MSTEN SPRE<2:0> PPRE<1:0>
bit 7 bit 0
20
Serial Peripheral
Interface (SPI)
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
SDOx SDIx
SDIx SDOx
Shift Register Shift Register
(SPIxSR) (SPIxSR)
Serial Clock
SCKx SCKx SPI Buffer
SPI Buffer
(SPIxBUF) (SPIxBUF)
SSx SSx
.
The following steps should be taken to set up the SPI module for the Master mode of operation:
1. If using interrupts:
• Clear the SPIxIF bit in the respective IFSn register.
• Set the SPIxIE bit in the respective IECn register.
• Write the SPIxIP bits in the respective IPCn register.
2. Write the desired settings to the SPIxCON register with MSTEN (SPIxCON<5>) = 1.
3. Clear the SPIROV bit (SPIxSTAT<6>).
4. Enable SPI operation by setting the SPIEN bit (SPIxSTAT<15>).
5. Write the data to be transmitted to the SPIxBUF register. Transmission (and Reception)
will start as soon as data is written to the SPIxBUF register.
In Master mode, the system clock is prescaled and then used as the serial clock. The prescaling
is based on the settings in the PPRE<1:0> (SPIxCON<1:0>) and SPRE<1:0> (SPIxCON<4:2>)
bits. The serial clock is output via the SCKx pin to slave devices. Clock pulses are only generated
when there is data to be transmitted. For further information, refer to Section 20.4 “SPI Master
Mode Clock Frequency”.
The CKP and CKE bits determine on which edge of the clock, data transmission occurs.
Both data to be transmitted and data that is received are respectively written into or read from
the SPIxBUF register.
The following describes the SPI module operation in Master mode:
1. Once the module is set up for Master mode of operation and enabled, data to be
transmitted is written to the SPIxBUF register. The SPITBF (SPIxSTAT<1>) bit is set.
2. The contents of SPIxTXB are moved to the shift register, SPIxSR, and the SPITBF bit is
cleared by the module.
3. A series of 8/16 clock pulses shifts out 8/16 bits of transmit data from the SPIxSR to the
SDOx pin and simultaneously shifts in the data at the SDIx pin into the SPIxSR.
4. When the transfer is complete, the following events will occur:
• The interrupt flag bit, SPIxIF, is set. SPI interrupts can be enabled by setting the
interrupt enable bit SPIxIE. The SPIxIF flag is not cleared automatically by the
hardware.
• Also, when the ongoing transmit and receive operation is completed, the contents of
the SPIxSR are moved to the SPIxRXB register.
• The SPIRBF (SPIxSTAT<0>) bit is set by the module, indicating that the receive buffer
is full. Once the SPIxBUF register is read by the user code, the hardware clears the
SPIRBF bit.
5. If the SPIRBF bit is set (receive buffer is full) when the SPI module needs to transfer data
from SPIxSR to SPIxRXB, the module will set the SPIROV (SPIxSTAT<6>) bit, indicating
an overflow condition.
6. Data to be transmitted can be written to SPIxBUF by the user software at any time as long
as the SPITBF (SPIxSTAT<1>) bit is clear. The write can occur while SPIxSR is shifting
out the previously written data, allowing continuous transmission.
Note: The SPIxSR register cannot be written into directly by the user. All writes to the
SPIxSR register are performed through the SPIxBUF register.
User writes
User writes new data
to SPIxBUF
SPIxTXB to SPIxSR during transmission
SPITBF
SCKx
(CKP = 0
CKE = 0)
4 Clock
SCKx modes
(CKP = 1 (clock
CKE = 0) output at
the SCKx
SCKx pin in
(CKP = 0
Master
CKE = 1)
mode)
SCKx
(CKP = 1
CKE = 0)
SDIx
(SMP = 0) bit0
bit7
Input
Sample Two modes
(SMP = 0) available
for SMP
SDIx control
(SMP = 1) bit (see
bit7 bit0
Note 4)
Input
Sample
(SMP = 1)
SPIxIF
SPIxSR moved
into SPIxRXB
SPIRBF
(SPIxSTAT<0>)
User reads
SPIxBUF 20
Serial Peripheral
Interface (SPI)
Note 1: Four SPI Clock modes shown to demonstrate CKP (SPIxCON<6>) and CKE (SPIxCON<8>) bit functionality only.
Only one of the four modes can be chosen for operation.
2: SDI and input sample shown for two different values of the SMP (SPIxCON<9>) bit, for demonstration purposes
only. Only one of the two configurations of the SMP bit can be chosen during operation.
3: If there are no pending transmissions, SPIxTXB is transferred to SPIxSR as soon as the user writes to SPIxBUF.
4: Operation for 8-bit mode shown. The 16-bit mode is similar.
The following steps should be taken to set up the SPI module for the Slave mode of operation:
1. Clear the SPIxBUF register.
2. If using interrupts:
• Clear the SPIxIF bit in the respective IFSn register.
• Set the SPIxIE bit in the respective IECn register.
• Write the SPIxIP bits in the respective IPCn register.
3. Write the desired settings to the SPIxCON register with MSTEN (SPIxCON<5>) = 0.
4. Clear the SMP bit.
5. If the CKE bit is set, then the SSEN bit must be set, thus enabling the SSx pin.
6. Clear the SPIROV bit (SPIxSTAT<6>) and,
7. Enable SPI operation by setting the SPIEN bit (SPIxSTAT<15>).
In Slave mode, data is transmitted and received as the external clock pulses appear on the SCKx
pin. The CKP (SPIxCON<6>) and CKE (SPIxCON<8>) bits determine on which edge of the clock
data transmission occurs.
Both data to be transmitted and data that is received are respectively written into or read from
the SPIxBUF register.
The rest of the operation of the module is identical to that in the Master mode.
A few additional features provided in the Slave mode are:
Slave Select Synchronization: The SSx pin allows a Synchronous Slave mode. If the SSEN
(SPIxCON<7>) bit is set, transmission and reception is enabled in Slave mode only if the SSx
pin is driven to a low state. The port output or other peripheral outputs must not be driven in order
to allow the SSx pin to function as an input. If the SSEN bit is set and the SSx pin is driven high,
the SDOx pin is no longer driven and will tri-state even if the module is in the middle of a
transmission. An aborted transmission will be retried the next time the SSx pin is driven low using
the data held in the SPIxTXB register. If the SSEN bit is not set, the SSx pin does not affect the
module operation in Slave mode.
SPITBF Status Flag Operation: The function of the SPITBF (SPIxSTAT<1>) bit is different in
the Slave mode of operation. The following describes the function of the SPITBF for various
settings of the Slave mode of operation:
1. If SSEN (SPIxCON<7>) is cleared, the SPITBF is set when the SPIxBUF is loaded by the
user code. It is cleared when the module transfers SPIxTXB to SPIxSR. This is similar to
the SPITBF bit function in Master mode.
2. If SSEN (SPIxCON<7>) is set, the SPITBF is set when the SPIxBUF is loaded by the user
code. However, it is cleared only when the SPIx module completes data transmission. A
transmission will be aborted when the SSx pin goes high and may be retried at a later
time. Each data word is held in SPIxTXB until all bits are transmitted to the receiver.
Note: To meet module timing requirements, the SSx pin must be enabled in Slave mode
when CKE = 1. (Refer to Figure 20-6 for details.)
Figure 20-4: SPI Slave Mode Operation: Slave Select Pin Disabled
SCKx Input
(CKP = 0
CKE = 0)
SCKx Input
(CKP = 1
CKE = 0)
SDIx Input
(SMP = 0) bit7 bit0
Input
Sample
(SMP = 0)
User writes to
SPIxBUF
SPITBF
SPISR to
SPIxRXB
SPIRBF
Note 1: Two SPI Clock modes shown only to demonstrate CKP (SPIxCON<6>) and CKE (SPIxCON<8>) bit functionality.
Any combination of CKP and CKE bits can be chosen for module operation.
2: If there are no pending transmissions or a transmission in progress, SPIxBUF is transferred to SPIxSR as soon
as the user writes to SPIxBUF.
3: Operation for 8-bit mode shown. The 16-bit mode is similar.
20
Serial Peripheral
Interface (SPI)
Figure 20-5: SPI Slave Mode Operation with Slave Select Pin Enabled
SSx
SCKx
(CKP = 0
CKE = 0)
SCKx
(CKP = 1
CKE = 0)
User writes SPIxBUF
to to
SPIxBUF SPIxSR
SPITBF
SDIx
(SMP = 0) bit7 bit0
Input
Sample
(SMP = 0)
SPIxIF
1 instruction
cycle latency
SPIxSR to
SPIxBUF
SPIRBF
User reads
SPIxBUF
Note 1: When the SSEN (SPIxCON<7>) bit is set to ‘1’, the SSx pin must be driven low so as to enable transmission and
reception in Slave mode.
2: Transmit data is held in SPIxTXB and SPITBF remains set until all bits are transmitted.
3: Operation for 8-bit mode shown. The 16-bit mode is similar.
SSx
(see Note 1)
SCK Input
(CKP = 0
CKE = 1)
SCK Input
(CKP = 1
CKE = 1)
SDI Input
(SMP = 0) bit7 bit0
Input
Sample
(SMP = 0)
SPIxIF
Write to SPISR to
SPIxBUF SPIRXB
SPITBF
SPIxRBF
Note 1: The SSx pin must be used for Slave mode operation when CKE = 1.
2: When the SSEN (SPIxCON<7>) bit is set to ‘1’, the SSx pin must be driven low so as to enable transmission and
reception in Slave mode.
3: Transmit data is held in SPIxTXB and SPITBF remains set until all bits are transmitted.
4: Operation for 8-bit mode shown. The 16-bit mode is similar.
20
Serial Peripheral
Interface (SPI)
SDOx SDIx
Serial Clock
SPI Buffer SCKx SCKx SPI Buffer
(SPIxBUF) (SPIxBUF)
SSx SSx
Frame Sync.
Pulse
Note 1: In Framed SPI modes, the SSx pin is used to transmit/receive the frame synchronization pulse.
2: Framed SPI modes require the use of all four pins (i.e., using the SSx pin is not optional).
3: The SPIxTXB and SPIxRXB registers are memory mapped to the SPIxBUF register.
When FRMEN (SPIxCON<14>) = 1 and MSTEN (SPIxCON<5>) = 1, the SCKx pin becomes an
output and the SPI clock at SCKx becomes a free running clock.
When FRMEN = 1 and MSTEN = 0, the SCKx pin becomes an input. The source clock provided
to the SCKx pin is assumed to be a free running clock.
The polarity of the clock is selected by the CKP (SPIxCON<6>) bit. The CKE (SPIxCON<8>) bit
is not used for the Framed SPI modes and should be programmed to ‘0’ by the user software.
When CKP = 0, the frame sync pulse output and the SDOx data output change on the rising edge
of the clock pulses at the SCKx pin. Input data is sampled at the SDIx input pin on the falling edge
of the serial clock.
When CKP = 1, the frame sync pulse output and the SDOx data output change on the falling
edge of the clock pulses at the SCKx pin. Input data is sampled at the SDIx input pin on the rising
edge of the serial clock.
20
Serial Peripheral
Interface (SPI)
When SPIFSD (SPIxCON<13>) = 0, the SPIx module is in the Frame Master mode of operation.
In this mode, the frame sync pulse is initiated by the module when the user software writes the
transmit data to SPIxBUF location (thus writing the SPIxTXB register with transmit data). At
the end of the frame sync pulse, the SPIxTXB is transferred to the SPIxSR and data
transmission/reception begins.
When SPIFSD (SPIxCON<13>) = 1, the module is in Frame Slave mode. In this mode, the frame
sync pulse is generated by an external source. When the module samples the frame sync pulse,
it will transfer the contents of the SPIxTXB register to the SPIxSR and data transmission/
reception begins. The user must make sure that the correct data is loaded into the SPIxBUF for
transmission before the frame sync pulse is received.
Note: Receiving a frame sync pulse will start a transmission, regardless of whether data
was written to SPIxBUF. If no write was performed, the old contents of SPIxTXB will
be transmitted.
This Framed SPI mode is enabled by setting the MSTEN (SPIxCON<5>) and FRMEN
(SPIxCON<14>) bits to ‘1’ and the SPIFSD (SPIxCON<13>) bit to ‘0’. In this mode, the serial
clock will be output continuously at the SCKx pin, regardless of whether the module is
transmitting. When the SPIxBUF is written, the SSx pin will be driven high on the next transmit
edge of the SCKx clock. The SSx pin will be high for one SCKx clock cycle. The module will start
transmitting data on the next transmit edge of the SCKx, as shown in Figure 20-8. A connection
diagram indicating signal directions for this Operating mode is shown in Figure 20-7.
SCKx
(CKP = 1)
SCKx
(CKP = 0)
SSx
This Framed SPI mode is enabled by setting the MSTEN, FRMEN and the SPIFSD bits to ‘1’.
The SSx pin is an input, and it is sampled on the sample edge of the SPI clock. When it is
sampled high, data will be transmitted on the subsequent transmit edge of the SPI clock, as
shown in Figure 20-9. The interrupt flag, SPIxIF, is set when the transmission is complete. The
user must make sure that the correct data is loaded into the SPIxBUF for transmission before the
signal is received at the SSx pin. A connection diagram indicating signal directions for this
Operating mode is shown in Figure 20-10.
SCKx
(CKP = 1)
SCK
(CKP = 0)
FSYNC
dsPIC30F PROCESSOR 2
[SPI Master, Frame Slave]
SDOx SDIx
SDIx SDOx
Serial Clock
SCKx SCKx
SSx SSx
Frame Sync.
Pulse
Note 1: In Framed SPI modes, the SSx pin is used to transmit/receive the frame synchronization
pulse.
2: Framed SPI modes require the use of all four pins (i.e., Using the SSx pin is not optional).
20
Serial Peripheral
Interface (SPI)
This framed SPI mode is enabled by setting the MSTEN (SPIxCON<5>) bit to ‘0’, the FRMEN
(SPIxCON<14>) bit to ‘1’ and the SPIFSD (SPIxCON<13>) bit to ‘0’. The input SPI clock will be
continuous in Slave mode. The SSx pin will be an output when the SPIFSD bit is low. Therefore,
when the SPIBUF is written, the module will drive the SSx pin high on the next transmit edge of
the SPI clock. The SSx pin will be driven high for one SPI clock cycle. Data will start transmitting
on the next SPI clock transmit edge. A connection diagram indicating signal directions for this
Operating mode is shown in Figure 20-11.
dsPIC30F PROCESSOR 2
[SPI Slave, Frame Slave]
SDOx SDIx
SDIx SDOx
Serial Clock
SCKx SCKx
SSx SSx
Frame Sync.
Pulse
Note 1: In Framed SPI modes, the SSx pin is used to transmit/receive the frame synchronization
pulse.
2: Framed SPI modes require the use of all four pins (i.e., Using the SSx pin is not optional).
This Framed SPI mode is enabled by setting the MSTEN (SPIxCON<5>) bit to ‘0’, the FRMEN
bit (SPIxCON<14>) to ‘1’ and the SPIFSD (SPIxCON<13>) bit to ‘1’. Therefore, both the SCKx
and SSx pins will be inputs. The SSx pin will be sampled on the sample edge of the SPI clock.
When SSx is sampled high, data will be transmitted on the next transmit edge of SCKx. A
connection diagram indicating signal directions for this Operating mode is shown in Figure 20-12.
dsPIC30F PROCESSOR 2
[SPI Master, Frame Slave]
SDOx SDIx
SDIx SDOx
Serial Clock
SCKx SCKx
SSx SSx
Frame Sync.
Pulse
Note 1: In Framed SPI modes, the SSx pin is used to transmit/receive the frame synchronization
pulse.
2: Framed SPI modes require the use of all four pins (i.e., Using the SSx pin is not optional).
Note: Note that the SCKx signal clock is not free running for normal SPI modes. It will only
run for 8 or 16 pulses when the SPIxBUF is loaded with data. It will however, be
continuous for Framed modes.
Equation 20-1 can be used to calculate the SCKx clock frequency as a function of the primary
and secondary prescaler settings.
Equation 20-1:
FCY
FSCK =
Primary Prescaler * Secondary Prescaler
Some sample SPI clock frequencies (in kHz) are shown in the table below:
Note: Not all clock rates are supported. For further information, refer to the SPI timing
specifications in the specific device data sheet.
20
Serial Peripheral
Interface (SPI)
The following are a consequence of entering SLEEP mode when the SPIx module is configured
for master operation:
• The baud rate generator in the SPIx module stops and is reset.
• If the SPIx module enters SLEEP mode in the middle of a transmission/reception, then the
transmission/reception is aborted. Since there is no automatic way to prevent an entry into
SLEEP mode if a transmission or reception is pending, the user software must synchronize
entry into SLEEP with SPI module operation to avoid aborted transmissions.
• The transmitter and receiver will stop in SLEEP. The transmitter or receiver does not
continue with a partially completed transmission at wake-up.
Since the clock pulses at SCKx are externally provided for Slave mode, the module will continue
to function in SLEEP mode. It will complete any transactions during the transition into SLEEP. On
completion of a transaction, the SPIRBF flag is set. Consequently, the SPIxIF bit will be set. If
SPI interrupts are enabled (SPIxIE = 1), the device will wake from SLEEP. If the SPI interrupt
priority level is greater than the present CPU priority level, code execution will resume at the SPIx
interrupt vector location. Otherwise, code execution will continue with the instruction following the
PWRSAV instruction that previously invoked SLEEP mode. The module is not reset on entering
SLEEP mode if it is operating as a slave device.
Register contents are not affected when the SPIx module is going into or coming out of SLEEP
mode.
20
Serial Peripheral
Interface (SPI)
DS70067B-page 20-22
SPI1BUF 0224 Transmit and Receive Buffer Address shared by SPI1TXB and SPI1RXB registers 0000 0000 0000 0000
IEC1 008E IC6IE IC5IE IC4IE IC3IE C1IE SPI2IE U2TXIE U2RXIE INT2IE T5IE T4IE OC4IE OC3IE IC8IE IC7IE INT1IE 0000 0000 0000 0000
IPC2 0098 — ADIP<2:0> — U1TXIP<2:0> — U1RXIP<2:0> — SPI1IP<2:0> 0100 0100 0100 0100
Advance Information
IPC6 00A0 — C1IP<2:0> — SPI2IP<2:0> — U2TXIP<2:0> — U2RXIP<2:0> 0100 0100 0100 0100
Note: Please visit the Microchip web site (www.microchip.com) for additional Application
Notes and code examples for the dsPIC30F Family of devices.
20
Serial Peripheral
Interface (SPI)