blob: 6c58544396e236db6de95412144104f610f4db6a [file] [log] [blame]
Eya8a83a682020-01-29 12:42:18 +01001/**
2 ******************************************************************************
3 * @file stm32f7xx_ll_lptim.c
4 * @author MCD Application Team
5 * @brief LPTIM LL module driver.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
10 * All rights reserved.</center></h2>
11 *
12 * This software component is licensed by ST under BSD 3-Clause license,
13 * the "License"; You may not use this file except in compliance with the
14 * License. You may obtain a copy of the License at:
15 * opensource.org/licenses/BSD-3-Clause
16 *
17 ******************************************************************************
18 */
19#if defined(USE_FULL_LL_DRIVER)
20
21/* Includes ------------------------------------------------------------------*/
22#include "stm32f7xx_ll_lptim.h"
23#include "stm32f7xx_ll_bus.h"
Ali Labbene243e61a2020-03-12 16:07:28 +010024#include "stm32f7xx_ll_rcc.h"
25
Eya8a83a682020-01-29 12:42:18 +010026
27#ifdef USE_FULL_ASSERT
Ali Labbene243e61a2020-03-12 16:07:28 +010028#include "stm32_assert.h"
Eya8a83a682020-01-29 12:42:18 +010029#else
Ali Labbene243e61a2020-03-12 16:07:28 +010030#define assert_param(expr) ((void)0U)
Eya8a83a682020-01-29 12:42:18 +010031#endif
32
33/** @addtogroup STM32F7xx_LL_Driver
34 * @{
35 */
36
Ali Labbene243e61a2020-03-12 16:07:28 +010037#if defined (LPTIM1)
Eya8a83a682020-01-29 12:42:18 +010038
39/** @addtogroup LPTIM_LL
40 * @{
41 */
42
43/* Private types -------------------------------------------------------------*/
44/* Private variables ---------------------------------------------------------*/
45/* Private constants ---------------------------------------------------------*/
46/* Private macros ------------------------------------------------------------*/
47/** @addtogroup LPTIM_LL_Private_Macros
48 * @{
49 */
50#define IS_LL_LPTIM_CLOCK_SOURCE(__VALUE__) (((__VALUE__) == LL_LPTIM_CLK_SOURCE_INTERNAL) \
Eyabcfd32d2021-03-03 16:11:08 +010051 || ((__VALUE__) == LL_LPTIM_CLK_SOURCE_EXTERNAL))
Eya8a83a682020-01-29 12:42:18 +010052
53#define IS_LL_LPTIM_CLOCK_PRESCALER(__VALUE__) (((__VALUE__) == LL_LPTIM_PRESCALER_DIV1) \
Eyabcfd32d2021-03-03 16:11:08 +010054 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV2) \
55 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV4) \
56 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV8) \
57 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV16) \
58 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV32) \
59 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV64) \
60 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV128))
Eya8a83a682020-01-29 12:42:18 +010061
62#define IS_LL_LPTIM_WAVEFORM(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_PWM) \
Eyabcfd32d2021-03-03 16:11:08 +010063 || ((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_SETONCE))
Eya8a83a682020-01-29 12:42:18 +010064
65#define IS_LL_LPTIM_OUTPUT_POLARITY(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_REGULAR) \
Eyabcfd32d2021-03-03 16:11:08 +010066 || ((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_INVERSE))
Eya8a83a682020-01-29 12:42:18 +010067/**
68 * @}
69 */
70
71
72/* Private function prototypes -----------------------------------------------*/
Ali Labbene243e61a2020-03-12 16:07:28 +010073/* Private functions ---------------------------------------------------------*/
74/** @defgroup LPTIM_Private_Functions LPTIM Private Functions
75 * @{
76 */
77/**
78 * @}
79 */
Eya8a83a682020-01-29 12:42:18 +010080/* Exported functions --------------------------------------------------------*/
81/** @addtogroup LPTIM_LL_Exported_Functions
82 * @{
83 */
84
85/** @addtogroup LPTIM_LL_EF_Init
86 * @{
87 */
88
89/**
90 * @brief Set LPTIMx registers to their reset values.
91 * @param LPTIMx LP Timer instance
92 * @retval An ErrorStatus enumeration value:
93 * - SUCCESS: LPTIMx registers are de-initialized
94 * - ERROR: invalid LPTIMx instance
95 */
Ali Labbene243e61a2020-03-12 16:07:28 +010096ErrorStatus LL_LPTIM_DeInit(LPTIM_TypeDef *LPTIMx)
Eya8a83a682020-01-29 12:42:18 +010097{
98 ErrorStatus result = SUCCESS;
99
100 /* Check the parameters */
Ali Labbene243e61a2020-03-12 16:07:28 +0100101 assert_param(IS_LPTIM_INSTANCE(LPTIMx));
102
Eya8a83a682020-01-29 12:42:18 +0100103 if (LPTIMx == LPTIM1)
104 {
105 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPTIM1);
Ali Labbene243e61a2020-03-12 16:07:28 +0100106 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPTIM1);
Eya8a83a682020-01-29 12:42:18 +0100107 }
Eya8a83a682020-01-29 12:42:18 +0100108 else
109 {
110 result = ERROR;
111 }
Ali Labbene243e61a2020-03-12 16:07:28 +0100112
Eya8a83a682020-01-29 12:42:18 +0100113 return result;
114}
115
116/**
117 * @brief Set each fields of the LPTIM_InitStruct structure to its default
118 * value.
119 * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
120 * @retval None
121 */
Ali Labbene243e61a2020-03-12 16:07:28 +0100122void LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
Eya8a83a682020-01-29 12:42:18 +0100123{
124 /* Set the default configuration */
125 LPTIM_InitStruct->ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL;
126 LPTIM_InitStruct->Prescaler = LL_LPTIM_PRESCALER_DIV1;
127 LPTIM_InitStruct->Waveform = LL_LPTIM_OUTPUT_WAVEFORM_PWM;
128 LPTIM_InitStruct->Polarity = LL_LPTIM_OUTPUT_POLARITY_REGULAR;
129}
130
131/**
132 * @brief Configure the LPTIMx peripheral according to the specified parameters.
133 * @note LL_LPTIM_Init can only be called when the LPTIM instance is disabled.
134 * @note LPTIMx can be disabled using unitary function @ref LL_LPTIM_Disable().
135 * @param LPTIMx LP Timer Instance
136 * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
137 * @retval An ErrorStatus enumeration value:
138 * - SUCCESS: LPTIMx instance has been initialized
139 * - ERROR: LPTIMx instance hasn't been initialized
140 */
Ali Labbene243e61a2020-03-12 16:07:28 +0100141ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef *LPTIMx, LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
Eya8a83a682020-01-29 12:42:18 +0100142{
143 ErrorStatus result = SUCCESS;
Ali Labbene243e61a2020-03-12 16:07:28 +0100144 /* Check the parameters */
145 assert_param(IS_LPTIM_INSTANCE(LPTIMx));
146 assert_param(IS_LL_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->ClockSource));
147 assert_param(IS_LL_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->Prescaler));
148 assert_param(IS_LL_LPTIM_WAVEFORM(LPTIM_InitStruct->Waveform));
149 assert_param(IS_LL_LPTIM_OUTPUT_POLARITY(LPTIM_InitStruct->Polarity));
150
151 /* The LPTIMx_CFGR register must only be modified when the LPTIM is disabled
Eya8a83a682020-01-29 12:42:18 +0100152 (ENABLE bit is reset to 0).
153 */
Ali Labbene243e61a2020-03-12 16:07:28 +0100154 if (LL_LPTIM_IsEnabled(LPTIMx) == 1UL)
Eya8a83a682020-01-29 12:42:18 +0100155 {
156 result = ERROR;
157 }
158 else
159 {
Ali Labbene243e61a2020-03-12 16:07:28 +0100160 /* Set CKSEL bitfield according to ClockSource value */
161 /* Set PRESC bitfield according to Prescaler value */
162 /* Set WAVE bitfield according to Waveform value */
163 /* Set WAVEPOL bitfield according to Polarity value */
164 MODIFY_REG(LPTIMx->CFGR,
165 (LPTIM_CFGR_CKSEL | LPTIM_CFGR_PRESC | LPTIM_CFGR_WAVE | LPTIM_CFGR_WAVPOL),
166 LPTIM_InitStruct->ClockSource | \
167 LPTIM_InitStruct->Prescaler | \
168 LPTIM_InitStruct->Waveform | \
169 LPTIM_InitStruct->Polarity);
Eya8a83a682020-01-29 12:42:18 +0100170 }
171
172 return result;
173}
174
175/**
Ali Labbene243e61a2020-03-12 16:07:28 +0100176 * @brief Disable the LPTIM instance
177 * @rmtoll CR ENABLE LL_LPTIM_Disable
178 * @param LPTIMx Low-Power Timer instance
179 * @note The following sequence is required to solve LPTIM disable HW limitation.
180 * Please check Errata Sheet ES0335 for more details under "MCU may remain
181 * stuck in LPTIM interrupt when entering Stop mode" section.
182 * @retval None
183 */
184void LL_LPTIM_Disable(LPTIM_TypeDef *LPTIMx)
185{
186 LL_RCC_ClocksTypeDef rcc_clock;
187 uint32_t tmpclksource = 0;
188 uint32_t tmpIER;
189 uint32_t tmpCFGR;
190 uint32_t tmpCMP;
191 uint32_t tmpARR;
192
193 /* Check the parameters */
194 assert_param(IS_LPTIM_INSTANCE(LPTIMx));
195
196 __disable_irq();
197
198 /********** Save LPTIM Config *********/
199 /* Save LPTIM source clock */
200 switch ((uint32_t)LPTIMx)
201 {
202 case LPTIM1_BASE:
203 tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE);
204 break;
205 default:
206 break;
207 }
208
209 /* Save LPTIM configuration registers */
210 tmpIER = LPTIMx->IER;
211 tmpCFGR = LPTIMx->CFGR;
212 tmpCMP = LPTIMx->CMP;
213 tmpARR = LPTIMx->ARR;
214
215 /************* Reset LPTIM ************/
216 (void)LL_LPTIM_DeInit(LPTIMx);
217
218 /********* Restore LPTIM Config *******/
219 LL_RCC_GetSystemClocksFreq(&rcc_clock);
220
221 if ((tmpCMP != 0UL) || (tmpARR != 0UL))
222 {
223 /* Force LPTIM source kernel clock from APB */
224 switch ((uint32_t)LPTIMx)
225 {
226 case LPTIM1_BASE:
227 LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE_PCLK1);
228 break;
229 default:
230 break;
231 }
232
233 if (tmpCMP != 0UL)
234 {
235 /* Restore CMP and ARR registers (LPTIM should be enabled first) */
236 LPTIMx->CR |= LPTIM_CR_ENABLE;
237 LPTIMx->CMP = tmpCMP;
238
239 /* Polling on CMP write ok status after above restore operation */
240 do
241 {
242 rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
Eyabcfd32d2021-03-03 16:11:08 +0100243 } while (((LL_LPTIM_IsActiveFlag_CMPOK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
Ali Labbene243e61a2020-03-12 16:07:28 +0100244
245 LL_LPTIM_ClearFlag_CMPOK(LPTIMx);
246 }
247
248 if (tmpARR != 0UL)
249 {
250 LPTIMx->CR |= LPTIM_CR_ENABLE;
251 LPTIMx->ARR = tmpARR;
252
253 LL_RCC_GetSystemClocksFreq(&rcc_clock);
254 /* Polling on ARR write ok status after above restore operation */
255 do
256 {
257 rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
258 }
259 while (((LL_LPTIM_IsActiveFlag_ARROK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
260
261 LL_LPTIM_ClearFlag_ARROK(LPTIMx);
262 }
263
264
265 /* Restore LPTIM source kernel clock */
266 LL_RCC_SetLPTIMClockSource(tmpclksource);
267 }
268
269 /* Restore configuration registers (LPTIM should be disabled first) */
270 LPTIMx->CR &= ~(LPTIM_CR_ENABLE);
271 LPTIMx->IER = tmpIER;
272 LPTIMx->CFGR = tmpCFGR;
273
274 __enable_irq();
275}
276
277/**
Eya8a83a682020-01-29 12:42:18 +0100278 * @}
279 */
280
281/**
282 * @}
283 */
284
285/**
286 * @}
287 */
288
Ali Labbene243e61a2020-03-12 16:07:28 +0100289#endif /* LPTIM1 */
Eya8a83a682020-01-29 12:42:18 +0100290
291/**
292 * @}
293 */
Ali Labbene243e61a2020-03-12 16:07:28 +0100294
Eya8a83a682020-01-29 12:42:18 +0100295#endif /* USE_FULL_LL_DRIVER */
296
297/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/