2020-08-20 04:27 PM
Hi,
I have a code in which I am controlling a PWM output in Center Aligned Mode using Timer 2 Channels 2 (inverted polarity) and 3, both having their duty cycles changed by the functions:
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,(duty_c-0.05)*400);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3,(duty_c)*400);
When I do not use these functions, all my HAL_Delay functions in main.c work properly and so does the LCD, with the 50us interrupt working properly as well.
As soon as I add those two lines using __HAL_TIM_SET_COMPARE to change the duty cycle(CCR registers), the code gets stuck in a HAL_Delay loop, and I don't get why that happens.
The code is stuck in the infinite here:
while ((HAL_GetTick() - tickstart) < wait)
__weak uint32_t HAL_GetTick(void)
{
return uwTick;
}
Why does it happen only when this line is added?
I would like to understand how to solve such issue, it's botching me for a long time so far.
2020-08-20 06:33 PM
What is your call stack when this occurs? Sounds like it is stuck in an interrupt handler that isn't be pre-empted by the SysTick interrupt.
2020-08-20 06:44 PM
Be careful not to use blocking functions in interrupt handlers and callback routines, and code they call.
Make sure that SysTick has highest priority in the system and can preempt other interrupts, otherwise the HAL code is likely to function in undesirable ways, unless you're very attentive to detail and potential interactions.
2020-08-20 06:52 PM
I thought of increasing SysTick priority but I realized that all functions in the interrupt table are assigned 0 priority (highest), so should I decrease all the others to 1 and leave SysTick as 0?
Additionally, I am not calling any function inside the interrupt other than the _HAL_TIM_SET_COMPARE, they LCD ones are all inside my main.c. Also, when I try to set the value straight to the register (htim2.Instance->CCR= xx;) the same bug takes place.
2020-08-20 06:56 PM
> so should I decrease all the others to 1 and leave SysTick as 0?
That's one way to solve it.
> Additionally, I am not calling any function inside the interrupt, they are all inside my main.c
Then it shouldn't be blocking. Did you disable interrupts? What is your call stack when it occurs?
2020-08-20 07:07 PM
I am sorry, I made a mistake stating I had no function in my interrupt handler, Actually I do which is the _HAL_TIM_SET_COMPARE. Even though when I try to change it writing directly to the register (htim2.Instance->CCR=xx ; ) the same bug happens. I did not disable interrupts, they are pre-set when I generated the code from CubeMX. I am trying to look the call stack but it's only written main, its location/value and type, I can't see anything else.
2020-08-20 07:13 PM
2020-08-20 07:23 PM
2020-08-20 07:46 PM
I have this 50us interrupt which comes from the interrupt in order to change the pwm duty cycle using HAL_TIM_SET_COMPARE, and inside my main I have this HAL_Delay(1) which I use to control the LCD. It works fine when I do not insert that HAL_TIM_SET_COMPARE function, but as soon as I add it, the code gets stuck exactly inside that HAL_Delay(1).
2020-08-20 11:48 PM
50us interrupt? You simply exhaust the processor.
JW