1 /** 2 ****************************************************************************** 3 * @file stm32wbaxx_hal_timebase_tim_template.c 4 * @author MCD Application Team 5 * @brief HAL time base based on the hardware TIM. 6 * 7 * This file overrides the native HAL time base functions (defined as weak) 8 * the TIM time base: 9 * + Initializes the TIM peripheral to 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) 2023 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 'stm32n6xx_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 stm32n6xx_hal_conf.h 32 @endverbatim 33 */ 34 35 /* Includes ------------------------------------------------------------------*/ 36 #include "stm32n6xx_hal.h" 37 38 /** @addtogroup STM32N6xx_HAL_Driver 39 * @{ 40 */ 41 42 /** @addtogroup HAL_TimeBase 43 * @{ 44 */ 45 46 /* Private typedef -----------------------------------------------------------*/ 47 /* Private define ------------------------------------------------------------*/ 48 #define TIM_CNT_FREQ 1000000U /* Timer frequency counter : 1 MHz */ 49 #define TIM_FREQ 1000U /* Timer frequency : 1 kHz => to have 1 ms interrupt */ 50 51 /* Private macro -------------------------------------------------------------*/ 52 /* Private variables ---------------------------------------------------------*/ 53 static TIM_HandleTypeDef TimHandle; 54 55 /* Private function prototypes -----------------------------------------------*/ 56 void TIM2_IRQHandler(void); 57 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1U) 58 void TimeBase_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim); 59 #endif /* (USE_HAL_TIM_REGISTER_CALLBACKS == 1U) */ 60 /* Private functions ---------------------------------------------------------*/ 61 62 /** 63 * @brief This function configures the TIM2 as a time base source. 64 * The time source is configured to have 1ms time base with a dedicated 65 * Tick interrupt priority. 66 * @note This function is called automatically at the beginning of program after 67 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). 68 * @param TickPriority Tick interrupt priority. 69 * @retval HAL Status 70 */ HAL_InitTick(uint32_t TickPriority)71HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) 72 { 73 RCC_ClkInitTypeDef clkconfig; 74 uint32_t uwTimclock; 75 uint32_t uwAPB1Prescaler; 76 uint32_t uwPrescalerValue; 77 HAL_StatusTypeDef Status; 78 79 /* Enable TIM2 clock */ 80 __HAL_RCC_TIM2_CLK_ENABLE(); 81 82 /* Get clock configuration */ 83 HAL_RCC_GetClockConfig(&clkconfig); 84 85 /* Get APB1 prescaler */ 86 uwAPB1Prescaler = clkconfig.APB1CLKDivider; 87 88 /* Compute TIM2 clock */ 89 if (uwAPB1Prescaler == RCC_HCLK_DIV1) 90 { 91 uwTimclock = HAL_RCC_GetPCLK1Freq(); 92 } 93 else 94 { 95 uwTimclock = 2UL * HAL_RCC_GetPCLK1Freq(); 96 } 97 98 /* Compute the prescaler value to have TIM2 counter clock equal to TIM_CNT_FREQ */ 99 uwPrescalerValue = (uint32_t)((uwTimclock / TIM_CNT_FREQ) - 1U); 100 101 /* Initialize TIM2 */ 102 TimHandle.Instance = TIM2; 103 104 /* Initialize TIMx peripheral as follow: 105 + Period = [(TIM_CNT_FREQ/TIM_FREQ) - 1]. to have a (1/TIM_FREQ) s time base. 106 + Prescaler = (uwTimclock/TIM_CNT_FREQ - 1) to have a TIM_CNT_FREQ counter clock. 107 + ClockDivision = 0 108 + Counter direction = Up 109 */ 110 TimHandle.Init.Period = (TIM_CNT_FREQ / TIM_FREQ) - 1U; 111 TimHandle.Init.Prescaler = uwPrescalerValue; 112 TimHandle.Init.ClockDivision = 0; 113 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; 114 Status = HAL_TIM_Base_Init(&TimHandle); 115 if (Status == HAL_OK) 116 { 117 /* Start the TIM time Base generation in interrupt mode */ 118 Status = HAL_TIM_Base_Start_IT(&TimHandle); 119 if (Status == HAL_OK) 120 { 121 if (TickPriority < (1UL << __NVIC_PRIO_BITS)) 122 { 123 /* Enable the TIM2 global Interrupt */ 124 HAL_NVIC_SetPriority(TIM2_IRQn, TickPriority, 0); 125 uwTickPrio = TickPriority; 126 } 127 else 128 { 129 Status = HAL_ERROR; 130 } 131 } 132 } 133 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1U) 134 HAL_TIM_RegisterCallback(&TimHandle, HAL_TIM_PERIOD_ELAPSED_CB_ID, TimeBase_TIM_PeriodElapsedCallback); 135 #endif /* (USE_HAL_TIM_REGISTER_CALLBACKS == 1U) */ 136 137 HAL_NVIC_EnableIRQ(TIM2_IRQn); 138 139 /* Return function Status */ 140 return Status; 141 } 142 143 /** 144 * @brief Suspend Tick increment. 145 * @note Disable the tick increment by disabling TIM2 update interrupt. 146 * @param None 147 * @retval None 148 */ HAL_SuspendTick(void)149void HAL_SuspendTick(void) 150 { 151 /* Disable TIM2 update Interrupt */ 152 __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE); 153 } 154 155 /** 156 * @brief Resume Tick increment. 157 * @note Enable the tick increment by Enabling TIM2 update interrupt. 158 * @param None 159 * @retval None 160 */ HAL_ResumeTick(void)161void HAL_ResumeTick(void) 162 { 163 /* Enable TIM2 Update interrupt */ 164 __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE); 165 } 166 167 /** 168 * @brief Period elapsed callback in non blocking mode 169 * @note This function is called when TIM2 interrupt took place, inside 170 * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment 171 * a global variable "uwTick" used as application time base. 172 * @param htim TIM handle 173 * @retval None 174 */ 175 #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1U) TimeBase_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)176void TimeBase_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) 177 #else 178 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) 179 #endif /* (USE_HAL_TIM_REGISTER_CALLBACKS == 1U) */ 180 { 181 /* Prevent unused argument(s) compilation warning */ 182 UNUSED(htim); 183 184 HAL_IncTick(); 185 } 186 187 /** 188 * @brief This function handles TIM interrupt request. 189 * @param None 190 * @retval None 191 */ TIM2_IRQHandler(void)192void TIM2_IRQHandler(void) 193 { 194 HAL_TIM_IRQHandler(&TimHandle); 195 } 196 197 /** 198 * @} 199 */ 200 201 /** 202 * @} 203 */ 204