1 /** 2 ****************************************************************************** 3 * @file stm32wb0x_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 * + Initializes 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 * Copyright (c) 2024 STMicroelectronics. 16 * All rights reserved. 17 * 18 * This software is licensed under terms that can be found in the LICENSE file 19 * in the root directory of this software component. 20 * If no LICENSE file comes with this software, it is provided AS-IS. 21 * 22 ****************************************************************************** 23 @verbatim 24 ============================================================================== 25 ##### How to use this driver ##### 26 ============================================================================== 27 [..] 28 This file must be copied to the application folder and modified as follows: 29 (#) Rename it to 'stm32wb0x_hal_timebase_tim.c' 30 (#) Add this file and the TIM HAL drivers to your project and uncomment 31 HAL_TIM_MODULE_ENABLED define in stm32wb0x_hal_conf.h 32 33 @endverbatim 34 */ 35 36 /* Includes ------------------------------------------------------------------*/ 37 #include "stm32wb0x_hal.h" 38 39 /** @addtogroup STM32WB0x_HAL_Driver 40 * @{ 41 */ 42 43 /** @addtogroup HAL_TimeBase_TIM 44 * @{ 45 */ 46 47 /* Private typedef -----------------------------------------------------------*/ 48 /* Private define ------------------------------------------------------------*/ 49 /* Private macro -------------------------------------------------------------*/ 50 /* Private variables ---------------------------------------------------------*/ 51 extern TIM_HandleTypeDef TimHandle; 52 TIM_HandleTypeDef TimHandle; 53 /* Private function prototypes -----------------------------------------------*/ 54 void TIM1_IRQHandler(void); 55 /* Private functions ---------------------------------------------------------*/ 56 57 /** 58 * @brief This function configures the TIMx as a time base source. 59 * The time source is configured to have 1ms time base with a dedicated 60 * Tick interrupt priority. 61 * @note This function is called automatically at the beginning of program after 62 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). 63 * @param TickPriority: Tick interrupt priority. 64 * @retval HAL status 65 */ HAL_InitTick(uint32_t TickPriority)66HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) 67 { 68 uint32_t uwTimclock; 69 uint32_t uwPrescalerValue; 70 71 #if defined(TIM1) 72 /*Configure the TIM1 IRQ priority */ 73 HAL_NVIC_SetPriority(TIM1_IRQn, TickPriority, 0); 74 75 /* Enable the TIM1 global Interrupt */ 76 HAL_NVIC_EnableIRQ(TIM1_IRQn); 77 78 /* Enable TIM1 clock */ 79 __HAL_RCC_TIM1_CLK_ENABLE(); 80 81 /* Initialize TIM1 */ 82 TimHandle.Instance = TIM1; 83 #elif defined(TIM2) 84 /*Configure the TIM1 IRQ priority */ 85 HAL_NVIC_SetPriority(TIM2_IRQn, TickPriority, 0); 86 87 /* Enable the TIM2 global Interrupt */ 88 HAL_NVIC_EnableIRQ(TIM2_IRQn); 89 90 /* Enable TIM2 clock */ 91 __HAL_RCC_TIM2_CLK_ENABLE(); 92 93 /* Initialize TIM2 */ 94 TimHandle.Instance = TIM2; 95 #endif 96 97 uwTimclock = HAL_RCC_GetSysClockFreq(); 98 99 /* Compute the prescaler value to have TIMx counter clock equal to 1MHz */ 100 uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U); 101 102 /* Initialize TIMx peripheral as follow: 103 + Period = [(TIMxCLK/1000) - 1]. to have a (1/1000) s time base. 104 + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. 105 + ClockDivision = 0 106 + Counter direction = Up 107 */ 108 TimHandle.Init.Period = (1000000U / 1000U) - 1U; 109 TimHandle.Init.Prescaler = uwPrescalerValue; 110 TimHandle.Init.ClockDivision = 0U; 111 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; 112 if (HAL_TIM_Base_Init(&TimHandle) == HAL_OK) 113 { 114 /* Start the TIM time Base generation in interrupt mode */ 115 return HAL_TIM_Base_Start_IT(&TimHandle); 116 } 117 118 /* Return function status */ 119 return HAL_ERROR; 120 } 121 122 /** 123 * @brief Suspend Tick increment. 124 * @note Disable the tick increment by disabling TIM1 update interrupt. 125 * @retval None 126 */ HAL_SuspendTick(void)127void HAL_SuspendTick(void) 128 { 129 /* Disable TIM1 update Interrupt */ 130 __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE); 131 } 132 133 /** 134 * @brief Resume Tick increment. 135 * @note Enable the tick increment by Enabling TIM1 update interrupt. 136 * @retval None 137 */ HAL_ResumeTick(void)138void HAL_ResumeTick(void) 139 { 140 /* Enable TIM1 Update interrupt */ 141 __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE); 142 } 143 144 /** 145 * @brief Period elapsed callback in non blocking mode 146 * @note This function is called when TIM1 interrupt took place, inside 147 * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment 148 * a global variable "uwTick" used as application time base. 149 * @param htim : TIM handle 150 * @retval None 151 */ HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)152void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) 153 { 154 HAL_IncTick(); 155 } 156 157 /** 158 * @brief This function handles TIM interrupt request. 159 * @retval None 160 */ TIM1_IRQHandler(void)161void TIM1_IRQHandler(void) 162 { 163 HAL_TIM_IRQHandler(&TimHandle); 164 } 165 166 /** 167 * @} 168 */ 169 170 /** 171 * @} 172 */ 173