Skip to main content
Associate III
May 3, 2024
Solved

callback not getting triggered by 1us timer (TIM5)

  • May 3, 2024
  • 4 replies
  • 1492 views

HI,

I set TIM5 up to trigger every 1us (APB1 42MHz) with the below function and I expect HAL_TIM_PeriodElapsedCallback() to be invoked every 1us but that's not the case. How do I make sure that the callback gets invoked properly?

My init function:

static void manual_TIM5_Init(void)

{

__HAL_RCC_TIM5_CLK_ENABLE(); // start the Timer5 clock




TIM_ClockConfigTypeDef sClockSourceConfig = {0};

TIM_MasterConfigTypeDef sMasterConfig = {0};

htim5.Instance = TIM5;

htim5.Init.Prescaler = 1;

htim5.Init.CounterMode = TIM_COUNTERMODE_UP;

htim5.Init.Period = 20;

htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

if (HAL_TIM_Base_Init(&htim5) != HAL_OK)

{

Error_Handler();

}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

if (HAL_TIM_ConfigClockSource(&htim5, &sClockSourceConfig) != HAL_OK)

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

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

{

Error_Handler();

}

}
    This topic has been closed for replies.
    Best answer by AScha.3

    Hi,

    seems same "problem" ... :)

    see:

    https://community.st.com/t5/stm32-mcus-products/stm32g474re-general-purpose-timer-tim16/m-p/669419#M242931

     

    >How do I make sure that the callback gets invoked properly?

    Make it 100x slower...

    4 replies

    debugAuthor
    Associate III
    May 3, 2024

    I missed to mention that it does get started with:

    HAL_TIM_Base_Start_IT(&htim5);

     

    AScha.3
    AScha.3Best answer
    Super User
    May 3, 2024

    Hi,

    seems same "problem" ... :)

    see:

    https://community.st.com/t5/stm32-mcus-products/stm32g474re-general-purpose-timer-tim16/m-p/669419#M242931

     

    >How do I make sure that the callback gets invoked properly?

    Make it 100x slower...

    If you feel a post has answered your question, please click on " Best Answer ".
    debugAuthor
    Associate III
    May 3, 2024

    Yes for a work around, I have timer every 1 second and it just gets the microsecond timer value TIM5->CNT.

    Tesla DeLorean
    Guru
    May 3, 2024

    Prescaler and Period as set as N-1

    So 20 is DIV21

    Interrupting at 1us / 1MHz is impractical, even without the excess baggage of HAL with it's call-ins and call-backs. The processor will saturate with not productive work.

    Use a maximal TIM clocking the CNT at 1 MHz, such that you can count off elapsed time.

    Clocking fast will improve your ability to resolve

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