1 /** 2 ****************************************************************************** 3 * @file stm32f1xx_hal_timebase_tim_template.c 4 * @author MCD Application Team 5 * @brief HAL time base based on the hardware TIM Template. 6 * 7 * This file overrides the native HAL time base functions (defined as weak) 8 * the TIM time base: 9 * + Intializes the TIM peripheral generate a Period elapsed Event each 1ms 10 * + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms 11 * 12 ****************************************************************************** 13 * @attention 14 * 15 * <h2><center>© Copyright (c) 2017 STMicroelectronics. 16 * All rights reserved.</center></h2> 17 * 18 * This software component is licensed by ST under BSD 3-Clause license, 19 * the "License"; You may not use this file except in compliance with the 20 * License. You may obtain a copy of the License at: 21 * opensource.org/licenses/BSD-3-Clause 22 * 23 ****************************************************************************** 24 */ 25 26 /* Includes ------------------------------------------------------------------*/ 27 #include "stm32f1xx_hal.h" 28 29 /** @addtogroup STM32F1xx_HAL_Driver 30 * @{ 31 */ 32 33 /** @addtogroup HAL_TimeBase_TIM 34 * @{ 35 */ 36 37 /* Private typedef -----------------------------------------------------------*/ 38 /* Private define ------------------------------------------------------------*/ 39 /* Private macro -------------------------------------------------------------*/ 40 /* Private variables ---------------------------------------------------------*/ 41 TIM_HandleTypeDef TimHandle; 42 /* Private function prototypes -----------------------------------------------*/ 43 void TIM2_IRQHandler(void); 44 /* Private functions ---------------------------------------------------------*/ 45 46 /** 47 * @brief This function configures the TIM2 as a time base source. 48 * The time source is configured to have 1ms time base with a dedicated 49 * Tick interrupt priority. 50 * @note This function is called automatically at the beginning of program after 51 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). 52 * @param TickPriority Tick interrupt priority. 53 * @retval HAL status 54 */ HAL_InitTick(uint32_t TickPriority)55HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) 56 { 57 RCC_ClkInitTypeDef clkconfig; 58 uint32_t uwTimclock, uwAPB1Prescaler = 0U; 59 uint32_t uwPrescalerValue = 0U; 60 uint32_t pFLatency; 61 62 /*Configure the TIM2 IRQ priority */ 63 HAL_NVIC_SetPriority(TIM2_IRQn, TickPriority, 0U); 64 65 /* Enable the TIM2 global Interrupt */ 66 HAL_NVIC_EnableIRQ(TIM2_IRQn); 67 68 /* Enable TIM2 clock */ 69 __HAL_RCC_TIM2_CLK_ENABLE(); 70 71 /* Get clock configuration */ 72 HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); 73 74 /* Get APB1 prescaler */ 75 uwAPB1Prescaler = clkconfig.APB1CLKDivider; 76 77 /* Compute TIM2 clock */ 78 if (uwAPB1Prescaler == RCC_HCLK_DIV1) 79 { 80 uwTimclock = HAL_RCC_GetPCLK1Freq(); 81 } 82 else 83 { 84 uwTimclock = 2 * HAL_RCC_GetPCLK1Freq(); 85 } 86 87 /* Compute the prescaler value to have TIM2 counter clock equal to 1MHz */ 88 uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U); 89 90 /* Initialize TIM2 */ 91 TimHandle.Instance = TIM2; 92 93 /* Initialize TIMx peripheral as follow: 94 + Period = [(TIM2CLK/1000) - 1]. to have a (1/1000) s time base. 95 + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. 96 + ClockDivision = 0 97 + Counter direction = Up 98 */ 99 TimHandle.Init.Period = (1000000U / 1000U) - 1U; 100 TimHandle.Init.Prescaler = uwPrescalerValue; 101 TimHandle.Init.ClockDivision = 0U; 102 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; 103 TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; 104 if (HAL_TIM_Base_Init(&TimHandle) == HAL_OK) 105 { 106 /* Start the TIM time Base generation in interrupt mode */ 107 return HAL_TIM_Base_Start_IT(&TimHandle); 108 } 109 110 /* Return function status */ 111 return HAL_ERROR; 112 } 113 114 /** 115 * @brief Suspend Tick increment. 116 * @note Disable the tick increment by disabling TIM2 update interrupt. 117 * @retval None 118 */ HAL_SuspendTick(void)119void HAL_SuspendTick(void) 120 { 121 /* Disable TIM2 update Interrupt */ 122 __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE); 123 } 124 125 /** 126 * @brief Resume Tick increment. 127 * @note Enable the tick increment by Enabling TIM2 update interrupt. 128 * @retval None 129 */ HAL_ResumeTick(void)130void HAL_ResumeTick(void) 131 { 132 /* Enable TIM2 Update interrupt */ 133 __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE); 134 } 135 136 /** 137 * @brief Period elapsed callback in non blocking mode 138 * @note This function is called when TIM2 interrupt took place, inside 139 * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment 140 * a global variable "uwTick" used as application time base. 141 * @param htim TIM handle 142 * @retval None 143 */ HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)144void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) 145 { 146 HAL_IncTick(); 147 } 148 149 /** 150 * @brief This function handles TIM interrupt request. 151 * @retval None 152 */ TIM2_IRQHandler(void)153void TIM2_IRQHandler(void) 154 { 155 HAL_TIM_IRQHandler(&TimHandle); 156 } 157 158 /** 159 * @} 160 */ 161 162 /** 163 * @} 164 */ 165 166 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 167