cancel
Showing results for 
Search instead for 
Did you mean: 

[Need Help] Hard Fault Analysis On LR after ADC check HAL_OK

dios_kuri
Associate III
Hi i am a student, building a program using STM32F4 Discoery board for my research's hardware. So my program which i write for STM32F4 Discovery board has been stuck in Hard Fault after certain ADC Poll code. An overview of my code is :
  • Use different ADC Channels from ADC1 only (just 1 ADC, multi channel) to read 3 sensors.
  • Each time i about to read any ADC channel i configure the channels (e.g 2 or 5) >> start hadc1 >> poll conversion >> get the value >> stop hadc1 and i repeat that for other channels.
  • After some calculation programs i fetch it to USART
 
After reading a blog about HardFault analyzer, i managed to tweak my code and get the info about on which line i got stuck, including the LR and PC (showed by number 1 in the pic, top left). Turns out it is faulting when it is checking whether HAL_OK is TRUE in the main.c (number 3 in the pic, bottom).
The LR (call return address) has the adress 0x08000955 whis is not listed in the dissasembly (number 2 in the pic, top right)
HARDFAULT KONTOL.JPG

 

I think the bad LR address might be the problem, i.e. reffering to the non-exist address in disassembly, note that in disassembly window (number 2) there is no address 0x8000955 after 0x08000954, but it goes straight to 0x8000956 instead.
But i am not really sure about it. Also, if thats the case then i would need group member's suggestion on how to deal with the fault. So i dont really know how what to do in this situation. Could anyone please help me?
 
 
some notes:
The USART works fine, i did try the code by commenting out the ADC related code (using //) and using incremental values as dummy data.
 
 
I had some trouble with HAL getting locked which send me to the Hal_Locked instance
But, I changed the Hal_Locked function so it can unlock itself in stm32f4xx_hal_def.h.
heres the Hal_locked function 
HARDFAULT KONTOL1.JPGThanks in advance!
_____________________________________________________________
 
 
14 REPLIES 14
Sarra.S
ST Employee

Hello @dios_kuri

Could you please share your project? 

Thank you! 

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.

Pavel A.
Evangelist III

>The LR (call return address) has the adress 0x08000955 whis is not listed in the dissasembly

The address is actually 0x08000954. (Clear the lowest bit. this is because of thumb instruction set  TL;DR)

 

 

Thanks for the insight, Pavel. But I am sorry i don't understand how to clear the lowest bit, and i dont know where to put it since the code is an if code to check if hal_adc_configchannel is OK. May i get an example?
Any kind of suggestion helps. 

Hi Sarra. Heres the rar of my project. Sorry but my main.c is a mess because i try to tweak it to only run the adc and PWM. Looking forward to your next reply regarding my proj.

Many thanks in advance

Inconveniently you truncate the register display in the screen shot.

Problem is likely with one of the parameters / structures passed to HAL_ADC_ConfigChannel()

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

More registers, denser display

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

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

Hi Tesla.
Below the complete view of the registers, printf debug, and disassembly window. Hope this can give more insight. The code is slightly different then the one i posted, but the problem is still the same (i.e. LR address doesnt exist and on the same line of code which is for checking if hal config adc is OK.

Many thanks in advance.

-Dios

==================================================

Line of code

code.JPG

==================================================

Registers:

reg.JPG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

==================================================

full printf debug window:

printf.JPG

==================================================

Disassembly window:

disassm.JPG

==================================================

 

 

Bob S
Principal

> LR address doesnt exist

As @Pavel A. said, [in your head] mask the LR LSB (set it to 0) to get 0x8001ec2 AS THE RETURN ADDRESS.

But LR is not what you should be paying attention to.  Look at the PC value 0x8000203a, THAT is where the problem was.  As @Tesla DeLorean said, it is probably an issue with the configuration structure you pass (0 in some field that expects a non-zero value, perhaps).

You're interested in the code that faults, not the return address for the last function call.

The fault is likely somewhere in HAL_ADC_ConfigChannel() when you use the parameters / pointers passed in. Probably &hadc1

Suggest if you have printf() working, that a) you print out the pointer "%p", b) dump out the structure, and c) the pointer for hadc->Instance

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