#include "stm32f1xx_hal.h"

HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
    RCC_ClkInitTypeDef clkconfig;
    uint32_t pFLatency;
    uint32_t uwTimclock;
    uint32_t uwPrescalerValue;

    __HAL_RCC_TIM4_CLK_ENABLE();

    HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
    uwTimclock = HAL_RCC_GetPCLK1Freq();
    if (clkconfig.APB1CLKDivider != RCC_HCLK_DIV1) {
        uwTimclock *= 2u;
    }

    uwPrescalerValue = (uwTimclock / 1000000u) - 1u;

    TIM4->PSC = uwPrescalerValue;
    TIM4->ARR = 1000u - 1u;
    TIM4->EGR = TIM_EGR_UG;
    TIM4->DIER |= TIM_DIER_UIE;
    TIM4->CR1 |= TIM_CR1_CEN;

    HAL_NVIC_SetPriority(TIM4_IRQn, TickPriority, 0u);
    HAL_NVIC_EnableIRQ(TIM4_IRQn);
    return HAL_OK;
}

HAL_StatusTypeDef HAL_DeInitTick(void)
{
    TIM4->CR1 &= ~TIM_CR1_CEN;
    TIM4->DIER &= ~TIM_DIER_UIE;
    HAL_NVIC_DisableIRQ(TIM4_IRQn);
    return HAL_OK;
}

void HAL_SuspendTick(void)
{
    TIM4->DIER &= ~TIM_DIER_UIE;
}

void HAL_ResumeTick(void)
{
    TIM4->DIER |= TIM_DIER_UIE;
}
