STM32CubeIDE causes hardfault only debugging
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-11-19 6:54 AM - last edited on ‎2024-11-19 6:59 AM by mƎALLEm
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?
- Labels:
-
STM32CubeIDE
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-11-19 7:02 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-11-19 7:12 AM - edited ‎2024-11-19 7:13 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-11-19 7:19 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-11-19 7:30 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-11-19 7:34 AM - edited ‎2024-11-19 7:34 AM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-11-19 7:43 AM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-11-19 7:51 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-11-19 7:52 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-11-19 8:14 AM
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.
The problem is only occured in dubgging mode.
