2019-10-18 08:56 AM
Hi,
I'm practicing with the timers of my STM32F0-Disco.
Quite simply, I'm trying to use TIM6 or TIM14 as a simple time base. In order to verify its correct functioning I have put a GPIO call to toggle a LED whenever the interrupt handling function is called.
I used STMCubeMX to generate the code (very simple, since it uses only TIM14 plus two GPIOs, the on-board green and blue LEDs).
The thing that has been driving me crazy for two days is that the TIM14_IRQHandler () interrupt handling function is never called and consequently the LED does not flash.
I have checked and re-checked the generated code several times, also adding the timer activation call, LL_TIM_EnableCounter (TIM14).
The code follows:
static void MX_TIM14_Init(void)
{
/* USER CODE BEGIN TIM14_Init 0 */
/* USER CODE END TIM14_Init 0 */
LL_TIM_InitTypeDef TIM_InitStruct = {0};
/* Peripheral clock enable */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM14);
/* TIM14 interrupt Init */
NVIC_SetPriority(TIM14_IRQn, 0);
NVIC_EnableIRQ(TIM14_IRQn);
/* USER CODE BEGIN TIM14_Init 1 */
/* USER CODE END TIM14_Init 1 */
TIM_InitStruct.Prescaler = 47999;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload = 499;
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
LL_TIM_Init(TIM14, &TIM_InitStruct);
LL_TIM_EnableARRPreload(TIM14);
/* USER CODE BEGIN TIM14_Init 2 */
/* USER CODE END TIM14_Init 2 */
/* Enable counter */
LL_TIM_EnableCounter(TIM14);
}
The previous function is called in my main() bode, before the usual while(1) loop.
In my stm32f0xx_it.c I've written the interrupt handler as follows:
void TIM14_IRQHandler(void)
{
LL_TIM_ClearFlag_UPDATE(TIM14);
LL_GPIO_TogglePin(LD4_GPIO_Port,LD4_Pin);
}
With the programmed values for prescaler and autoreload register I would expect an update event every 0.5 seconds, having a system core clock of 48 MHz.
Bute the IRQ handler never gets called (the LED don't flash).
Did I forget something trivial?
Max
2019-10-18 09:05 AM
There’s an update interrupt enable bit in the timer somewhere. Is that set? You can also check for the update interrupt flag being set.
Does tim14 have its own interrupt handler? Make sure the name matches whats in the interrupt vector list.
2019-10-18 09:21 AM
Where do you set the update interrupt enable (UIE) bit in TIM14_DIER?
I don't Cube.
JW
2019-10-18 09:49 AM
Oh yeah... thanks to you all!
I've found two functions, seeking in the HAL-LL documentation:
LL_TIM_EnableUpdateEvent(TIM14);
LL_TIM_EnableIT_UPDATE(TIM14);
The second finally enabled the interrupt! The code now works!
I dont' understand the purpose of the first funcion, anyway. Seems to have no effect for the interrupt operation, whether I invoke it or not.
In my opinion the CubeMX code generator would have had to insert that function call! I dont' understand why some fundamental activation steps are missing!
Thanks again,
Max
2019-10-18 09:54 AM
> I dont' understand the purpose of the first funcion, anyway. Seems to have no effect for the interrupt operation, whether I invoke it or not.
Cube is open source, so you can simply look up what does that function do and then look that up in the RM.
> In my opinion the CubeMX code generator would have had to insert that function call!
Maybe it does if you click on the right places. You may take it as a lesson.
JW
2019-10-18 11:22 AM
Yeah, you gotta check the actual peripheral registers to make sure that the MXcube code enabled the interrupts. You can see those bits when you debug.
"> I dont' understand the purpose of the first funcion, "
That's why I don't use MXcube except for maybe my original deploying of initialization... THEN I go see what MXcube missed and finish it using normal C programming.