cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F Unexpected Hardfault cause by code not reached

tfde
Associate II

Hi ! I'm working on a STM32F429VET, with keil as an IDE. 

My problem is the next, this code which is NOT reached (because it need a command from the terminal to be). Make my entire program crash in contact of loop, causing an IACCVIOL if i let this part of the code commented, works fine if i uncomment it. Anybody have encounter similiar behavior ? I dont get it at all.

void adc_measure(uint16_t channel, uint8_t *result)
{
  measureInProgress = true;
  adc_init(channel);

  HAL_TIM_Base_Start_IT(&htim2);

//  while (measure_pending_count < NB_MEASURES)
//  {
//   
//      adc_read();
//      __disable_irq(); // Prevent race condition
//      measure_pending_count++;
//      __enable_irq();
//    
//  }
  

  HAL_ADC_Stop(&hadc1);
  float measure = adc_compute_value(channel); // convert the value toward needed unit (mA or V)
  //snprintf((char *)result, RESULT_PRECISION, "%f", measure);
}

 

 

 

16 REPLIES 16

@tfde wrote:

It seems like the program go out of memory bound


That's a hypothesis - so the next step is to test that hypothesis.

As suggested earlier, you might set a breakpoint shortly before the fault occurs, then step and see what happens...

Or, if the "out of bounds" access is consistent, perhaps set a breakpoint (data or code) at the affected address...

 


@tfde wrote:

i dont get how commenting a file change the issue of that. 


As noted earlier, it's possible that the extra code changes the memory layout; it's then possible that a previously benign bug becomes a problem ...

Again, this is just speculation - it needs to be tested.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

adc_reset_value() is only called once. So measureCount, used as your array index, is not set to zero so it keeps increasing beyond the length of the buffer. This is a buffer overflow!

Edit: I see you are calling adc_init() every time you do a measurement instead of once. This is not the way you should use the ADC. You need to initialize your peripheral only once. Also avoid using global variables if possible. But at least this clears measureCount. The problem is elsewhere.

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.

Keil welcome me with an error if try to put a bp close to where it crash

tfde_0-1753867413794.png

 

Hi! Maybe im overlooking something but, its call everytime i use adc_measure () so the value are reset everytime they need to be ? 

The entire code could use improvement:

  1. avoid global variables. Some variables are really not needed. Such as measureCount.
  2. don't define variables/const data in a header file. Use extern declaration.
  3. Don't put private definitions or declarations of constants in the header file. The header file is the interface of your c file so don't put things there that are only used internally in the c file to avoid spaghetti code.
  4. Use correct types. uint8_t **adcResults is used as an array of string pointers. So use char, not uint8_t.
  5. Avoid blocking calls.
Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.

@tfde wrote:

I do really think its some kind of stack overflow but 1. i don't know how to prove that and 2. i dont know how to debug that. 


Take a look at Keil's Application Note AN316Determining the stack usage of applications...

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

Random failing, I'd double check the PLL settings, and FLASH WAIT STATES, and electrically the voltage observed on VCAP pins, and the amount of capacitance actually placed, ie 2u2 per pin or 4u7 in total

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..