Skip to main content
Graduate II
September 15, 2024
Solved

TIM2 clock source disabld but timer still running correctly?

  • September 15, 2024
  • 1 reply
  • 1172 views

Hi,

as you can see from the screenshot below I have TIM2 clock source set to disable but the PWM is still running as configured and generates PWM. And it has been like that for the entire project for a while so I just noticed.

Does the time start function automatically select the source to some default like the Internal clock? Or is that Clock source referring to something else? Just asking because I want to make sure the code is running correctly for the right configuration not because of some other settings that somehow override these.

Thank you

 

Ricko_0-1726436375212.png

 

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    When Clock source is disabled, HAL_TIM_ConfigClockSource() is not generated and when "Internal Clock" is selected, the following code is generated:

     sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
     if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
     {
     Error_Handler();
     }

    And according to HAL_TIM_ConfigClockSource() implementation TIM_CLOCKSOURCE_INTERNAL does almost nothing, just some bit reset before: 

     /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
     tmpsmcr = htim->Instance->SMCR;
     tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
     tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
     htim->Instance->SMCR = tmpsmcr;
    
     switch (sClockSourceConfig->ClockSource)
     {
     case TIM_CLOCKSOURCE_INTERNAL:
     {
     assert_param(IS_TIM_INSTANCE(htim->Instance));
     break;
     }

    So "Internal Clock" and "Disable" seems to have the same behavior.

     

    1 reply

    mƎALLEmAnswer
    Technical Moderator
    September 16, 2024

    Hello,

    When Clock source is disabled, HAL_TIM_ConfigClockSource() is not generated and when "Internal Clock" is selected, the following code is generated:

     sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
     if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
     {
     Error_Handler();
     }

    And according to HAL_TIM_ConfigClockSource() implementation TIM_CLOCKSOURCE_INTERNAL does almost nothing, just some bit reset before: 

     /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
     tmpsmcr = htim->Instance->SMCR;
     tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
     tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
     htim->Instance->SMCR = tmpsmcr;
    
     switch (sClockSourceConfig->ClockSource)
     {
     case TIM_CLOCKSOURCE_INTERNAL:
     {
     assert_param(IS_TIM_INSTANCE(htim->Instance));
     break;
     }

    So "Internal Clock" and "Disable" seems to have the same behavior.