Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Rev. 0, 08/2009
Application Note
1 Introduction Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The purpose of this application note is to describe the 1.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Definition of the SPI. . . . . . . . . . . . . . . . . . . . . . . . . 1
serial peripheral interface bus controller (SPI) 2 Description of the SPI module . . . . . . . . . . . . . . . . . . . . . 2
implemented on Freescales MPC5121e microcontroller. 2.1 SPI module in MPC5121e . . . . . . . . . . . . . . . . . . . . 2
It describes how to configure and use the programmable 2.2 Serial peripheral interface register list . . . . . . . . . . . 3
2.3 Signal description and connection scheme . . . . . . . 6
serial controller (PSC) and PSC centralized FIFO 3 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
controller (FIFOC) in all supported SPI modes. 3.1 PIN muxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 IPS bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.3 MCLK frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.1 Objective 3.4 Data polling, interrupts, and DMA . . . . . . . . . . . . . . 9
3.5 PSC and FIFOC initialization. . . . . . . . . . . . . . . . . 10
3.6 Interrupt initialization . . . . . . . . . . . . . . . . . . . . . . . 15
The objective of this application note is to describe the 3.7 Direct memory access . . . . . . . . . . . . . . . . . . . . . . 17
necessary steps needed to initialize and configure PSC in 4 Modes of operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
the SPI mode at all supported modes. 4.1 SPI configuration . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.2 Master mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.3 Slave mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.4 Interrupt mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.2 Definition of the SPI 4.5 DMA mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
The Serial Peripheral Interface (SPI) protocol is
asynchronous serial data standard, primarily used to
allow a microprocessor to communicate with other
microprocessors or ICs such as memories, liquid crystal
Interrupt
controller
IPB interface
PSC9 Tx FIFO slice
PSC9 Rx FIFO slice Programmable
External
Tx/Rx clock
clock source
generation
Core
Configuration
register PSC 0..11
The PSC requests new data if the Tx shift register is empty or writes Rx data to FIFOC if the Rx shift
register is full. This communication is independent of external interrupt or request signals. To make sure
the transfer is successful and avoid an overrun/underrun event, both transceiver and receiver must be
always enabled and the core/DMA must make sure that the data slices or Tx or Rx shift registers never
become full/empty.
Address
Register Name Description
118 70
0x80 Command register for PSCn Tx slice Provides commands to the FIFOC
PSCn_Tx_CMD
0x84 Alarm level for PSCn Tx slice Defines alarm level
PSCn_Tx_ALARM
0x88 Status register for PSCn Tx slice Shows internal status of the FIFO slice
PSCn_Tx_SR
0x8C Interrupt status register for PSCn Tx slice Status of all potential interrupts
PSCn_Tx_ISR
0x90 Interrupt mask register for PSCn Tx slice Selects corresponding bits in the ISR that
PSCn_Tx_IMR cause an interrupt
0x94 FIFO count for PSCn Tx slice Number of bytes in the FIFO
PSCn_Tx_COUNT
0x98 FIFO pointer for PSCn Tx slice Reads or modifies pointer in the FIFO
PSCn_Tx_POINTER slice
0x9C FIFO size register for PSCn Tx slice Sets start address and size of the FIFO
PSCn_Tx_SIZE slice
Base 0xBC FIFO data register for PSCn Tx slice FIFO data register
Address + PSCn_Tx_DATA
0x0n
(n = PSC 0xC0 Command register for PSCn Rx slice Provides commands to the FIFOC
number) PSCn_Rx_CMD
0xC4 Alarm level for PSCn Rx slice Defines alarm level
PSCn_Rx_ALARM
0xC8 Status register for PSCn Rx slice Shows internal status of the FIFO slice
PSCn_Rx_STAT
0xCC Interrupt status register for PSCn Rx slice Status of all potential interrupts
PSCn_Rx_INTSTAT
0xD0 Interrupt mask register for PSCn Rx slice Selects corresponding bits in the ISR that
PSCn_Rx_INTMASK cause an interrupt
0xD4 FIFO count for PSCn Rx slice Number of bytes in the FIFO
PSCn_Rx_COUNT
0xD8 FIFO pointer for PSCn Rx slice Reads or modifies pointer in the FIFO
PSCn_Rx_POINTER slice
0xDC FIFO size register for PSCn Rx slice Sets start address and size of the FIFO
PSCn_Rx_SIZE slice
0xFC FIFO data register for PSCn Rx slice FIFO data register
PSCn_Rx_DATA
Base Address + 0xF00 FIFO command Default
Base Address + 0xF04 FIFO interrupt status Shows all PSCs with currently pending
interrupts
Address
Register Name Description
118 70
Base Address + 0xF08 FIFO DMA request Shows all PSCs with currently pending
requests
Base Address + 0xF0C FIFO AXE request Default
Base Address + 0xF10 FIFO debug Default
Signal Description
SCLK Serial clock signal with direction from master to slave device.
MOSI/SIMO Master-out/slave-in signal for data transmission from master to slave in 8-bit, 12-bit, 16-bit,
20-bit, 24-bit, or 32-bit width (output from master).
MISO/SOMI Master-in/slave-out signal for data transmission from slave to master in 8-bit, 12-bit, 16-bit,
20-bit, 24-bit, or 32-bit width (output from slave).
SS Slave select signal, initiated by master to select slave device.
SCLK
MOSI
Master MISO Slave
SS
SCLK SCLK
MOSI MOSI
Master Slave 1
MISO MISO
SS1 SS
SS2
SS3 SCLK
MOSI
Slave 2
MISO
SS
SCLK
MOSI
Slave 3
MISO
SS
3 Initialization
To assure proper PSC-SPI module functionality, each module must be initialized before usage. When
initializing the PSC-SPI module, the user must obey these instructions.
MCLK_EN
SYS_CLK MCLK_DIV
REF_CLK
Clock MCLK_DIV
PSC_MCLK_IN gate
SPDIF_RXCLK PSC_MCLK_OUT
SPDIF_TXCLK
MCLK_1_SRC
MCLK_0_SRC
Figure 4. PSC (MCLK) clock generation
Equation 1 and Equation 2 show the calculation of a 1 MHz SCLK frequency derived from the SYS_CLK
frequency.
CAUTION
IPS frequency must be greater than or equal to PSC_MCLK_OUT
frequency.
The maximum SCLK frequency for SYS_CLK input frequency can be easily calculated as 20 MHz see
Equation 3 and Equation 4.
SYS_CLK 400MHz
PSC_MCLK_OUT = -------------------------------------- = --------------------- 80MHz IPS_CLK ( 83MHz )
MCLK_DIV + 1 4+1
Eqn. 3
PSC_MCLK_OUT 80MHz
SCLK Frequency = --------------------------------------------- = ------------------ = 20MHz Eqn. 4
BCLK_DIV + 1 3+1
NOTE
The PSC:CCR register contains BCLKDiv (bit clock divider) information.
Note that BCLKDiv bytes in CCR register are swapped.
NOTE
Minimum BCLKDiv for SPI mode equals 3.
Disadvantages end of frame mode is not supported in DMA mode. The last data packet has to
be sent using polling or interrupt mode. All data has to be pre-prepared in the array/buffer before
the DMA transfer starts.
The following sections show how to initialize each one of these modes.
FrameSyncDiv + 1 32 + 1
DSCKL Delay = ------------------------------------------------- = ------------------------6- = 0.5 s Eqn. 5
MCLK Frequency 66.6 10
4. Define the divider for the bit clock generation (not required for slave mode) in SPI master mode,
the bit clock frequency BCLK (SCKL) is generated by dividing the MCLK frequency. In addition
to the BCLK generation, the DSCLK delay and the DTL delay must be defined. SCKL is generated
internally by dividing the MCLK frequency, as determined by Equation 6. This equation contains
the example for SCKLFrequency = 1 MHz.
6
MCLK Frequency 66.6 10
SCK Frequency = -------------------------------------
- = ------------------------- = 0.99MHz Eqn. 6
BCLKDiv + 1 66 + 1
NOTE
PSC:CCR register contains BCLKDiv (bit clock divider) information. Note
that BCLKDiv bytes in the CCR register are swapped.
5. Set delay after transfer (not required for slave mode) the delay between consecutive transfers is
created by dividing the IPS_CLK clock frequency. The delay after transfer is usually set to 2 s.
Equation 7 determines the actual delay after transfer.
CT + 2 3 132 + 2
DTL = ------------------------------------------- = -------------------------------------- = ------------------------6- = 2 s Eqn. 7
IPS_CLK Frequency MCLK Frequency 66.6 10
where
CT[0:7] = CTUR[0:7]
CT[8:15] = CTLR [0:7]
6. Reset mode registers PSC-MR1 and PSC-MR2 mode register 1 and mode register 2 must be set
to 0 before SPI communication.
7. Enable receiver and transmitter enable the Tx and Rx part in the PSC-CMD register.
Table 4. Serial interface control register used in SPI mode
34!24
$ISABLE 03# RECEIVER AND TRANSMITTER
3ET THE MAIN OPERATION MODE
03# 3ERIAL )NTERFACE #ONTROL 2EGISTER 3)#2
$EFINE DIVIDER FOR &RAME3YNC
03#
#ODEC #LOCK 2EGISTER ##2
&RAME3YNC$IV
$EFINE DIVIDER FOR "IT #LOCK
3ET $ELAY !FTER 4RANSFER $4,
03#
#OUNTER 4IMER 5PPER 2EGISTER #452
03#
#OUNTER 4IME ,OWER 2EGISTER #4,2
2ESET -ODE 2EGISTER
03#
-ODE REGISTER -2
03#
-ODE REGISTER -2
%NABLE 03# RECEIVER AND TRANSMITTER
03#
#OMMAND 2EGISTER
4# 2# FIELDS
%.$
34!24
$ISABLE 03# RECEIVER AND TRANSMITTER
03#
#OMMAND 2EGISTER
4# 2# FIELDS
3ET THE MAIN OPERATION MODE
03# 3ERIAL )NTERFACE #ONTROL 2EGISTER 3)#2
2ESET -ODE 2EGISTER
03#
-ODE REGISTER -2
03#
-ODE REGISTER -2
%NABLE 03# RECEIVER AND TRANSMITTER
03#
#OMMAND 2EGISTER
4# 2# FIELDS
%.$
NOTE
The allocated area does not depend on the defined transfer mode in other
words, it does not depend on whether 8-, 16-, or 32-bit data will be stored in
the FIFO slice. When the user defines the size of the slice, always define n
32-bit words.
3. Set an alarm level for the Tx/Rx FIFO slice the alarm level defines the number of data bytes in
the FIFO when the alarm status appears. For the Tx FIFO area, the alarm status appears if the
amount of data in the Tx FIFO is below this alarm level. For the Rx FIFO area, the alarm status
appears if the amount of data in the Rx FIFO is above this alarm level. The alarm level and the
request lines deassert if the amount of data crosses this level again.
4. Reset Tx/Rx FIFO slice The reset FIFO slice command mainly clears all data in the FIFO slice
and resets the slices internal pointer. It also clears the underrun, overrun, and memory access error
bits.
NOTE
If the reset FIFO slice command is used, then the FIFO slice becomes
disabled.
5. Enable Tx/Rx FIFO slice Enable FIFO slice command enables the slice. The FIFO controller
provides the data for the transmitter and stores the data from the receiver. If the size of the FIFO
slice is zero, it is not possible to enable the FIFO.
Tx alarm level
Tx FIFO slice
Tx FIFO slice
Tx slice start address
Rx alarm level
Rx FIFO slice Rx FIFO slice
Rx slice start address = 0x0
Figure 7. FIFOC setting parameters
NOTE
For better debugging of user code, the internal pointer register FIFOC:PTR
is very useful. This register shows the current write and read positions in the
FIFO memory without the offset for the slice number, and is writable in
debug mode.
34!24
3ET THE START ADDRESS FOR
4X2X &)&/ SLICE
&/#
4X2X &)&/ 3IZE 2EGISTER
&)&/ 34!24 !$$
3ET THE SIZE FOR 4X2X &)&/ SLICE
3ET AN ALARM LEVEL FOR 4X2X &)&/ SLICE
2ESET 4X2X &)&/ SLICE
%NABLE 4X2X &)&/ SLICE
%.$
For further information on how to set up FIFOC and PSC in the SPI mode see these chapters in Freescale
document MPC5121ERM, MPC5121e Microcontroller Reference Manual:
Chapter 30, Programmable Serial Controller
Chapter 31, PSC Centralized FIFO Controller
register (IMR) in the FIFO controller. Figure 9 shows that initial sequence. The interrupts are used to
detect:
MEM ERROR memory access error (access to the data register)
DATA READY data ready status
ORERR overrun error
URERR underrun error
ALARM FIFO alarm
FULL FIFO full
EMPTY FIFO empty
Interrupts are enabled by writing one to the proper position in the interrupt mask register in the FIFO host
controller and also in the IPIC.
34!24
%NABLE EXTERNAL INTERRUPTS
)NITIALIZE )NTERRUPT?(ANDLER FUNCTION
%NABLE INTERRUPT
FROM &)&/# IN THE )0)#
%NABLE SPECIFIC INTERRUPT SOURCES
%.$
For an example and description of how to use SPI in the interrupt mode, see Section 4.4, Interrupt mode.
For further information on how to enable and handle interrupts, see these chapters in Freescale document
MPC5121ERM, MPC5121eMicrocontroller Reference Manual:
Chapter 20, Integrated Programmable Interrupt Controller
Chapter 30, Programmable Serial Controller
Chapter 31, PSC Centralized FIFO Controller
3 4! 2 4
%NABLE EXTERNAL INTERRUPTS
)NITIALIZE )NTERRUPT?(ANDLER
FUNCTION
%NABLE INTERRUPT FROM $-! IN
THE )0)#
)0)#
3)-32?, REGISTER
$-! BIT
%NABLE REQUEST TO $-!
ENGINE IN &)&/#
3ET 4RANSFER #ONTROL $ESCRIPTOR
4#$N
4#$
BYTE MEMORY STRUCTURE
3EE -0#%2- SECTION
#LEAR THE SPECIFIED CHANNELgS
DONE BIT IN THE SPECIFED
CHANNELgS 4#$
$-!
$-!#$.% REGISTER
%NABLE $-! CHANNEL INTERRUPT
IN 4RANSFER #ONTROL $ESCRIPTOR
%.$
For further information on how to enable interrupts and handle interrupts and enable DMA see these
chapters in Freescale document MPC5121ERM, MPC5121eMicrocontroller Reference Manual:
Chapter 11, Direct Memory Access
Chapter 20, Integrated Programmable Interrupt Controller
4 Modes of operation
This section describes the operation of the SPI, focusing on:
Features
PSC bit width
End of frame mode
Bit clock polarity, clock phase and polarity
Shift direction
Mode of operation
Polling mode (master and slave)
Interrupt mode (master and slave)
DMA mode (master and slave)
All PSCs operates in master and slave mode and support a full duplex SPI mode. Master and slave modes
support polling, interrupt, and DMA mode as well. For the proper functionality perform the steps given in
Section 4.1, SPI configuration, Section 4.2, Master mode, Section 4.3, Slave mode, and Section 4.4,
Interrupt mode.
0 Data in is sampled on the falling edge of the BCLK and data out is shifted on the rising edge
ClkPol
1 Data in is sampled on the rising edge of the BCLK and data out is shifted on the falling edge
0 Active-low clocks selected; SCKL idles high
CPOL
1 Active-high clocks selected; SCKL idles low
0 Data transfer starts with assertion of SS
CPHA
1 Data transfer starts with the first edge of SCKL
0 MSB first
SHDIR
1 LSB first
CAUTION
The bit clock polarity PSC-SICR[ClkPol] has to be set to 0 when PSC
behaves as an SPI device. Bit clock polarity is used for other protocols of
the codec mode.
Emptying Tx FIFO and filling of Rx FIFO occurs simultaneously in master and in slave.
1. Clock initialization see Section 3.2, IPS bus and Section 3.3, MCLK frequency.
2. PSC master initialization see Section 3.5.1, PSC initialization.
3. FIFOC initialization see Section 3.5.2, FIFOC initialization.
4. If EOF mode is used, then the EOF flag has to be set. This flag defines the next data written to the
data register as the last in this frame.
5. The user application writes data to the Tx FIFO data register.
6. Test if the Rx FIFO slice is empty. If the Rx FIFO slice contains data then this data has to be read
by the application.
7. Before sending the next set of data to the Tx FIFO, test if the Tx FIFO slices ALARM is reached.
If the ALARM level is reached then you cannot send any data to the Tx FIFO, due to the possibility
of overflow.
8. If you want send more data, then go to step 4; otherwise continue to step 9.
9. There is a possibility that the Tx FIFO contains some data which has to be sent out to the SPI shift
register. You have to test if the Tx FIFO slice is empty or contains data waiting for transfer. If the
Tx FIFO slice contains data then you have to wait. An Rx FIFO slice overflow may occur in this
wait time, and you have to test if the Rx FIFO slice is empty in this wait time.
10. When step 7 goes to step 8, then the Tx FIFO slice does not contain any data. However, we have
to count with the delay (a few processor ticks) between the Tx/Rx FIFO slices and the Tx/Rx shift
register in the SPI module. There are a few possible ways to do this. The simplest way to cover it
is to compare the amount of transmitted data and amount of received data. This is why polling
mode is not recommended for SPI mode.
NOTE
In SPI master mode the PSC controls the serial data transfers. If the Tx FIFO
becomes empty (underrun) or the Rx FIFO becomes full (overflow) in the
middle of a multi-byte transfer, rather than set the Tx underrun or Rx
overflow status bits, the PSC keeps the slave select signal low/active and
stops the SCKL serial clock. When the Tx FIFO is no longer empty and the
Rx FIFO no longer full, the transfer proceeds.
34!24
#,/#+ INITIALIZATION
03# INITIALIZATION
&)&/# INITIALIZATION !
.O ,AST DATA SHOULD BE
WRITTEN TO &)&/#
5SE %ND OF &RAME MODE 9ES
!
03#
3)#2
5SE%/&