1 /**
2   ******************************************************************************
3   * @file    stm32h7rsxx_ll_lptim.c
4   * @author  MCD Application Team
5   * @brief   LPTIM LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2022 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 "stm32h7rsxx_ll_lptim.h"
22 #include "stm32h7rsxx_ll_bus.h"
23 #include "stm32h7rsxx_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 STM32H7RSxx_LL_Driver
33   * @{
34   */
35 
36 #if defined (LPTIM1) || defined (LPTIM2) || defined (LPTIM3) || defined (LPTIM4) || defined (LPTIM5)
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_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPTIM1);
103     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPTIM1);
104   }
105   else if (LPTIMx == LPTIM2)
106   {
107     LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM2);
108     LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM2);
109   }
110   else if (LPTIMx == LPTIM3)
111   {
112     LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM3);
113     LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM3);
114   }
115   else if (LPTIMx == LPTIM4)
116   {
117     LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM4);
118     LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM4);
119   }
120   else if (LPTIMx == LPTIM5)
121   {
122     LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM5);
123     LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM5);
124   }
125   else
126   {
127     result = ERROR;
128   }
129 
130   return result;
131 }
132 
133 /**
134   * @brief  Set each fields of the LPTIM_InitStruct structure to its default
135   *         value.
136   * @param  LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
137   * @retval None
138   */
LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef * LPTIM_InitStruct)139 void LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
140 {
141   /* Set the default configuration */
142   LPTIM_InitStruct->ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL;
143   LPTIM_InitStruct->Prescaler   = LL_LPTIM_PRESCALER_DIV1;
144   LPTIM_InitStruct->Waveform    = LL_LPTIM_OUTPUT_WAVEFORM_PWM;
145 }
146 
147 /**
148   * @brief  Configure the LPTIMx peripheral according to the specified parameters.
149   * @note LL_LPTIM_Init can only be called when the LPTIM instance is disabled.
150   * @note LPTIMx can be disabled using unitary function @ref LL_LPTIM_Disable().
151   * @param  LPTIMx LP Timer Instance
152   * @param  LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
153   * @retval An ErrorStatus enumeration value:
154   *          - SUCCESS: LPTIMx instance has been initialized
155   *          - ERROR: LPTIMx instance hasn't been initialized
156   */
LL_LPTIM_Init(LPTIM_TypeDef * LPTIMx,const LL_LPTIM_InitTypeDef * LPTIM_InitStruct)157 ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef *LPTIMx, const LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
158 {
159   ErrorStatus result = SUCCESS;
160   /* Check the parameters */
161   assert_param(IS_LPTIM_INSTANCE(LPTIMx));
162   assert_param(IS_LL_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->ClockSource));
163   assert_param(IS_LL_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->Prescaler));
164   assert_param(IS_LL_LPTIM_WAVEFORM(LPTIM_InitStruct->Waveform));
165 
166   /* The LPTIMx_CFGR register must only be modified when the LPTIM is disabled
167      (ENABLE bit is reset to 0).
168   */
169   if (LL_LPTIM_IsEnabled(LPTIMx) == 1UL)
170   {
171     result = ERROR;
172   }
173   else
174   {
175     /* Set CKSEL bitfield according to ClockSource value */
176     /* Set PRESC bitfield according to Prescaler value */
177     /* Set WAVE bitfield according to Waveform value */
178     MODIFY_REG(LPTIMx->CFGR,
179                (LPTIM_CFGR_CKSEL | LPTIM_CFGR_PRESC | LPTIM_CFGR_WAVE),
180                LPTIM_InitStruct->ClockSource | \
181                LPTIM_InitStruct->Prescaler | \
182                LPTIM_InitStruct->Waveform);
183   }
184 
185   return result;
186 }
187 
188 /**
189   * @}
190   */
191 
192 /**
193   * @}
194   */
195 
196 /**
197   * @}
198   */
199 
200 #endif /* LPTIM1 || LPTIM2 ||  LPTIM3 || LPTIM4 || LPTIM5 */
201 
202 /**
203   * @}
204   */
205 
206 #endif /* USE_FULL_LL_DRIVER */
207