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