cancel
Showing results for 
Search instead for 
Did you mean: 

Hardfault error in SPI slave - polling mode

jg_spitfire
Senior

Hi, I am using cubemx to configure a stm32f411ve board as slave in receive only, polling mode and sofware NSS, in the code generated I have only added a few lines of code, only which I consider neccesary

**the master sends 12 bytes

outside main function:

uint8_t rxbuffer[12];
 
void Timeout_Error_Handler()
{
	while(1){
		HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_13); //led orange
		HAL_Delay(500);
	}
 
}
 
 
void Error_Handler2()
{
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,GPIO_PIN_SET); //led red
}
 

inside main function:

// I have commented the while (1) because for my test the master sends data just one time 
  //while (1)
  {
    /* USER CODE END WHILE */
    MX_USB_HOST_Process();
 
    /* USER CODE BEGIN 3 */
 
 
   switch (HAL_SPI_Receive(&hspi5,rxbuffer,12,5000))
    {
     case HAL_OK:
       /* Turn LED6 on: Transfer in reception process is correct */
       HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15,GPIO_PIN_SET); //led blue
       break;
 
     case HAL_TIMEOUT:
       Timeout_Error_Handler();
       break;
 
     case HAL_ERROR:
       Error_Handler2();
       break;
 
     default:
       break;
     }

the issue is that in debug mode I can see after the HAL_SPI_Receive function the program goes to a hardfault handler function.

do you know what could be the problem?

thanks

9 REPLIES 9
KnarfB
Principal III

When you are in the hard fault handler, what is the call stack?

How is rxbuffer defined?

Hi, sorry but I dont know what is the call stack, how can I find it?

and this is the definition of rx buffer: uint8_t rxbuffer[12]; it is in the line 1 of the first code

Depends on your toolchain. In CubeIDE it could look like:

0693W000000UInfQAG.png

left. You can walk up the call stack and find out the cause. In the example this is the machine instruction

080001e7:   str     r2, [r3, #0]

generated from the C code line

*p = 42;

0693W000000UIqKQAW.jpgok, this is my debug window , what should I do now?

sorry but how do you kow that the cause is this?

    080001e7:   str     r2, [r3, #0]

and the c code line is this?

    *p = 42;

In the debug view click on main(). This will put you to the source code line.

The fault analyzer window says its an data access fault. So I'm looking fo the last load or store instruction before the current (fault handler) instruction.

Its also an imprecise fault, so it is caused by the write buffer (store).

I select main indebug view and shows me this line

0693W000000UJbkQAG.jpg

but that line is the end of this part of the code, there is nothing declared there

0693W000000UJcEQAW.jpg

I am using SW4STM32 and it seems that it does not have a fault analyzer window

Go single step by single step though your code and watch when its crashing. Can even go single step at instruction level if you dare to.