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