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, uwAPB1Prescaler; 71 uint32_t uwPrescalerValue; 72 uint32_t pFLatency; 73 74 /*Configure the TIM3 IRQ priority */ 75 HAL_NVIC_SetPriority(TIM3_IRQn, TickPriority, 0U); 76 77 /* Enable the TIM3 global Interrupt */ 78 HAL_NVIC_EnableIRQ(TIM3_IRQn); 79 80 /* Enable TIM3 clock */ 81 __HAL_RCC_TIM3_CLK_ENABLE(); 82 83 /* Get clock configuration */ 84 HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); 85 86 /* Get APB1 prescaler */ 87 uwAPB1Prescaler = clkconfig.APB1CLKDivider; 88 89 /* Compute TIM3 clock */ 90 if (uwAPB1Prescaler == RCC_HCLK_DIV1) 91 { 92 uwTimclock = HAL_RCC_GetPCLK1Freq(); 93 } 94 else 95 { 96 uwTimclock = 2U * HAL_RCC_GetPCLK1Freq(); 97 } 98 99 /* Compute the prescaler value to have TIM3 counter clock equal to 1MHz */ 100 uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U); 101 102 /* Initialize TIM3 */ 103 TimHandle.Instance = TIM3; 104 105 /* Initialize TIMx peripheral as follow: 106 + Period = [(TIM3CLK/1000) - 1]. to have a (1/1000) s time base. 107 + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. 108 + ClockDivision = 0 109 + Counter direction = Up 110 */ 111 TimHandle.Init.Period = (1000000U / 1000U) - 1U; 112 TimHandle.Init.Prescaler = uwPrescalerValue; 113 TimHandle.Init.ClockDivision = 0U; 114 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; 115 TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; 116 if (HAL_TIM_Base_Init(&TimHandle) == HAL_OK) 117 { 118 /* Start the TIM time Base generation in interrupt mode */ 119 return HAL_TIM_Base_Start_IT(&TimHandle); 120 } 121 122 /* Return function status */ 123 return HAL_ERROR; 124 } 125 126 /** 127 * @brief Suspend Tick increment. 128 * @note Disable the tick increment by disabling TIM3 update interrupt. 129 * @retval None 130 */ HAL_SuspendTick(void)131void HAL_SuspendTick(void) 132 { 133 /* Disable TIM3 update interrupt */ 134 __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE); 135 } 136 137 /** 138 * @brief Resume Tick increment. 139 * @note Enable the tick increment by enabling TIM3 update interrupt. 140 * @retval None 141 */ HAL_ResumeTick(void)142void HAL_ResumeTick(void) 143 { 144 /* Enable TIM3 update interrupt */ 145 __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE); 146 } 147 148 /** 149 * @brief Period elapsed callback in non blocking mode 150 * @note This function is called when TIM3 interrupt took place, inside 151 * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment 152 * a global variable "uwTick" used as application time base. 153 * @param htim TIM handle 154 * @retval None 155 */ HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)156void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) 157 { 158 HAL_IncTick(); 159 } 160 161 /** 162 * @brief This function handles TIM interrupt request. 163 * @retval None 164 */ TIM3_IRQHandler(void)165void TIM3_IRQHandler(void) 166 { 167 HAL_TIM_IRQHandler(&TimHandle); 168 } 169 170 /** 171 * @} 172 */ 173 174 /** 175 * @} 176 */ 177