STM32L486xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_ll_lpuart.c 00004 * @author MCD Application Team 00005 * @brief LPUART LL module driver. 00006 ****************************************************************************** 00007 * @attention 00008 * 00009 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00010 * 00011 * Redistribution and use in source and binary forms, with or without modification, 00012 * are permitted provided that the following conditions are met: 00013 * 1. Redistributions of source code must retain the above copyright notice, 00014 * this list of conditions and the following disclaimer. 00015 * 2. Redistributions in binary form must reproduce the above copyright notice, 00016 * this list of conditions and the following disclaimer in the documentation 00017 * and/or other materials provided with the distribution. 00018 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00019 * may be used to endorse or promote products derived from this software 00020 * without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00023 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00024 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00025 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00026 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00027 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00028 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00030 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00031 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 * 00033 ****************************************************************************** 00034 */ 00035 #if defined(USE_FULL_LL_DRIVER) 00036 00037 /* Includes ------------------------------------------------------------------*/ 00038 #include "stm32l4xx_ll_lpuart.h" 00039 #include "stm32l4xx_ll_rcc.h" 00040 #include "stm32l4xx_ll_bus.h" 00041 #ifdef USE_FULL_ASSERT 00042 #include "stm32_assert.h" 00043 #else 00044 #define assert_param(expr) ((void)0U) 00045 #endif 00046 00047 /** @addtogroup STM32L4xx_LL_Driver 00048 * @{ 00049 */ 00050 00051 #if defined (LPUART1) 00052 00053 /** @addtogroup LPUART_LL 00054 * @{ 00055 */ 00056 00057 /* Private types -------------------------------------------------------------*/ 00058 /* Private variables ---------------------------------------------------------*/ 00059 /* Private constants ---------------------------------------------------------*/ 00060 /** @addtogroup LPUART_LL_Private_Constants 00061 * @{ 00062 */ 00063 00064 /** 00065 * @} 00066 */ 00067 00068 00069 /* Private macros ------------------------------------------------------------*/ 00070 /** @addtogroup LPUART_LL_Private_Macros 00071 * @{ 00072 */ 00073 00074 /* Check of parameters for configuration of LPUART registers */ 00075 00076 #if defined(USART_PRESC_PRESCALER) 00077 #define IS_LL_LPUART_PRESCALER(__VALUE__) (((__VALUE__) == LL_LPUART_PRESCALER_DIV1) \ 00078 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV2) \ 00079 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV4) \ 00080 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV6) \ 00081 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV8) \ 00082 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV10) \ 00083 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV12) \ 00084 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV16) \ 00085 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV32) \ 00086 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV64) \ 00087 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV128) \ 00088 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV256)) 00089 00090 #endif 00091 /* __BAUDRATE__ Depending on constraints applicable for LPUART BRR register */ 00092 /* value : */ 00093 /* - fck must be in the range [3 x baudrate, 4096 x baudrate] */ 00094 /* - LPUART_BRR register value should be >= 0x300 */ 00095 /* - LPUART_BRR register value should be <= 0xFFFFF (20 bits) */ 00096 /* Baudrate specified by the user should belong to [8, 40000000].*/ 00097 #define IS_LL_LPUART_BAUDRATE(__BAUDRATE__) (((__BAUDRATE__) <= 40000000U) && ((__BAUDRATE__) >= 8U)) 00098 00099 /* __VALUE__ BRR content must be greater than or equal to 0x300. */ 00100 #define IS_LL_LPUART_BRR_MIN(__VALUE__) ((__VALUE__) >= 0x300U) 00101 00102 /* __VALUE__ BRR content must be lower than or equal to 0xFFFFF. */ 00103 #define IS_LL_LPUART_BRR_MAX(__VALUE__) ((__VALUE__) <= 0x000FFFFFU) 00104 00105 #define IS_LL_LPUART_DIRECTION(__VALUE__) (((__VALUE__) == LL_LPUART_DIRECTION_NONE) \ 00106 || ((__VALUE__) == LL_LPUART_DIRECTION_RX) \ 00107 || ((__VALUE__) == LL_LPUART_DIRECTION_TX) \ 00108 || ((__VALUE__) == LL_LPUART_DIRECTION_TX_RX)) 00109 00110 #define IS_LL_LPUART_PARITY(__VALUE__) (((__VALUE__) == LL_LPUART_PARITY_NONE) \ 00111 || ((__VALUE__) == LL_LPUART_PARITY_EVEN) \ 00112 || ((__VALUE__) == LL_LPUART_PARITY_ODD)) 00113 00114 #define IS_LL_LPUART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_LPUART_DATAWIDTH_7B) \ 00115 || ((__VALUE__) == LL_LPUART_DATAWIDTH_8B) \ 00116 || ((__VALUE__) == LL_LPUART_DATAWIDTH_9B)) 00117 00118 #define IS_LL_LPUART_STOPBITS(__VALUE__) (((__VALUE__) == LL_LPUART_STOPBITS_1) \ 00119 || ((__VALUE__) == LL_LPUART_STOPBITS_2)) 00120 00121 #define IS_LL_LPUART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_LPUART_HWCONTROL_NONE) \ 00122 || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS) \ 00123 || ((__VALUE__) == LL_LPUART_HWCONTROL_CTS) \ 00124 || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS_CTS)) 00125 00126 /** 00127 * @} 00128 */ 00129 00130 /* Private function prototypes -----------------------------------------------*/ 00131 00132 /* Exported functions --------------------------------------------------------*/ 00133 /** @addtogroup LPUART_LL_Exported_Functions 00134 * @{ 00135 */ 00136 00137 /** @addtogroup LPUART_LL_EF_Init 00138 * @{ 00139 */ 00140 00141 /** 00142 * @brief De-initialize LPUART registers (Registers restored to their default values). 00143 * @param LPUARTx LPUART Instance 00144 * @retval An ErrorStatus enumeration value: 00145 * - SUCCESS: LPUART registers are de-initialized 00146 * - ERROR: not applicable 00147 */ 00148 ErrorStatus LL_LPUART_DeInit(USART_TypeDef *LPUARTx) 00149 { 00150 ErrorStatus status = SUCCESS; 00151 00152 /* Check the parameters */ 00153 assert_param(IS_LPUART_INSTANCE(LPUARTx)); 00154 00155 if (LPUARTx == LPUART1) 00156 { 00157 /* Force reset of LPUART peripheral */ 00158 LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_LPUART1); 00159 00160 /* Release reset of LPUART peripheral */ 00161 LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_LPUART1); 00162 } 00163 else 00164 { 00165 status = ERROR; 00166 } 00167 00168 return (status); 00169 } 00170 00171 /** 00172 * @brief Initialize LPUART registers according to the specified 00173 * parameters in LPUART_InitStruct. 00174 * @note As some bits in LPUART configuration registers can only be written when the LPUART is disabled (USART_CR1_UE bit =0), 00175 * LPUART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned. 00176 * @note Baud rate value stored in LPUART_InitStruct BaudRate field, should be valid (different from 0). 00177 * @param LPUARTx LPUART Instance 00178 * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure 00179 * that contains the configuration information for the specified LPUART peripheral. 00180 * @retval An ErrorStatus enumeration value: 00181 * - SUCCESS: LPUART registers are initialized according to LPUART_InitStruct content 00182 * - ERROR: Problem occurred during LPUART Registers initialization 00183 */ 00184 ErrorStatus LL_LPUART_Init(USART_TypeDef *LPUARTx, LL_LPUART_InitTypeDef *LPUART_InitStruct) 00185 { 00186 ErrorStatus status = ERROR; 00187 uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO; 00188 00189 /* Check the parameters */ 00190 assert_param(IS_LPUART_INSTANCE(LPUARTx)); 00191 #if defined(USART_PRESC_PRESCALER) 00192 assert_param(IS_LL_LPUART_PRESCALER(LPUART_InitStruct->PrescalerValue)); 00193 #endif 00194 assert_param(IS_LL_LPUART_BAUDRATE(LPUART_InitStruct->BaudRate)); 00195 assert_param(IS_LL_LPUART_DATAWIDTH(LPUART_InitStruct->DataWidth)); 00196 assert_param(IS_LL_LPUART_STOPBITS(LPUART_InitStruct->StopBits)); 00197 assert_param(IS_LL_LPUART_PARITY(LPUART_InitStruct->Parity)); 00198 assert_param(IS_LL_LPUART_DIRECTION(LPUART_InitStruct->TransferDirection)); 00199 assert_param(IS_LL_LPUART_HWCONTROL(LPUART_InitStruct->HardwareFlowControl)); 00200 00201 /* LPUART needs to be in disabled state, in order to be able to configure some bits in 00202 CRx registers. Otherwise (LPUART not in Disabled state) => return ERROR */ 00203 if (LL_LPUART_IsEnabled(LPUARTx) == 0U) 00204 { 00205 /*---------------------------- LPUART CR1 Configuration ----------------------- 00206 * Configure LPUARTx CR1 (LPUART Word Length, Parity and Transfer Direction bits) with parameters: 00207 * - DataWidth: USART_CR1_M bits according to LPUART_InitStruct->DataWidth value 00208 * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to LPUART_InitStruct->Parity value 00209 * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to LPUART_InitStruct->TransferDirection value 00210 */ 00211 MODIFY_REG(LPUARTx->CR1, 00212 (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE), 00213 (LPUART_InitStruct->DataWidth | LPUART_InitStruct->Parity | LPUART_InitStruct->TransferDirection)); 00214 00215 /*---------------------------- LPUART CR2 Configuration ----------------------- 00216 * Configure LPUARTx CR2 (Stop bits) with parameters: 00217 * - Stop Bits: USART_CR2_STOP bits according to LPUART_InitStruct->StopBits value. 00218 */ 00219 LL_LPUART_SetStopBitsLength(LPUARTx, LPUART_InitStruct->StopBits); 00220 00221 /*---------------------------- LPUART CR3 Configuration ----------------------- 00222 * Configure LPUARTx CR3 (Hardware Flow Control) with parameters: 00223 * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to LPUART_InitStruct->HardwareFlowControl value. 00224 */ 00225 LL_LPUART_SetHWFlowCtrl(LPUARTx, LPUART_InitStruct->HardwareFlowControl); 00226 00227 /*---------------------------- LPUART BRR Configuration ----------------------- 00228 * Retrieve Clock frequency used for LPUART Peripheral 00229 */ 00230 periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE); 00231 00232 /* Configure the LPUART Baud Rate : 00233 #if defined(USART_PRESC_PRESCALER) 00234 - prescaler value is required 00235 #endif 00236 - valid baud rate value (different from 0) is required 00237 - Peripheral clock as returned by RCC service, should be valid (different from 0). 00238 */ 00239 if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO) 00240 && (LPUART_InitStruct->BaudRate != 0U)) 00241 { 00242 status = SUCCESS; 00243 LL_LPUART_SetBaudRate(LPUARTx, 00244 periphclk, 00245 #if defined(USART_PRESC_PRESCALER) 00246 LPUART_InitStruct->PrescalerValue, 00247 #endif 00248 LPUART_InitStruct->BaudRate); 00249 00250 /* Check BRR is greater than or equal to 0x300 */ 00251 assert_param(IS_LL_LPUART_BRR_MIN(LPUARTx->BRR)); 00252 00253 /* Check BRR is lower than or equal to 0xFFFF */ 00254 assert_param(IS_LL_LPUART_BRR_MAX(LPUARTx->BRR)); 00255 } 00256 00257 #if defined(USART_PRESC_PRESCALER) 00258 /*---------------------------- LPUART PRESC Configuration ----------------------- 00259 * Configure LPUARTx PRESC (Prescaler) with parameters: 00260 * - PrescalerValue: LPUART_PRESC_PRESCALER bits according to LPUART_InitStruct->PrescalerValue value. 00261 */ 00262 LL_LPUART_SetPrescaler(LPUARTx, LPUART_InitStruct->PrescalerValue); 00263 #endif 00264 } 00265 00266 return (status); 00267 } 00268 00269 /** 00270 * @brief Set each @ref LL_LPUART_InitTypeDef field to default value. 00271 * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure 00272 * whose fields will be set to default values. 00273 * @retval None 00274 */ 00275 00276 void LL_LPUART_StructInit(LL_LPUART_InitTypeDef *LPUART_InitStruct) 00277 { 00278 /* Set LPUART_InitStruct fields to default values */ 00279 #if defined(USART_PRESC_PRESCALER) 00280 LPUART_InitStruct->PrescalerValue = LL_LPUART_PRESCALER_DIV1; 00281 #endif 00282 LPUART_InitStruct->BaudRate = 9600U; 00283 LPUART_InitStruct->DataWidth = LL_LPUART_DATAWIDTH_8B; 00284 LPUART_InitStruct->StopBits = LL_LPUART_STOPBITS_1; 00285 LPUART_InitStruct->Parity = LL_LPUART_PARITY_NONE ; 00286 LPUART_InitStruct->TransferDirection = LL_LPUART_DIRECTION_TX_RX; 00287 LPUART_InitStruct->HardwareFlowControl = LL_LPUART_HWCONTROL_NONE; 00288 } 00289 00290 /** 00291 * @} 00292 */ 00293 00294 /** 00295 * @} 00296 */ 00297 00298 /** 00299 * @} 00300 */ 00301 00302 #endif /* defined (LPUART1) */ 00303 00304 /** 00305 * @} 00306 */ 00307 00308 #endif /* USE_FULL_LL_DRIVER */ 00309 00310 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 00311