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