
Serial peripheral interface (SPI)
RM0008
682/1096
Doc ID 13902 Rev 12
25.3.3 Configuring
the
SPI in master mode
In the master configuration, the serial clock is generated on the SCK pin.
Procedure
1.
Select the BR[2:0] bits to define the serial clock baud rate (see SPI_CR1 register).
2.
Select the CPOL and CPHA bits to define one of the four relationships between the
data transfer and the serial clock (see
3.
Set the DFF bit to define 8- or 16-bit data frame format
4.
Configure the LSBFIRST bit in the SPI_CR1 register to define the frame format.
5.
If the NSS pin is required in input mode, in hardware mode, connect the NSS pin to a
high-level signal during the complete byte transmit sequence. In NSS software mode,
set the SSM and SSI bits in the SPI_CR1 register. If the NSS pin is required in output
mode, the SSOE bit only should be set.
6.
The MSTR and SPE bits must be set (they remain set only if the NSS pin is connected
to a high-level signal).
In this configuration the MOSI pin is a data output and the MISO pin is a data input.
Transmit sequence
The transmit sequence begins when a byte is written in the Tx Buffer.
The data byte is parallel-loaded into the shift register (from the internal bus) during the first
bit transmission and then shifted out serially to the MOSI pin MSB first or LSB first
depending on the LSBFIRST bit in the SPI_CR1 register. The TXE flag is set on the transfer
of data from the Tx Buffer to the shift register and an interrupt is generated if the TXEIE bit in
the SPI_CR2 register is set.
Receive sequence
For the receiver, when data transfer is complete:
●
The data in the shift register is transferred to the RX Buffer and the RXNE flag is set
●
An interrupt is generated if the RXNEIE bit is set in the SPI_CR2 register
At the last sampling clock edge the RXNE bit is set, a copy of the data byte received in the
shift register is moved to the Rx buffer. When the SPI_DR register is read, the SPI
peripheral returns this buffered value.
Clearing the RXNE bit is performed by reading the SPI_DR register.
A continuous transmit stream can be maintained if the next data to be transmitted is put in
the Tx buffer once the transmission is started. Note that TXE flag should be ‘1 before any
attempt to write the Tx buffer is made.
Note:
In the NSS hardware mode, the slave's NSS input is controlled by the NSS pin or another
GPIO pin that has to be controlled by software.