Code misbehaves when debugger is not connected
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
