0
\$\begingroup\$

I use an STM32F103c8t6. What I want is to measure time between several statements. I found out HAL_getTick does not work since the resolution is too small (1 ms).

However, I cannot seem to initialize the timer correctly. With CubeMX I selected:

TIM2: Slave mode: Trigger mode Trigger Source: ITR0 Clock Source: Internal Clock Rest disabled

And the following code is generated:

/* TIM2 init function */
static void MX_TIM2_Init(void)
{

  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_SlaveConfigTypeDef sSlaveConfig;
  TIM_MasterConfigTypeDef sMasterConfig;

  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 35999;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 35999;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

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

  if (HAL_TIM_OnePulse_Init(&htim2, TIM_OPMODE_SINGLE) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
  sSlaveConfig.InputTrigger = TIM_TS_ITR0;
  if (HAL_TIM_SlaveConfigSynchronization(&htim2, &sSlaveConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

My own code in the main / while loop:

....
__HAL_RCC_TIM2_CLK_ENABLE();

And later I get the value with

uint32_t value = __HAL_TIM_GET_COUNTER(&htim2);

However, I only see 0's returning from this function.

I wonder if the timer is started at all (I tried to do it very soon after initialization of the timer, but did not make a difference). The timer always has state HAL_TIM_STATE_READY.

How should I start (and possibly restart) the timer?

\$\endgroup\$
  • \$\begingroup\$ I've never used __HAL_RCC_TIM2_CLK_ENABLE();, instead I've always used __TIM2_CLK_ENABLE(); \$\endgroup\$ – user162889 Feb 11 '18 at 0:08
  • \$\begingroup\$ @user162889 That command cannot be used in HAL (undefined reference). \$\endgroup\$ – Michel Keijzers Feb 11 '18 at 0:17
  • 1
    \$\begingroup\$ Its always a good idea to have a look into the reference manual to see details. \$\endgroup\$ – Sean87 Feb 11 '18 at 0:40
  • \$\begingroup\$ @Sean87 True, but since I'm relatively new to STM32 there is a lot of different documents and sometimes too much documentation to know where to search. \$\endgroup\$ – Michel Keijzers Feb 12 '18 at 8:06
  • 1
    \$\begingroup\$ @MichelKeijzers Have you even downloaded the RM? I mean a simple search in the PDF file brings what you want in front of you. I have used the RM manual and it is very very helpful...just try it! \$\endgroup\$ – Sean87 Feb 12 '18 at 9:36
1
\$\begingroup\$

The code generated looks fine so far, however it isn't included the call to the timer start function, hence the timer is initialized but not started. To start it in non interruption mode use HAL_TIM_Base_Start(&htim2); after the initialization.

\$\endgroup\$
  • \$\begingroup\$ Thank you for the tip. Could you please elaborate? (Otherwise, this post is more of a comment than answer. A bit thin for an answer.) \$\endgroup\$ – Nick Alexeev Feb 11 '18 at 1:03
  • \$\begingroup\$ @NickAlexeev not much else to say, anyway... \$\endgroup\$ – user162889 Feb 11 '18 at 14:05
  • \$\begingroup\$ It works indeed ... although I found out I better can use a different way to handle my original problem, but need to read more about timers/interrupts. \$\endgroup\$ – Michel Keijzers Feb 12 '18 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.