cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_SPI_TransmitReceive problems

Ron54
Associate

I am having problems trying to use the HAL_SPI_TransmitReceive function. 

I am using STM32CubeIDE along with the STM32U5G9J-DK2, I am configuring my side as a full duplex slave using the following parameters:

Ron54_1-1741900491831.png

I am working with an embedded processor that is acting as the Master, the features of which I do not have a complete understanding of. I do know it's implemented as a full duplex Master with the SPI1 port enabled. We have the embedded processor connected to the STM chip using the following pins.

Ron54_2-1741900774954.png

I was able to fully implement the receive portion on my end first using HAL_SPI_Receive and then changing that to HAL_SPI_ReceiveDMA. Both of these worked for me and I was able to receive correct data.

I then attempted to add transmit to my end using HAL_SPI_TransmitReceiveDMA and immediately ran into weird problems. Note that the Master is not yet configured to process any of the data I am sending it, but I can see that I am transmitting what looks like correct data using a logic analyzer. However, even though the transmit looks good, I realized I was no longer receiving correct data. There is a smattering of bits getting inserted into my receive buffer that corrupts the data. 

What's super strange is if I initialize my transmit buffer to all zeroes, I am able to correctly receive data again. I can even initialize my transmit buffer to 0xAAAAAAAA and it's fine. As long as there's no more than 2 bits set to 1 in a row, I am able to receive correct data. But as soon as I set my transmit buffer words to all 0xFFFFFFFF, 0x33333333, or even 0x00000003, then I start getting corrupt bits in my receive buffer.  

In order to simplify things, I have reverted to using HAL_SPI_TransmitReceive instead of the DMA version so I can step through some of the code in stm32u5xx_hal_spi.c and get an idea of what's going on. I get the same strange issues with a corrupted receive stream when using this function if I set my transmit buffer to all 0xFFFFFFF. I have found that when calling HAL_SPI_TransmitReceive, the software gets stuck in this loop...

Ron54_3-1741902302260.png

...because the TXP flag is set to 0 indicating that there's not enough free space in the TxFIFO. Also, the SPI_SR_UDR is set indicating there was an underrun in the transmit. Again, if I set my transmit stream back to all zeros, I do not hang in this loop and my receive data is not corrupted. (Also, nothing was hanging when I was using the DMA version of the function). 

My function call looks like this:

 

 

  #define rxBufferWordSize 100

  uint32_t pRxBuffer[rxBufferWordSize];
  uint32_t pTxBuffer[rxBufferWordSize];

  memset(pRxBuffer, 0, sizeof(pRxBuffer));
  for (int ii = 0; ii < rxBufferWordSize; ii = ii + 1)
	  pTxBuffer[ii] = 0xFFFFFFFF;

  SPI_Status = HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)pTxBuffer, (uint8_t *)pRxBuffer, rxBufferWordSize, 2000);

 

 

 

Does anyone have any suggestions? This is really starting to frustrate me. 

 

 

 

1 REPLY 1
TDK
Guru

Perhaps crosstalk: MISO causing spikes/pulses on the SCK line during transitions.

If you feel a post has answered your question, please click "Accept as Solution".