cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeIDE causes hardfault only debugging

CorgiCorgi
Visitor

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?

9 REPLIES 9
SofLit
ST Employee

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 on "Accept as Solution" on the reply which solved your issue or answered your question.

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);
zeboss49
Associate III

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.

 

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?

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 on "Accept as Solution" on the reply which solved your issue or answered your question.

I got it from sensors that connected through I2C.

I'm using HAL_I2C_Mem_Read to get raw data from sensors.

 

Here is functions that the struct members takes.

int16_t *ICM42670::getRawAccel(){
	static int16_t output[3] = {0};
	uint8_t regAddr = ICM42670_ACCEL_DATA_X1, recv[6] = {0};
	uint16_t error = 0;
	while(HAL_I2C_IsDeviceReady(&config.i2c, config.addr, 10, config.timeout) != HAL_OK){};
	error = HAL_I2C_Mem_Read(&config.i2c, config.addr, regAddr, 1, recv, 6, config.timeout);
	if(error != HAL_OK){
		return 0;
	}
	while(HAL_I2C_GetState(&config.i2c) != HAL_I2C_STATE_READY){};
	for(uint8_t i=0;i<3;i++){
		output[i] = (recv[2*i] << 8) | recv[2*i + 1];
	}
	return output;
}

 

and this is setting structure for sensor

struct ICM42670_CONFIG{
		I2C_HandleTypeDef i2c;
        uint8_t addr;
        uint32_t timeout = 10000;
        uint8_t accel_range;
        uint8_t accel_rate;
        uint8_t accel_lpf;
        uint8_t gyro_range;
        uint8_t gyro_rate;
        uint8_t gyro_lpf;
};

 And member is access to this as follow :

flight_data.imu0_accel = imu0.getRawAccel();

As I said, need to debug it:

https://community.st.com/t5/stm32-mcus/how-to-debug-a-hardfault-on-an-arm-cortex-m-stm32/ta-p/672235

As we don't have your environment, it's impossible to debug it.. So need to do it yourself and follow what was described in the article.

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

@CorgiCorgi wrote:

How can I initialize the struct?


That is just a C language question:

https://en.cppreference.com/w/c/language/struct_initialization

https://publications.gbdirect.co.uk/c_book/chapter6/initialization.html

 


@CorgiCorgi wrote:

It is initialized like this 

 

struct FLIGHT_DATA flight_data;

 

is it not a proper initialization?


That is just a declaration/definition - it has no initialisation at all.

Actually, there's no problems when I use this style on nRF52 series.

The weirdest thing is that it works fine when I execute it in running mode.

CorgiCorgi_0-1732032837595.png

The problem is only occured in dubgging mode.