cancel
Showing results for 
Search instead for 
Did you mean: 

Using structure causes Hardfault exception

yifan115
Associate II
Posted on July 23, 2014 at 23:49

Hi everybody,

I'm trying migrate a program from 28335 to stm32f407. I was using a structure to store some data, and passing the pointer to this structure to a function.

Now I find that if I define the structure variable as global before main, it will always cause Hardfault exception. If I define it in main, a Hardfault exception will happen if I call the function that will use the structure variable in main.

In both situation, the Hardfault exception happens once the main is entered, that is, after the startup file's jump instruction (BX) and the first { after main(void), before any statement in main. So am I using the structure wrong?

Thank you!
4 REPLIES 4
Posted on July 23, 2014 at 23:55

So am I using the structure wrong?

Apparently the processor thinks so...

Present a concise example that demonstrates the problem.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
yifan115
Associate II
Posted on July 23, 2014 at 23:59

typedef struct

{

float Kp;

float Ki;

float Error_new;

float Error_old;

float PIout_new;

float PIout_old;

float Maxvalue;

float Minvalue;

float Ref;

float Det;

}PIdata;

PIdata IL_ctrl;

If I put these before main, a hardfault exception will happen once it enters main.

Posted on July 24, 2014 at 01:13

So my wild guess would be that your unspecified tool chain is generating FPU instructions for the F4, and you don't have them enabled in your code.

You need to look at your defines, and the enabling code in SystemInit(), system_stm32f4xx.c

/**
* @brief Setup the microcontroller system
* Initialize the Embedded Flash Interface, the PLL and update the
* SystemFrequency variable.
* @param None
* @retval None
*/
void SystemInit(void)
{
/* FPU settings ------------------------------------------------------------*/
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
SCB->CPACR |= ((3UL << 
10
*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
#endif
/* Reset the RCC clock configuration to the default reset state ------------*/
/* Set HSION bit */
RCC->CR |= (uint32_t)0x00000001;
...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
yifan115
Associate II
Posted on July 24, 2014 at 17:47

Thank you! That works.

I didn't expect that they didn't enable FPU in the example code.

I had a look at the disassembly, VPUSH is generated at the beginning of main when I use structure.