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