cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L0 Timer 2 CH1 not triggering interrupt

Yuti Amonkar
Associate II
Posted on May 11, 2018 at 10:33

Hi

I am using B-L072Z-LRWAN STM32 Eval board for configuring sensor.The sensor gives PWM as output.To measure the width I am using GPIOA 5 (PA5) pin as Timer input.I am using the following code for Timer configuration:

&sharpdefine TIMx                           TIM2

&sharpdefine TIMx_CLK_ENABLE()              __HAL_RCC_TIM2_CLK_ENABLE()

/* Definition for TIMx Pins */

&sharpdefine TIMx_CHANNEL_GPIO_PORT()       __HAL_RCC_GPIOA_CLK_ENABLE()

&sharpdefine GPIO_PORT                      GPIOA

&sharpdefine GPIO_PIN_CHANNEL2              GPIO_PIN_5

&sharpdefine GPIO_AF_TIMx                   GPIO_AF5_TIM2

/* Definition for TIMx's NVIC */

&sharpdefine TIMx_IRQn                      TIM2_IRQn

&sharpdefine TIMx_IRQHandler                TIM2_IRQHandler

/* Timer handler declaration */

TIM_HandleTypeDef TimHandle;

/* Timer Input Capture Configuration Structure declaration */

TIM_IC_InitTypeDef sConfig;

/* Slave configuration structure */

TIM_SlaveConfigTypeDef sSlaveConfig;

/* Captured Value */

uint32_t uwIC2Value = 0;

uint32_t uwIC2Value_f = 0;

/* Duty Cycle Value */

uint32_t uwDutyCycle = 0;

/* Frequency Value */

uint32_t uwFrequency = 0;

void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)

{

PRINTF('IOINIT\n');

GPIO_InitTypeDef GPIO_InitStruct;

/*♯♯-1- Enable peripherals and GPIO Clocks ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

/* TIMx Peripheral clock enable */

TIMx_CLK_ENABLE();

/* Enable GPIO channels Clock */

TIMx_CHANNEL_GPIO_PORT();

/* Configure (TIMx_Channel) in Alternate function, push-pull and 100MHz speed */

GPIO_InitStruct.Pin = GPIO_PIN_CHANNEL2@♯

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF_TIMx;

HAL_GPIO_Init(GPIO_PORT, &GPIO_InitStruct);

/*♯♯-2- Configure the NVIC for TIMx ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

/* Set the TIMx priority */

//PRINTF('%d\n\r', TIMx_IRQn);

HAL_NVIC_SetPriority(TIMx_IRQn, 0, 1);

/* Enable the TIMx global Interrupt */

HAL_NVIC_EnableIRQ(TIMx_IRQn);

}

void Pwm_sensor_init(void)

{

/*♯♯-1- Configure the TIM peripheral ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

/* Set TIMx instance */

TimHandle.Instance = TIMx;

/* 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 */

PRINTF('ERROR\n\r');

}

/*♯♯-2- Configure the Input Capture channels ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

/* Common configuration */

sConfig.ICPrescaler = TIM_ICPSC_DIV1;

sConfig.ICFilter = 0;

/* Configure the Input Capture of channel 1 */

sConfig.ICPolarity = TIM_ICPOLARITY_RISING;

sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;

if(HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)

{

/* Configuration Error */

PRINTF('ERROR\n\r');

}

/* Configure the Input Capture of channel 2 */

sConfig.ICPolarity = TIM_ICPOLARITY_FALLING;

sConfig.ICSelection = TIM_ICSELECTION_INDIRECTTI;

if(HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)

{

/* Configuration Error */

PRINTF('ERROR\n\r');

}

/*♯♯-3- Configure the slave mode ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

/* Select the slave Mode: Reset Mode */

sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;

sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;

sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;

sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;

sSlaveConfig.TriggerFilter = 0;

if(HAL_TIM_SlaveConfigSynchronization(&TimHandle, &sSlaveConfig) != HAL_OK)

{

/* Configuration Error */

PRINTF('ERROR\n\r');

}

/*♯♯-4- Start the Input Capture in interrupt mode ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

if(HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_1) != HAL_OK)

{

/* Starting Error */

PRINTF('ERROR\n\r');

}

/*♯♯-5- Start the Input Capture in interrupt mode ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

//if(HAL_TIM_IC_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)

//{

/* Starting Error */

// PRINTF('ERROR\n\r');

// }

//PRINTF('%d\n\r',__HAL_TIM_GET_FLAG(&TimHandle,TIM_FLAG_CC1));

//PRINTF('%d\n\r',__HAL_TIM_GET_FLAG(&TimHandle,TIM_FLAG_CC2));

// PRINTF('Init Done\n\r');

}

/**

* @brief Input Capture callback in non blocking mode

* @param htim : TIM IC handle

* @retval None

*/

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)

{

PRINTF('Interrupt\n\r');

/*♯♯-5- Start the Input Capture in interrupt mode ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

if(HAL_TIM_IC_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)

{

/* Starting Error */

PRINTF('ERROR\n\r');

}

if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)

{

/* Get the Input Capture value */

uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);

if (uwIC2Value != 0)

{

/* Duty cycle computation */

uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1)) * 100) / uwIC2Value;

/* uwFrequency computation

TIM4 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */

uwFrequency = (HAL_RCC_GetHCLKFreq())/2 / uwIC2Value;

}

else

{

uwDutyCycle = 0;

uwFrequency = 0;

}

// PRINTF('DUTY : %d\n\r', uwDutyCycle);

// PRINTF('FREQUENCY : %d\n\r', uwFrequency);

}

I have added TIMx_IRQHandler in stm32l0xx_it.c and stm32l0xx_it.h files.

CC1IF flag is set in the Status Register of the timer but Interrupt Handler is not getting called.

Is there anything else that has to be configured?

#stm32l0-timer #b-l072z-lrwan1 #interrups
4 REPLIES 4
Posted on May 11, 2018 at 20:53

I have added TIMx_IRQHandler in stm32l0xx_it.c and stm32l0xx_it.h files.

And where is the

#define TIMx_IRQHandler                TIM2_IRQHandler

line? Is it in a common header included also in stm32l0xx_it.c? If not, then rename the handler in stm32l0xx_it.c explicitly to TIM2_IRQHandler(),

JW

Posted on May 14, 2018 at 08:39

Hi

Waclawek.Jan

,

Thanks

After changing the name to TIM2_Handler I am getting interrupt.

How to calculate width using input capture?

I used the following logic but seems to be getting wrong width value.

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)

{

if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {

HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_2);//Start the timer after rising edge to calculate width

uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);

}

if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {

uwIC2Value_f = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);//Capture value at falling edge and stop timer

HAL_TIM_IC_Stop_IT(&TimHandle, TIM_CHANNEL_2);

HAL_TIM_IC_Stop_IT(&TimHandle, TIM_CHANNEL_1);

}

PRINTF('%d\n\r', uwIC2Value_f - uwIC2Value);

Posted on May 14, 2018 at 13:19

I don't know - I don't use Cube/HAL, sorry.

JW

Posted on May 14, 2018 at 15:30

Ok Thanks