Skip to main content
CorgiCorgi
Associate
November 19, 2024
Question

STM32CubeIDE causes hardfault only debugging

CorgiCorgi_0-1732027804311.png

If I run debug, I always got hardfault.

Here is source codes that cause hardfault.

 

flight_data.imu0_accel = imu0.getRawAccel();
 		flight_data.imu0_gyro = imu0.getRawGyro();
 		flight_data.imu1_accel = imu1.getRawAccel();
 		flight_data.imu1_gyro = imu1.getRawGyro();
 		for(uint8_t i=0;i<3;i++){
 			flight_data.accel[i] = (int16_t)((flight_data.imu0_accel[i] + flight_data.imu1_accel[i]) / 2.0f);
 			flight_data.gyro[i] = (int16_t)((flight_data.imu0_gyro[i] + flight_data.imu1_gyro[i]) / 2.0f);
 		}
 		Logging("%5d,%5d,%5d,%5d,%5d,%5d,%5d,%5d,%5d\r\n",
 				flight_data.imu0_accel[0], flight_data.imu1_accel[0], flight_data.accel[0],
				flight_data.imu0_accel[1], flight_data.imu1_accel[1], flight_data.accel[1],
				flight_data.imu0_accel[2], flight_data.imu1_accel[2], flight_data.accel[2]);

 

Everything is alright except for hardfault. I got hardfault when I read struct data that I defined like this

 

struct FLIGHT_DATA{
		int16_t *imu0_accel;
		int16_t *imu0_gyro;
		int16_t *imu1_accel;
		int16_t *imu1_gyro;
		int16_t accel[3];
		int16_t gyro[3];
		float attitude[3] = {0};
		float position[2] = {0};
		float velocity = 0;
		float altitude = 0;
};

 

There are no problems get data from I2C sensors.(I'm using HAL_I2C_Mem_Read)

I'm using J-Link Base probe for debugging and download.

But when I press "Run" button on the top panel, no hardfault.

What's the problem?

This topic has been closed for replies.

9 replies

mƎALLEm
ST Technical Moderator
November 19, 2024

Hello @CorgiCorgi and welcome to the community.

Not obvious to tell what is the problem is based on the information you provided.

Need also to provide the MCU part number and debug yourself the behavior. This article may help you: https://community.st.com/t5/stm32-mcus/how-to-debug-a-hardfault-on-an-arm-cortex-m-stm32/ta-p/672235

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
CorgiCorgi
Associate
November 19, 2024

Sorry for unclear explain.

My problem is "Reading struct data cause hardfault only in debug running"

When I got data and save it from sensor through I2C interface, there's no problems.

My MCU is STM32H503RBT6.

 

This code cause hardfault

flight_data.accel[i] = (int16_t)((flight_data.imu0_accel[i] + flight_data.imu1_accel[i]) / 2.0f);
 			flight_data.gyro[i] = (int16_t)((flight_data.imu0_gyro[i] + flight_data.imu1_gyro[i]) / 2.0f);
Senior
November 19, 2024

Hello,

I faced a rather similar issue and discovered that some elements was not properly initialized.

The debugger initialzes things it needs to do its job, and some things may be initialized in a different way when debugging.

 

CorgiCorgi
Associate
November 19, 2024

Thank you for reply.

 

How can I initialize the struct?

 

It is initialized like this 

struct FLIGHT_DATA flight_data;

 

is it not a proper initialization?

mƎALLEm
ST Technical Moderator
November 19, 2024

The only thing to care about in the struct is the access to the pointers. You need to be sure that these pointers are not NULL when you read from them:

struct FLIGHT_DATA{
		int16_t *imu0_accel;
		int16_t *imu0_gyro;
		int16_t *imu1_accel;
		int16_t *imu1_gyro;

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.