Skip to main content
Karan 123
Senior
June 25, 2020
Question

STM32G0 Variable Corrupt/Memory Fault/ HardFault inside the interrupts

  • June 25, 2020
  • 8 replies
  • 2714 views

Hi,

I am working on STM32G071 MCU with STM32Cube IDE1.3.0 .

STM32G071 have to operate 7-Segment on Scanning Mode .

I have to decrement the counter value (RunningData[][])inside the comparator interrupt (every 20ms) until reaches to zero.

The volatile uint32_t RunningData[11][7] corrupted

I don't know what may be the reason . 

1) Some times program hangs as Hard Fault ,

2) Some Time unexpected behavior counter like garbage data on 7-Segment.

Like Corrupt/Memory Fault/ HardFault inside the interrupts.

Below is the snip short . This below I have not faced this kind of issue with 8-bit/16-bit MCU .

// Send to 7-Segment // Call In Timer ISR
unsigned char _7Segment_Pin_8 ;
unsigned char _7Segment_Pin_7 ;
unsigned char _7Segment_Pin_6 ;
unsigned char _7Segment_Pin_5 ;
unsigned char _7Segment_Pin_4 ;
unsigned char _7Segment_Pin_3 ;
unsigned char _7Segment_Pin_2 ;
unsigned char _7Segment_Pin_1 ;
 
unsigned char Seg7var[20] ;
volatile uint32_t RunningData[11][7] ; 
 
// Low Priority 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
 
	if (htim->Instance==TIM2) //check if the interrupt comes from TIM2
	{
 
		HAL_GPIO_TogglePin (SOLENIOD_2_ON_OPTOCOUPLER_GPIO_Port ,SOLENIOD_2_ON_OPTOCOUPLER_Pin) ;
		 Reset_All7Segments();
			if(Seg7counter == 0)
			{
				 CombineIndividualPinFor7Segments(Seg7var[Seg7counter]);
				 HAL_GPIO_WritePin(SELECT_SEG7_1_GPIO_Port, SELECT_SEG7_1_Pin , GPIO_PIN_SET) ;
			}
			else if(Seg7counter == 1)
			{
 			 CombineIndividualPinFor7Segments(Seg7var[Seg7counter]);
 			 HAL_GPIO_WritePin(SELECT_SEG7_2_GPIO_Port, SELECT_SEG7_2_Pin , GPIO_PIN_SET) ;
			}
			|
			|
			|
	}
}
void CombineIndividualPinFor7Segments(unsigned char temp)
{
	 _7Segment_Pin_1 = ((temp >> 7 ) & 1 );
	_7Segment_Pin_2 = ((temp >> 6 ) & 1 );
 
 _7Segment_Pin_3 = ((temp >> 5 ) & 1 );
 _7Segment_Pin_4 = ((temp >> 4 ) & 1 );
 
 _7Segment_Pin_5 = ((temp >> 3 ) & 1 );
 _7Segment_Pin_6 = ((temp >> 2 ) & 1 );
 
 _7Segment_Pin_7 = ((temp >> 1 ) & 1 );
 _7Segment_Pin_8 = ((temp >> 0 ) & 1 );
 
		 if( _7Segment_Pin_8 == 0)
				HAL_GPIO_WritePin(SEG7_PIN_A_GPIO_Port, SEG7_PIN_A_Pin , GPIO_PIN_SET) ;
		 else
				HAL_GPIO_WritePin(SEG7_PIN_A_GPIO_Port, SEG7_PIN_A_Pin , GPIO_PIN_RESET) ;
 
		 if( _7Segment_Pin_7 == 0)
				HAL_GPIO_WritePin(SEG7_PIN_B_GPIO_Port, SEG7_PIN_B_Pin , GPIO_PIN_SET) ;
		 else
				HAL_GPIO_WritePin(SEG7_PIN_B_GPIO_Port, SEG7_PIN_B_Pin , GPIO_PIN_RESET) ;
 
 
		 	if( _7Segment_Pin_6 == 0)
				HAL_GPIO_WritePin(SEG7_PIN_C_GPIO_Port, SEG7_PIN_C_Pin , GPIO_PIN_SET) ;
		 else
				HAL_GPIO_WritePin(SEG7_PIN_C_GPIO_Port, SEG7_PIN_C_Pin , GPIO_PIN_RESET) ;
 
		 	if( _7Segment_Pin_5 == 0)
				HAL_GPIO_WritePin(SEG7_PIN_D_GPIO_Port, SEG7_PIN_D_Pin , GPIO_PIN_SET) ;
		 else
				HAL_GPIO_WritePin(SEG7_PIN_D_GPIO_Port, SEG7_PIN_D_Pin , GPIO_PIN_RESET) ;
 
 
		 	if( _7Segment_Pin_4 == 0)
				HAL_GPIO_WritePin(SEG7_PIN_E_GPIO_Port, SEG7_PIN_E_Pin , GPIO_PIN_SET) ;
		 else
				HAL_GPIO_WritePin(SEG7_PIN_E_GPIO_Port, SEG7_PIN_E_Pin , GPIO_PIN_RESET) ;
 
		 	if( _7Segment_Pin_3 == 0)
				HAL_GPIO_WritePin(SEG7_PIN_F_GPIO_Port, SEG7_PIN_F_Pin , GPIO_PIN_SET) ;
		 else
				HAL_GPIO_WritePin(SEG7_PIN_F_GPIO_Port, SEG7_PIN_F_Pin , GPIO_PIN_RESET) ;
 
		 	if( _7Segment_Pin_2 == 0)
				HAL_GPIO_WritePin(SEG7_PIN_G_GPIO_Port, SEG7_PIN_G_Pin , GPIO_PIN_SET) ;
		 else
				HAL_GPIO_WritePin(SEG7_PIN_G_GPIO_Port, SEG7_PIN_G_Pin , GPIO_PIN_RESET) ;
 
		 	if( _7Segment_Pin_1 == 0)
				HAL_GPIO_WritePin(SEG7_PIN_DP_GPIO_Port, SEG7_PIN_DP_Pin , GPIO_PIN_SET) ;
		 else
				HAL_GPIO_WritePin(SEG7_PIN_DP_GPIO_Port, SEG7_PIN_DP_Pin , GPIO_PIN_RESET) ;
 
}
void SplitIntoTwoDigitsAndDisplayOn7Segments(unsigned int Digit1, unsigned int Digit2, unsigned int temp)
{
	Seg7var[Digit1] = DisplaySegmentPattern(temp/10) ;
	Seg7var[Digit2] = DisplaySegmentPattern(temp%10) ;
}
 
// High Priority 
void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
{
	 RunningData[ProgramNumber][SQUEEZE_TIME_VALUE ]--;
 SplitIntoTwoDigitsAndDisplayOn7Segments(SQUEEZE_SEG_1, SQUEEZE_SEG_2 ,RunningData[ProgramNumber][SQUEEZE_TIME_VALUE ] );
		if( RunningData[ProgramNumber][SQUEEZE_TIME_VALUE] <= 0 )
		{
				State++ ;
		}
		break ;
		|
		|
		|
		|
		|
		RunningData[ProgramNumber][WELD_TIME_VALUE ]-- ;
		SplitIntoTwoDigitsAndDisplayOn7Segments(WELD_ON_SEG_1 , WELD_ON_SEG_2 , RunningData[ProgramNumber][WELD_TIME_VALUE ] );
		if( RunningData[ProgramNumber][WELD_TIME_VALUE ] <= 0)
		{
			 	State++ ;
		}		
		break ;
}

Please help to sort out this issue .

--

Thanks

This topic has been closed for replies.

8 replies

berendi
Principal
June 25, 2020

RunningData is volatile, but Seg7var is not. Why?

Check ProgramNumber bounds in the handler. It should be volatile as well.

Karan 123
Karan 123Author
Senior
June 25, 2020

Hi,

Thanks..

ProgramNumber is volatile and Seg7var changed to volatile but problem still persists...

Any other hint ?

volatile int ProgramNumber = 0,	IgnoreOnce = 1 ;
 
void CombineIndividualPinFor7Segments(unsigned char temp) ;
 
volatile unsigned char Seg7var[20] ;
 
 unsigned char DisplaySegmentPattern(unsigned char no)
{
		unsigned char Pattern;
		unsigned char SEGMENT[ ] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF};
		Pattern = SEGMENT[no] ; // Pattern to return
		return (Pattern);
}

Karan 123
Karan 123Author
Senior
June 25, 2020

Deleted

TDK
Super User
June 25, 2020

If you're getting a hard fault, ignore everything else for the moment and find out the source of that hard fault. Debug the code and figure out where and why this is happening. One possibility is that the indices on RunningData are out of range.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Karan 123
Karan 123Author
Senior
June 25, 2020

But data change is from 0 to 99.

Indices means ​data in array.

When RunningData is less than or equal to 0 .

I have reloaded that again with any value in between from 0 to 99 in ISR.

Will volatile or ​uint32_t can cause issue?

berendi
Principal
June 25, 2020

uint32_t can't be less than 0. If you decrease an uint32_t variable which holds 0, it will wrap around to 4294967295 (2^32 -1).

Karan 123
Karan 123Author
Senior
June 25, 2020

But this faults doesn't occur always.

What should I change to int32_t ? ​

Will solve the problem.?​

berendi
Principal
June 25, 2020

Changing variable types randomly will not solve any problems.

Verify your program line by line, ensure that all variables are within their expected bounds. Handle possible overflows.

You will not get a pup-up window with exception trace in C if you have an out-of-bounds array index, but it will do either of the following

  • Stop with a hardfault
  • Overwrite unused parts of the memory, and continue harmlessly
  • Overwrite otherwise used parts of the memory, and crash in more interesting ways
  • Overwrite a memory mapped register, possibly setting some pin to output which is not supposed to be, and permanently damage the MCU or the board.
NLE F.1
Associate
June 26, 2020

A good approach is to check RunningData[ProgramNumber][SQUEEZE_TIME_VALUE ] before decrease the value => so you are sure to be inside your functional values.

Tesla DeLorean
Guru
June 26, 2020

Your coding style contributes significantly to your headaches.

Use subroutines for replicated code

Use tables for replicated sequencing and association of groups of definitions.

Use those so you're not copy-n-pasting blocks of code, and then changing 8 values that repeat/interrelate hoping you catch all of them.

Scope the range for temp in SplitIntoTwoDigitsAndDisplayOn7Segments() so you can understand if it is being asked to display something below 0, or above 99, resulting in unspecified behaviour. Use an assert() or breakpoint to catch failure of sanity checks real time.

Get a proper HardFault Handler so you can immediately establish which instructions are faulting, if it turns out they are using bad pointers/parameters, again sanity check things and assert/breakpoint so you can track down where your code initially goes off the rails.

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