AnsweredAssumed Answered

STM32F051K6T6 hardfault with HAL and SPI

Question asked by schperplata on Aug 3, 2016
Latest reply on Aug 9, 2016 by schperplata
Hello,

I've got a project build on custom PCB around STM32F051K6T6. There are a couple of other things on this PCB: ULN2003 driver, CD4511B driver , TLV73333 3.3V regulator and the main "problem" NRF24L01+ module, connected through SPI1.

Board peripherals and init code are generated with CubeMX software. So far everything worked just well: PWM on gpio, timer 6 for some periodic interrupts, CD4511 7 segment driver... 
When I tried to connect NRF24L01+ through SPI with custom library (modified tm_nrf library) using SPI HAL drivers, strange things happen. uC halts in Hard Fault Handler after a few SPI transmit and/or receive  commands. 
It halts at in stm32fxx_hal_spi.c file in function HAL_SPI_Transmit() at specific line (13.):
01.HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout){
02.  /*
03.  ...other code...
04.  */
05.  /* Transmit data in 8 Bit mode */
06.  else  {
07.    while (hspi->TxXferCount > 0){
08.      /* Wait until TXE flag is set to send data */
09.      if((hspi->Instance->SR & SPI_FLAG_TXE) == SPI_FLAG_TXE){
10. 
11.        if(hspi->TxXferCount > 1){
12.          /* write on the data register in packing mode */
13.          hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
14.          hspi->pTxBuffPtr += sizeof(uint16_t);
15.          hspi->TxXferCount -= 2;
16.        }
17.        else{
18.          *((__IO uint8_t*)&hspi->Instance->DR) = (*hspi->pTxBuffPtr++);
19.          hspi->TxXferCount--;
20.        }
21.      }
22.      else{
23.        /* Timeout management */
24.        if((Timeout == 0) || ((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >=  Timeout))){
25.          errorcode = HAL_TIMEOUT;
26.          goto error;
27.        }
28.      }
29.    }
30.  }
31.  /*
32.  ...other code...
33.  */
34.}
... and than halts at HarFault_Handler.

spi_hardfault_registers.PNGspi_call_stack_locals.PNGspi_hardfault.PNG

This is my first HardFault ever, so I don't really know how to handle it. I've google it alot, tried to change stack size and played with other SPI settings, but with no success. The same NRF24L01 board works with Arduino, so there must be an error somewhere in my code/pcb. 

Let me know what additional informations are needed. 
Thanks!

Outcomes