ashwatermelon

ashwatermelon

【Study Notes】STM32 Microcontroller SPI Communication

I finished learning about STM32's SPI communication these days, and today I'm writing my study notes.

(1) What is SPI communication?#

SPI (Serial Peripheral Interface) is a general-purpose data bus developed by Motorola, consisting of four communication lines: SCK (Serial Clock), MOSI (Master Output Slave Input), MISO (Master Input Slave Output), and SS (Slave Select). It is synchronous, full-duplex communication that supports multiple devices on the bus (one master and multiple slaves).

(2) SPI hardware circuit#

The hardware circuit diagram of SPI is as follows (the image can finally be sent!):

image

The SCK clock line is connected to all devices, sending clock signals;
MOSI is the line for the master to send data to the slave, while MISO is the line for the master to receive data from the slave;
SS is the chip select, with the master and all slaves having a chip select line for connection, used to specify the slave.

(3) SPI timing#

The basic unit of SPI timing is: start, end, exchange one byte.
Compared to USART and I2C, exchanging one byte is a major feature of SPI, where the master and slave directly exchange data rather than one side sending and the other receiving.
The start and end timing is relatively simple; you just need to set SS high or low:

image

Exchanging one byte is determined by two parameters, CPOL and CPHA, which define four modes.
CPOL: idle state SCK level.
CPHA: data is sampled on the rising or falling edge of SCK.
The timing diagrams for the four modes are as follows:

image

image

image

image

(4) Software SPI#

Software SPI requires selecting four pins for configuration, where SS, SCK, and MOSI are configured as push-pull outputs, and MISO is configured as a pull-up input:

image

Then we write the three basic timing units:

image

Subsequently, we can use these three basic timing units to form a complete timing sequence for SPI communication.
Here, we wrote several functions for the W25Q64, primarily sending commands, starting with the start timing, then sending the command, followed by some parameters of the command, and finally ending. Here is the command to read the ID:

image

(5) Hardware SPI#

The STM32 has an internal SPI communication peripheral, and its basic structure is as follows:

image

Unlike I2C, SPI has separate buffers for sending and receiving, but shares a shift register.

The configuration process is as follows:

image

Here, we need to configure MOSI and SCK as multiplexed push-pull outputs, while the other two remain unchanged (SS is not used for the SPI peripheral because NSS does not have that function). Then we configure the structure, with the structure parameters as follows:
SPI_BaudRatePrescaler——Baud rate, determines the speed of transmission
SPI_CPHA——The previously mentioned CPHA
SPI_CPOL——The previously mentioned CPOL
SPI_CRCPolynomial——CRC check, which we do not need here, but cannot be set to 0; don’t ask me how I know.
SPI_DataSize——Data size, we use 8 bits for transmission.
SPI_Direction——Transmission direction, we choose full-duplex.
SPI_FirstBit——Whether to send the most significant bit first or the least significant bit first; we choose most significant bit first.
SPI_Mode——SPI mode, we choose master mode.
SPI_NSS——NSS, used for multi-master, which we do not need, so we can choose any one.

Then the start and end timing code does not need to be changed; we modify the data exchange code:

image

The code mainly follows the timing diagram below:

image

That’s all about SPI, 886.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.