cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F Unexpected Hardfault cause by code not reached

tfde
Associate

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);
}

 

 

 

3 REPLIES 3
Andrew Neil
Super User

Are you sure it's not reached?

Have you tried setting a breakpoint in that code to confirm?

 

How to debug Hard faults:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/tac-p/708193/highlight/true#M51

 

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.

Instrument and root cause the Hard Fault

https://github.com/cturvey/RandomNinjaChef/blob/main/KeilHardFault.c

We lack significant context here. We don't see the fault, the variable definitions, the adc_read() function, or the initialization or handlers.

Probably something not initialized correctly prior to function entrance. Or perhaps stack overflow due to local/auto variables in one of the called routines.

Keil tend to have a very small stack allocation. Make it several KB in startup.s file, and perhaps mark it with a pattern so you make some high/low water mark determination.

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

Code that isn't executed can't cause a hard fault. The bug is elsewhere.

If you feel a post has answered your question, please click "Accept as Solution".