Rev 61 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
77 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * @file stm32l1xx_hal_timebase_tim_template.c |
||
4 | * @author MCD Application Team |
||
5 | * @brief HAL time base based on the hardware TIM Template. |
||
6 | * |
||
7 | * This file override 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) 2017 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 'stm32l1xx_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 stm32l1xx_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 | |||
41 | /* Includes ------------------------------------------------------------------*/ |
||
42 | #include "stm32l1xx_hal.h" |
||
43 | |||
44 | /** @addtogroup STM32L1xx_HAL_Driver |
||
45 | * @{ |
||
46 | */ |
||
47 | |||
48 | /** @addtogroup HAL_TimeBase_TIM |
||
49 | * @{ |
||
50 | */ |
||
51 | |||
52 | /* Private typedef -----------------------------------------------------------*/ |
||
53 | /* Private define ------------------------------------------------------------*/ |
||
54 | /* Private macro -------------------------------------------------------------*/ |
||
55 | /* Private variables ---------------------------------------------------------*/ |
||
56 | TIM_HandleTypeDef TimHandle; |
||
57 | /* Private function prototypes -----------------------------------------------*/ |
||
58 | void TIM6_IRQHandler(void); |
||
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 | */ |
||
70 | HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority) |
||
71 | { |
||
72 | RCC_ClkInitTypeDef clkconfig; |
||
73 | uint32_t uwTimclock, uwAPB1Prescaler = 0U; |
||
74 | uint32_t uwPrescalerValue = 0U; |
||
75 | uint32_t pFLatency; |
||
76 | HAL_StatusTypeDef status = HAL_OK; |
||
77 | |||
78 | /* Enable TIM6 clock */ |
||
79 | __HAL_RCC_TIM6_CLK_ENABLE(); |
||
80 | |||
81 | /* Get clock configuration */ |
||
82 | HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); |
||
83 | |||
84 | /* Get APB1 prescaler */ |
||
85 | uwAPB1Prescaler = clkconfig.APB1CLKDivider; |
||
86 | |||
87 | /* Compute TIM6 clock */ |
||
88 | if (uwAPB1Prescaler == RCC_HCLK_DIV1) |
||
89 | { |
||
90 | uwTimclock = HAL_RCC_GetPCLK1Freq(); |
||
91 | } |
||
92 | else |
||
93 | { |
||
94 | uwTimclock = 2U * HAL_RCC_GetPCLK1Freq(); |
||
95 | } |
||
96 | |||
97 | /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */ |
||
98 | uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); |
||
99 | |||
100 | /* Initialize TIM6 */ |
||
101 | TimHandle.Instance = TIM6; |
||
102 | |||
103 | /* Initialize TIMx peripheral as follow: |
||
104 | + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base. |
||
105 | + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. |
||
106 | + ClockDivision = 0 |
||
107 | + Counter direction = Up |
||
108 | */ |
||
109 | TimHandle.Init.Period = (1000000U / 1000U) - 1U; |
||
110 | TimHandle.Init.Prescaler = uwPrescalerValue; |
||
111 | TimHandle.Init.ClockDivision = 0U; |
||
112 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
113 | status = HAL_TIM_Base_Init(&TimHandle); |
||
114 | if (status == HAL_OK) |
||
115 | { |
||
116 | /* Start the TIM time Base generation in interrupt mode */ |
||
117 | status = HAL_TIM_Base_Start_IT(&TimHandle); |
||
118 | if (status == HAL_OK) |
||
119 | { |
||
120 | /* Enable the TIM6 global Interrupt */ |
||
121 | HAL_NVIC_EnableIRQ(TIM6_IRQn); |
||
122 | |||
123 | if (TickPriority < (1UL << __NVIC_PRIO_BITS)) |
||
124 | { |
||
125 | /*Configure the TIM6 IRQ priority */ |
||
126 | HAL_NVIC_SetPriority(TIM6_IRQn, TickPriority ,0); |
||
127 | uwTickPrio = TickPriority; |
||
128 | } |
||
129 | else |
||
130 | { |
||
131 | status = HAL_ERROR; |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /* Return function status */ |
||
137 | return status; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @brief Suspend Tick increment. |
||
142 | * @note Disable the tick increment by disabling TIM6 update interrupt. |
||
143 | * @param None |
||
144 | * @retval None |
||
145 | */ |
||
146 | void HAL_SuspendTick(void) |
||
147 | { |
||
148 | /* Disable TIM6 update interrupt */ |
||
149 | __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @brief Resume Tick increment. |
||
154 | * @note Enable the tick increment by enabling TIM6 update interrupt. |
||
155 | * @param None |
||
156 | * @retval None |
||
157 | */ |
||
158 | void HAL_ResumeTick(void) |
||
159 | { |
||
160 | /* Enable TIM6 update interrupt */ |
||
161 | __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @brief Period elapsed callback in non blocking mode |
||
166 | * @note This function is called when TIM6 interrupt took place, inside |
||
167 | * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment |
||
168 | * a global variable "uwTick" used as application time base. |
||
169 | * @param htim TIM handle |
||
170 | * @retval None |
||
171 | */ |
||
172 | void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
||
173 | { |
||
174 | HAL_IncTick(); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @brief This function handles TIM interrupt request. |
||
179 | * @param None |
||
180 | * @retval None |
||
181 | */ |
||
182 | void TIM6_IRQHandler(void) |
||
183 | { |
||
184 | HAL_TIM_IRQHandler(&TimHandle); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @} |
||
189 | */ |
||
190 | |||
191 | /** |
||
192 | * @} |
||
193 | */ |
||
194 | |||
195 |