Rev 61 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 61 | Rev 77 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /** |
1 | /** |
2 | ****************************************************************************** |
2 | ****************************************************************************** |
3 | * @file stm32l1xx_hal_timebase_tim_template.c |
3 | * @file stm32l1xx_hal_timebase_tim_template.c |
4 | * @author MCD Application Team |
4 | * @author MCD Application Team |
5 | * @brief HAL time base based on the hardware TIM Template. |
5 | * @brief HAL time base based on the hardware TIM Template. |
6 | * |
6 | * |
7 | * This file override the native HAL time base functions (defined as weak) |
7 | * This file override the native HAL time base functions (defined as weak) |
8 | * the TIM time base: |
8 | * the TIM time base: |
9 | * + Intializes the TIM peripheral to generate a Period elapsed Event each 1ms |
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 |
10 | * + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms |
11 | * |
11 | * |
12 | @verbatim |
12 | ****************************************************************************** |
13 | ============================================================================== |
13 | * @attention |
14 | ##### How to use this driver ##### |
14 | * |
15 | ============================================================================== |
15 | * Copyright (c) 2017 STMicroelectronics. |
16 | [..] |
16 | * All rights reserved. |
17 | This file must be copied to the application folder and modified as follows: |
17 | * |
18 | (#) Rename it to 'stm32l1xx_hal_timebase_tim.c' |
18 | * This software is licensed under terms that can be found in the LICENSE file |
19 | (#) Add this file and the TIM HAL driver files to your project and make sure |
19 | * in the root directory of this software component. |
20 | HAL_TIM_MODULE_ENABLED is defined in stm32l1xx_hal_conf.h |
20 | * If no LICENSE file comes with this software, it is provided AS-IS. |
21 | 21 | * |
|
22 | [..] |
22 | ****************************************************************************** |
23 | (@) The application needs to ensure that the time base is always set to 1 millisecond |
23 | @verbatim |
24 | to have correct HAL operation. |
24 | ============================================================================== |
25 | 25 | ##### How to use this driver ##### |
|
26 | @endverbatim |
26 | ============================================================================== |
27 | ****************************************************************************** |
27 | [..] |
28 | * @attention |
28 | This file must be copied to the application folder and modified as follows: |
29 | * |
29 | (#) Rename it to 'stm32l1xx_hal_timebase_tim.c' |
30 | * <h2><center>© Copyright (c) 2017 STMicroelectronics. |
30 | (#) Add this file and the TIM HAL driver files to your project and make sure |
31 | * All rights reserved.</center></h2> |
31 | HAL_TIM_MODULE_ENABLED is defined in stm32l1xx_hal_conf.h |
32 | * |
32 | |
33 | * This software component is licensed by ST under BSD 3-Clause license, |
33 | [..] |
34 | * the "License"; You may not use this file except in compliance with the |
34 | (@) The application needs to ensure that the time base is always set to 1 millisecond |
35 | * License. You may obtain a copy of the License at: |
35 | to have correct HAL operation. |
36 | * opensource.org/licenses/BSD-3-Clause |
36 | |
37 | * |
37 | @endverbatim |
38 | ****************************************************************************** |
38 | ****************************************************************************** |
39 | */ |
39 | */ |
40 | 40 | ||
41 | /* Includes ------------------------------------------------------------------*/ |
41 | /* Includes ------------------------------------------------------------------*/ |
42 | #include "stm32l1xx_hal.h" |
42 | #include "stm32l1xx_hal.h" |
43 | 43 | ||
44 | /** @addtogroup STM32L1xx_HAL_Driver |
44 | /** @addtogroup STM32L1xx_HAL_Driver |
45 | * @{ |
45 | * @{ |
46 | */ |
46 | */ |
47 | 47 | ||
48 | /** @addtogroup HAL_TimeBase_TIM |
48 | /** @addtogroup HAL_TimeBase_TIM |
49 | * @{ |
49 | * @{ |
50 | */ |
50 | */ |
51 | 51 | ||
52 | /* Private typedef -----------------------------------------------------------*/ |
52 | /* Private typedef -----------------------------------------------------------*/ |
53 | /* Private define ------------------------------------------------------------*/ |
53 | /* Private define ------------------------------------------------------------*/ |
54 | /* Private macro -------------------------------------------------------------*/ |
54 | /* Private macro -------------------------------------------------------------*/ |
55 | /* Private variables ---------------------------------------------------------*/ |
55 | /* Private variables ---------------------------------------------------------*/ |
56 | TIM_HandleTypeDef TimHandle; |
56 | TIM_HandleTypeDef TimHandle; |
57 | /* Private function prototypes -----------------------------------------------*/ |
57 | /* Private function prototypes -----------------------------------------------*/ |
58 | void TIM6_IRQHandler(void); |
58 | void TIM6_IRQHandler(void); |
59 | /* Private functions ---------------------------------------------------------*/ |
59 | /* Private functions ---------------------------------------------------------*/ |
60 | 60 | ||
61 | /** |
61 | /** |
62 | * @brief This function configures the TIM6 as a time base source. |
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 |
63 | * The time source is configured to have 1ms time base with a dedicated |
64 | * Tick interrupt priority. |
64 | * Tick interrupt priority. |
65 | * @note This function is called automatically at the beginning of program after |
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(). |
66 | * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). |
67 | * @param TickPriority Tick interrupt priority. |
67 | * @param TickPriority Tick interrupt priority. |
68 | * @retval HAL status |
68 | * @retval HAL status |
69 | */ |
69 | */ |
70 | HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority) |
70 | HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority) |
71 | { |
71 | { |
72 | RCC_ClkInitTypeDef clkconfig; |
72 | RCC_ClkInitTypeDef clkconfig; |
73 | uint32_t uwTimclock, uwAPB1Prescaler = 0U; |
73 | uint32_t uwTimclock, uwAPB1Prescaler = 0U; |
74 | uint32_t uwPrescalerValue = 0U; |
74 | uint32_t uwPrescalerValue = 0U; |
75 | uint32_t pFLatency; |
75 | uint32_t pFLatency; |
76 | HAL_StatusTypeDef status = HAL_OK; |
76 | HAL_StatusTypeDef status = HAL_OK; |
77 | 77 | ||
78 | /* Enable TIM6 clock */ |
78 | /* Enable TIM6 clock */ |
79 | __HAL_RCC_TIM6_CLK_ENABLE(); |
79 | __HAL_RCC_TIM6_CLK_ENABLE(); |
80 | 80 | ||
81 | /* Get clock configuration */ |
81 | /* Get clock configuration */ |
82 | HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); |
82 | HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); |
83 | 83 | ||
84 | /* Get APB1 prescaler */ |
84 | /* Get APB1 prescaler */ |
85 | uwAPB1Prescaler = clkconfig.APB1CLKDivider; |
85 | uwAPB1Prescaler = clkconfig.APB1CLKDivider; |
86 | 86 | ||
87 | /* Compute TIM6 clock */ |
87 | /* Compute TIM6 clock */ |
88 | if (uwAPB1Prescaler == RCC_HCLK_DIV1) |
88 | if (uwAPB1Prescaler == RCC_HCLK_DIV1) |
89 | { |
89 | { |
90 | uwTimclock = HAL_RCC_GetPCLK1Freq(); |
90 | uwTimclock = HAL_RCC_GetPCLK1Freq(); |
91 | } |
91 | } |
92 | else |
92 | else |
93 | { |
93 | { |
94 | uwTimclock = 2U * HAL_RCC_GetPCLK1Freq(); |
94 | uwTimclock = 2U * HAL_RCC_GetPCLK1Freq(); |
95 | } |
95 | } |
96 | 96 | ||
97 | /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */ |
97 | /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */ |
98 | uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); |
98 | uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); |
99 | 99 | ||
100 | /* Initialize TIM6 */ |
100 | /* Initialize TIM6 */ |
101 | TimHandle.Instance = TIM6; |
101 | TimHandle.Instance = TIM6; |
102 | 102 | ||
103 | /* Initialize TIMx peripheral as follow: |
103 | /* Initialize TIMx peripheral as follow: |
104 | + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base. |
104 | + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base. |
105 | + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. |
105 | + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. |
106 | + ClockDivision = 0 |
106 | + ClockDivision = 0 |
107 | + Counter direction = Up |
107 | + Counter direction = Up |
108 | */ |
108 | */ |
109 | TimHandle.Init.Period = (1000000U / 1000U) - 1U; |
109 | TimHandle.Init.Period = (1000000U / 1000U) - 1U; |
110 | TimHandle.Init.Prescaler = uwPrescalerValue; |
110 | TimHandle.Init.Prescaler = uwPrescalerValue; |
111 | TimHandle.Init.ClockDivision = 0U; |
111 | TimHandle.Init.ClockDivision = 0U; |
112 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
112 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
113 | status = HAL_TIM_Base_Init(&TimHandle); |
113 | status = HAL_TIM_Base_Init(&TimHandle); |
114 | if (status == HAL_OK) |
114 | if (status == HAL_OK) |
115 | { |
115 | { |
116 | /* Start the TIM time Base generation in interrupt mode */ |
116 | /* Start the TIM time Base generation in interrupt mode */ |
117 | status = HAL_TIM_Base_Start_IT(&TimHandle); |
117 | status = HAL_TIM_Base_Start_IT(&TimHandle); |
118 | if (status == HAL_OK) |
118 | if (status == HAL_OK) |
119 | { |
119 | { |
120 | /* Enable the TIM6 global Interrupt */ |
120 | /* Enable the TIM6 global Interrupt */ |
121 | HAL_NVIC_EnableIRQ(TIM6_IRQn); |
121 | HAL_NVIC_EnableIRQ(TIM6_IRQn); |
122 | 122 | ||
123 | if (TickPriority < (1UL << __NVIC_PRIO_BITS)) |
123 | if (TickPriority < (1UL << __NVIC_PRIO_BITS)) |
124 | { |
124 | { |
125 | /*Configure the TIM6 IRQ priority */ |
125 | /*Configure the TIM6 IRQ priority */ |
126 | HAL_NVIC_SetPriority(TIM6_IRQn, TickPriority ,0); |
126 | HAL_NVIC_SetPriority(TIM6_IRQn, TickPriority ,0); |
127 | uwTickPrio = TickPriority; |
127 | uwTickPrio = TickPriority; |
128 | } |
128 | } |
129 | else |
129 | else |
130 | { |
130 | { |
131 | status = HAL_ERROR; |
131 | status = HAL_ERROR; |
132 | } |
132 | } |
133 | } |
133 | } |
134 | } |
134 | } |
135 | 135 | ||
136 | /* Return function status */ |
136 | /* Return function status */ |
137 | return status; |
137 | return status; |
138 | } |
138 | } |
139 | 139 | ||
140 | /** |
140 | /** |
141 | * @brief Suspend Tick increment. |
141 | * @brief Suspend Tick increment. |
142 | * @note Disable the tick increment by disabling TIM6 update interrupt. |
142 | * @note Disable the tick increment by disabling TIM6 update interrupt. |
143 | * @param None |
143 | * @param None |
144 | * @retval None |
144 | * @retval None |
145 | */ |
145 | */ |
146 | void HAL_SuspendTick(void) |
146 | void HAL_SuspendTick(void) |
147 | { |
147 | { |
148 | /* Disable TIM6 update interrupt */ |
148 | /* Disable TIM6 update interrupt */ |
149 | __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE); |
149 | __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE); |
150 | } |
150 | } |
151 | 151 | ||
152 | /** |
152 | /** |
153 | * @brief Resume Tick increment. |
153 | * @brief Resume Tick increment. |
154 | * @note Enable the tick increment by enabling TIM6 update interrupt. |
154 | * @note Enable the tick increment by enabling TIM6 update interrupt. |
155 | * @param None |
155 | * @param None |
156 | * @retval None |
156 | * @retval None |
157 | */ |
157 | */ |
158 | void HAL_ResumeTick(void) |
158 | void HAL_ResumeTick(void) |
159 | { |
159 | { |
160 | /* Enable TIM6 update interrupt */ |
160 | /* Enable TIM6 update interrupt */ |
161 | __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE); |
161 | __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE); |
162 | } |
162 | } |
163 | 163 | ||
164 | /** |
164 | /** |
165 | * @brief Period elapsed callback in non blocking mode |
165 | * @brief Period elapsed callback in non blocking mode |
166 | * @note This function is called when TIM6 interrupt took place, inside |
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 |
167 | * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment |
168 | * a global variable "uwTick" used as application time base. |
168 | * a global variable "uwTick" used as application time base. |
169 | * @param htim TIM handle |
169 | * @param htim TIM handle |
170 | * @retval None |
170 | * @retval None |
171 | */ |
171 | */ |
172 | void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
172 | void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
173 | { |
173 | { |
174 | HAL_IncTick(); |
174 | HAL_IncTick(); |
175 | } |
175 | } |
176 | 176 | ||
177 | /** |
177 | /** |
178 | * @brief This function handles TIM interrupt request. |
178 | * @brief This function handles TIM interrupt request. |
179 | * @param None |
179 | * @param None |
180 | * @retval None |
180 | * @retval None |
181 | */ |
181 | */ |
182 | void TIM6_IRQHandler(void) |
182 | void TIM6_IRQHandler(void) |
183 | { |
183 | { |
184 | HAL_TIM_IRQHandler(&TimHandle); |
184 | HAL_TIM_IRQHandler(&TimHandle); |
185 | } |
185 | } |
186 | 186 | ||
187 | /** |
187 | /** |
188 | * @} |
188 | * @} |
189 | */ |
189 | */ |
190 | 190 | ||
191 | /** |
191 | /** |
192 | * @} |
192 | * @} |
193 | */ |
193 | */ |
194 | 194 | ||
195 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
195 |