# How to get a Timer started without interrupt?

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;
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?

• I've never used __HAL_RCC_TIM2_CLK_ENABLE();, instead I've always used __TIM2_CLK_ENABLE(); – user162889 Feb 11 '18 at 0:08
• @user162889 That command cannot be used in HAL (undefined reference). – Michel Keijzers Feb 11 '18 at 0:17
• Its always a good idea to have a look into the reference manual to see details. – Sean87 Feb 11 '18 at 0:40
• @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. – Michel Keijzers Feb 12 '18 at 8:06
• @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! – Sean87 Feb 12 '18 at 9:36

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.