1 /**
2 ******************************************************************************
3 * @file stm32l4xx_ll_usart.c
4 * @author MCD Application Team
5 * @brief USART LL module driver.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>© 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 "stm32l4xx_ll_usart.h"
23 #include "stm32l4xx_ll_rcc.h"
24 #include "stm32l4xx_ll_bus.h"
25 #ifdef USE_FULL_ASSERT
26 #include "stm32_assert.h"
27 #else
28 #define assert_param(expr) ((void)0U)
29 #endif /* USE_FULL_ASSERT */
30
31 /** @addtogroup STM32L4xx_LL_Driver
32 * @{
33 */
34
35 #if defined (USART1) || defined (USART2) || defined (USART3) || defined (UART4) || defined (UART5)
36
37 /** @addtogroup USART_LL
38 * @{
39 */
40
41 /* Private types -------------------------------------------------------------*/
42 /* Private variables ---------------------------------------------------------*/
43 /* Private constants ---------------------------------------------------------*/
44 /* Private macros ------------------------------------------------------------*/
45 /** @addtogroup USART_LL_Private_Macros
46 * @{
47 */
48
49 #if defined(USART_PRESC_PRESCALER)
50 #define IS_LL_USART_PRESCALER(__VALUE__) (((__VALUE__) == LL_USART_PRESCALER_DIV1) \
51 || ((__VALUE__) == LL_USART_PRESCALER_DIV2) \
52 || ((__VALUE__) == LL_USART_PRESCALER_DIV4) \
53 || ((__VALUE__) == LL_USART_PRESCALER_DIV6) \
54 || ((__VALUE__) == LL_USART_PRESCALER_DIV8) \
55 || ((__VALUE__) == LL_USART_PRESCALER_DIV10) \
56 || ((__VALUE__) == LL_USART_PRESCALER_DIV12) \
57 || ((__VALUE__) == LL_USART_PRESCALER_DIV16) \
58 || ((__VALUE__) == LL_USART_PRESCALER_DIV32) \
59 || ((__VALUE__) == LL_USART_PRESCALER_DIV64) \
60 || ((__VALUE__) == LL_USART_PRESCALER_DIV128) \
61 || ((__VALUE__) == LL_USART_PRESCALER_DIV256))
62
63 #endif /* USART_PRESC_PRESCALER */
64 /* __BAUDRATE__ The maximum Baud Rate is derived from the maximum clock available
65 * divided by the smallest oversampling used on the USART (i.e. 8) */
66 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
67 #define IS_LL_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 15000000U)
68 #else
69 #define IS_LL_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 10000000U)
70 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
71
72 /* __VALUE__ In case of oversampling by 16 and 8, BRR content must be greater than or equal to 16d. */
73 #define IS_LL_USART_BRR_MIN(__VALUE__) ((__VALUE__) >= 16U)
74
75 /* __VALUE__ BRR content must be lower than or equal to 0xFFFF. */
76 #define IS_LL_USART_BRR_MAX(__VALUE__) ((__VALUE__) <= 0x0000FFFFU)
77
78 #define IS_LL_USART_DIRECTION(__VALUE__) (((__VALUE__) == LL_USART_DIRECTION_NONE) \
79 || ((__VALUE__) == LL_USART_DIRECTION_RX) \
80 || ((__VALUE__) == LL_USART_DIRECTION_TX) \
81 || ((__VALUE__) == LL_USART_DIRECTION_TX_RX))
82
83 #define IS_LL_USART_PARITY(__VALUE__) (((__VALUE__) == LL_USART_PARITY_NONE) \
84 || ((__VALUE__) == LL_USART_PARITY_EVEN) \
85 || ((__VALUE__) == LL_USART_PARITY_ODD))
86
87 #define IS_LL_USART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_USART_DATAWIDTH_7B) \
88 || ((__VALUE__) == LL_USART_DATAWIDTH_8B) \
89 || ((__VALUE__) == LL_USART_DATAWIDTH_9B))
90
91 #define IS_LL_USART_OVERSAMPLING(__VALUE__) (((__VALUE__) == LL_USART_OVERSAMPLING_16) \
92 || ((__VALUE__) == LL_USART_OVERSAMPLING_8))
93
94 #define IS_LL_USART_LASTBITCLKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_LASTCLKPULSE_NO_OUTPUT) \
95 || ((__VALUE__) == LL_USART_LASTCLKPULSE_OUTPUT))
96
97 #define IS_LL_USART_CLOCKPHASE(__VALUE__) (((__VALUE__) == LL_USART_PHASE_1EDGE) \
98 || ((__VALUE__) == LL_USART_PHASE_2EDGE))
99
100 #define IS_LL_USART_CLOCKPOLARITY(__VALUE__) (((__VALUE__) == LL_USART_POLARITY_LOW) \
101 || ((__VALUE__) == LL_USART_POLARITY_HIGH))
102
103 #define IS_LL_USART_CLOCKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_CLOCK_DISABLE) \
104 || ((__VALUE__) == LL_USART_CLOCK_ENABLE))
105
106 #define IS_LL_USART_STOPBITS(__VALUE__) (((__VALUE__) == LL_USART_STOPBITS_0_5) \
107 || ((__VALUE__) == LL_USART_STOPBITS_1) \
108 || ((__VALUE__) == LL_USART_STOPBITS_1_5) \
109 || ((__VALUE__) == LL_USART_STOPBITS_2))
110
111 #define IS_LL_USART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_USART_HWCONTROL_NONE) \
112 || ((__VALUE__) == LL_USART_HWCONTROL_RTS) \
113 || ((__VALUE__) == LL_USART_HWCONTROL_CTS) \
114 || ((__VALUE__) == LL_USART_HWCONTROL_RTS_CTS))
115
116 /**
117 * @}
118 */
119
120 /* Private function prototypes -----------------------------------------------*/
121
122 /* Exported functions --------------------------------------------------------*/
123 /** @addtogroup USART_LL_Exported_Functions
124 * @{
125 */
126
127 /** @addtogroup USART_LL_EF_Init
128 * @{
129 */
130
131 /**
132 * @brief De-initialize USART registers (Registers restored to their default values).
133 * @param USARTx USART Instance
134 * @retval An ErrorStatus enumeration value:
135 * - SUCCESS: USART registers are de-initialized
136 * - ERROR: USART registers are not de-initialized
137 */
LL_USART_DeInit(USART_TypeDef * USARTx)138 ErrorStatus LL_USART_DeInit(USART_TypeDef *USARTx)
139 {
140 ErrorStatus status = SUCCESS;
141
142 /* Check the parameters */
143 assert_param(IS_UART_INSTANCE(USARTx));
144
145 if (USARTx == USART1)
146 {
147 /* Force reset of USART clock */
148 LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART1);
149
150 /* Release reset of USART clock */
151 LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART1);
152 }
153 else if (USARTx == USART2)
154 {
155 /* Force reset of USART clock */
156 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART2);
157
158 /* Release reset of USART clock */
159 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART2);
160 }
161 #if defined(USART3)
162 else if (USARTx == USART3)
163 {
164 /* Force reset of USART clock */
165 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART3);
166
167 /* Release reset of USART clock */
168 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART3);
169 }
170 #endif /* USART3 */
171 #if defined(UART4)
172 else if (USARTx == UART4)
173 {
174 /* Force reset of UART clock */
175 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART4);
176
177 /* Release reset of UART clock */
178 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART4);
179 }
180 #endif /* UART4 */
181 #if defined(UART5)
182 else if (USARTx == UART5)
183 {
184 /* Force reset of UART clock */
185 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART5);
186
187 /* Release reset of UART clock */
188 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART5);
189 }
190 #endif /* UART5 */
191 else
192 {
193 status = ERROR;
194 }
195
196 return (status);
197 }
198
199 /**
200 * @brief Initialize USART registers according to the specified
201 * parameters in USART_InitStruct.
202 * @note As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
203 * USART Peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
204 * @note Baud rate value stored in USART_InitStruct BaudRate field, should be valid (different from 0).
205 * @param USARTx USART Instance
206 * @param USART_InitStruct pointer to a LL_USART_InitTypeDef structure
207 * that contains the configuration information for the specified USART peripheral.
208 * @retval An ErrorStatus enumeration value:
209 * - SUCCESS: USART registers are initialized according to USART_InitStruct content
210 * - ERROR: Problem occurred during USART Registers initialization
211 */
LL_USART_Init(USART_TypeDef * USARTx,LL_USART_InitTypeDef * USART_InitStruct)212 ErrorStatus LL_USART_Init(USART_TypeDef *USARTx, LL_USART_InitTypeDef *USART_InitStruct)
213 {
214 ErrorStatus status = ERROR;
215 uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
216
217 /* Check the parameters */
218 assert_param(IS_UART_INSTANCE(USARTx));
219 #if defined(USART_PRESC_PRESCALER)
220 assert_param(IS_LL_USART_PRESCALER(USART_InitStruct->PrescalerValue));
221 #endif /* USART_PRESC_PRESCALER */
222 assert_param(IS_LL_USART_BAUDRATE(USART_InitStruct->BaudRate));
223 assert_param(IS_LL_USART_DATAWIDTH(USART_InitStruct->DataWidth));
224 assert_param(IS_LL_USART_STOPBITS(USART_InitStruct->StopBits));
225 assert_param(IS_LL_USART_PARITY(USART_InitStruct->Parity));
226 assert_param(IS_LL_USART_DIRECTION(USART_InitStruct->TransferDirection));
227 assert_param(IS_LL_USART_HWCONTROL(USART_InitStruct->HardwareFlowControl));
228 assert_param(IS_LL_USART_OVERSAMPLING(USART_InitStruct->OverSampling));
229
230 /* USART needs to be in disabled state, in order to be able to configure some bits in
231 CRx registers */
232 if (LL_USART_IsEnabled(USARTx) == 0U)
233 {
234 /*---------------------------- USART CR1 Configuration ---------------------
235 * Configure USARTx CR1 (USART Word Length, Parity, Mode and Oversampling bits) with parameters:
236 * - DataWidth: USART_CR1_M bits according to USART_InitStruct->DataWidth value
237 * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to USART_InitStruct->Parity value
238 * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to USART_InitStruct->TransferDirection value
239 * - Oversampling: USART_CR1_OVER8 bit according to USART_InitStruct->OverSampling value.
240 */
241 MODIFY_REG(USARTx->CR1,
242 (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
243 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
244 (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
245 USART_InitStruct->TransferDirection | USART_InitStruct->OverSampling));
246
247 /*---------------------------- USART CR2 Configuration ---------------------
248 * Configure USARTx CR2 (Stop bits) with parameters:
249 * - Stop Bits: USART_CR2_STOP bits according to USART_InitStruct->StopBits value.
250 * - CLKEN, CPOL, CPHA and LBCL bits are to be configured using LL_USART_ClockInit().
251 */
252 LL_USART_SetStopBitsLength(USARTx, USART_InitStruct->StopBits);
253
254 /*---------------------------- USART CR3 Configuration ---------------------
255 * Configure USARTx CR3 (Hardware Flow Control) with parameters:
256 * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to USART_InitStruct->HardwareFlowControl value.
257 */
258 LL_USART_SetHWFlowCtrl(USARTx, USART_InitStruct->HardwareFlowControl);
259
260 /*---------------------------- USART BRR Configuration ---------------------
261 * Retrieve Clock frequency used for USART Peripheral
262 */
263 if (USARTx == USART1)
264 {
265 periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART1_CLKSOURCE);
266 }
267 else if (USARTx == USART2)
268 {
269 periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART2_CLKSOURCE);
270 }
271 #if defined(USART3)
272 else if (USARTx == USART3)
273 {
274 periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART3_CLKSOURCE);
275 }
276 #endif /* USART3 */
277 #if defined(UART4)
278 else if (USARTx == UART4)
279 {
280 periphclk = LL_RCC_GetUARTClockFreq(LL_RCC_UART4_CLKSOURCE);
281 }
282 #endif /* UART4 */
283 #if defined(UART5)
284 else if (USARTx == UART5)
285 {
286 periphclk = LL_RCC_GetUARTClockFreq(LL_RCC_UART5_CLKSOURCE);
287 }
288 #endif /* UART5 */
289 else
290 {
291 /* Nothing to do, as error code is already assigned to ERROR value */
292 }
293
294 /* Configure the USART Baud Rate :
295 #if defined(USART_PRESC_PRESCALER)
296 - prescaler value is required
297 #endif
298 - valid baud rate value (different from 0) is required
299 - Peripheral clock as returned by RCC service, should be valid (different from 0).
300 */
301 if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
302 && (USART_InitStruct->BaudRate != 0U))
303 {
304 status = SUCCESS;
305 LL_USART_SetBaudRate(USARTx,
306 periphclk,
307 #if defined(USART_PRESC_PRESCALER)
308 USART_InitStruct->PrescalerValue,
309 #endif /* USART_PRESC_PRESCALER */
310 USART_InitStruct->OverSampling,
311 USART_InitStruct->BaudRate);
312
313 /* Check BRR is greater than or equal to 16d */
314 assert_param(IS_LL_USART_BRR_MIN(USARTx->BRR));
315
316 /* Check BRR is lower than or equal to 0xFFFF */
317 assert_param(IS_LL_USART_BRR_MAX(USARTx->BRR));
318 }
319 #if defined(USART_PRESC_PRESCALER)
320
321 /*---------------------------- USART PRESC Configuration -----------------------
322 * Configure USARTx PRESC (Prescaler) with parameters:
323 * - PrescalerValue: USART_PRESC_PRESCALER bits according to USART_InitStruct->PrescalerValue value.
324 */
325 LL_USART_SetPrescaler(USARTx, USART_InitStruct->PrescalerValue);
326 #endif /* USART_PRESC_PRESCALER */
327 }
328 /* Endif (=> USART not in Disabled state => return ERROR) */
329
330 return (status);
331 }
332
333 /**
334 * @brief Set each @ref LL_USART_InitTypeDef field to default value.
335 * @param USART_InitStruct pointer to a @ref LL_USART_InitTypeDef structure
336 * whose fields will be set to default values.
337 * @retval None
338 */
339
LL_USART_StructInit(LL_USART_InitTypeDef * USART_InitStruct)340 void LL_USART_StructInit(LL_USART_InitTypeDef *USART_InitStruct)
341 {
342 /* Set USART_InitStruct fields to default values */
343 #if defined(USART_PRESC_PRESCALER)
344 USART_InitStruct->PrescalerValue = LL_USART_PRESCALER_DIV1;
345 #endif /* USART_PRESC_PRESCALER */
346 USART_InitStruct->BaudRate = 9600U;
347 USART_InitStruct->DataWidth = LL_USART_DATAWIDTH_8B;
348 USART_InitStruct->StopBits = LL_USART_STOPBITS_1;
349 USART_InitStruct->Parity = LL_USART_PARITY_NONE ;
350 USART_InitStruct->TransferDirection = LL_USART_DIRECTION_TX_RX;
351 USART_InitStruct->HardwareFlowControl = LL_USART_HWCONTROL_NONE;
352 USART_InitStruct->OverSampling = LL_USART_OVERSAMPLING_16;
353 }
354
355 /**
356 * @brief Initialize USART Clock related settings according to the
357 * specified parameters in the USART_ClockInitStruct.
358 * @note As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
359 * USART Peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
360 * @param USARTx USART Instance
361 * @param USART_ClockInitStruct pointer to a @ref LL_USART_ClockInitTypeDef structure
362 * that contains the Clock configuration information for the specified USART peripheral.
363 * @retval An ErrorStatus enumeration value:
364 * - SUCCESS: USART registers related to Clock settings are initialized according to USART_ClockInitStruct content
365 * - ERROR: Problem occurred during USART Registers initialization
366 */
LL_USART_ClockInit(USART_TypeDef * USARTx,LL_USART_ClockInitTypeDef * USART_ClockInitStruct)367 ErrorStatus LL_USART_ClockInit(USART_TypeDef *USARTx, LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
368 {
369 ErrorStatus status = SUCCESS;
370
371 /* Check USART Instance and Clock signal output parameters */
372 assert_param(IS_UART_INSTANCE(USARTx));
373 assert_param(IS_LL_USART_CLOCKOUTPUT(USART_ClockInitStruct->ClockOutput));
374
375 /* USART needs to be in disabled state, in order to be able to configure some bits in
376 CRx registers */
377 if (LL_USART_IsEnabled(USARTx) == 0U)
378 {
379 /*---------------------------- USART CR2 Configuration -----------------------*/
380 /* If Clock signal has to be output */
381 if (USART_ClockInitStruct->ClockOutput == LL_USART_CLOCK_DISABLE)
382 {
383 /* Deactivate Clock signal delivery :
384 * - Disable Clock Output: USART_CR2_CLKEN cleared
385 */
386 LL_USART_DisableSCLKOutput(USARTx);
387 }
388 else
389 {
390 /* Ensure USART instance is USART capable */
391 assert_param(IS_USART_INSTANCE(USARTx));
392
393 /* Check clock related parameters */
394 assert_param(IS_LL_USART_CLOCKPOLARITY(USART_ClockInitStruct->ClockPolarity));
395 assert_param(IS_LL_USART_CLOCKPHASE(USART_ClockInitStruct->ClockPhase));
396 assert_param(IS_LL_USART_LASTBITCLKOUTPUT(USART_ClockInitStruct->LastBitClockPulse));
397
398 /*---------------------------- USART CR2 Configuration -----------------------
399 * Configure USARTx CR2 (Clock signal related bits) with parameters:
400 * - Enable Clock Output: USART_CR2_CLKEN set
401 * - Clock Polarity: USART_CR2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
402 * - Clock Phase: USART_CR2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
403 * - Last Bit Clock Pulse Output: USART_CR2_LBCL bit according to USART_ClockInitStruct->LastBitClockPulse value.
404 */
405 MODIFY_REG(USARTx->CR2,
406 USART_CR2_CLKEN | USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_LBCL,
407 USART_CR2_CLKEN | USART_ClockInitStruct->ClockPolarity |
408 USART_ClockInitStruct->ClockPhase | USART_ClockInitStruct->LastBitClockPulse);
409 }
410 }
411 /* Else (USART not in Disabled state => return ERROR */
412 else
413 {
414 status = ERROR;
415 }
416
417 return (status);
418 }
419
420 /**
421 * @brief Set each field of a @ref LL_USART_ClockInitTypeDef type structure to default value.
422 * @param USART_ClockInitStruct pointer to a @ref LL_USART_ClockInitTypeDef structure
423 * whose fields will be set to default values.
424 * @retval None
425 */
LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef * USART_ClockInitStruct)426 void LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
427 {
428 /* Set LL_USART_ClockInitStruct fields with default values */
429 USART_ClockInitStruct->ClockOutput = LL_USART_CLOCK_DISABLE;
430 USART_ClockInitStruct->ClockPolarity = LL_USART_POLARITY_LOW; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
431 USART_ClockInitStruct->ClockPhase = LL_USART_PHASE_1EDGE; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
432 USART_ClockInitStruct->LastBitClockPulse = LL_USART_LASTCLKPULSE_NO_OUTPUT; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
433 }
434
435 /**
436 * @}
437 */
438
439 /**
440 * @}
441 */
442
443 /**
444 * @}
445 */
446
447 #endif /* USART1 || USART2 || USART3 || UART4 || UART5 */
448
449 /**
450 * @}
451 */
452
453 #endif /* USE_FULL_LL_DRIVER */
454
455 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
456
457