AnsweredAssumed Answered

STM32L053 - TIM21 - Input Compare

Question asked by jb f on Jan 9, 2018
Latest reply on Jan 10, 2018 by waclawek.jan

Hello,

 

I am trying to setup the timer TIM21 channel 2 (PA.3) as input compare in order to find out the frequency applied to that pin.

 

On the basis of the ST provided example (which relies on TIM2 and PB.3 and works perfectly fine) I changed the timer reference to TIM21 and edited the GPIO pin configuration to match PA.3.

 

Bellow is part of the main code:

/*##-1- Configure the TIM peripheral #######################################*/ 
/* Set TIMx instance */
TimHandle.Instance = TIM21;

/* Initialize TIMx peripheral as follow:
+ Period = 0xFFFF
+ Prescaler = 0
+ ClockDivision = 0
+ Counter direction = Up
*/

TimHandle.Init.Period = 0xFFFF;
TimHandle.Init.Prescaler = 0;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_IC_Init(&TimHandle) != HAL_OK) {
    /* Initialization Error */
    ErrorHandler();
}

/*##-2- Configure the Input Capture channel ################################*/
/* Configure the Input Capture of channel 2 */
sICConfig.ICPolarity = TIM_ICPOLARITY_RISING;
sICConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
sICConfig.ICPrescaler = TIM_ICPSC_DIV1;
sICConfig.ICFilter = 0;
if(HAL_TIM_IC_ConfigChannel(&TimHandle, &sICConfig, TIM_CHANNEL_2) != HAL_OK) {
    /* Configuration Error */
    ErrorHandler();
}

/*##-3- Start the Input Capture in interrupt mode ##########################*/
if(HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_2) != HAL_OK) {
    /* Starting Error */
    ErrorHandler();
}

 

Bellow is the HAL-called setup function:

void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
{
   GPIO_InitTypeDef GPIO_InitStruct;

    /*##-1- Enable peripherals and GPIO Clocks #################################*/
    /* TIM2 Peripheral clock enable */
    __HAL_RCC_TIM21_CLK_ENABLE();

    /* Enable GPIO channels Clock */
    __HAL_RCC_GPIOA_CLK_ENABLE();

    /* Configure (TIM21_Channel2) in Alternate function, push-pull and High speed */
    GPIO_InitStruct.Pin = GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF0_TIM21;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

   /*##-2- Configure the NVIC for TIMx #########################################*/
    /* Set the TIM2 global Interrupt */
    HAL_NVIC_SetPriority(TIM21_IRQn, 0, 1);

    /* Enable the TIM2 global Interrupt */
    HAL_NVIC_EnableIRQ(TIM21_IRQn);
}

 

And finally, bellow is the IRQ function:

void TIM21_IRQHandler(void)
{
    HAL_TIM_IRQHandler(&TimHandle);
}

 

I can get the code working as long as I keep using the TIM2 peripheral. Using TIM21 does not trigger the interrupt. Any clue on what is wrong or misconfigured?

 

Many thanks in advance,

 

Kind regards,

Jean-Baptiste

Outcomes