1 /**
2   ******************************************************************************
3   * @file    stm32u5xx_ll_lptim.c
4   * @author  MCD Application Team
5   * @brief   LPTIM LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2021 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 #if defined(USE_FULL_LL_DRIVER)
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32u5xx_ll_lptim.h"
22 #include "stm32u5xx_ll_bus.h"
23 #include "stm32u5xx_ll_rcc.h"
24 
25 
26 #ifdef  USE_FULL_ASSERT
27 #include "stm32_assert.h"
28 #else
29 #define assert_param(expr) ((void)0U)
30 #endif /* USE_FULL_ASSERT */
31 
32 /** @addtogroup STM32U5xx_LL_Driver
33   * @{
34   */
35 
36 #if defined (LPTIM1) || defined (LPTIM2) || defined (LPTIM3) || defined (LPTIM4)
37 
38 /** @addtogroup LPTIM_LL
39   * @{
40   */
41 
42 /* Private types -------------------------------------------------------------*/
43 /* Private variables ---------------------------------------------------------*/
44 /* Private constants ---------------------------------------------------------*/
45 /* Private macros ------------------------------------------------------------*/
46 /** @addtogroup LPTIM_LL_Private_Macros
47   * @{
48   */
49 #define IS_LL_LPTIM_CLOCK_SOURCE(__VALUE__) (((__VALUE__) == LL_LPTIM_CLK_SOURCE_INTERNAL) \
50                                              || ((__VALUE__) == LL_LPTIM_CLK_SOURCE_EXTERNAL))
51 
52 #define IS_LL_LPTIM_CLOCK_PRESCALER(__VALUE__) (((__VALUE__) == LL_LPTIM_PRESCALER_DIV1)   \
53                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV2)   \
54                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV4)   \
55                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV8)   \
56                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV16)  \
57                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV32)  \
58                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV64)  \
59                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV128))
60 
61 #define IS_LL_LPTIM_WAVEFORM(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_PWM) \
62                                          || ((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_SETONCE))
63 
64 /**
65   * @}
66   */
67 
68 
69 /* Private function prototypes -----------------------------------------------*/
70 /* Private functions ---------------------------------------------------------*/
71 /** @defgroup LPTIM_Private_Functions LPTIM Private Functions
72   * @{
73   */
74 /**
75   * @}
76   */
77 /* Exported functions --------------------------------------------------------*/
78 /** @addtogroup LPTIM_LL_Exported_Functions
79   * @{
80   */
81 
82 /** @addtogroup LPTIM_LL_EF_Init
83   * @{
84   */
85 
86 /**
87   * @brief  Set LPTIMx registers to their reset values.
88   * @param  LPTIMx LP Timer instance
89   * @retval An ErrorStatus enumeration value:
90   *          - SUCCESS: LPTIMx registers are de-initialized
91   *          - ERROR: invalid LPTIMx instance
92   */
LL_LPTIM_DeInit(const LPTIM_TypeDef * LPTIMx)93 ErrorStatus LL_LPTIM_DeInit(const LPTIM_TypeDef *LPTIMx)
94 {
95   ErrorStatus result = SUCCESS;
96 
97   /* Check the parameters */
98   assert_param(IS_LPTIM_INSTANCE(LPTIMx));
99 
100   if (LPTIMx == LPTIM1)
101   {
102     LL_APB3_GRP1_ForceReset(LL_APB3_GRP1_PERIPH_LPTIM1);
103     LL_APB3_GRP1_ReleaseReset(LL_APB3_GRP1_PERIPH_LPTIM1);
104   }
105   else if (LPTIMx == LPTIM2)
106   {
107     LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_LPTIM2);
108     LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_LPTIM2);
109   }
110   else if (LPTIMx == LPTIM3)
111   {
112     LL_APB3_GRP1_ForceReset(LL_APB3_GRP1_PERIPH_LPTIM3);
113     LL_APB3_GRP1_ReleaseReset(LL_APB3_GRP1_PERIPH_LPTIM3);
114   }
115   else if (LPTIMx == LPTIM4)
116   {
117     LL_APB3_GRP1_ForceReset(LL_APB3_GRP1_PERIPH_LPTIM4);
118     LL_APB3_GRP1_ReleaseReset(LL_APB3_GRP1_PERIPH_LPTIM4);
119   }
120   else
121   {
122     result = ERROR;
123   }
124 
125   return result;
126 }
127 
128 /**
129   * @brief  Set each fields of the LPTIM_InitStruct structure to its default
130   *         value.
131   * @param  LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
132   * @retval None
133   */
LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef * LPTIM_InitStruct)134 void LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
135 {
136   /* Set the default configuration */
137   LPTIM_InitStruct->ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL;
138   LPTIM_InitStruct->Prescaler   = LL_LPTIM_PRESCALER_DIV1;
139   LPTIM_InitStruct->Waveform    = LL_LPTIM_OUTPUT_WAVEFORM_PWM;
140 }
141 
142 /**
143   * @brief  Configure the LPTIMx peripheral according to the specified parameters.
144   * @note LL_LPTIM_Init can only be called when the LPTIM instance is disabled.
145   * @note LPTIMx can be disabled using unitary function @ref LL_LPTIM_Disable().
146   * @param  LPTIMx LP Timer Instance
147   * @param  LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
148   * @retval An ErrorStatus enumeration value:
149   *          - SUCCESS: LPTIMx instance has been initialized
150   *          - ERROR: LPTIMx instance hasn't been initialized
151   */
LL_LPTIM_Init(LPTIM_TypeDef * LPTIMx,const LL_LPTIM_InitTypeDef * LPTIM_InitStruct)152 ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef *LPTIMx, const LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
153 {
154   ErrorStatus result = SUCCESS;
155   /* Check the parameters */
156   assert_param(IS_LPTIM_INSTANCE(LPTIMx));
157   assert_param(IS_LL_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->ClockSource));
158   assert_param(IS_LL_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->Prescaler));
159   assert_param(IS_LL_LPTIM_WAVEFORM(LPTIM_InitStruct->Waveform));
160 
161   /* The LPTIMx_CFGR register must only be modified when the LPTIM is disabled
162      (ENABLE bit is reset to 0).
163   */
164   if (LL_LPTIM_IsEnabled(LPTIMx) == 1UL)
165   {
166     result = ERROR;
167   }
168   else
169   {
170     /* Set CKSEL bitfield according to ClockSource value */
171     /* Set PRESC bitfield according to Prescaler value */
172     /* Set WAVE bitfield according to Waveform value */
173     MODIFY_REG(LPTIMx->CFGR,
174                (LPTIM_CFGR_CKSEL | LPTIM_CFGR_PRESC | LPTIM_CFGR_WAVE),
175                LPTIM_InitStruct->ClockSource | \
176                LPTIM_InitStruct->Prescaler | \
177                LPTIM_InitStruct->Waveform);
178   }
179 
180   return result;
181 }
182 
183 /**
184   * @}
185   */
186 
187 /**
188   * @}
189   */
190 
191 /**
192   * @}
193   */
194 
195 #endif /* LPTIM1 || LPTIM2 ||  LPTIM3 || LPTIM4 */
196 
197 /**
198   * @}
199   */
200 
201 #endif /* USE_FULL_LL_DRIVER */
202