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