Showing results for 
Search instead for 
Did you mean: 

Incorrect operation HAL_SPI_Transmit?

Vladimir Yakovlev
Posted on August 23, 2017 at 15:27

Hello! I work under STM32F051. When working with the HAL_SPI_Transmit and HAL_SPI_TransmitReceive functions,

one thing happened: when data is transferred in SPI_DIRECTION_2LINES mode, the data is also received in the 32 bit RX FIFO buffer,

so if the next called function is HAL_SPI_TransmitReceive or HAL_SPI_Receive, the garbage data in RX FIFO will be read when Transfer.

To solve the problem, I had to empty the RX FIFO inside HAL_SPI_Transmit after the transfer was completed.

Do I reason correctly, or do the developers lay another logic for these functions?

Matthew Staben
Associate II
Posted on August 24, 2017 at 17:58

SPI Traffic is typically full-duplex (sending out characters will cause one to be received).  It is up to the developer and the end peripheral to determine when to care or not about what is received.  Sometimes, only data is being sent, other times, data is being received; but something (e.g. NULL) must be sent for data to be returned.  

For example, if the program on the other side of the SPI bus expects an address and read count, the requesting program must send the address, and count of data expected, perhaps followed by some pin toggling after which the RX FIFO may be emptied.  It is then another stream of data (nulls, etc.) will result in return data that is meaningful.  

I have found the HAL_SPI_TransmitReceive ( ) function to be a very useful function that has covered all my SPI data transfer needs.

Posted on August 24, 2017 at 18:13

Thanks for your reply. Undoubtedly, the use of the HAL_SPI_TransmitReceive function with full-duplex SPI will be enough for most applications and the developer should realize that in this case the data is both received and transmitted. This assumes the very context of the functions. However, the function HAL_SPI_Transmit does not assume reception of data, but only transmission (in spite of the fact that it actually works in full-duplex mode). Therefore, this function must take care that the received data is not, since the use of this function does not imply that the data should be received.