2020-06-25 02:51 AM
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
2020-06-26 06:41 AM
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.