Академический Документы
Профессиональный Документы
Культура Документы
SPI
NuMicro@nuvoton.com
2013/3/1
Agenda
• Features
• Block Diagram
• Function Descriptions
• Sample Code
1
Features (I)
• Up to three SPI controllers
• Support Master (max. 32 MHz) or Slave (max. 16
MHz) mode operation
• Each SPI controller can drive up to two slave devices
in Master mode
• Support 1-bit (channel) or 2-bit (channel) serial data
IN/OUT
• Support Dual IO transfer mode
• Configurable bit length of a transaction (8 to 32 bits)
• MSB or LSB first
2
Features (II)
• Support Word- and Byte-Suspend function
• Support variable serial clock in Master mode
• Provide separate 8-level depth Tx/Rx FIFO
• Support wake-up function
• Support PDMA transfer
• Support 3-wire, no slave select, bi-direction interface
• Support general SPI Mode 0 ~ Mode 3
3
Block Diagram
Status/Control
SPICLKx
Register
APB
Interface SPISSx0
Control SYNC Gray
Control Codec 1-channel I/O
SPISSx1
MOSIx1
8 Level TX Buffer
PDMA MISOx0
Control
2-channel I/O
x : indicates the number of SPI controller
4
SPI Basic Function Setting (I)
0x08 8-bit
~ ~
0x1F 31-bit
5
SPI Basic Function Setting (II)
• Support the general SPI Mode 0~3.
Function Clock Polarity Transmitting Edge Receiving Edge
CLKP TX_NEG RX_NEG
Value (SPI_CTL[11]) (SPI_CTL[2]) (SPI_CTL[1])
6
SPI Basic Function Setting (III)
Function No Slave Select Auto Slave Slave Select Level Slave Select
NOSLVSEL Select Trigger Active Level
Value (SPI_SSR[5]) AUTOSS SS_LTRIG SS_LVL
(SPI_SSR[3]) (SPI_SSR[4]) (SPI_SSR[2])
7
Serial Clock
• Master:
• DIVIDER1 (SPI_CLKDIV[7:0]):
• SPI serial clock frequency
fsclk feclk /( DIVIDER1 1)
feclk Is the SPI engine clock source (HCLK or PLL output)
• DIVIDER2 (SPI_CLKDIV[23:16]):
• SPI serial clock frequency.
fsclk feclk /(( DIVIDER2 1) * 2)
• Slave:
• The SPI clock is driven by off-chip Master device. The SPI
engine clock must be faster than the serial clock from
Master. 8
Variable Clock Function
• VARCLK_EN (SPI_CTL[23]):
• VARCLK_EN = 1, variable serial clock freq. enable.
• VARCLK_EN = 0, Disable.
• 16-bit length only.
Clock Sequence 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
SPICLKX
VARCLK 0 00 00 00 00 11 11 11 11 11 11 11 11 00 00 11 1
(0x007FFF87)
Clock 1
(DIVIDER1)
Clock 2
(DIVIDER2)
9
Word-/Byte-Suspend Function
• SP_CYCLE (SPI_CTL[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 32-bit length.
SPISS
Suspend interval
SPICLK
MSB
Tx0[30] Tx0[24] Tx0[23] Tx0[22] Tx0[16]
MOSI Tx0[31]
MSB
MISO Rx0[31]
Rx0[30] Rx0[24] Rx0[23] Rx0[22] Rx0[16]
10
Byte Reorder Function
• REORDER (SPI_CTL[20:19]):
• TX/RX FIFO will be rearranged.
Byte3 Byte2 Byte1 Byte0 REORDER = 2'b01 or 2'b10 Byte0 Byte1 Byte2 Byte3
TX_BIT_LEN = 32 bits
MSB first
nn nn Byte0 Byte1
TX_BIT_LEN = 16 bits
nn = unknown byte
11
2-Bit (Two-Channel) Mode
• TWOB (SPI_CTL[22]):
SPI SCLK
SPICLK
RX_Buffer0 MISO[0] MISO External
TX_Buffer0 MOSI[0] MOSI SPI Device 0
RX_Buffer1 MISO[1]
SS
TX_Buffer1 MOSI[1]
SPISS[0]
SPISS[1] SCLK
External
MISO
SPI Device 1
MOSI
SS
12
Timing Diagram of 2-Bit Mode in Master
SS_LVL=1
SPISS
SS_LVL=0
CLKP=0
SPICLK
CLKP=1
Channel 0
MSB LSB
MISOx0 Tx0[30] Tx0[16] Tx0[15] Tx0[14]
Tx0[31] Tx0[0]
MSB LSB
MOSIx0 Rx0[30] Rx0[16] Rx0[15] Rx0[14]
Rx0[31] Rx0[0]
MSB LSB
MISOx1 Tx1[30] Tx1[16] Tx1[15] Tx1[14]
Tx1[31] Tx1[0]
MSB LSB
MOSIx1 Rx1[30] Rx1[16] Rx1[15] Rx1[14]
Rx1[31] Rx1[0]
Channel 1
13
Dual-IO Mode
• DUAL_IO_EN (SPI_CTL[29]):
• =1, Dual I/O mode enabled.
• =0, Disabled.
• DUAL_IO_DIR (SPI_CTL[28]):
• =1, Data write.
• =0, Data read.
SPI_SS SPI_SS
SPI_CLK SPI_CLK
SPI_MOSI 7 6 5 4 3 2 1 0 6 4 2 0 6 4 2 0 6 4 2 0 6 4 2 0 SPI_MOSI 7 6 5 4 3 2 1 0 6 4 2 0 6 4 2 0 6 4 2 0 6 4 2 0
Master Output Output Master Output Input
Slave Input Slave Input
SPI_MISO 7 5 3 1 7 5 3 1 7 5 3 1 7 5 3 1 SPI_MISO 7 5 3 1 7 5 3 1 7 5 3 1 7 5 3 1
Master Input Master Input
Slave Output Output Slave Output Input
DUAL_IO_EN DUAL_IO_EN
DUAL_IO_DIR DUAL_IO_DIR
14
FIFO Mode
• FIFO (SPI_CTL[21]):
• =1, FIFO mode enabled.
• =0, Disabled.
Write
SPI_TX Buffer
n
MISO Pin in Master Mode Receive buffer n 2
Receivebuffer
buffer 2
ReceiveBuffer 01 1
or Receive
MOSI Pin in Slave Mode
Read
SPI_RX Buffer
15
Wake-up Function
• Receive a toggle signal of SPICLK from the external
off-chip device
• wake-up the chip from power down mode.
16
Interrupt
• INTEN (SPI_CTL[17]):
• 1: enable SPI interrupt.
• 0: disable SPI interrupt.
• Interrupt Flags :
INTSTS SLV_START_INTSTS TIME_OUT_STS TXINT_STS RXINT_STS
(SPI_STATUS[7]) (SPI_STATUS[6]) (SPI_STATUS[12]) (SPI_STATUS[10]) (SPI_STATUS[8])
Unit Transfer 3-Wrie Slave Mode Receive FIFO Time- Transmit FIFO Event Receive FIFO Event
Done Event Start Event out Event TX data count <= RX data count >
TXTHRESHOLD. RXTHRESHOLD
17
Sample Code (I)
#define TEST_NUMBER 1 /* Page numbers */
#define TEST_LENGTH 256 /* Length */
uint8_t SrcArray[TEST_LENGTH];
uint8_t DestArray[TEST_LENGTH];
/* Main */
int main(void)
{
STR_PDMA_T sPDMA;
uint32_t u32ByteCount, u32FlashAddress, u32PageNumber;
uint32_t nError = 0;
SPI_DATA_T conf;
MFP_SPI0_TO_PORTE();
SPI_Init(SPI0);
/* Diable AutoSS */
SPI_DisableAutoSS(SPI0);
18
Sample Code (II)
/* Set the active level of slave select. */
SPI_SetSlaveSelectActiveLow(SPI0);
/* PDMA Init */
PDMA_Init();
sPDMA.SrcAddr = (uint32_t)&(SPI0->RX0);
sPDMA.DestAddr = (uint32_t)&DestArray[0];
sPDMA.SrcCtrl = PDMA_CSR_SAD_FIX;
sPDMA.DestCtrl = PDMA_CSR_DAD_INC;
sPDMA.TransWidth = PDMA_CSR_APB_TWS_8BIT;
sPDMA.Mode = PDMA_CSR_IP2MEM;
sPDMA.ByteCnt = TEST_LENGTH;
sPDMA.IsTimeOutEnable = 0;
PDMA_Open(PDMA1, &sPDMA);
PDMA_SPI0_RX_TO_CH1();
/* Enable INT */
PDMA_EnableInt(PDMA1, PDMA_IER_TD);
19
Sample Code (III)
/* Configure PDMA Channel 2 to receive SPI0 Tx0 */
/* Tx PDMA Setting */
PDMA_EnableChannelClock(PDMAGCR_CSR_CLK2); /* Enable Channel 2 Clock */
sPDMA.SrcAddr = (uint32_t)&SrcArray[0];
sPDMA.DestAddr = (uint32_t)&(SPI0->TX0);
sPDMA.SrcCtrl = PDMA_CSR_SAD_INC;
sPDMA.DestCtrl = PDMA_CSR_DAD_FIX;
sPDMA.TransWidth = PDMA_CSR_APB_TWS_8BIT;
sPDMA.Mode = PDMA_CSR_MEM2IP;
sPDMA.ByteCnt = TEST_LENGTH;
sPDMA.IsTimeOutEnable = 0;
PDMA_Open(PDMA2, &sPDMA);
PDMA_SPI0_TX_TO_CH2();
/* Enable INT */
PDMA_EnableInt(PDMA2, PDMA_IER_TD);
/* Wait Ready */
SpiFlash_WaitReady();
20
Sample Code (IV)
/* Program SPI Flash */
u32FlashAddress = 0;
for(u32PageNumber=0;u32PageNumber<TEST_NUMBER; u32PageNumber++){
/* Trigger PDMA Specified Channel */
PDMA_CHEnableTransfer(PDMA2);
/* Page Program */
SpiFlash_PageProgram(u32FlashAddress, 256);
SpiFlash_WaitReady();
u32FlashAddress += 0x100;
}
/* Read SPI Flash */
u32FlashAddress = 0;
for(u32PageNumber=0; u32PageNumber<TEST_NUMBER; u32PageNumber++){
/* Trigger PDMA Specified Channel */
PDMA_CHEnableTransfer(PDMA1);
/* Page Read */
SpiFlash_ReadData(u32FlashAddress, 256);
u32FlashAddress += 0x100;
22