
GD32F403xx User Manual
463
SPI initialization sequence
Before transmiting or receiving data, application should follow the SPI initialization sequence
described below:
1.
If master mode or slave TI mode is used, program the PSC [2:0] bits in SPI_CTL0 reg ister
to generate SCK with desired baud rate or configure the Td time in TI mode, otherwise,
ignore this step.
2.
Program data format (FF16 bit in the SPI_CTL0 register).
3.
Program the clock timing register (CKPL and CKPH bits in the SPI_CTL0 register).
4.
Program the frame format (LF bit in the SPI_CTL0 register).
5.
Program the NSS mode (SWNSSEN and NSSDRV bits in the SPI_CTL0 register)
according to the application’s demand as described above in
section.
6.
If TI mode is used, set TMOD bit in SPI_CTL1 register, otherwise, ignore this step.
7.
If NSSP mode is used, set NSSP bit in SPI_CTL1 register, otherwise, ignore this step.
8.
Conf igure MSTMOD, RO, BDEN and BDOEN depending on the operation modes
described above.
9.
If Quad-SPI mode is used, set the QMOD bit in SPI_QCTL register. Ignore this step if
Quad-SPI mode is not used.
10. Enable the SPI (set the SPIEN bit).
SPI basic transmission and reception sequence
Transmission sequence
After the initialization sequence, the SPI is enabled and stays at idle state. In master mode,
the transmission starts when the application writes a data into the transmit buffer. In slave
mode, the transmission starts when SCK clock signal begins to toggle at SCK pin and NSS
level is low, so application should ensure that data is already written into transmit buffer before
the transmission starts in slave mode.
When SPI begins to send a data frame, it first loads this data frame from the data buffer to
the shift register and then begins to transmit the loaded data frame, TBE (transmit buffer
empty) flag is set after the first bit of this frame is transmited. After TBE flag is set, which
means the transmit buffer is empty, the application should write SPI_DATA register again if it
has more data to transmit.
In master mode, software should write the next data into SPI_DATA register before the
transmission of current data frame is completed if it desires to generate continuous
transmission.
Reception sequence
The incoming data will be moved from shift register to the receive buffer after the last valid
sample clock and also, RBNE (receive buffer not empty) will be set. The application should
read SPI_DATA register to get the received data and this will clear the RBNE flag automatically.
In MRU and MRB modes, hardware continuously sends clock signal to receive the next data