STM32L443xx 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 * Copyright (c) 2017 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 "stm32l4xx_ll_lpuart.h" 00022 #include "stm32l4xx_ll_rcc.h" 00023 #include "stm32l4xx_ll_bus.h" 00024 #ifdef USE_FULL_ASSERT 00025 #include "stm32_assert.h" 00026 #else 00027 #define assert_param(expr) ((void)0U) 00028 #endif /* USE_FULL_ASSERT */ 00029 00030 /** @addtogroup STM32L4xx_LL_Driver 00031 * @{ 00032 */ 00033 00034 #if defined (LPUART1) 00035 00036 /** @addtogroup LPUART_LL 00037 * @{ 00038 */ 00039 00040 /* Private types -------------------------------------------------------------*/ 00041 /* Private variables ---------------------------------------------------------*/ 00042 /* Private constants ---------------------------------------------------------*/ 00043 /** @addtogroup LPUART_LL_Private_Constants 00044 * @{ 00045 */ 00046 00047 /** 00048 * @} 00049 */ 00050 00051 00052 /* Private macros ------------------------------------------------------------*/ 00053 /** @addtogroup LPUART_LL_Private_Macros 00054 * @{ 00055 */ 00056 00057 /* Check of parameters for configuration of LPUART registers */ 00058 00059 #if defined(USART_PRESC_PRESCALER) 00060 #define IS_LL_LPUART_PRESCALER(__VALUE__) (((__VALUE__) == LL_LPUART_PRESCALER_DIV1) \ 00061 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV2) \ 00062 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV4) \ 00063 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV6) \ 00064 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV8) \ 00065 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV10) \ 00066 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV12) \ 00067 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV16) \ 00068 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV32) \ 00069 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV64) \ 00070 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV128) \ 00071 || ((__VALUE__) == LL_LPUART_PRESCALER_DIV256)) 00072 00073 #endif /* USART_PRESC_PRESCALER */ 00074 /* __BAUDRATE__ Depending on constraints applicable for LPUART BRR register */ 00075 /* value : */ 00076 /* - fck must be in the range [3 x baudrate, 4096 x baudrate] */ 00077 /* - LPUART_BRR register value should be >= 0x300 */ 00078 /* - LPUART_BRR register value should be <= 0xFFFFF (20 bits) */ 00079 /* Baudrate specified by the user should belong to [8, 40000000].*/ 00080 #define IS_LL_LPUART_BAUDRATE(__BAUDRATE__) (((__BAUDRATE__) <= 40000000U) && ((__BAUDRATE__) >= 8U)) 00081 00082 /* __VALUE__ BRR content must be greater than or equal to 0x300. */ 00083 #define IS_LL_LPUART_BRR_MIN(__VALUE__) ((__VALUE__) >= 0x300U) 00084 00085 /* __VALUE__ BRR content must be lower than or equal to 0xFFFFF. */ 00086 #define IS_LL_LPUART_BRR_MAX(__VALUE__) ((__VALUE__) <= 0x000FFFFFU) 00087 00088 #define IS_LL_LPUART_DIRECTION(__VALUE__) (((__VALUE__) == LL_LPUART_DIRECTION_NONE) \ 00089 || ((__VALUE__) == LL_LPUART_DIRECTION_RX) \ 00090 || ((__VALUE__) == LL_LPUART_DIRECTION_TX) \ 00091 || ((__VALUE__) == LL_LPUART_DIRECTION_TX_RX)) 00092 00093 #define IS_LL_LPUART_PARITY(__VALUE__) (((__VALUE__) == LL_LPUART_PARITY_NONE) \ 00094 || ((__VALUE__) == LL_LPUART_PARITY_EVEN) \ 00095 || ((__VALUE__) == LL_LPUART_PARITY_ODD)) 00096 00097 #define IS_LL_LPUART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_LPUART_DATAWIDTH_7B) \ 00098 || ((__VALUE__) == LL_LPUART_DATAWIDTH_8B) \ 00099 || ((__VALUE__) == LL_LPUART_DATAWIDTH_9B)) 00100 00101 #define IS_LL_LPUART_STOPBITS(__VALUE__) (((__VALUE__) == LL_LPUART_STOPBITS_1) \ 00102 || ((__VALUE__) == LL_LPUART_STOPBITS_2)) 00103 00104 #define IS_LL_LPUART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_LPUART_HWCONTROL_NONE) \ 00105 || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS) \ 00106 || ((__VALUE__) == LL_LPUART_HWCONTROL_CTS) \ 00107 || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS_CTS)) 00108 00109 /** 00110 * @} 00111 */ 00112 00113 /* Private function prototypes -----------------------------------------------*/ 00114 00115 /* Exported functions --------------------------------------------------------*/ 00116 /** @addtogroup LPUART_LL_Exported_Functions 00117 * @{ 00118 */ 00119 00120 /** @addtogroup LPUART_LL_EF_Init 00121 * @{ 00122 */ 00123 00124 /** 00125 * @brief De-initialize LPUART registers (Registers restored to their default values). 00126 * @param LPUARTx LPUART Instance 00127 * @retval An ErrorStatus enumeration value: 00128 * - SUCCESS: LPUART registers are de-initialized 00129 * - ERROR: not applicable 00130 */ 00131 ErrorStatus LL_LPUART_DeInit(USART_TypeDef *LPUARTx) 00132 { 00133 ErrorStatus status = SUCCESS; 00134 00135 /* Check the parameters */ 00136 assert_param(IS_LPUART_INSTANCE(LPUARTx)); 00137 00138 if (LPUARTx == LPUART1) 00139 { 00140 /* Force reset of LPUART peripheral */ 00141 LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_LPUART1); 00142 00143 /* Release reset of LPUART peripheral */ 00144 LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_LPUART1); 00145 } 00146 else 00147 { 00148 status = ERROR; 00149 } 00150 00151 return (status); 00152 } 00153 00154 /** 00155 * @brief Initialize LPUART registers according to the specified 00156 * parameters in LPUART_InitStruct. 00157 * @note As some bits in LPUART configuration registers can only be written when 00158 * the LPUART is disabled (USART_CR1_UE bit =0), 00159 * LPUART Peripheral should be in disabled state prior calling this function. 00160 * Otherwise, ERROR result will be returned. 00161 * @note Baud rate value stored in LPUART_InitStruct BaudRate field, should be valid (different from 0). 00162 * @param LPUARTx LPUART Instance 00163 * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure 00164 * that contains the configuration information for the specified LPUART peripheral. 00165 * @retval An ErrorStatus enumeration value: 00166 * - SUCCESS: LPUART registers are initialized according to LPUART_InitStruct content 00167 * - ERROR: Problem occurred during LPUART Registers initialization 00168 */ 00169 ErrorStatus LL_LPUART_Init(USART_TypeDef *LPUARTx, LL_LPUART_InitTypeDef *LPUART_InitStruct) 00170 { 00171 ErrorStatus status = ERROR; 00172 uint32_t periphclk; 00173 00174 /* Check the parameters */ 00175 assert_param(IS_LPUART_INSTANCE(LPUARTx)); 00176 #if defined(USART_PRESC_PRESCALER) 00177 assert_param(IS_LL_LPUART_PRESCALER(LPUART_InitStruct->PrescalerValue)); 00178 #endif /* USART_PRESC_PRESCALER */ 00179 assert_param(IS_LL_LPUART_BAUDRATE(LPUART_InitStruct->BaudRate)); 00180 assert_param(IS_LL_LPUART_DATAWIDTH(LPUART_InitStruct->DataWidth)); 00181 assert_param(IS_LL_LPUART_STOPBITS(LPUART_InitStruct->StopBits)); 00182 assert_param(IS_LL_LPUART_PARITY(LPUART_InitStruct->Parity)); 00183 assert_param(IS_LL_LPUART_DIRECTION(LPUART_InitStruct->TransferDirection)); 00184 assert_param(IS_LL_LPUART_HWCONTROL(LPUART_InitStruct->HardwareFlowControl)); 00185 00186 /* LPUART needs to be in disabled state, in order to be able to configure some bits in 00187 CRx registers. Otherwise (LPUART not in Disabled state) => return ERROR */ 00188 if (LL_LPUART_IsEnabled(LPUARTx) == 0U) 00189 { 00190 /*---------------------------- LPUART CR1 Configuration ----------------------- 00191 * Configure LPUARTx CR1 (LPUART Word Length, Parity and Transfer Direction bits) with parameters: 00192 * - DataWidth: USART_CR1_M bits according to LPUART_InitStruct->DataWidth value 00193 * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to LPUART_InitStruct->Parity value 00194 * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to LPUART_InitStruct->TransferDirection value 00195 */ 00196 MODIFY_REG(LPUARTx->CR1, 00197 (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE), 00198 (LPUART_InitStruct->DataWidth | LPUART_InitStruct->Parity | LPUART_InitStruct->TransferDirection)); 00199 00200 /*---------------------------- LPUART CR2 Configuration ----------------------- 00201 * Configure LPUARTx CR2 (Stop bits) with parameters: 00202 * - Stop Bits: USART_CR2_STOP bits according to LPUART_InitStruct->StopBits value. 00203 */ 00204 LL_LPUART_SetStopBitsLength(LPUARTx, LPUART_InitStruct->StopBits); 00205 00206 /*---------------------------- LPUART CR3 Configuration ----------------------- 00207 * Configure LPUARTx CR3 (Hardware Flow Control) with parameters: 00208 * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according 00209 * to LPUART_InitStruct->HardwareFlowControl value. 00210 */ 00211 LL_LPUART_SetHWFlowCtrl(LPUARTx, LPUART_InitStruct->HardwareFlowControl); 00212 00213 /*---------------------------- LPUART BRR Configuration ----------------------- 00214 * Retrieve Clock frequency used for LPUART Peripheral 00215 */ 00216 periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE); 00217 00218 /* Configure the LPUART Baud Rate : 00219 #if defined(USART_PRESC_PRESCALER) 00220 - prescaler value is required 00221 #endif 00222 - valid baud rate value (different from 0) is required 00223 - Peripheral clock as returned by RCC service, should be valid (different from 0). 00224 */ 00225 if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO) 00226 && (LPUART_InitStruct->BaudRate != 0U)) 00227 { 00228 status = SUCCESS; 00229 LL_LPUART_SetBaudRate(LPUARTx, 00230 periphclk, 00231 #if defined(USART_PRESC_PRESCALER) 00232 LPUART_InitStruct->PrescalerValue, 00233 #endif /* USART_PRESC_PRESCALER */ 00234 LPUART_InitStruct->BaudRate); 00235 00236 /* Check BRR is greater than or equal to 0x300 */ 00237 assert_param(IS_LL_LPUART_BRR_MIN(LPUARTx->BRR)); 00238 00239 /* Check BRR is lower than or equal to 0xFFFFF */ 00240 assert_param(IS_LL_LPUART_BRR_MAX(LPUARTx->BRR)); 00241 } 00242 00243 #if defined(USART_PRESC_PRESCALER) 00244 /*---------------------------- LPUART PRESC Configuration ----------------------- 00245 * Configure LPUARTx PRESC (Prescaler) with parameters: 00246 * - PrescalerValue: LPUART_PRESC_PRESCALER bits according to LPUART_InitStruct->PrescalerValue value. 00247 */ 00248 LL_LPUART_SetPrescaler(LPUARTx, LPUART_InitStruct->PrescalerValue); 00249 #endif /* USART_PRESC_PRESCALER */ 00250 } 00251 00252 return (status); 00253 } 00254 00255 /** 00256 * @brief Set each @ref LL_LPUART_InitTypeDef field to default value. 00257 * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure 00258 * whose fields will be set to default values. 00259 * @retval None 00260 */ 00261 00262 void LL_LPUART_StructInit(LL_LPUART_InitTypeDef *LPUART_InitStruct) 00263 { 00264 /* Set LPUART_InitStruct fields to default values */ 00265 #if defined(USART_PRESC_PRESCALER) 00266 LPUART_InitStruct->PrescalerValue = LL_LPUART_PRESCALER_DIV1; 00267 #endif /* USART_PRESC_PRESCALER */ 00268 LPUART_InitStruct->BaudRate = 9600U; 00269 LPUART_InitStruct->DataWidth = LL_LPUART_DATAWIDTH_8B; 00270 LPUART_InitStruct->StopBits = LL_LPUART_STOPBITS_1; 00271 LPUART_InitStruct->Parity = LL_LPUART_PARITY_NONE ; 00272 LPUART_InitStruct->TransferDirection = LL_LPUART_DIRECTION_TX_RX; 00273 LPUART_InitStruct->HardwareFlowControl = LL_LPUART_HWCONTROL_NONE; 00274 } 00275 00276 /** 00277 * @} 00278 */ 00279 00280 /** 00281 * @} 00282 */ 00283 00284 /** 00285 * @} 00286 */ 00287 00288 #endif /* LPUART1 */ 00289 00290 /** 00291 * @} 00292 */ 00293 00294 #endif /* USE_FULL_LL_DRIVER */