STM32F4xx HAL Driver master
STM32CubeF4 HAL / LL Drivers API Reference
Loading...
Searching...
No Matches
stm32f4xx_ll_lptim.c
Go to the documentation of this file.
1
18#if defined(USE_FULL_LL_DRIVER)
19
20/* Includes ------------------------------------------------------------------*/
21#include "stm32f4xx_ll_lptim.h"
22#include "stm32f4xx_ll_bus.h"
23#include "stm32f4xx_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
35
36#if defined (LPTIM1)
37
41
42/* Private types -------------------------------------------------------------*/
43/* Private variables ---------------------------------------------------------*/
44/* Private constants ---------------------------------------------------------*/
45/* Private macros ------------------------------------------------------------*/
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#define IS_LL_LPTIM_OUTPUT_POLARITY(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_REGULAR) \
65 || ((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_INVERSE))
69
70
71/* Private function prototypes -----------------------------------------------*/
72/* Private functions ---------------------------------------------------------*/
79/* Exported functions --------------------------------------------------------*/
83
87
95ErrorStatus LL_LPTIM_DeInit(const LPTIM_TypeDef *LPTIMx)
96{
97 ErrorStatus result = SUCCESS;
98
99 /* Check the parameters */
100 assert_param(IS_LPTIM_INSTANCE(LPTIMx));
101
102 if (LPTIMx == LPTIM1)
103 {
104 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPTIM1);
105 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPTIM1);
106 }
107 else
108 {
109 result = ERROR;
110 }
111
112 return result;
113}
114
121void LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
122{
123 /* Set the default configuration */
124 LPTIM_InitStruct->ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL;
125 LPTIM_InitStruct->Prescaler = LL_LPTIM_PRESCALER_DIV1;
126 LPTIM_InitStruct->Waveform = LL_LPTIM_OUTPUT_WAVEFORM_PWM;
127 LPTIM_InitStruct->Polarity = LL_LPTIM_OUTPUT_POLARITY_REGULAR;
128}
129
140ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef *LPTIMx, const LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
141{
142 ErrorStatus result = SUCCESS;
143 /* Check the parameters */
144 assert_param(IS_LPTIM_INSTANCE(LPTIMx));
145 assert_param(IS_LL_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->ClockSource));
146 assert_param(IS_LL_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->Prescaler));
147 assert_param(IS_LL_LPTIM_WAVEFORM(LPTIM_InitStruct->Waveform));
148 assert_param(IS_LL_LPTIM_OUTPUT_POLARITY(LPTIM_InitStruct->Polarity));
149
150 /* The LPTIMx_CFGR register must only be modified when the LPTIM is disabled
151 (ENABLE bit is reset to 0).
152 */
153 if (LL_LPTIM_IsEnabled(LPTIMx) == 1UL)
154 {
155 result = ERROR;
156 }
157 else
158 {
159 /* Set CKSEL bitfield according to ClockSource value */
160 /* Set PRESC bitfield according to Prescaler value */
161 /* Set WAVE bitfield according to Waveform value */
162 /* Set WAVEPOL bitfield according to Polarity value */
163 MODIFY_REG(LPTIMx->CFGR,
164 (LPTIM_CFGR_CKSEL | LPTIM_CFGR_PRESC | LPTIM_CFGR_WAVE | LPTIM_CFGR_WAVPOL),
165 LPTIM_InitStruct->ClockSource | \
166 LPTIM_InitStruct->Prescaler | \
167 LPTIM_InitStruct->Waveform | \
168 LPTIM_InitStruct->Polarity);
169 }
170
171 return result;
172}
173
183void LL_LPTIM_Disable(LPTIM_TypeDef *LPTIMx)
184{
185 LL_RCC_ClocksTypeDef rcc_clock;
186 uint32_t tmpclksource = 0;
187 uint32_t tmpIER;
188 uint32_t tmpCFGR;
189 uint32_t tmpCMP;
190 uint32_t tmpARR;
191 uint32_t primask_bit;
192 uint32_t tmpOR;
193
194 /* Check the parameters */
195 assert_param(IS_LPTIM_INSTANCE(LPTIMx));
196
197 /* Enter critical section */
198 primask_bit = __get_PRIMASK();
199 __set_PRIMASK(1) ;
200
201 /********** Save LPTIM Config *********/
202 /* Save LPTIM source clock */
203 switch ((uint32_t)LPTIMx)
204 {
205 case LPTIM1_BASE:
206 tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE);
207 break;
208 default:
209 break;
210 }
211
212 /* Save LPTIM configuration registers */
213 tmpIER = LPTIMx->IER;
214 tmpCFGR = LPTIMx->CFGR;
215 tmpCMP = LPTIMx->CMP;
216 tmpARR = LPTIMx->ARR;
217 tmpOR = LPTIMx->OR;
218
219 /************* Reset LPTIM ************/
220 (void)LL_LPTIM_DeInit(LPTIMx);
221
222 /********* Restore LPTIM Config *******/
223 LL_RCC_GetSystemClocksFreq(&rcc_clock);
224
225 if ((tmpCMP != 0UL) || (tmpARR != 0UL))
226 {
227 /* Force LPTIM source kernel clock from APB */
228 switch ((uint32_t)LPTIMx)
229 {
230 case LPTIM1_BASE:
231 LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE_PCLK1);
232 break;
233 default:
234 break;
235 }
236
237 if (tmpCMP != 0UL)
238 {
239 /* Restore CMP and ARR registers (LPTIM should be enabled first) */
240 LPTIMx->CR |= LPTIM_CR_ENABLE;
241 LPTIMx->CMP = tmpCMP;
242
243 /* Polling on CMP write ok status after above restore operation */
244 do
245 {
246 rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
247 } while (((LL_LPTIM_IsActiveFlag_CMPOK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
248
249 LL_LPTIM_ClearFlag_CMPOK(LPTIMx);
250 }
251
252 if (tmpARR != 0UL)
253 {
254 LPTIMx->CR |= LPTIM_CR_ENABLE;
255 LPTIMx->ARR = tmpARR;
256
257 LL_RCC_GetSystemClocksFreq(&rcc_clock);
258 /* Polling on ARR write ok status after above restore operation */
259 do
260 {
261 rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
262 } while (((LL_LPTIM_IsActiveFlag_ARROK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
263
264 LL_LPTIM_ClearFlag_ARROK(LPTIMx);
265 }
266
267
268 /* Restore LPTIM source kernel clock */
269 LL_RCC_SetLPTIMClockSource(tmpclksource);
270 }
271
272 /* Restore configuration registers (LPTIM should be disabled first) */
273 LPTIMx->CR &= ~(LPTIM_CR_ENABLE);
274 LPTIMx->IER = tmpIER;
275 LPTIMx->CFGR = tmpCFGR;
276 LPTIMx->OR = tmpOR;
277
278 /* Exit critical section: restore previous priority mask */
279 __set_PRIMASK(primask_bit);
280}
281
285
289
293
294#endif /* LPTIM1 */
295
299
300#endif /* USE_FULL_LL_DRIVER */
#define assert_param(expr)
Header file of BUS LL module.
Header file of LPTIM LL module.
Header file of RCC LL module.