Random Usage faults running Hal code on 32F410
I have a fairly simple bit of code running in a CubeMX generated scheme, using HAL code to communicate over SPI. I run a loopback test a thousand times - sometimes it passes, other times I get an INVSTATE Usage Fault which appears (according to Keil) to be caused by the first line of the following code:
{
/* Transfer loop */
while(hspi->RxXferCount > 0U)
{
/* Check the RXNE flag */
if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))
{
*((uint16_t*)pData) = hspi->Instance->DR;
pData += sizeof(uint16_t);
hspi->RxXferCount--;
}
else
{
/* Timeout management */
if((Timeout == 0U) || ((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout)))
{
errorcode = HAL_TIMEOUT;
goto error;
}
}
}
Timeout is HAL_MAX_DELAY, so HAL_GetTick() is never called.
I'm particularly confused by the fact that this error appears only to be caused by trying to execute non-Thumb code. Can't see how this can happen unless it is mis-reading the flash randomly and rarely. The clock is 16MHz to give the flash an easy life.
