1 /** 2 ****************************************************************************** 3 * @file stm32c0xx_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) 2022 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 'stm32c0xx_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 stm32c0xx_hal_conf.h 32 33 @endverbatim 34 ****************************************************************************** 35 */ 36 37 /* Includes ------------------------------------------------------------------*/ 38 #include "stm32c0xx_hal.h" 39 40 /** @addtogroup STM32C0xx_HAL_Driver 41 * @{ 42 */ 43 44 /** @addtogroup HAL_TimeBase_TIM 45 * @{ 46 */ 47 48 /* Private typedef -----------------------------------------------------------*/ 49 /* Private define ------------------------------------------------------------*/ 50 /* Private macro -------------------------------------------------------------*/ 51 /* Private variables ---------------------------------------------------------*/ 52 extern TIM_HandleTypeDef TimHandle; 53 TIM_HandleTypeDef TimHandle; 54 /* Private function prototypes -----------------------------------------------*/ 55 void TIM3_IRQHandler(void); 56 /* Private functions ---------------------------------------------------------*/ 57 58 /** 59 * @brief This function configures the TIM3 as a time base source. 60 * The time source is configured to have 1ms time base with a dedicated 61 * Tick interrupt priority. 62 * @note This function is called automatically at the beginning of program after 63 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). 64 * @param TickPriority Tick interrupt priority. 65 * @retval HAL status 66 */ HAL_InitTick(uint32_t TickPriority)67HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) 68 { 69 RCC_ClkInitTypeDef clkconfig; 70 uint32_t uwTimclock; 71 uint32_t uwAPB1Prescaler; 72 uint32_t uwPrescalerValue; 73 uint32_t pFLatency; 74 75 /*Configure the TIM3 IRQ priority */ 76 HAL_NVIC_SetPriority(TIM3_IRQn, TickPriority, 0U); 77 78 /* Enable the TIM3 global Interrupt */ 79 HAL_NVIC_EnableIRQ(TIM3_IRQn); 80 81 /* Enable TIM3 clock */ 82 __HAL_RCC_TIM3_CLK_ENABLE(); 83 84 /* Get clock configuration */ 85 HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); 86 87 /* Get APB1 prescaler */ 88 uwAPB1Prescaler = clkconfig.APB1CLKDivider; 89 90 /* Compute TIM3 clock */ 91 if (uwAPB1Prescaler == RCC_APB1_DIV1) 92 { 93 uwTimclock = HAL_RCC_GetPCLK1Freq(); 94 } 95 else 96 { 97 uwTimclock = 2U * HAL_RCC_GetPCLK1Freq(); 98 } 99 100 /* Compute the prescaler value to have TIM3 counter clock equal to 1MHz */ 101 uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U); 102 103 /* Initialize TIM3 */ 104 TimHandle.Instance = TIM3; 105 106 /* Initialize TIMx peripheral as follow: 107 + Period = [(TIM3CLK/1000) - 1]. to have a (1/1000) s time base. 108 + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. 109 + ClockDivision = 0 110 + Counter direction = Up 111 */ 112 TimHandle.Init.Period = (1000000U / 1000U) - 1U; 113 TimHandle.Init.Prescaler = uwPrescalerValue; 114 TimHandle.Init.ClockDivision = 0U; 115 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; 116 TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; 117 if (HAL_TIM_Base_Init(&TimHandle) == HAL_OK) 118 { 119 /* Start the TIM time Base generation in interrupt mode */ 120 return HAL_TIM_Base_Start_IT(&TimHandle); 121 } 122 123 /* Return function status */ 124 return HAL_ERROR; 125 } 126 127 /** 128 * @brief Suspend Tick increment. 129 * @note Disable the tick increment by disabling TIM3 update interrupt. 130 * @retval None 131 */ HAL_SuspendTick(void)132void HAL_SuspendTick(void) 133 { 134 /* Disable TIM3 update interrupt */ 135 __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE); 136 } 137 138 /** 139 * @brief Resume Tick increment. 140 * @note Enable the tick increment by enabling TIM3 update interrupt. 141 * @retval None 142 */ HAL_ResumeTick(void)143void HAL_ResumeTick(void) 144 { 145 /* Enable TIM3 update interrupt */ 146 __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE); 147 } 148 149 /** 150 * @brief Period elapsed callback in non blocking mode 151 * @note This function is called when TIM3 interrupt took place, inside 152 * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment 153 * a global variable "uwTick" used as application time base. 154 * @param htim TIM handle 155 * @retval None 156 */ HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)157void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) 158 { 159 HAL_IncTick(); 160 } 161 162 /** 163 * @brief This function handles TIM interrupt request. 164 * @retval None 165 */ TIM3_IRQHandler(void)166void TIM3_IRQHandler(void) 167 { 168 HAL_TIM_IRQHandler(&TimHandle); 169 } 170 171 /** 172 * @} 173 */ 174 175 /** 176 * @} 177 */ 178