Stm32's SPI sending and receiving and hal library SPI blocking problem

1. The principle of SPI transmission and reception of stm32

Insert picture description here
The data register is split(分开) into 2 buffers - one for writing (Transmit Buffer) and another one for reading (Receive buffer). A write to the data register will write into the Tx buffer and a read from the data register will return the value held in the Rx buffer.理解最后一句话需要看stm32的电路结构。
Insert picture description here

The SPI register has 16 bits, of which 8 bits are used for read operations and the other 8 bits are used for write operations. Write data to the DR register, the data will be loaded into the TX buffer as shown in the figure, and the data in the buffer will be transmitted through the MOSI pin; on the contrary, if the DR register is read, the data in the RX buffer Will be returned to the programmer for processing, and the data in the RX buffer comes from the MISO pin.

Two, hal library function SPI blocking

2.1 Functions involved

HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size,uint32_t Timeout)

2.2 Explanation

HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size,uint32_t Timeout) function sends Size data and needs to receive Size data to end, otherwise it will be blocked in while ((hspi->TxXferCount> 0U ) || (hspi->RxXferCount> 0U)) At this position, until the receiving count value is equal to 0, it means that SIZE data is received. Before this, the subsequent program cannot be executed, but waits for the completion of the SPI receiving data.

Guess you like

Origin blog.csdn.net/weixin_43810563/article/details/111028826
SPI