cancel
Showing results for 
Search instead for 
Did you mean: 

Code misbehaves when debugger is not connected

Maunik Patel
Associate II

Hello All,

I am using STM32F030C8, with ST-LINK V2 debugger & Keil V5 IDE.

While working on my application, I found two cases, where some logic / functionalities works properly only if we are using debug session. Else, those functionalities doesn't works the way they are supposed to work.

(I am using debug session without any break points).

Please go through below cases, help me out.

Case 1: Generating PWM

Description:

I am controlling a buzzer using PWM, to generate some tones. code is given below.

The case is, tone 1 works good every time. But some time, other tones doesn't sounds in the pattern they are supposed to be.

If I connect debugger & run the application, all tones sounds exactly in the way, they are supposed to be.

Timer Configurations:

Timer 1   : 500ms, Priority: 1

Timer 3   : 10ms ,Priority: 1

Timer 14 : 500us, Priority: 0

void Tone_start(TIM_HandleTypeDef *htim, int channel, float frequency, uint8_t volume)
{
    htim->Init.Prescaler = (SystemCoreClock / frequency / 100) - 1;
    htim->Init.Period = 100-1;
	htim->Instance->CCR1=volume;
    HAL_TIM_PWM_Init(htim);
    HAL_TIM_PWM_Start(htim, channel);
}
 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	static uint8_t count = 0, stable_volt_cnt = 0;
	/* Prevent unused argument(s) compilation warning */
	UNUSED(htim);
	
	// to play tone on buzzer
	if(htim->Instance == TIM3)			// timer 3 is configured to generate interrupt at every 10ms (Priority: )
	{
		count50		= count50+10;
		count1s 	= count1s+10;
		count100 	= count100+10;
		count1m 	= count1m+10;
	
		if(count50==50)
		{
			count50_flag = true;
		}
		else if(count100==100)
		{
			count100_flag = true;
		}
		else if(count1s==1000)
		{
			count1s_flag = true;
			tone_start_flag = true;
		}
		else if(count1m >= (BUZZ_TIME * 1000))		// it stops buzzer ringing after predefined time
		{
			HAL_TIM_Base_Stop_IT(&htim3);
			HAL_TIM_PWM_Stop(&htim14, TIM_CHANNEL_1);
			count50=0,count1s=0,count100=0,count1m=0;
			alarm_ringing = false;
		}
		
		switch(buzzer.tone)		// buzzer tone is user selectable
		{
			case 1:
				if(tone_start_flag==1)
				{
					Tone_start(&htim14, TIM_CHANNEL_1, 1975.53, buzzer.volume);
					tone_start_flag=0;
					count50=0;
				}
				else if(count50_flag==1)
				{
					HAL_TIM_PWM_Stop(&htim14, TIM_CHANNEL_1);
					count50_flag=0;
					count1s=0;
				}
				break;
 
			case 2:
				if(tone_start_flag==1)
				{
					Tone_start(&htim14, TIM_CHANNEL_1, 1975.53, buzzer.volume);
					tone_start_flag=0;
					count50=0;
					count100=0;
					i++;
				}
				if(i==3&&count50>=50)
				{	
					tone_start_flag=0;
					count50_flag=1;
					i=0;
					count100=200;
					count1s=0;
				}
				else if(count50_flag==1&&i<3)
				{
					HAL_TIM_PWM_Stop(&htim14, TIM_CHANNEL_1);
					count50_flag=0;
				}
				else if(count100_flag==1&&i<3)
				{
					tone_start_flag=1;
					count100_flag=0;
					count100=0;
				}
				break;
			
			case 3:
				if(tone_start_flag==1)
				{
					Tone_start(&htim14, TIM_CHANNEL_1, 1975.53, buzzer.volume);
					tone_start_flag=0;
					count50=0;
					i++;
				}
				if(i==5&&count50>=50)
				{	
					tone_start_flag=0;
					count50_flag=1;
					i=0;
					count100=200;
					count1s=0;
				}
				else if(count50_flag==1&&i<5)
				{
					HAL_TIM_PWM_Stop(&htim14, TIM_CHANNEL_1);
					count50_flag=0;
					if(i!=0)
					{
					count100=0;
					}
				}
				else if(count100_flag==1&&i<5)
				{
					tone_start_flag=1;
					count100_flag=0;
					count100=0;
				}
				break;
			
			case 4:
				if(tone_start_flag==1)
				{
					Tone_start(&htim14, TIM_CHANNEL_1, 1975.53, buzzer.volume);
					tone_start_flag=0;
					count50=0;
					count100=0;
					i++;
				}
				if(i==2&&count50>=50)
				{	
					tone_start_flag=0;
					count50_flag=1;
					i=0;
					count100=200;
					count1s=0;
				}
				else if(count50_flag==1&&i<2)
				{
					HAL_TIM_PWM_Stop(&htim14, TIM_CHANNEL_1);
					count50_flag=0;
				}
				else if(count100_flag==1&&i<2)
				{
					tone_start_flag=1;
					count100_flag=0;
					count100=0;
				}
				break;
				
			default:
				break;
		}
	}
 
	if(htim->Instance == TIM1)					// timer 1 is configured to generate interrupt at every 500ms
	{
		// some other application dependent tasks
	}
}

Case 2: UART communication

Description:

I am using UART in interrupt mode. If I use my application in debug mode, I can receive data over UART. But if I remove debugger & let the application run itself, it doesn't receive data or it may have stuck to some point.

UART Configurations:

UART2, 115200, 8, N, 1, Priority 0

.

Awaiting some solution.

Thank You,

Maunik Patel

3 REPLIES 3

>> But if I remove debugger & let the application run itself, it doesn't receive data or it may have stuck to some point.

Identify the points and loops where it might get stuck and instrument those with output to USART or GPIO.

Dump UART register map on button press. Have HardFault_Handler and Error_Handler output actionable data.

Check if the UART is reporting error status, such as noise, framing, etc which would preclude reception.

Check that other interrupts aren't blocking, or stuck in a loop (repetitive tail-chaining).

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

Check if it is not a grounding issue.

Or the global / static variables are not properly initialized in the startup function and you get random data.

Hi Clive Two.Zero,

Your answers are always outstanding & I am very thankful to you for it.

I need to update many of the functions in my code such that they can report results (success / failure).

I haven't gave a try to your suggestions till now, due to above mentioned reason & some time limitations.

I'll get back to you, once I have some updates.

Thank you.