AnsweredAssumed Answered

[bug] HAL_SPI_TransmitReceive broken in STM32CubeF4 1.11.0

Question asked by serb.sergey on Mar 2, 2016
Latest reply on Nov 9, 2017 by Hugues Libotte
Hello, and the next SPI bug is inside HAL_SPI_TransmitReceive function.

It hangs communication after a few bytes shifted over SPI. The reason is that code logic is broken (it was correct in previous libraries).

I'm using 8 bit mode and sending 7 bytes packets, so I'll show only code I'm interested in:
/* Transmit and Receive data in 8 Bit mode */
  else
  {
    while((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))
    {
      /* check TXE flag */
      if((hspi->TxXferCount > 0U) && (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)))
      {
        *(__IO uint8_t *)&hspi->Instance->DR = (*pTxData++);
        hspi->TxXferCount--;
 
#ifdef USE_SPI_CRC
        /* Enable CRC Transmission */
        if((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
        {
          SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
        }
#endif
      }
 
      /* Wait until RXNE flag is reset */
      if((hspi->RxXferCount > 0U) && (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)))
      {
        (*(uint8_t *)pRxData++) = hspi->Instance->DR;
        hspi->RxXferCount--;
      }
      if((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >=  Timeout))
      {
        errorcode = HAL_TIMEOUT;
        goto error;
      }
    }
  }

This is wrong, you can't write next byte to DR untill you read received byte from it, otherwise some bytes would be lost on RX direction (overwitten with next TX byte).

Outcomes