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   ******************************************************************************
11   * @attention
12   *
13   * Copyright (c) 2016 STMicroelectronics.
14   * All rights reserved.
15   *
16   * This software is licensed under terms that can be found in the LICENSE file
17   * in the root directory of this software component.
18   * If no LICENSE file comes with this software, it is provided AS-IS.
19   *
20   ******************************************************************************
21   @verbatim
22   ==============================================================================
23                       ##### TIMER Extended features #####
24   ==============================================================================
25   [..]
26     The Timer Extended features include:
27     (#) Synchronization circuit to control the timer with external signals and to
28         interconnect several timers together.
29 
30   @endverbatim
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 macros ------------------------------------------------------------*/
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,const TIM_MasterConfigTypeDef * sMasterConfig)82 HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim,
83                                                         const TIM_MasterConfigTypeDef *sMasterConfig)
84 {
85   uint32_t tmpcr2;
86   uint32_t tmpsmcr;
87 
88   /* Check the parameters */
89   assert_param(IS_TIM_MASTER_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   /* Update TIMx CR2 */
111   htim->Instance->CR2 = tmpcr2;
112 
113   if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
114   {
115     /* Reset the MSM Bit */
116     tmpsmcr &= ~TIM_SMCR_MSM;
117     /* Set master mode */
118     tmpsmcr |= sMasterConfig->MasterSlaveMode;
119 
120     /* Update TIMx SMCR */
121     htim->Instance->SMCR = tmpsmcr;
122   }
123 
124   /* Change the htim state */
125   htim->State = HAL_TIM_STATE_READY;
126 
127   __HAL_UNLOCK(htim);
128 
129   return HAL_OK;
130 }
131 
132 /**
133   * @brief  Configures the TIMx Remapping input capabilities.
134   @if STM32L073xx
135   * @note   It is not possible to connect TIM2 and TIM21 on PB5(AF4) at the same time.
136   *         When selecting TIM3_TI2_GPIOB5_AF4, Channel2 of TIM3 will be
137   *         connected to PB5(AF4) and Channel2 of TIM21 will be connected to
138   *         some other GPIOs. (refer to alternate functions for more details)
139   *         When selecting TIM3_TI2_GPIO_DEF, Channel2 of Timer 3 will be
140   *         connected an GPIO (other than PB5(AF4)) and Channel2 of TIM21
141   *         will be connected to PB5(AF4).
142   * @note   When TIM2 ETR is fed with HSI48, this ETR must be prescaled internally
143   *         to the TIMER2 because the maximum system frequency is 32 MHz
144   @endif
145   * @param  htim TIM handle.
146   * @param  Remap specifies the TIM remapping source.
147   @if STM32L073xx
148   *         For TIM2, the parameter is a combination of 2 fields (field1 | field2):
149   *
150   *                   field1 can have the following values:
151   *           @arg TIM2_ETR_GPIO:      TIM2  ETR connected to GPIO (default):
152   *                                    PA0(AF5) or PA5(AF2) or PA15(AF2) or PE9(AF2)
153   *           @arg TIM2_ETR_HSI48:     TIM2  ETR connected to HSI48
154   *           @arg TIM2_ETR_HSI16:     TIM2  ETR connected to HSI16
155   *           @arg TIM2_ETR_LSE:       TIM2  ETR connected to LSE
156   *           @arg TIM2_ETR_COMP2_OUT: TIM2  ETR connected to COMP2 output
157   *           @arg TIM2_ETR_COMP1_OUT: TIM2  ETR connected to COMP1 output
158   *
159   *                   field2 can have the following values:
160   *           @arg TIM2_TI4_GPIO :     TIM2  TI4 connected to GPIO1(default):
161   *                                    PA3(AF2) or PB11(AF2) or PE12(AF0)
162   *           @arg TIM2_TI4_COMP1:     TIM2  TI4 connected to COMP1
163   *           @arg TIM2_TI4_COMP2:     TIM2  TI4 connected to COMP2
164   @endif
165   @if STM32L031xx
166   *         For TIM2, the parameter is a combination of 2 fields (field1 | field2):
167   *
168   *                   field1 can have the following values:
169   *           @arg TIM2_ETR_GPIO:      TIM2  ETR connected to GPIO (default):
170   *                                    PA0(AF5) or PA5(AF2) or PA15(AF2)
171   *           @arg TIM2_ETR_HSI16:     TIM2  ETR connected to HS16 (HSIOUT)
172   *           @arg TIM2_ETR_LSE:       TIM2  ETR connected to LSE
173   *           @arg TIM2_ETR_COMP2_OUT: TIM2  ETR connected to COMP2 output
174   *           @arg TIM2_ETR_COMP1_OUT: TIM2  ETR connected to COMP1 output
175   *
176   *                   field2 can have the following values:
177   *           @arg TIM2_TI4_GPIO :     TIM2  TI4 connected to GPIO (default):
178   *                                    PA3(AF2) or PB11(AF2) or PB1(AF5)
179   *           @arg TIM2_TI4_COMP1_OUT: TIM2  TI4 connected to COMP1 output
180   *           @arg TIM2_TI4_COMP2_OUT: TIM2  TI4 connected to COMP2 output
181   @endif
182   @if STM32L011xx
183   *         For TIM2, the parameter is a combination of 2 fields (field1 | field2):
184   *
185   *                   field1 can have the following values:
186   *           @arg TIM2_ETR_GPIO:      TIM2  ETR connected to GPIO (default):
187   *                                    PA0(AF5) or PA5(AF2) or PA15(AF2)
188   *           @arg TIM2_ETR_HSI16:     TIM2  ETR connected to HS16 (HSIOUT)
189   *           @arg TIM2_ETR_LSE:       TIM2  ETR connected to LSE
190   *           @arg TIM2_ETR_COMP2_OUT: TIM2  ETR connected to COMP2 output
191   *           @arg TIM2_ETR_COMP1_OUT: TIM2  ETR connected to COMP1 output
192   *
193   *                   field2 can have the following values:
194   *           @arg TIM2_TI4_GPIO :     TIM2  TI4 connected to GPIO (default):
195   *                                    PA3(AF2) or PB11(AF2) or PB1(AF5)
196   *           @arg TIM2_TI4_COMP1_OUT: TIM2  TI4 connected to COMP1 output
197   *           @arg TIM2_TI4_COMP2_OUT: TIM2  TI4 connected to COMP2 output
198   @endif
199   @if STM32L051xx
200   *         For TIM2, the parameter is a combination of 2 fields (field1 | field2):
201   *
202   *                   field1 can have the following values:
203   *           @arg TIM2_ETR_GPIO:      TIM2  ETR connected to GPIO (default):
204   *                                    PA0(AF5) or PA5(AF2) or PA15(AF2) or PE9(AF2)
205   *           @arg TIM2_ETR_HSI48:     TIM2  ETR connected to HSI48
206   *           @arg TIM2_ETR_LSE:       TIM2  ETR connected to LSE
207   *           @arg TIM2_ETR_COMP2_OUT: TIM2  ETR connected to COMP2 output
208   *           @arg TIM2_ETR_COMP1_OUT: TIM2  ETR connected to COMP1 output
209   *
210   *                   field2 can have the following values:
211   *           @arg TIM2_TI4_GPIO:      TIM2  TI4 connected to GPIO1(default):
212   *                                    PA3(AF2) or PB11(AF2) or PE12(AF0)
213   *           @arg TIM2_TI4_COMP1:     TIM2  TI4 connected to COMP1
214   *           @arg TIM2_TI4_COMP2:     TIM2  TI4 connected to COMP2
215   *           @arg TIM2_TI4_GPIO2:     TIM2  TI4 connected to GPIO2 :
216   *                                    PA3(AF2) or PB11(AF2) or PE12(AF0)
217   @endif
218   @if STM32L073xx
219   *
220   *         For TIM3, the parameter is a combination of 4 fields (field1 | field2 | field3 | field4):
221   *
222   *                   field1 can have the following values:
223   *           @arg TIM3_ETR_GPIO:      TIM3  ETR connected to GPIO (default):
224   *                                    PE2(AF2) or PD2(AF2) or PE2(AF2)
225   *           @arg TIM3_ETR_HSI:       TIM3 ETR connected to HSI
226   *
227   *                   field2 can have the following values:
228   *           @arg TIM3_TI1_USB_SOF:   TIM3 TI1 connected to USB_SOF (default)
229   *           @arg TIM3_TI1_GPIO:      TIM3 TI1 connected to GPIO :
230   *                                    PE3(AF2) or PA6(AF2) or PC6(AF2) or PB4(AF2)
231   *
232   *                   field3 can have the following values:
233   *           @arg TIM3_TI2_GPIOB5_AF4:TIM3 TI3 connected to P5(AF4)
234   *                                    (refer to note)
235   *           @arg TIM3_TI2_GPIO_DEF:  TIM3 TI3 connected to GPIO (default):
236   *                                    PA7(AF2) or PB5(AF4) or PC7(AF2) or PE7(AF2)
237   *
238   *                   field4 can have the following values:
239   *           @arg TIM3_TI4_GPIO_DEF:  TIM3 TI4 connected to GPIO:
240   *                                    PB1(AF2) or PE6(AF2)
241   *           @arg TIM3_TI4_GPIOC9_AF2:TIM3 TI4 connected to PC9(AF)2
242   @endif
243   @if STM32L073xx
244   *         For TIM21, the parameter is a combination of 3 fields (field1 | field2 | field3):
245   *
246   *                   field1 can have the following values:
247   *           @arg TIM21_ETR_GPIO:     TIM21 ETR connected to GPIO(default) :
248   *                                    PC9(AF0) or PA1(AF5)
249   *           @arg TIM21_ETR_COMP2_OUT:TIM21 ETR connected to COMP2 output
250   *           @arg TIM21_ETR_COMP1_OUT:TIM21 ETR connected to COMP1 output
251   *           @arg TIM21_ETR_LSE:      TIM21 ETR connected to LSE
252   *
253   *                   field2 can have the following values:
254   *           @arg TIM21_TI1_MCO:      TIM21 TI1 connected to MCO
255   *           @arg TIM21_TI1_RTC_WKUT_IT: TIM21 TI1 connected to RTC WAKEUP interrupt
256   *           @arg TIM21_TI1_HSE_RTC:  TIM21 TI1 connected to HSE_RTC
257   *           @arg TIM21_TI1_MSI:      TIM21 TI1 connected to MSI clock
258   *           @arg TIM21_TI1_LSE:      TIM21 TI1 connected to LSE
259   *           @arg TIM21_TI1_LSI:      TIM21 TI1 connected to LSI
260   *           @arg TIM21_TI1_COMP1_OUT:TIM21 TI1 connected to COMP1_OUT
261   *           @arg TIM21_TI1_GPIO:     TIM21 TI1 connected to GPIO(default):
262   *                                    PA2(AF0) or PB13(AF6) or PE5(AF0) or PD0(AF0)
263   *
264   *                   field3 can have the following values:
265   *           @arg TIM21_TI2_GPIO:     TIM21 TI2 connected to GPIO(default):
266   *                                    PA3(AF0) or PB14(AF6) or PE6(AF0) or PD7(AF1)
267   *           @arg TIM21_TI2_COMP2_OUT:TIM21 TI2 connected to COMP2 output
268   @endif
269   @if STM32L031xx
270   *         For TIM21, the parameter is a combination of 3 fields (field1 | field2 | field3):
271   *
272   *                   field1 can have the following values:
273   *           @arg TIM21_ETR_GPIO:     TIM21 ETR connected to GPIO(default) :
274   *                                    PA1(AF5)
275   *           @arg TIM21_ETR_COMP2_OUT:TIM21 ETR connected to COMP2 output
276   *           @arg TIM21_ETR_COMP1_OUT:TIM21 ETR connected to COMP1 output
277   *           @arg TIM21_ETR_LSE:      TIM21 ETR connected to LSE
278   *
279   *                   field2 can have the following values:
280   *           @arg TIM21_TI1_MCO:      TIM21 TI1 connected to MCO
281   *           @arg TIM21_TI1_RTC_WKUT_IT: TIM21 TI1 connected to RTC WAKEUP interrupt
282   *           @arg TIM21_TI1_HSE_RTC:  TIM21 TI1 connected to HSE_RTC
283   *           @arg TIM21_TI1_MSI:      TIM21 TI1 connected to MSI clock
284   *           @arg TIM21_TI1_LSE:      TIM21 TI1 connected to LSE
285   *           @arg TIM21_TI1_LSI:      TIM21 TI1 connected to LSI
286   *           @arg TIM21_TI1_COMP1_OUT:TIM21 TI1 connected to COMP1_OUT
287   *
288   *                   field3 can have the following values:
289   *           @arg TIM21_TI2_GPIO:     TIM21 TI2 connected to GPIO(default):
290   *                                    PA3(AF0) or PB14(AF6)
291   *           @arg TIM21_TI2_COMP2_OUT:TIM21 TI2 connected to COMP2 output
292   @endif
293   @if STM32L011xx
294   *         For TIM21, the parameter is a combination of 3 fields (field1 | field2 | field3):
295   *
296   *                   field1 can have the following values:
297   *           @arg TIM21_ETR_GPIO:     TIM21 ETR connected to GPIO(default) :
298   *                                    PA1(AF5)
299   *           @arg TIM21_ETR_COMP2_OUT:TIM21 ETR connected to COMP2 output
300   *           @arg TIM21_ETR_COMP1_OUT:TIM21 ETR connected to COMP1 output
301   *           @arg TIM21_ETR_LSE:      TIM21 ETR connected to LSE
302   *
303   *                   field2 can have the following values:
304   *           @arg TIM21_TI1_MCO:      TIM21 TI1 connected to MCO
305   *           @arg TIM21_TI1_RTC_WKUT_IT: TIM21 TI1 connected to RTC WAKEUP interrupt
306   *           @arg TIM21_TI1_HSE_RTC:  TIM21 TI1 connected to HSE_RTC
307   *           @arg TIM21_TI1_MSI:      TIM21 TI1 connected to MSI clock
308   *           @arg TIM21_TI1_LSE:      TIM21 TI1 connected to LSE
309   *           @arg TIM21_TI1_LSI:      TIM21 TI1 connected to LSI
310   *           @arg TIM21_TI1_COMP1_OUT:TIM21 TI1 connected to COMP1_OUT
311   *
312   *                   field3 can have the following values:
313   *           @arg TIM21_TI2_GPIO:     TIM21 TI2 connected to GPIO(default):
314   *                                    PA3(AF0) or PB14(AF6)
315   *           @arg TIM21_TI2_COMP2_OUT:TIM21 TI2 connected to COMP2 output
316   @endif
317   @if STM32L051xx
318   *         For TIM21, the parameter is a combination of 3 fields (field1 | field2 | field3):
319   *
320   *                   field1 can have the following values:
321   *           @arg TIM21_ETR_GPIO:     TIM21 ETR connected to GPIO(default) :
322   *                                    PC9(AF0) or PA1(AF5)
323   *           @arg TIM21_ETR_COMP2_OUT:TIM21 ETR connected to COMP2 output
324   *           @arg TIM21_ETR_COMP1_OUT:TIM21 ETR connected to COMP1 output
325   *           @arg TIM21_ETR_LSE:      TIM21 ETR connected to LSE
326   *
327   *                   field2 can have the following values:
328   *           @arg TIM21_TI1_MCO:      TIM21 TI1 connected to MCO
329   *           @arg TIM21_TI1_RTC_WKUT_IT: TIM21 TI1 connected to RTC WAKEUP interrupt
330   *           @arg TIM21_TI1_HSE_RTC:  TIM21 TI1 connected to HSE_RTC
331   *           @arg TIM21_TI1_MSI:      TIM21 TI1 connected to MSI clock
332   *           @arg TIM21_TI1_LSE:      TIM21 TI1 connected to LSE
333   *           @arg TIM21_TI1_LSI:      TIM21 TI1 connected to LSI
334   *           @arg TIM21_TI1_COMP1_OUT:TIM21 TI1 connected to COMP1_OUT
335   *           @arg TIM21_TI1_GPIO:     TIM21 TI1 connected to GPIO(default):
336   *                                    PA2(AF0) or PB13(AF6) or PE5(AF0) or PD0(AF0)
337   *
338   *                   field3 can have the following values:
339   *           @arg TIM21_TI2_GPIO:     TIM21 TI2 connected to GPIO(default):
340   *                                    PA3(AF0) or PB14(AF6) or PE6(AF0) or PD7(AF1)
341   *           @arg TIM21_TI2_COMP2_OUT:TIM21 TI2 connected to COMP2 output
342   @endif
343   @if STM32L073xx
344   *
345   *         For TIM22, the parameter can have the following values:
346   *           @arg TIM22_ETR_LSE:      TIM22 ETR connected to LSE
347   *           @arg TIM22_ETR_COMP2_OUT:TIM22 ETR connected to COMP2 output
348   *           @arg TIM22_ETR_COMP1_OUT:TIM22 ETR connected to COMP1 output
349   *           @arg TIM22_ETR_GPIO:     TIM22 ETR connected to GPIO(default):
350   *                                    PC8(AF0) or PA4(AF5)
351   *           @arg TIM22_TI1_GPIO:     TIM22 TI1 connected to GPIO(default):
352   *                                    PC6(AF0) or PA6(AF5) or PB4(AF4) or PE0(AF3)
353   *           @arg TIM22_TI1_COMP2_OUT:TIM22 TI1 connected to COMP2 output
354   *           @arg TIM22_TI1_COMP1_OUT:TIM22 TI1 connected to COMP1 output
355   @endif
356   @if STM32L031xx
357   *
358   *         For TIM22, the parameter is a combination of 2 fields (field1 | field2):
359   *
360   *                   field1 can have the following values:
361   *           @arg TIM22_ETR_LSE:      TIM22 ETR connected to LSE
362   *           @arg TIM22_ETR_COMP2_OUT:TIM22 ETR connected to COMP2 output
363   *           @arg TIM22_ETR_COMP1_OUT:TIM22 ETR connected to COMP1 output
364   *           @arg TIM22_ETR_GPIO:     TIM22 ETR connected to GPIO(default):
365   *                                    PA4(AF5)
366   *
367   *                   field2 can have the following values:
368   *           @arg TIM22_TI1_GPIO:     TIM22 TI1 connected to GPIO(default):
369   *                                    PC0(AF6) or PA5(AF6) or PB4(AF4)
370   *           @arg TIM22_TI1_COMP2_OUT:TIM22 TI1 connected to COMP2 output
371   *           @arg TIM22_TI1_COMP1_OUT:TIM22 TI1 connected to COMP1 output
372   *
373   @endif
374   @if STM32L051xx
375   *
376   *         For TIM22, the parameter is a combination of 2 fields (field1 | field2):
377   *
378   *                   field1 can have the following values:
379   *           @arg TIM22_ETR_LSE:      TIM22 ETR connected to LSE
380   *           @arg TIM22_ETR_COMP2_OUT:TIM22 ETR connected to COMP2 output
381   *           @arg TIM22_ETR_COMP1_OUT:TIM22 ETR connected to COMP1 output
382   *           @arg TIM22_ETR_GPIO:     TIM22 ETR connected to GPIO(default):
383   *                                    PC8(AF0) or PA4(AF5)
384   *
385   *                   field2 can have the following values:
386   *           @arg TIM22_TI1_GPIO:     TIM22 TI1 connected to GPIO(default):
387   *                                    PC6(AF0) or PA6(AF5) or PB4(AF4) or PE0(AF3)
388   *           @arg TIM22_TI1_COMP2_OUT:TIM22 TI1 connected to COMP2 output
389   *           @arg TIM22_TI1_COMP1_OUT:TIM22 TI1 connected to COMP1 output
390   @endif
391   *
392   * @retval HAL status
393   */
HAL_TIMEx_RemapConfig(TIM_HandleTypeDef * htim,uint32_t Remap)394 HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
395 {
396 
397   /* Check parameters */
398   assert_param(IS_TIM_REMAP(htim->Instance, Remap));
399 
400   __HAL_LOCK(htim);
401 
402   /* Set the Timer remapping configuration */
403   WRITE_REG(htim->Instance->OR, Remap);
404 
405   __HAL_UNLOCK(htim);
406 
407   return HAL_OK;
408 }
409 
410 /**
411   * @}
412   */
413 
414 /**
415   * @}
416   */
417 
418 
419 #endif /* HAL_TIM_MODULE_ENABLED */
420 /**
421   * @}
422   */
423 
424 /**
425   * @}
426   */
427