STMicroelectronics / stm32g0xx_hal_driver

Provides the STM32Cube MCU Component "hal_driver" of the STM32G0 series.
BSD 3-Clause "New" or "Revised" License
13 stars 4 forks source link

HAL_SPI_TransmitReceive_IT crashes when TX buffer is not 16 bit aligned #5

Open kasjer opened 3 weeks ago

kasjer commented 3 weeks ago

Describe the set-up

Describe the bug Calling function HAL_SPI_Receive_IT() or HAL_SPI_TransmitReceive_IT() with buffer not aligned to 16 bits crashes MCU

How To Reproduce

  1. Call HAL_SPI_Receive_IT() with unaligned buffer and more then 1 byte to send

Additional context Real problem however is in SPI_2linesTxISR_8BIT() function that tries to optimize send using 16 bits access if there is more data. For Cortex-M0+ unaligned read results in fault.

static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
{
  /* Transmit data in packing Bit mode */
  if (hspi->TxXferCount >= 2U &&
    ((uint32_t)(hspi->pTxBuffPtr) & 1) == 0) // <= !!!! Additional check to use 8 bit for unaligned buffer here fixes the problem
  {
    hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr); // <= !!! Crashes here on Cortex-M0+
    hspi->pTxBuffPtr += sizeof(uint16_t);
    hspi->TxXferCount -= 2U;
  }
...
KRASTM commented 2 weeks ago

ST internal Reference: 184492