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