2 ******************************************************************************
3 * @file stm32h7xx_ll_usart.c
4 * @author MCD Application Team
5 * @brief USART LL module driver.
6 ******************************************************************************
9 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
10 * All rights reserved.</center></h2>
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
17 ******************************************************************************
19 #if defined(USE_FULL_LL_DRIVER)
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32h7xx_ll_usart.h"
23 #include "stm32h7xx_ll_rcc.h"
24 #include "stm32h7xx_ll_bus.h"
25 #ifdef USE_FULL_ASSERT
26 #include "stm32_assert.h"
28 #define assert_param(expr) ((void)0U)
29 #endif /* USE_FULL_ASSERT */
31 /** @addtogroup STM32H7xx_LL_Driver
35 #if defined (USART1) || defined (USART2) || defined (USART3) || defined (USART6) || defined (UART4) || defined (UART5) || defined (UART7) || defined (UART8) || defined (UART9) || defined (USART10)
37 /** @addtogroup USART_LL
41 /* Private types -------------------------------------------------------------*/
42 /* Private variables ---------------------------------------------------------*/
43 /* Private constants ---------------------------------------------------------*/
44 /* Private macros ------------------------------------------------------------*/
45 /** @addtogroup USART_LL_Private_Macros
49 #define IS_LL_USART_PRESCALER(__VALUE__) (((__VALUE__) == LL_USART_PRESCALER_DIV1) \
50 || ((__VALUE__) == LL_USART_PRESCALER_DIV2) \
51 || ((__VALUE__) == LL_USART_PRESCALER_DIV4) \
52 || ((__VALUE__) == LL_USART_PRESCALER_DIV6) \
53 || ((__VALUE__) == LL_USART_PRESCALER_DIV8) \
54 || ((__VALUE__) == LL_USART_PRESCALER_DIV10) \
55 || ((__VALUE__) == LL_USART_PRESCALER_DIV12) \
56 || ((__VALUE__) == LL_USART_PRESCALER_DIV16) \
57 || ((__VALUE__) == LL_USART_PRESCALER_DIV32) \
58 || ((__VALUE__) == LL_USART_PRESCALER_DIV64) \
59 || ((__VALUE__) == LL_USART_PRESCALER_DIV128) \
60 || ((__VALUE__) == LL_USART_PRESCALER_DIV256))
62 /* __BAUDRATE__ The maximum Baud Rate is derived from the maximum clock available
63 * divided by the smallest oversampling used on the USART (i.e. 8) */
64 #define IS_LL_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 12500000U)
66 /* __VALUE__ In case of oversampling by 16 and 8, BRR content must be greater than or equal to 16d. */
67 #define IS_LL_USART_BRR_MIN(__VALUE__) ((__VALUE__) >= 16U)
69 /* __VALUE__ BRR content must be lower than or equal to 0xFFFF. */
70 #define IS_LL_USART_BRR_MAX(__VALUE__) ((__VALUE__) <= 0x0000FFFFU)
72 #define IS_LL_USART_DIRECTION(__VALUE__) (((__VALUE__) == LL_USART_DIRECTION_NONE) \
73 || ((__VALUE__) == LL_USART_DIRECTION_RX) \
74 || ((__VALUE__) == LL_USART_DIRECTION_TX) \
75 || ((__VALUE__) == LL_USART_DIRECTION_TX_RX))
77 #define IS_LL_USART_PARITY(__VALUE__) (((__VALUE__) == LL_USART_PARITY_NONE) \
78 || ((__VALUE__) == LL_USART_PARITY_EVEN) \
79 || ((__VALUE__) == LL_USART_PARITY_ODD))
81 #define IS_LL_USART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_USART_DATAWIDTH_7B) \
82 || ((__VALUE__) == LL_USART_DATAWIDTH_8B) \
83 || ((__VALUE__) == LL_USART_DATAWIDTH_9B))
85 #define IS_LL_USART_OVERSAMPLING(__VALUE__) (((__VALUE__) == LL_USART_OVERSAMPLING_16) \
86 || ((__VALUE__) == LL_USART_OVERSAMPLING_8))
88 #define IS_LL_USART_LASTBITCLKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_LASTCLKPULSE_NO_OUTPUT) \
89 || ((__VALUE__) == LL_USART_LASTCLKPULSE_OUTPUT))
91 #define IS_LL_USART_CLOCKPHASE(__VALUE__) (((__VALUE__) == LL_USART_PHASE_1EDGE) \
92 || ((__VALUE__) == LL_USART_PHASE_2EDGE))
94 #define IS_LL_USART_CLOCKPOLARITY(__VALUE__) (((__VALUE__) == LL_USART_POLARITY_LOW) \
95 || ((__VALUE__) == LL_USART_POLARITY_HIGH))
97 #define IS_LL_USART_CLOCKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_CLOCK_DISABLE) \
98 || ((__VALUE__) == LL_USART_CLOCK_ENABLE))
100 #define IS_LL_USART_STOPBITS(__VALUE__) (((__VALUE__) == LL_USART_STOPBITS_0_5) \
101 || ((__VALUE__) == LL_USART_STOPBITS_1) \
102 || ((__VALUE__) == LL_USART_STOPBITS_1_5) \
103 || ((__VALUE__) == LL_USART_STOPBITS_2))
105 #define IS_LL_USART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_USART_HWCONTROL_NONE) \
106 || ((__VALUE__) == LL_USART_HWCONTROL_RTS) \
107 || ((__VALUE__) == LL_USART_HWCONTROL_CTS) \
108 || ((__VALUE__) == LL_USART_HWCONTROL_RTS_CTS))
114 /* Private function prototypes -----------------------------------------------*/
116 /* Exported functions --------------------------------------------------------*/
117 /** @addtogroup USART_LL_Exported_Functions
121 /** @addtogroup USART_LL_EF_Init
126 * @brief De-initialize USART registers (Registers restored to their default values).
127 * @param USARTx USART Instance
128 * @retval An ErrorStatus enumeration value:
129 * - SUCCESS: USART registers are de-initialized
130 * - ERROR: USART registers are not de-initialized
132 ErrorStatus
LL_USART_DeInit(USART_TypeDef
*USARTx
)
134 ErrorStatus status
= SUCCESS
;
136 /* Check the parameters */
137 assert_param(IS_UART_INSTANCE(USARTx
));
139 if (USARTx
== USART1
)
141 /* Force reset of USART clock */
142 LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART1
);
144 /* Release reset of USART clock */
145 LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART1
);
147 else if (USARTx
== USART2
)
149 /* Force reset of USART clock */
150 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART2
);
152 /* Release reset of USART clock */
153 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART2
);
155 else if (USARTx
== USART3
)
157 /* Force reset of USART clock */
158 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART3
);
160 /* Release reset of USART clock */
161 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART3
);
163 else if (USARTx
== UART4
)
165 /* Force reset of UART clock */
166 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART4
);
168 /* Release reset of UART clock */
169 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART4
);
171 else if (USARTx
== UART5
)
173 /* Force reset of UART clock */
174 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART5
);
176 /* Release reset of UART clock */
177 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART5
);
179 else if (USARTx
== USART6
)
181 /* Force reset of USART clock */
182 LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART6
);
184 /* Release reset of USART clock */
185 LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART6
);
187 else if (USARTx
== UART7
)
189 /* Force reset of UART clock */
190 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART7
);
192 /* Release reset of UART clock */
193 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART7
);
195 else if (USARTx
== UART8
)
197 /* Force reset of UART clock */
198 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART8
);
200 /* Release reset of UART clock */
201 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART8
);
204 else if (USARTx
== UART9
)
206 /* Force reset of UART clock */
207 LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_UART9
);
209 /* Release reset of UART clock */
210 LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_UART9
);
214 else if (USARTx
== USART10
)
216 /* Force reset of USART clock */
217 LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART10
);
219 /* Release reset of USART clock */
220 LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART10
);
232 * @brief Initialize USART registers according to the specified
233 * parameters in USART_InitStruct.
234 * @note As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
235 * USART Peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
236 * @note Baud rate value stored in USART_InitStruct BaudRate field, should be valid (different from 0).
237 * @param USARTx USART Instance
238 * @param USART_InitStruct pointer to a LL_USART_InitTypeDef structure
239 * that contains the configuration information for the specified USART peripheral.
240 * @retval An ErrorStatus enumeration value:
241 * - SUCCESS: USART registers are initialized according to USART_InitStruct content
242 * - ERROR: Problem occurred during USART Registers initialization
244 ErrorStatus
LL_USART_Init(USART_TypeDef
*USARTx
, LL_USART_InitTypeDef
*USART_InitStruct
)
246 ErrorStatus status
= ERROR
;
247 uint32_t periphclk
= LL_RCC_PERIPH_FREQUENCY_NO
;
249 /* Check the parameters */
250 assert_param(IS_UART_INSTANCE(USARTx
));
251 assert_param(IS_LL_USART_PRESCALER(USART_InitStruct
->PrescalerValue
));
252 assert_param(IS_LL_USART_BAUDRATE(USART_InitStruct
->BaudRate
));
253 assert_param(IS_LL_USART_DATAWIDTH(USART_InitStruct
->DataWidth
));
254 assert_param(IS_LL_USART_STOPBITS(USART_InitStruct
->StopBits
));
255 assert_param(IS_LL_USART_PARITY(USART_InitStruct
->Parity
));
256 assert_param(IS_LL_USART_DIRECTION(USART_InitStruct
->TransferDirection
));
257 assert_param(IS_LL_USART_HWCONTROL(USART_InitStruct
->HardwareFlowControl
));
258 assert_param(IS_LL_USART_OVERSAMPLING(USART_InitStruct
->OverSampling
));
260 /* USART needs to be in disabled state, in order to be able to configure some bits in
262 if (LL_USART_IsEnabled(USARTx
) == 0U)
264 /*---------------------------- USART CR1 Configuration ---------------------
265 * Configure USARTx CR1 (USART Word Length, Parity, Mode and Oversampling bits) with parameters:
266 * - DataWidth: USART_CR1_M bits according to USART_InitStruct->DataWidth value
267 * - Parity: USART_CR1_PCE, USART_CR1_PS bits according to USART_InitStruct->Parity value
268 * - TransferDirection: USART_CR1_TE, USART_CR1_RE bits according to USART_InitStruct->TransferDirection value
269 * - Oversampling: USART_CR1_OVER8 bit according to USART_InitStruct->OverSampling value.
271 MODIFY_REG(USARTx
->CR1
,
272 (USART_CR1_M
| USART_CR1_PCE
| USART_CR1_PS
|
273 USART_CR1_TE
| USART_CR1_RE
| USART_CR1_OVER8
),
274 (USART_InitStruct
->DataWidth
| USART_InitStruct
->Parity
|
275 USART_InitStruct
->TransferDirection
| USART_InitStruct
->OverSampling
));
277 /*---------------------------- USART CR2 Configuration ---------------------
278 * Configure USARTx CR2 (Stop bits) with parameters:
279 * - Stop Bits: USART_CR2_STOP bits according to USART_InitStruct->StopBits value.
280 * - CLKEN, CPOL, CPHA and LBCL bits are to be configured using LL_USART_ClockInit().
282 LL_USART_SetStopBitsLength(USARTx
, USART_InitStruct
->StopBits
);
284 /*---------------------------- USART CR3 Configuration ---------------------
285 * Configure USARTx CR3 (Hardware Flow Control) with parameters:
286 * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to USART_InitStruct->HardwareFlowControl value.
288 LL_USART_SetHWFlowCtrl(USARTx
, USART_InitStruct
->HardwareFlowControl
);
290 /*---------------------------- USART BRR Configuration ---------------------
291 * Retrieve Clock frequency used for USART Peripheral
293 if (USARTx
== USART1
)
295 periphclk
= LL_RCC_GetUSARTClockFreq(LL_RCC_USART16_CLKSOURCE
);
297 else if (USARTx
== USART2
)
299 periphclk
= LL_RCC_GetUSARTClockFreq(LL_RCC_USART234578_CLKSOURCE
);
301 else if (USARTx
== USART3
)
303 periphclk
= LL_RCC_GetUSARTClockFreq(LL_RCC_USART234578_CLKSOURCE
);
305 else if (USARTx
== UART4
)
307 periphclk
= LL_RCC_GetUSARTClockFreq(LL_RCC_USART234578_CLKSOURCE
);
309 else if (USARTx
== UART5
)
311 periphclk
= LL_RCC_GetUSARTClockFreq(LL_RCC_USART234578_CLKSOURCE
);
313 else if (USARTx
== USART6
)
315 periphclk
= LL_RCC_GetUSARTClockFreq(LL_RCC_USART16_CLKSOURCE
);
317 else if (USARTx
== UART7
)
319 periphclk
= LL_RCC_GetUSARTClockFreq(LL_RCC_USART234578_CLKSOURCE
);
321 else if (USARTx
== UART8
)
323 periphclk
= LL_RCC_GetUSARTClockFreq(LL_RCC_USART234578_CLKSOURCE
);
326 else if (USARTx
== UART9
)
328 periphclk
= LL_RCC_GetUSARTClockFreq(LL_RCC_USART16_CLKSOURCE
);
332 else if (USARTx
== USART10
)
334 periphclk
= LL_RCC_GetUSARTClockFreq(LL_RCC_USART16_CLKSOURCE
);
339 /* Nothing to do, as error code is already assigned to ERROR value */
342 /* Configure the USART Baud Rate :
343 - prescaler value is required
344 - valid baud rate value (different from 0) is required
345 - Peripheral clock as returned by RCC service, should be valid (different from 0).
347 if ((periphclk
!= LL_RCC_PERIPH_FREQUENCY_NO
)
348 && (USART_InitStruct
->BaudRate
!= 0U))
351 LL_USART_SetBaudRate(USARTx
,
353 USART_InitStruct
->PrescalerValue
,
354 USART_InitStruct
->OverSampling
,
355 USART_InitStruct
->BaudRate
);
357 /* Check BRR is greater than or equal to 16d */
358 assert_param(IS_LL_USART_BRR_MIN(USARTx
->BRR
));
360 /* Check BRR is lower than or equal to 0xFFFF */
361 assert_param(IS_LL_USART_BRR_MAX(USARTx
->BRR
));
364 /*---------------------------- USART PRESC Configuration -----------------------
365 * Configure USARTx PRESC (Prescaler) with parameters:
366 * - PrescalerValue: USART_PRESC_PRESCALER bits according to USART_InitStruct->PrescalerValue value.
368 LL_USART_SetPrescaler(USARTx
, USART_InitStruct
->PrescalerValue
);
370 /* Endif (=> USART not in Disabled state => return ERROR) */
376 * @brief Set each @ref LL_USART_InitTypeDef field to default value.
377 * @param USART_InitStruct pointer to a @ref LL_USART_InitTypeDef structure
378 * whose fields will be set to default values.
382 void LL_USART_StructInit(LL_USART_InitTypeDef
*USART_InitStruct
)
384 /* Set USART_InitStruct fields to default values */
385 USART_InitStruct
->PrescalerValue
= LL_USART_PRESCALER_DIV1
;
386 USART_InitStruct
->BaudRate
= 9600U;
387 USART_InitStruct
->DataWidth
= LL_USART_DATAWIDTH_8B
;
388 USART_InitStruct
->StopBits
= LL_USART_STOPBITS_1
;
389 USART_InitStruct
->Parity
= LL_USART_PARITY_NONE
;
390 USART_InitStruct
->TransferDirection
= LL_USART_DIRECTION_TX_RX
;
391 USART_InitStruct
->HardwareFlowControl
= LL_USART_HWCONTROL_NONE
;
392 USART_InitStruct
->OverSampling
= LL_USART_OVERSAMPLING_16
;
396 * @brief Initialize USART Clock related settings according to the
397 * specified parameters in the USART_ClockInitStruct.
398 * @note As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
399 * USART Peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
400 * @param USARTx USART Instance
401 * @param USART_ClockInitStruct pointer to a @ref LL_USART_ClockInitTypeDef structure
402 * that contains the Clock configuration information for the specified USART peripheral.
403 * @retval An ErrorStatus enumeration value:
404 * - SUCCESS: USART registers related to Clock settings are initialized according to USART_ClockInitStruct content
405 * - ERROR: Problem occurred during USART Registers initialization
407 ErrorStatus
LL_USART_ClockInit(USART_TypeDef
*USARTx
, LL_USART_ClockInitTypeDef
*USART_ClockInitStruct
)
409 ErrorStatus status
= SUCCESS
;
411 /* Check USART Instance and Clock signal output parameters */
412 assert_param(IS_UART_INSTANCE(USARTx
));
413 assert_param(IS_LL_USART_CLOCKOUTPUT(USART_ClockInitStruct
->ClockOutput
));
415 /* USART needs to be in disabled state, in order to be able to configure some bits in
417 if (LL_USART_IsEnabled(USARTx
) == 0U)
419 /*---------------------------- USART CR2 Configuration -----------------------*/
420 /* If Clock signal has to be output */
421 if (USART_ClockInitStruct
->ClockOutput
== LL_USART_CLOCK_DISABLE
)
423 /* Deactivate Clock signal delivery :
424 * - Disable Clock Output: USART_CR2_CLKEN cleared
426 LL_USART_DisableSCLKOutput(USARTx
);
430 /* Ensure USART instance is USART capable */
431 assert_param(IS_USART_INSTANCE(USARTx
));
433 /* Check clock related parameters */
434 assert_param(IS_LL_USART_CLOCKPOLARITY(USART_ClockInitStruct
->ClockPolarity
));
435 assert_param(IS_LL_USART_CLOCKPHASE(USART_ClockInitStruct
->ClockPhase
));
436 assert_param(IS_LL_USART_LASTBITCLKOUTPUT(USART_ClockInitStruct
->LastBitClockPulse
));
438 /*---------------------------- USART CR2 Configuration -----------------------
439 * Configure USARTx CR2 (Clock signal related bits) with parameters:
440 * - Enable Clock Output: USART_CR2_CLKEN set
441 * - Clock Polarity: USART_CR2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
442 * - Clock Phase: USART_CR2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
443 * - Last Bit Clock Pulse Output: USART_CR2_LBCL bit according to USART_ClockInitStruct->LastBitClockPulse value.
445 MODIFY_REG(USARTx
->CR2
,
446 USART_CR2_CLKEN
| USART_CR2_CPHA
| USART_CR2_CPOL
| USART_CR2_LBCL
,
447 USART_CR2_CLKEN
| USART_ClockInitStruct
->ClockPolarity
|
448 USART_ClockInitStruct
->ClockPhase
| USART_ClockInitStruct
->LastBitClockPulse
);
451 /* Else (USART not in Disabled state => return ERROR */
461 * @brief Set each field of a @ref LL_USART_ClockInitTypeDef type structure to default value.
462 * @param USART_ClockInitStruct pointer to a @ref LL_USART_ClockInitTypeDef structure
463 * whose fields will be set to default values.
466 void LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef
*USART_ClockInitStruct
)
468 /* Set LL_USART_ClockInitStruct fields with default values */
469 USART_ClockInitStruct
->ClockOutput
= LL_USART_CLOCK_DISABLE
;
470 USART_ClockInitStruct
->ClockPolarity
= LL_USART_POLARITY_LOW
; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
471 USART_ClockInitStruct
->ClockPhase
= LL_USART_PHASE_1EDGE
; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
472 USART_ClockInitStruct
->LastBitClockPulse
= LL_USART_LASTCLKPULSE_NO_OUTPUT
; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
487 #endif /* USART1 || USART2 || USART3 || USART6 || UART4 || UART5 || UART7 || UART8 || UART9 || USART10 */
493 #endif /* USE_FULL_LL_DRIVER */
495 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/