1 /**
2 ******************************************************************************
3 * @file stm32l0xx_hal_tim_ex.c
4 * @author MCD Application Team
5 * @brief TIM HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Timer Extended peripheral:
8 * + Time Master and Slave synchronization configuration
9 * + Timer remapping capabilities configuration
10 @verbatim
11 ==============================================================================
12 ##### TIMER Extended features #####
13 ==============================================================================
14 [..]
15 The Timer Extended features include:
16 (#) Synchronization circuit to control the timer with external signals and to
17 interconnect several timers together.
18
19 @endverbatim
20 ******************************************************************************
21 * @attention
22 *
23 * <h2><center>© Copyright (c) 2016 STMicroelectronics.
24 * All rights reserved.</center></h2>
25 *
26 * This software component is licensed by ST under BSD 3-Clause license,
27 * the "License"; You may not use this file except in compliance with the
28 * License. You may obtain a copy of the License at:
29 * opensource.org/licenses/BSD-3-Clause
30 *
31 ******************************************************************************
32 */
33
34 /* Includes ------------------------------------------------------------------*/
35 #include "stm32l0xx_hal.h"
36
37 /** @addtogroup STM32L0xx_HAL_Driver
38 * @{
39 */
40
41 /** @defgroup TIMEx TIMEx
42 * @brief TIM Extended HAL module driver
43 * @{
44 */
45
46 #ifdef HAL_TIM_MODULE_ENABLED
47
48 /* Private typedef -----------------------------------------------------------*/
49 /* Private define ------------------------------------------------------------*/
50 /* Private macro -------------------------------------------------------------*/
51 /* Private variables ---------------------------------------------------------*/
52 /* Private function prototypes -----------------------------------------------*/
53
54 /* Exported functions --------------------------------------------------------*/
55 /** @defgroup TIMEx_Exported_Functions TIM Extended Exported Functions
56 * @{
57 */
58 /** @defgroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions
59 * @brief Peripheral Control functions
60 *
61 @verbatim
62 ==============================================================================
63 ##### Peripheral Control functions #####
64 ==============================================================================
65 [..]
66 This section provides functions allowing to:
67 (+) Configure Master synchronization.
68 (+) Configure timer remapping capabilities.
69
70 @endverbatim
71 * @{
72 */
73
74 /**
75 * @brief Configures the TIM in master mode.
76 * @param htim TIM handle.
77 * @param sMasterConfig pointer to a TIM_MasterConfigTypeDef structure that
78 * contains the selected trigger output (TRGO) and the Master/Slave
79 * mode.
80 * @retval HAL status
81 */
HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef * htim,TIM_MasterConfigTypeDef * sMasterConfig)82 HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim,
83 TIM_MasterConfigTypeDef *sMasterConfig)
84 {
85 uint32_t tmpcr2;
86 uint32_t tmpsmcr;
87
88 /* Check the parameters */
89 assert_param(IS_TIM_SYNCHRO_INSTANCE(htim->Instance));
90 assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
91 assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
92
93 /* Check input state */
94 __HAL_LOCK(htim);
95
96 /* Change the handler state */
97 htim->State = HAL_TIM_STATE_BUSY;
98
99 /* Get the TIMx CR2 register value */
100 tmpcr2 = htim->Instance->CR2;
101
102 /* Get the TIMx SMCR register value */
103 tmpsmcr = htim->Instance->SMCR;
104
105 /* Reset the MMS Bits */
106 tmpcr2 &= ~TIM_CR2_MMS;
107 /* Select the TRGO source */
108 tmpcr2 |= sMasterConfig->MasterOutputTrigger;
109
110 /* Reset the MSM Bit */
111 tmpsmcr &= ~TIM_SMCR_MSM;
112 /* Set master mode */
113 tmpsmcr |= sMasterConfig->MasterSlaveMode;
114
115 /* Update TIMx CR2 */
116 htim->Instance->CR2 = tmpcr2;
117
118 /* Update TIMx SMCR */
119 htim->Instance->SMCR = tmpsmcr;
120
121 /* Change the htim state */
122 htim->State = HAL_TIM_STATE_READY;
123
124 __HAL_UNLOCK(htim);
125
126 return HAL_OK;
127 }
128
129 /**
130 * @brief Configures the TIMx Remapping input capabilities.
131 @if STM32L073xx
132 * @note It is not possible to connect TIM2 and TIM21 on PB5(AF4) at the same time.
133 * When selecting TIM3_TI2_GPIOB5_AF4, Channel2 of TIM3 will be
134 * connected to PB5(AF4) and Channel2 of TIM21 will be connected to
135 * some other GPIOs. (refer to alternate functions for more details)
136 * When selecting TIM3_TI2_GPIO_DEF, Channel2 of Timer 3 will be
137 * connected an GPIO (other than PB5(AF4)) and Channel2 of TIM21
138 * will be connected to PB5(AF4).
139 * @note When TIM2 ETR is fed with HSI48, this ETR must be prescaled internally
140 * to the TIMER2 because the maximum system frequency is 32 MHz
141 @endif
142 * @param htim TIM handle.
143 * @param Remap specifies the TIM remapping source.
144 @if STM32L073xx
145 * For TIM2, the parameter is a combination of 2 fields (field1 | field2):
146 *
147 * field1 can have the following values:
148 * @arg TIM2_ETR_GPIO: TIM2 ETR connected to GPIO (default):
149 * PA0(AF5) or PA5(AF2) or PA15(AF2) or PE9(AF2)
150 * @arg TIM2_ETR_HSI48: TIM2 ETR connected to HSI48
151 * @arg TIM2_ETR_HSI16: TIM2 ETR connected to HSI16
152 * @arg TIM2_ETR_LSE: TIM2 ETR connected to LSE
153 * @arg TIM2_ETR_COMP2_OUT: TIM2 ETR connected to COMP2 output
154 * @arg TIM2_ETR_COMP1_OUT: TIM2 ETR connected to COMP1 output
155 *
156 * field2 can have the following values:
157 * @arg TIM2_TI4_GPIO : TIM2 TI4 connected to GPIO1(default):
158 * PA3(AF2) or PB11(AF2) or PE12(AF0)
159 * @arg TIM2_TI4_COMP1: TIM2 TI4 connected to COMP1
160 * @arg TIM2_TI4_COMP2: TIM2 TI4 connected to COMP2
161 @endif
162 @if STM32L031xx
163 * For TIM2, the parameter is a combination of 2 fields (field1 | field2):
164 *
165 * field1 can have the following values:
166 * @arg TIM2_ETR_GPIO: TIM2 ETR connected to GPIO (default):
167 * PA0(AF5) or PA5(AF2) or PA15(AF2)
168 * @arg TIM2_ETR_HSI16: TIM2 ETR connected to HS16 (HSIOUT)
169 * @arg TIM2_ETR_LSE: TIM2 ETR connected to LSE
170 * @arg TIM2_ETR_COMP2_OUT: TIM2 ETR connected to COMP2 output
171 * @arg TIM2_ETR_COMP1_OUT: TIM2 ETR connected to COMP1 output
172 *
173 * field2 can have the following values:
174 * @arg TIM2_TI4_GPIO : TIM2 TI4 connected to GPIO (default):
175 * PA3(AF2) or PB11(AF2) or PB1(AF5)
176 * @arg TIM2_TI4_COMP1_OUT: TIM2 TI4 connected to COMP1 output
177 * @arg TIM2_TI4_COMP2_OUT: TIM2 TI4 connected to COMP2 output
178 @endif
179 @if STM32L011xx
180 * For TIM2, the parameter is a combination of 2 fields (field1 | field2):
181 *
182 * field1 can have the following values:
183 * @arg TIM2_ETR_GPIO: TIM2 ETR connected to GPIO (default):
184 * PA0(AF5) or PA5(AF2) or PA15(AF2)
185 * @arg TIM2_ETR_HSI16: TIM2 ETR connected to HS16 (HSIOUT)
186 * @arg TIM2_ETR_LSE: TIM2 ETR connected to LSE
187 * @arg TIM2_ETR_COMP2_OUT: TIM2 ETR connected to COMP2 output
188 * @arg TIM2_ETR_COMP1_OUT: TIM2 ETR connected to COMP1 output
189 *
190 * field2 can have the following values:
191 * @arg TIM2_TI4_GPIO : TIM2 TI4 connected to GPIO (default):
192 * PA3(AF2) or PB11(AF2) or PB1(AF5)
193 * @arg TIM2_TI4_COMP1_OUT: TIM2 TI4 connected to COMP1 output
194 * @arg TIM2_TI4_COMP2_OUT: TIM2 TI4 connected to COMP2 output
195 @endif
196 @if STM32L051xx
197 * For TIM2, the parameter is a combination of 2 fields (field1 | field2):
198 *
199 * field1 can have the following values:
200 * @arg TIM2_ETR_GPIO: TIM2 ETR connected to GPIO (default):
201 * PA0(AF5) or PA5(AF2) or PA15(AF2) or PE9(AF2)
202 * @arg TIM2_ETR_HSI48: TIM2 ETR connected to HSI48
203 * @arg TIM2_ETR_LSE: TIM2 ETR connected to LSE
204 * @arg TIM2_ETR_COMP2_OUT: TIM2 ETR connected to COMP2 output
205 * @arg TIM2_ETR_COMP1_OUT: TIM2 ETR connected to COMP1 output
206 *
207 * field2 can have the following values:
208 * @arg TIM2_TI4_GPIO: TIM2 TI4 connected to GPIO1(default):
209 * PA3(AF2) or PB11(AF2) or PE12(AF0)
210 * @arg TIM2_TI4_COMP1: TIM2 TI4 connected to COMP1
211 * @arg TIM2_TI4_COMP2: TIM2 TI4 connected to COMP2
212 * @arg TIM2_TI4_GPIO2: TIM2 TI4 connected to GPIO2 :
213 * PA3(AF2) or PB11(AF2) or PE12(AF0)
214 @endif
215 @if STM32L073xx
216 *
217 * For TIM3, the parameter is a combination of 4 fields (field1 | field2 | field3 | field4):
218 *
219 * field1 can have the following values:
220 * @arg TIM3_ETR_GPIO: TIM3 ETR connected to GPIO (default):
221 * PE2(AF2) or PD2(AF2) or PE2(AF2)
222 * @arg TIM3_ETR_HSI: TIM3 ETR connected to HSI
223 *
224 * field2 can have the following values:
225 * @arg TIM3_TI1_USB_SOF: TIM3 TI1 connected to USB_SOF (default)
226 * @arg TIM3_TI1_GPIO: TIM3 TI1 connected to GPIO :
227 * PE3(AF2) or PA6(AF2) or PC6(AF2) or PB4(AF2)
228 *
229 * field3 can have the following values:
230 * @arg TIM3_TI2_GPIOB5_AF4:TIM3 TI3 connected to P5(AF4)
231 * (refer to note)
232 * @arg TIM3_TI2_GPIO_DEF: TIM3 TI3 connected to GPIO (default):
233 * PA7(AF2) or PB5(AF4) or PC7(AF2) or PE7(AF2)
234 *
235 * field4 can have the following values:
236 * @arg TIM3_TI4_GPIO_DEF: TIM3 TI4 connected to GPIO:
237 * PB1(AF2) or PE6(AF2)
238 * @arg TIM3_TI4_GPIOC9_AF2:TIM3 TI4 connected to PC9(AF)2
239 @endif
240 @if STM32L073xx
241 * For TIM21, the parameter is a combination of 3 fields (field1 | field2 | field3):
242 *
243 * field1 can have the following values:
244 * @arg TIM21_ETR_GPIO: TIM21 ETR connected to GPIO(default) :
245 * PC9(AF0) or PA1(AF5)
246 * @arg TIM21_ETR_COMP2_OUT:TIM21 ETR connected to COMP2 output
247 * @arg TIM21_ETR_COMP1_OUT:TIM21 ETR connected to COMP1 output
248 * @arg TIM21_ETR_LSE: TIM21 ETR connected to LSE
249 *
250 * field2 can have the following values:
251 * @arg TIM21_TI1_MCO: TIM21 TI1 connected to MCO
252 * @arg TIM21_TI1_RTC_WKUT_IT: TIM21 TI1 connected to RTC WAKEUP interrupt
253 * @arg TIM21_TI1_HSE_RTC: TIM21 TI1 connected to HSE_RTC
254 * @arg TIM21_TI1_MSI: TIM21 TI1 connected to MSI clock
255 * @arg TIM21_TI1_LSE: TIM21 TI1 connected to LSE
256 * @arg TIM21_TI1_LSI: TIM21 TI1 connected to LSI
257 * @arg TIM21_TI1_COMP1_OUT:TIM21 TI1 connected to COMP1_OUT
258 * @arg TIM21_TI1_GPIO: TIM21 TI1 connected to GPIO(default):
259 * PA2(AF0) or PB13(AF6) or PE5(AF0) or PD0(AF0)
260 *
261 * field3 can have the following values:
262 * @arg TIM21_TI2_GPIO: TIM21 TI2 connected to GPIO(default):
263 * PA3(AF0) or PB14(AF6) or PE6(AF0) or PD7(AF1)
264 * @arg TIM21_TI2_COMP2_OUT:TIM21 TI2 connected to COMP2 output
265 @endif
266 @if STM32L031xx
267 * For TIM21, the parameter is a combination of 3 fields (field1 | field2 | field3):
268 *
269 * field1 can have the following values:
270 * @arg TIM21_ETR_GPIO: TIM21 ETR connected to GPIO(default) :
271 * PA1(AF5)
272 * @arg TIM21_ETR_COMP2_OUT:TIM21 ETR connected to COMP2 output
273 * @arg TIM21_ETR_COMP1_OUT:TIM21 ETR connected to COMP1 output
274 * @arg TIM21_ETR_LSE: TIM21 ETR connected to LSE
275 *
276 * field2 can have the following values:
277 * @arg TIM21_TI1_MCO: TIM21 TI1 connected to MCO
278 * @arg TIM21_TI1_RTC_WKUT_IT: TIM21 TI1 connected to RTC WAKEUP interrupt
279 * @arg TIM21_TI1_HSE_RTC: TIM21 TI1 connected to HSE_RTC
280 * @arg TIM21_TI1_MSI: TIM21 TI1 connected to MSI clock
281 * @arg TIM21_TI1_LSE: TIM21 TI1 connected to LSE
282 * @arg TIM21_TI1_LSI: TIM21 TI1 connected to LSI
283 * @arg TIM21_TI1_COMP1_OUT:TIM21 TI1 connected to COMP1_OUT
284 *
285 * field3 can have the following values:
286 * @arg TIM21_TI2_GPIO: TIM21 TI2 connected to GPIO(default):
287 * PA3(AF0) or PB14(AF6)
288 * @arg TIM21_TI2_COMP2_OUT:TIM21 TI2 connected to COMP2 output
289 @endif
290 @if STM32L011xx
291 * For TIM21, the parameter is a combination of 3 fields (field1 | field2 | field3):
292 *
293 * field1 can have the following values:
294 * @arg TIM21_ETR_GPIO: TIM21 ETR connected to GPIO(default) :
295 * PA1(AF5)
296 * @arg TIM21_ETR_COMP2_OUT:TIM21 ETR connected to COMP2 output
297 * @arg TIM21_ETR_COMP1_OUT:TIM21 ETR connected to COMP1 output
298 * @arg TIM21_ETR_LSE: TIM21 ETR connected to LSE
299 *
300 * field2 can have the following values:
301 * @arg TIM21_TI1_MCO: TIM21 TI1 connected to MCO
302 * @arg TIM21_TI1_RTC_WKUT_IT: TIM21 TI1 connected to RTC WAKEUP interrupt
303 * @arg TIM21_TI1_HSE_RTC: TIM21 TI1 connected to HSE_RTC
304 * @arg TIM21_TI1_MSI: TIM21 TI1 connected to MSI clock
305 * @arg TIM21_TI1_LSE: TIM21 TI1 connected to LSE
306 * @arg TIM21_TI1_LSI: TIM21 TI1 connected to LSI
307 * @arg TIM21_TI1_COMP1_OUT:TIM21 TI1 connected to COMP1_OUT
308 *
309 * field3 can have the following values:
310 * @arg TIM21_TI2_GPIO: TIM21 TI2 connected to GPIO(default):
311 * PA3(AF0) or PB14(AF6)
312 * @arg TIM21_TI2_COMP2_OUT:TIM21 TI2 connected to COMP2 output
313 @endif
314 @if STM32L051xx
315 * For TIM21, the parameter is a combination of 3 fields (field1 | field2 | field3):
316 *
317 * field1 can have the following values:
318 * @arg TIM21_ETR_GPIO: TIM21 ETR connected to GPIO(default) :
319 * PC9(AF0) or PA1(AF5)
320 * @arg TIM21_ETR_COMP2_OUT:TIM21 ETR connected to COMP2 output
321 * @arg TIM21_ETR_COMP1_OUT:TIM21 ETR connected to COMP1 output
322 * @arg TIM21_ETR_LSE: TIM21 ETR connected to LSE
323 *
324 * field2 can have the following values:
325 * @arg TIM21_TI1_MCO: TIM21 TI1 connected to MCO
326 * @arg TIM21_TI1_RTC_WKUT_IT: TIM21 TI1 connected to RTC WAKEUP interrupt
327 * @arg TIM21_TI1_HSE_RTC: TIM21 TI1 connected to HSE_RTC
328 * @arg TIM21_TI1_MSI: TIM21 TI1 connected to MSI clock
329 * @arg TIM21_TI1_LSE: TIM21 TI1 connected to LSE
330 * @arg TIM21_TI1_LSI: TIM21 TI1 connected to LSI
331 * @arg TIM21_TI1_COMP1_OUT:TIM21 TI1 connected to COMP1_OUT
332 * @arg TIM21_TI1_GPIO: TIM21 TI1 connected to GPIO(default):
333 * PA2(AF0) or PB13(AF6) or PE5(AF0) or PD0(AF0)
334 *
335 * field3 can have the following values:
336 * @arg TIM21_TI2_GPIO: TIM21 TI2 connected to GPIO(default):
337 * PA3(AF0) or PB14(AF6) or PE6(AF0) or PD7(AF1)
338 * @arg TIM21_TI2_COMP2_OUT:TIM21 TI2 connected to COMP2 output
339 @endif
340 @if STM32L073xx
341 *
342 * For TIM22, the parameter can have the following values:
343 * @arg TIM22_ETR_LSE: TIM22 ETR connected to LSE
344 * @arg TIM22_ETR_COMP2_OUT:TIM22 ETR connected to COMP2 output
345 * @arg TIM22_ETR_COMP1_OUT:TIM22 ETR connected to COMP1 output
346 * @arg TIM22_ETR_GPIO: TIM22 ETR connected to GPIO(default):
347 * PC8(AF0) or PA4(AF5)
348 * @arg TIM22_TI1_GPIO: TIM22 TI1 connected to GPIO(default):
349 * PC6(AF0) or PA6(AF5) or PB4(AF4) or PE0(AF3)
350 * @arg TIM22_TI1_COMP2_OUT:TIM22 TI1 connected to COMP2 output
351 * @arg TIM22_TI1_COMP1_OUT:TIM22 TI1 connected to COMP1 output
352 @endif
353 @if STM32L031xx
354 *
355 * For TIM22, the parameter is a combination of 2 fields (field1 | field2):
356 *
357 * field1 can have the following values:
358 * @arg TIM22_ETR_LSE: TIM22 ETR connected to LSE
359 * @arg TIM22_ETR_COMP2_OUT:TIM22 ETR connected to COMP2 output
360 * @arg TIM22_ETR_COMP1_OUT:TIM22 ETR connected to COMP1 output
361 * @arg TIM22_ETR_GPIO: TIM22 ETR connected to GPIO(default):
362 * PA4(AF5)
363 *
364 * field2 can have the following values:
365 * @arg TIM22_TI1_GPIO: TIM22 TI1 connected to GPIO(default):
366 * PC0(AF6) or PA5(AF6) or PB4(AF4)
367 * @arg TIM22_TI1_COMP2_OUT:TIM22 TI1 connected to COMP2 output
368 * @arg TIM22_TI1_COMP1_OUT:TIM22 TI1 connected to COMP1 output
369 *
370 @endif
371 @if STM32L051xx
372 *
373 * For TIM22, the parameter is a combination of 2 fields (field1 | field2):
374 *
375 * field1 can have the following values:
376 * @arg TIM22_ETR_LSE: TIM22 ETR connected to LSE
377 * @arg TIM22_ETR_COMP2_OUT:TIM22 ETR connected to COMP2 output
378 * @arg TIM22_ETR_COMP1_OUT:TIM22 ETR connected to COMP1 output
379 * @arg TIM22_ETR_GPIO: TIM22 ETR connected to GPIO(default):
380 * PC8(AF0) or PA4(AF5)
381 *
382 * field2 can have the following values:
383 * @arg TIM22_TI1_GPIO: TIM22 TI1 connected to GPIO(default):
384 * PC6(AF0) or PA6(AF5) or PB4(AF4) or PE0(AF3)
385 * @arg TIM22_TI1_COMP2_OUT:TIM22 TI1 connected to COMP2 output
386 * @arg TIM22_TI1_COMP1_OUT:TIM22 TI1 connected to COMP1 output
387 @endif
388 *
389 * @retval HAL status
390 */
HAL_TIMEx_RemapConfig(TIM_HandleTypeDef * htim,uint32_t Remap)391 HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
392 {
393 __HAL_LOCK(htim);
394
395 /* Check parameters */
396 assert_param(IS_TIM_REMAP(htim->Instance, Remap));
397
398 /* Set the Timer remapping configuration */
399 WRITE_REG(htim->Instance->OR, Remap);
400
401 __HAL_UNLOCK(htim);
402
403 return HAL_OK;
404 }
405
406 /**
407 * @}
408 */
409
410 /**
411 * @}
412 */
413
414
415 #endif /* HAL_TIM_MODULE_ENABLED */
416 /**
417 * @}
418 */
419
420 /**
421 * @}
422 */
423
424 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
425