cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 HAL timer interrupt settings do not work on nucleo stmF4x

roseanne qiu
Associate II
Posted on June 19, 2018 at 16:39

Dear Sir/Madam:

I am using STM32F4x nucleo HAL lib, and try to set timer interrupt. It does not work. Here are the settings for timer interrupt:

..... // Timer is configured to be triggered every .05 secs.

__TIM4_CLK_ENABLE();

Tim4Handle.Init.Prescaler = 16400;

Tim4Handle.Init.CounterMode = TIM_COUNTERMODE_UP;

Tim4Handle.Init.Period = 20;

Tim4Handle.Instance = TIM4;

Tim4Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

if ( HAL_TIM_Base_Init(&Tim4Handle) != HAL_OK )

{

 Error_Handler(); }

TIM_SlaveConfigTypeDef sSlaveConfig;

TIM_MasterConfigTypeDef sMasterConfig;

sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;

sSlaveConfig.InputTrigger = TIM_TS_ITR0;

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

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&Tim4Handle, &sMasterConfig) != HAL_OK)

{

Error_Handler();

}

HAL_TIM_Base_Start_IT(&Tim4Handle);

HAL_NVIC_SetPriority(TIM4_IRQn, 0, 1);

HAL_NVIC_EnableIRQ(TIM4_IRQn); .....

here is my interrupt routine(it is never got called):

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

// send gpio signal to scope

}

would you please help me to see any problem?

thanks roseanne

#stm32f4-nucleo #initialization-timer-hal #stm32l4-hal-timer #stm-mcu #timer-interrupts
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on June 19, 2018 at 18:40

>>Do I need to use 

TIM4_IRQHandler()

  to call HAL?

Yes, otherwise how will the plumbing work?

/**

  * @brief  This function handles TIM interrupt request.

  * @param  None

  * @retval None

  */

void TIM4_IRQHandler(void)

{

  HAL_TIM_IRQHandler(&Tim4Handle);

}

There is a lot of example code covering multiple boards, and portable

STM32Cube_FW_F4_V1.21.0\Projects\STM32F401-Discovery\Examples\TIM\TIM_TimeBase\Src

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

4 REPLIES 4
Posted on June 19, 2018 at 17:34

Seems to be a lot of unnecessary code just to set up a time-base.

What frequency are you running the part at?

Make sure the Init structure is clear/clean.

Review Prescaler and Period settings, these should be in the N-1 form, ie for N counts 0 thru N-1, the values describe the limit.

..... // Timer is configured to be triggered every .05 secs (20 Hz)

/* TIM handle declaration - global */

TIM_HandleTypeDef    Tim4Handle = {0};

__TIM4_CLK_ENABLE();

Tim4Handle.Init.Prescaler = 8400 - 1;// APB1 @ 42 MHz, TIMCLK @ 84 MHz

Tim4Handle.Init.CounterMode = TIM_COUNTERMODE_UP;

Tim4Handle.Init.Period = 5000 - 1; // 100KHz to 20Hz

Tim4Handle.Instance = TIM4;

Tim4Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

if ( HAL_TIM_Base_Init(&Tim4Handle) != HAL_OK )

{

   Error_Handler();

}

HAL_TIM_Base_Start_IT(&Tim4Handle);

HAL_NVIC_SetPriority(TIM4_IRQn, 0, 1);

HAL_NVIC_EnableIRQ(TIM4_IRQn); .....

And you have a TIM4_IRQHandler() calling into the HAL?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 19, 2018 at 18:02

Dear Sir:

thanks for your quick response!

About your last question, : 

you have a TIM4_IRQHandler() calling into the HAL?

in my understand to HAL lib, I only need to define :

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

// send gpio signal to scope

}

which will be called once interrupt happen.

Do I need to use 

TIM4_IRQHandler()

  to call HAL? if so, How could I do that?

thanks

ro

Posted on June 19, 2018 at 18:40

>>Do I need to use 

TIM4_IRQHandler()

  to call HAL?

Yes, otherwise how will the plumbing work?

/**

  * @brief  This function handles TIM interrupt request.

  * @param  None

  * @retval None

  */

void TIM4_IRQHandler(void)

{

  HAL_TIM_IRQHandler(&Tim4Handle);

}

There is a lot of example code covering multiple boards, and portable

STM32Cube_FW_F4_V1.21.0\Projects\STM32F401-Discovery\Examples\TIM\TIM_TimeBase\Src

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 19, 2018 at 19:15

Dear Clive:

thanks for your help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

after I add it, both uart and timer interrupt all work!!!!!!!!!!!!!!!!!!!!!

thanks

rose