How to use timers in STM32 board and HAL library in order to measure speed of motor?
Best way is to set up a timer in encoder mode to count the pulses of your sensor. If you have both A and B pulses then choose encoder mode TIM_ENCODERMODE_TI12 if only pulse A then TIM_ENCODERMODE_TI1. (This code works fine on an STM32F4 Discovery board.)

void MX_TIM3_Init(void)
{
    TIM_Encoder_InitTypeDef encoderConfig;

    __TIM3_CLK_ENABLE();

    htim3.Instance = TIM3;
    htim3.Init.Prescaler = 0;
    htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim3.Init.Period = 0xFFFF;
    htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

    encoderConfig.EncoderMode = TIM_ENCODERMODE_TI12;

    encoderConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
    encoderConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
    encoderConfig.IC1Prescaler = TIM_ICPSC_DIV1;
    encoderConfig.IC1Filter = 0x00;

    encoderConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
    encoderConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
    encoderConfig.IC2Prescaler = TIM_ICPSC_DIV1;
    encoderConfig.IC2Filter = 0x00;

    if(HAL_TIM_Encoder_Init(&amp;htim3, &amp;encoderConfig) != HAL_OK)
    {

    }
}




/**TIM3 GPIO Configuration - Encoder
PB4     ------&gt; TIM3_CH1
PB5     ------&gt; TIM3_CH2
*/
GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOB, &amp;GPIO_InitStruct);




Actually you did not tell us the exact type of your STM32 so you should check the timer, pin and alternate function values for yours. After setting up the timer the following simple functions can be used.

Then set up another timer with periodic interrupt and call the Encoder_Read function in the ISR to query the current increment value. 

uint32_t Encoder_Read(void)
{
    return TIM3-&gt;CNT;
}

void Encoder_Start(void)
{
    HAL_TIM_Encoder_Start(&amp;htim3, TIM_CHANNEL_ALL);
}

void Encoder_Stop(void)
{
    HAL_TIM_Encoder_Stop(&amp;htim3, TIM_CHANNEL_ALL);
}


In every ISR you can calculate the difference between the previous and actual counter state. That will be the number of pulses over the period from which you can calulate the speed (given you know that how much distance 1 increment means). Be aware of possible counter overflows.

update:
Combined Channel setting: Encoder Mode. Here is a pic.