AnsweredAssumed Answered

Bug: STM32F0xx HAL SPI Transmit

Question asked by Goodman.Stephen.001 on Sep 23, 2016
Latest reply on Sep 16, 2017 by Emrah Duatepe
I have found a bug in the STM32F0xx HAL SPI driver, specifically in the HAL_SPI_Transmit and HAL_SPI_TransmitReceive functions.  Basically they are converting a pointer to uint8_t into a pointer to uint16_t.  The problem with that is that the M0 cannot support unaligned accesses and the uint8_t pointer is not guaranteed to be on a 16 bit boundary.  In fact this has happened to me and the software hard faults when the conversion is attempted.

The functions are defined as:
HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)

They are specifically asking for a pointer to 8 bit data.

In the function:
/* Transmit and Receive data in 16 Bit mode */
if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
/* Transmit and Receive data in 8 Bit mode */
  hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
  hspi->pTxBuffPtr += sizeof(uint16_t);
  hspi->TxXferCount -= 2;

It actually checks whether the data being sent is 8 bits and then converts the pointer to the 8 bit data into a pointer to 16 bit data.