提交 | 用户 | age
|
483170
|
1 |
/** |
Q |
2 |
****************************************************************************** |
|
3 |
* @file stm32f0xx_hal_timebase_tim_template.c |
|
4 |
* @brief HAL time base based on the hardware TIM Template. |
|
5 |
* |
|
6 |
* This file override the native HAL time base functions (defined as weak) |
|
7 |
* the TIM time base: |
|
8 |
* + Intializes the TIM peripheral generate a Period elapsed Event each 1ms |
|
9 |
* + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms |
|
10 |
* |
|
11 |
****************************************************************************** |
|
12 |
* @attention |
|
13 |
* |
|
14 |
* <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
|
15 |
* |
|
16 |
* Redistribution and use in source and binary forms, with or without modification, |
|
17 |
* are permitted provided that the following conditions are met: |
|
18 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
19 |
* this list of conditions and the following disclaimer. |
|
20 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
21 |
* this list of conditions and the following disclaimer in the documentation |
|
22 |
* and/or other materials provided with the distribution. |
|
23 |
* 3. Neither the name of STMicroelectronics nor the names of its contributors |
|
24 |
* may be used to endorse or promote products derived from this software |
|
25 |
* without specific prior written permission. |
|
26 |
* |
|
27 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
28 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
29 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
30 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
31 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
32 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
33 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
34 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
35 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
36 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
37 |
* |
|
38 |
****************************************************************************** |
|
39 |
*/ |
|
40 |
|
|
41 |
/* Includes ------------------------------------------------------------------*/ |
|
42 |
#include "stm32f0xx_hal.h" |
|
43 |
|
|
44 |
/** @addtogroup STM32F0xx_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_DAC_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 |
|
|
77 |
/*Configure the TIM6 IRQ priority */ |
|
78 |
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0U); |
|
79 |
|
|
80 |
/* Enable the TIM6 global Interrupt */ |
|
81 |
HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); |
|
82 |
|
|
83 |
/* Enable TIM6 clock */ |
|
84 |
__HAL_RCC_TIM6_CLK_ENABLE(); |
|
85 |
|
|
86 |
/* Get clock configuration */ |
|
87 |
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); |
|
88 |
|
|
89 |
/* Get APB1 prescaler */ |
|
90 |
uwAPB1Prescaler = clkconfig.APB1CLKDivider; |
|
91 |
|
|
92 |
/* Compute TIM6 clock */ |
|
93 |
if (uwAPB1Prescaler == RCC_HCLK_DIV1) |
|
94 |
{ |
|
95 |
uwTimclock = HAL_RCC_GetPCLK1Freq(); |
|
96 |
} |
|
97 |
else |
|
98 |
{ |
|
99 |
uwTimclock = 2*HAL_RCC_GetPCLK1Freq(); |
|
100 |
} |
|
101 |
|
|
102 |
/* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */ |
|
103 |
uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); |
|
104 |
|
|
105 |
/* Initialize TIM6 */ |
|
106 |
TimHandle.Instance = TIM6; |
|
107 |
|
|
108 |
/* Initialize TIMx peripheral as follow: |
|
109 |
+ Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base. |
|
110 |
+ Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. |
|
111 |
+ ClockDivision = 0 |
|
112 |
+ Counter direction = Up |
|
113 |
*/ |
|
114 |
TimHandle.Init.Period = (1000000U / 1000U) - 1U; |
|
115 |
TimHandle.Init.Prescaler = uwPrescalerValue; |
|
116 |
TimHandle.Init.ClockDivision = 0U; |
|
117 |
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
|
118 |
TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
|
119 |
if(HAL_TIM_Base_Init(&TimHandle) == HAL_OK) |
|
120 |
{ |
|
121 |
/* Start the TIM time Base generation in interrupt mode */ |
|
122 |
return HAL_TIM_Base_Start_IT(&TimHandle); |
|
123 |
} |
|
124 |
|
|
125 |
/* Return function status */ |
|
126 |
return HAL_ERROR; |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* @brief Suspend Tick increment. |
|
131 |
* @note Disable the tick increment by disabling TIM6 update interrupt. |
|
132 |
* @param None |
|
133 |
* @retval None |
|
134 |
*/ |
|
135 |
void HAL_SuspendTick(void) |
|
136 |
{ |
|
137 |
/* Disable TIM6 update Interrupt */ |
|
138 |
__HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE); |
|
139 |
} |
|
140 |
|
|
141 |
/** |
|
142 |
* @brief Resume Tick increment. |
|
143 |
* @note Enable the tick increment by Enabling TIM6 update interrupt. |
|
144 |
* @param None |
|
145 |
* @retval None |
|
146 |
*/ |
|
147 |
void HAL_ResumeTick(void) |
|
148 |
{ |
|
149 |
/* Enable TIM6 Update interrupt */ |
|
150 |
__HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE); |
|
151 |
} |
|
152 |
|
|
153 |
/** |
|
154 |
* @brief Period elapsed callback in non blocking mode |
|
155 |
* @note This function is called when TIM6 interrupt took place, inside |
|
156 |
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment |
|
157 |
* a global variable "uwTick" used as application time base. |
|
158 |
* @param htim TIM handle |
|
159 |
* @retval None |
|
160 |
*/ |
|
161 |
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
|
162 |
{ |
|
163 |
HAL_IncTick(); |
|
164 |
} |
|
165 |
|
|
166 |
/** |
|
167 |
* @brief This function handles TIM interrupt request. |
|
168 |
* @param None |
|
169 |
* @retval None |
|
170 |
*/ |
|
171 |
void TIM6_DAC_IRQHandler(void) |
|
172 |
{ |
|
173 |
HAL_TIM_IRQHandler(&TimHandle); |
|
174 |
} |
|
175 |
|
|
176 |
/** |
|
177 |
* @} |
|
178 |
*/ |
|
179 |
|
|
180 |
/** |
|
181 |
* @} |
|
182 |
*/ |
|
183 |
|
|
184 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |