|
STM32G474xx HAL用户手册
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32g4xx_ll_lptim.c 00004 * @author MCD Application Team 00005 * @brief LPTIM LL module driver. 00006 ****************************************************************************** 00007 * @attention 00008 * 00009 * Copyright (c) 2019 STMicroelectronics. 00010 * All rights reserved. 00011 * 00012 * This software is licensed under terms that can be found in the LICENSE file 00013 * in the root directory of this software component. 00014 * If no LICENSE file comes with this software, it is provided AS-IS. 00015 * 00016 ****************************************************************************** 00017 */ 00018 #if defined(USE_FULL_LL_DRIVER) 00019 00020 /* Includes ------------------------------------------------------------------*/ 00021 #include "stm32g4xx_ll_lptim.h" 00022 #include "stm32g4xx_ll_bus.h" 00023 #include "stm32g4xx_ll_rcc.h" 00024 00025 00026 #ifdef USE_FULL_ASSERT 00027 #include "stm32_assert.h" 00028 #else 00029 #define assert_param(expr) ((void)0U) 00030 #endif /* USE_FULL_ASSERT */ 00031 00032 /** @addtogroup STM32G4xx_LL_Driver 00033 * @{ 00034 */ 00035 00036 00037 00038 /** @addtogroup LPTIM_LL 00039 * @{ 00040 */ 00042 00043 /* Private types -------------------------------------------------------------*/ 00044 /* Private variables ---------------------------------------------------------*/ 00045 /* Private constants ---------------------------------------------------------*/ 00046 /* Private macros ------------------------------------------------------------*/ 00047 /** @addtogroup LPTIM_LL_Private_Macros 00048 * @{ 00049 */ 00049 #define IS_LL_LPTIM_CLOCK_SOURCE(__VALUE__) (((__VALUE__) == LL_LPTIM_CLK_SOURCE_INTERNAL) \ 00051 || ((__VALUE__) == LL_LPTIM_CLK_SOURCE_EXTERNAL)) 00052 00052 #define IS_LL_LPTIM_CLOCK_PRESCALER(__VALUE__) (((__VALUE__) == LL_LPTIM_PRESCALER_DIV1) \ 00054 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV2) \ 00055 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV4) \ 00056 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV8) \ 00057 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV16) \ 00058 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV32) \ 00059 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV64) \ 00060 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV128)) 00061 00061 #define IS_LL_LPTIM_WAVEFORM(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_PWM) \ 00063 || ((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_SETONCE)) 00064 00064 #define IS_LL_LPTIM_OUTPUT_POLARITY(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_REGULAR) \ 00066 || ((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_INVERSE)) 00067 /** 00068 * @} 00069 */ 00070 00071 /* Private function prototypes -----------------------------------------------*/ 00072 /* Private functions ---------------------------------------------------------*/ 00073 /** @defgroup LPTIM_Private_Functions LPTIM Private Functions 00074 * @{ 00075 */ 00076 /** 00077 * @} 00078 */ 00079 /* Exported functions --------------------------------------------------------*/ 00080 /** @addtogroup LPTIM_LL_Exported_Functions 00081 * @{ 00082 */ 00083 00084 /** @addtogroup LPTIM_LL_EF_Init 00085 * @{ 00086 */ 00087 00088 /** 00089 * @brief Set LPTIMx registers to their reset values. 00090 * @param LPTIMx LP Timer instance 00091 * @retval An ErrorStatus enumeration value: 00092 * - SUCCESS: LPTIMx registers are de-initialized 00093 * - ERROR: invalid LPTIMx instance 00094 */ 00095 ErrorStatus LL_LPTIM_DeInit(const LPTIM_TypeDef *LPTIMx) 00096 { 00097 ErrorStatus result = SUCCESS; 00098 00099 /* Check the parameters */ 00100 assert_param(IS_LPTIM_INSTANCE(LPTIMx)); 00101 00102 if (LPTIMx == LPTIM1) 00103 { 00104 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPTIM1); 00105 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPTIM1); 00106 } 00107 else 00108 { 00109 result = ERROR; 00111 } 00112 00113 return result; 00114 } 00115 00116 /** 00117 * @brief Set each fields of the LPTIM_InitStruct structure to its default 00118 * value. 00119 * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure 00120 * @retval None 00121 */ 00121 void LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef *LPTIM_InitStruct) 00122 { 00123 /* Set the default configuration */ 00124 LPTIM_InitStruct->ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL; 00125 LPTIM_InitStruct->Prescaler = LL_LPTIM_PRESCALER_DIV1; 00126 LPTIM_InitStruct->Waveform = LL_LPTIM_OUTPUT_WAVEFORM_PWM; 00127 LPTIM_InitStruct->Polarity = LL_LPTIM_OUTPUT_POLARITY_REGULAR; 00128 } 00129 00130 /** 00131 * @brief Configure the LPTIMx peripheral according to the specified parameters. 00132 * @note LL_LPTIM_Init can only be called when the LPTIM instance is disabled. 00133 * @note LPTIMx can be disabled using unitary function @ref LL_LPTIM_Disable(). 00134 * @param LPTIMx LP Timer Instance 00135 * @param LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure 00136 * @retval An ErrorStatus enumeration value: 00137 * - SUCCESS: LPTIMx instance has been initialized 00138 * - ERROR: LPTIMx instance hasn't been initialized 00139 */ 00140 ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef *LPTIMx, const LL_LPTIM_InitTypeDef *LPTIM_InitStruct) 00141 { 00142 ErrorStatus result = SUCCESS; 00143 /* Check the parameters */ 00144 assert_param(IS_LPTIM_INSTANCE(LPTIMx)); 00145 assert_param(IS_LL_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->ClockSource)); 00146 assert_param(IS_LL_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->Prescaler)); 00147 assert_param(IS_LL_LPTIM_WAVEFORM(LPTIM_InitStruct->Waveform)); 00148 assert_param(IS_LL_LPTIM_OUTPUT_POLARITY(LPTIM_InitStruct->Polarity)); 00149 00150 /* The LPTIMx_CFGR register must only be modified when the LPTIM is disabled 00151 (ENABLE bit is reset to 0). 00152 */ 00153 if (LL_LPTIM_IsEnabled(LPTIMx) == 1UL) 00154 { 00155 result = ERROR; 00156 } 00157 else 00158 { 00159 /* Set CKSEL bitfield according to ClockSource value */ 00160 /* Set PRESC bitfield according to Prescaler value */ 00161 /* Set WAVE bitfield according to Waveform value */ 00162 /* Set WAVEPOL bitfield according to Polarity value */ 00163 MODIFY_REG(LPTIMx->CFGR, 00164 (LPTIM_CFGR_CKSEL | LPTIM_CFGR_PRESC | LPTIM_CFGR_WAVE | LPTIM_CFGR_WAVPOL), 00165 LPTIM_InitStruct->ClockSource | \ 00166 LPTIM_InitStruct->Prescaler | \ 00167 LPTIM_InitStruct->Waveform | \ 00168 LPTIM_InitStruct->Polarity); 00169 } 00169 00171 return result; 00172 } 00173 00174 /** 00175 * @brief Disable the LPTIM instance 00176 * @rmtoll CR ENABLE LL_LPTIM_Disable 00177 * @param LPTIMx Low-Power Timer instance 00178 * @note The following sequence is required to solve LPTIM disable HW limitation. 00179