Professional Documents
Culture Documents
June 2016
Agenda
• Features
• Block Diagram
• Function Descriptions
• Functions and Sample Code in BSP
• Sample Code (with PDMA)
2
Features (1/2) -Revised
• SPI Mode
- Up to 2 sets of SPI controllers
- Supports Support Master (max. 36 MHz) or Slave (max. 18 MHz) mode
operation
- Support Dual I/O transfer mode
- Configurable bit-length of a transaction word 8- ~ 32-bit
- Provides separate 8-level depth transmit and receive FIFO buffers
- Supports MSB first or LSB first transfer sequence
3
Features (2/2)
• SPI Mode
- Supports Byte Reorder function
- Supports PDMA transfer
- Supports 3-Wire, no slave selection signal, bi-direction interface
- Support Byte or Word Suspend function
4
Block Diagram
0x08 8-bit
~ ~
0x1F 31-bit
6
SPI Basic Function Setting (2/3)
• Support the general SPI Mode 0~3
Function Clock Polarity Transmitting Edge Receiving Edge
CLKPOL TXNEG RXNEG
7
SPI Basic Function Setting (3/3)
8
Serial Clock
• Master:
– DIVIDER (SPI_DIVIDER[7:0]):
• SPI peripheral clock frequency
9
Serial Clock (2/2)
• Slave:
- The SPI clock is provided by off-chip Master device. The SPI clock must
be faster than the clock rate from Master device.
- HCLK > SPI Clock >external clock of SPI master device
Note: The frequency of SPI peripheral clock cannot be faster than the system clock rate
regardless of Master or Slave mode.
10
Master/Slave Mode
• Master
SPIx_CLK SPIx_CLK
SPIx_SS SPIx_SS
Note: x = 0, 1
• Slave
SPIx_CLK SPIx_CLK
SPIx_SS SPIx_SS
Note: x = 0, 1
11
Byte Reorder Function
• REORDER (SPI_CNTRL[19]):
- TX/RX FIFO will be rearranged.
12
Word-/Byte-Suspend Function
• SP_CYCLE (SPI_CNTRL[15:12]):
- The suspend interval depends on the setting of SP_CYCLE. (0.5~15.5
serial clock periods)
• Word-Suspend only in FIFO mode.
• Byte-Suspend must be in Reorder Mode and 16/24/32-bit length.
Suspend interval
Suspend
Interval
CLKP=0
SPIn_CLK
CLKP=1
MSB
SPIn_MISO0 TX0[31]
TX0[30] TX0[24] TX0[23] TX0[22] TX0[16]
MSB
SPIn_MOSI0 RX0[31]
RX0[30] RX0[24] RX0[23] RX0[22] RX0[16]
14
FIFO Mode
• FIFO (SPI_CNTRL[21]):
- =1, FIFO mode enabled.
- =0, Disabled.
15
Interrupt
• IE (SPI_CNTRL[17]):
- =1, SPI interrupt Enabled.
- =0, Disabled.
• Interrupt Flags :
16
Sample Code Path
NUC029xEEBSPv3.00.001
SampleCode
StdDriver
SPI_Loopback
SPI_Loopback.uvproj
KEIL
17
Functions and Sample Code in BSP
• \SampleCode\StdDriver
- SPI_Loopback
- SPI_MasterFifoMode
- SPI_PDMA_Loopback
- SPI_SlaveFifoMode
18
Functions and Sample Code in BSP
• spi.c
- uint32_t SPI_Open(SPI_T *spi, uint32_t u32MasterSlave, uint32_t u32SPIMode,
uint32_t u32DataWidth, uint32_t u32BusClock)
This function make SPI module be ready to transfer
• spi.h
- SPI_READ_RX(spi)
Get the datum read from RX register
- SPI_WRITE_TX(spi, u32TxData)
Write datum to TX register
- SPI_SET_SS_LOW(spi)
Set SPIx_SS pin to low state 19
SPI Sample Code
int main (void)
{ ……
/* Configure as a master, clock idle low, 32-bit transaction, drive output on falling clock edge and
latch input on rising edge. */ /* Set IP clock divider. SPI clock rate = 2 MHz */
SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 32, 2000000);
/* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW);
……
u32Err = 0;
for(u32TestCount = 0; u32TestCount < 0x1000; u32TestCount++) {
/* set the source data and clear the destination buffer */
for(u32DataCount = 0; u32DataCount < TEST_COUNT; u32DataCount++) {
g_au32SourceData[u32DataCount] = u32DataCount;
g_au32DestinationData[u32DataCount] = 0;
}
u32DataCount = 0;
20
SPI Sample Code
while(1) {
/* Write to TX register */
SPI_WRITE_TX(SPI0, g_au32SourceData[u32DataCount]);