STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_ll_usart.c
Go to the documentation of this file.
1 
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 
35 #if defined (USART1) || defined (USART2) || defined (USART3) || defined (UART4) || defined (UART5)
36 
41 /* Private types -------------------------------------------------------------*/
42 /* Private variables ---------------------------------------------------------*/
43 /* Private constants ---------------------------------------------------------*/
52 /* Private macros ------------------------------------------------------------*/
57 #if defined(USART_PRESC_PRESCALER)
58 #define IS_LL_USART_PRESCALER(__VALUE__) (((__VALUE__) == LL_USART_PRESCALER_DIV1) \
59  || ((__VALUE__) == LL_USART_PRESCALER_DIV2) \
60  || ((__VALUE__) == LL_USART_PRESCALER_DIV4) \
61  || ((__VALUE__) == LL_USART_PRESCALER_DIV6) \
62  || ((__VALUE__) == LL_USART_PRESCALER_DIV8) \
63  || ((__VALUE__) == LL_USART_PRESCALER_DIV10) \
64  || ((__VALUE__) == LL_USART_PRESCALER_DIV12) \
65  || ((__VALUE__) == LL_USART_PRESCALER_DIV16) \
66  || ((__VALUE__) == LL_USART_PRESCALER_DIV32) \
67  || ((__VALUE__) == LL_USART_PRESCALER_DIV64) \
68  || ((__VALUE__) == LL_USART_PRESCALER_DIV128) \
69  || ((__VALUE__) == LL_USART_PRESCALER_DIV256))
70 
71 #endif /* USART_PRESC_PRESCALER */
72 /* __BAUDRATE__ The maximum Baud Rate is derived from the maximum clock available
73  * divided by the smallest oversampling used on the USART (i.e. 8) */
74 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
75 #define IS_LL_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 15000000U)
76 #else
77 #define IS_LL_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 10000000U)
78 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
79 
80 /* __VALUE__ In case of oversampling by 16 and 8, BRR content must be greater than or equal to 16d. */
81 #define IS_LL_USART_BRR_MIN(__VALUE__) ((__VALUE__) >= 16U)
82 
83 /* __VALUE__ BRR content must be lower than or equal to 0xFFFF. */
84 #define IS_LL_USART_BRR_MAX(__VALUE__) ((__VALUE__) <= 0x0000FFFFU)
85 
86 #define IS_LL_USART_DIRECTION(__VALUE__) (((__VALUE__) == LL_USART_DIRECTION_NONE) \
87  || ((__VALUE__) == LL_USART_DIRECTION_RX) \
88  || ((__VALUE__) == LL_USART_DIRECTION_TX) \
89  || ((__VALUE__) == LL_USART_DIRECTION_TX_RX))
90 
91 #define IS_LL_USART_PARITY(__VALUE__) (((__VALUE__) == LL_USART_PARITY_NONE) \
92  || ((__VALUE__) == LL_USART_PARITY_EVEN) \
93  || ((__VALUE__) == LL_USART_PARITY_ODD))
94 
95 #define IS_LL_USART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_USART_DATAWIDTH_7B) \
96  || ((__VALUE__) == LL_USART_DATAWIDTH_8B) \
97  || ((__VALUE__) == LL_USART_DATAWIDTH_9B))
98 
99 #define IS_LL_USART_OVERSAMPLING(__VALUE__) (((__VALUE__) == LL_USART_OVERSAMPLING_16) \
100  || ((__VALUE__) == LL_USART_OVERSAMPLING_8))
101 
102 #define IS_LL_USART_LASTBITCLKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_LASTCLKPULSE_NO_OUTPUT) \
103  || ((__VALUE__) == LL_USART_LASTCLKPULSE_OUTPUT))
104 
105 #define IS_LL_USART_CLOCKPHASE(__VALUE__) (((__VALUE__) == LL_USART_PHASE_1EDGE) \
106  || ((__VALUE__) == LL_USART_PHASE_2EDGE))
107 
108 #define IS_LL_USART_CLOCKPOLARITY(__VALUE__) (((__VALUE__) == LL_USART_POLARITY_LOW) \
109  || ((__VALUE__) == LL_USART_POLARITY_HIGH))
110 
111 #define IS_LL_USART_CLOCKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_CLOCK_DISABLE) \
112  || ((__VALUE__) == LL_USART_CLOCK_ENABLE))
113 
114 #define IS_LL_USART_STOPBITS(__VALUE__) (((__VALUE__) == LL_USART_STOPBITS_0_5) \
115  || ((__VALUE__) == LL_USART_STOPBITS_1) \
116  || ((__VALUE__) == LL_USART_STOPBITS_1_5) \
117  || ((__VALUE__) == LL_USART_STOPBITS_2))
118 
119 #define IS_LL_USART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_USART_HWCONTROL_NONE) \
120  || ((__VALUE__) == LL_USART_HWCONTROL_RTS) \
121  || ((__VALUE__) == LL_USART_HWCONTROL_CTS) \
122  || ((__VALUE__) == LL_USART_HWCONTROL_RTS_CTS))
123 
128 /* Private function prototypes -----------------------------------------------*/
129 
130 /* Exported functions --------------------------------------------------------*/
146 ErrorStatus LL_USART_DeInit(USART_TypeDef *USARTx)
147 {
148  ErrorStatus status = SUCCESS;
149 
150  /* Check the parameters */
151  assert_param(IS_UART_INSTANCE(USARTx));
152 
153  if (USARTx == USART1)
154  {
155  /* Force reset of USART clock */
156  LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART1);
157 
158  /* Release reset of USART clock */
159  LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART1);
160  }
161  else if (USARTx == USART2)
162  {
163  /* Force reset of USART clock */
164  LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART2);
165 
166  /* Release reset of USART clock */
167  LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART2);
168  }
169 #if defined(USART3)
170  else if (USARTx == USART3)
171  {
172  /* Force reset of USART clock */
173  LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART3);
174 
175  /* Release reset of USART clock */
176  LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART3);
177  }
178 #endif /* USART3 */
179 #if defined(UART4)
180  else if (USARTx == UART4)
181  {
182  /* Force reset of UART clock */
183  LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART4);
184 
185  /* Release reset of UART clock */
186  LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART4);
187  }
188 #endif /* UART4 */
189 #if defined(UART5)
190  else if (USARTx == UART5)
191  {
192  /* Force reset of UART clock */
193  LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART5);
194 
195  /* Release reset of UART clock */
196  LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART5);
197  }
198 #endif /* UART5 */
199  else
200  {
201  status = ERROR;
202  }
203 
204  return (status);
205 }
206 
220 ErrorStatus LL_USART_Init(USART_TypeDef *USARTx, 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 USART_InitStruct->HardwareFlowControl value.
265  */
266  LL_USART_SetHWFlowCtrl(USARTx, USART_InitStruct->HardwareFlowControl);
267 
268  /*---------------------------- USART BRR Configuration ---------------------
269  * Retrieve Clock frequency used for USART Peripheral
270  */
271  if (USARTx == USART1)
272  {
273  periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART1_CLKSOURCE);
274  }
275  else if (USARTx == USART2)
276  {
277  periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART2_CLKSOURCE);
278  }
279 #if defined(USART3)
280  else if (USARTx == USART3)
281  {
282  periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART3_CLKSOURCE);
283  }
284 #endif /* USART3 */
285 #if defined(UART4)
286  else if (USARTx == UART4)
287  {
288  periphclk = LL_RCC_GetUARTClockFreq(LL_RCC_UART4_CLKSOURCE);
289  }
290 #endif /* UART4 */
291 #if defined(UART5)
292  else if (USARTx == UART5)
293  {
294  periphclk = LL_RCC_GetUARTClockFreq(LL_RCC_UART5_CLKSOURCE);
295  }
296 #endif /* UART5 */
297  else
298  {
299  /* Nothing to do, as error code is already assigned to ERROR value */
300  }
301 
302  /* Configure the USART Baud Rate :
303  #if defined(USART_PRESC_PRESCALER)
304  - prescaler value is required
305  #endif
306  - valid baud rate value (different from 0) is required
307  - Peripheral clock as returned by RCC service, should be valid (different from 0).
308  */
309  if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
310  && (USART_InitStruct->BaudRate != 0U))
311  {
312  status = SUCCESS;
313  LL_USART_SetBaudRate(USARTx,
314  periphclk,
315 #if defined(USART_PRESC_PRESCALER)
316  USART_InitStruct->PrescalerValue,
317 #endif /* USART_PRESC_PRESCALER */
318  USART_InitStruct->OverSampling,
319  USART_InitStruct->BaudRate);
320 
321  /* Check BRR is greater than or equal to 16d */
322  assert_param(IS_LL_USART_BRR_MIN(USARTx->BRR));
323 
324  /* Check BRR is lower than or equal to 0xFFFF */
325  assert_param(IS_LL_USART_BRR_MAX(USARTx->BRR));
326  }
327 #if defined(USART_PRESC_PRESCALER)
328 
329  /*---------------------------- USART PRESC Configuration -----------------------
330  * Configure USARTx PRESC (Prescaler) with parameters:
331  * - PrescalerValue: USART_PRESC_PRESCALER bits according to USART_InitStruct->PrescalerValue value.
332  */
333  LL_USART_SetPrescaler(USARTx, USART_InitStruct->PrescalerValue);
334 #endif /* USART_PRESC_PRESCALER */
335  }
336  /* Endif (=> USART not in Disabled state => return ERROR) */
337 
338  return (status);
339 }
340 
349 {
350  /* Set USART_InitStruct fields to default values */
351 #if defined(USART_PRESC_PRESCALER)
352  USART_InitStruct->PrescalerValue = LL_USART_PRESCALER_DIV1;
353 #endif /* USART_PRESC_PRESCALER */
354  USART_InitStruct->BaudRate = 9600U;
355  USART_InitStruct->DataWidth = LL_USART_DATAWIDTH_8B;
356  USART_InitStruct->StopBits = LL_USART_STOPBITS_1;
357  USART_InitStruct->Parity = LL_USART_PARITY_NONE ;
358  USART_InitStruct->TransferDirection = LL_USART_DIRECTION_TX_RX;
359  USART_InitStruct->HardwareFlowControl = LL_USART_HWCONTROL_NONE;
360  USART_InitStruct->OverSampling = LL_USART_OVERSAMPLING_16;
361 }
362 
375 ErrorStatus LL_USART_ClockInit(USART_TypeDef *USARTx, 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  /*---------------------------- USART CR2 Configuration -----------------------*/
388  /* If Clock signal has to be output */
389  if (USART_ClockInitStruct->ClockOutput == LL_USART_CLOCK_DISABLE)
390  {
391  /* Deactivate Clock signal delivery :
392  * - Disable Clock Output: USART_CR2_CLKEN cleared
393  */
395  }
396  else
397  {
398  /* Ensure USART instance is USART capable */
399  assert_param(IS_USART_INSTANCE(USARTx));
400 
401  /* Check clock related parameters */
402  assert_param(IS_LL_USART_CLOCKPOLARITY(USART_ClockInitStruct->ClockPolarity));
403  assert_param(IS_LL_USART_CLOCKPHASE(USART_ClockInitStruct->ClockPhase));
404  assert_param(IS_LL_USART_LASTBITCLKOUTPUT(USART_ClockInitStruct->LastBitClockPulse));
405 
406  /*---------------------------- USART CR2 Configuration -----------------------
407  * Configure USARTx CR2 (Clock signal related bits) with parameters:
408  * - Enable Clock Output: USART_CR2_CLKEN set
409  * - Clock Polarity: USART_CR2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
410  * - Clock Phase: USART_CR2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
411  * - Last Bit Clock Pulse Output: USART_CR2_LBCL bit according to USART_ClockInitStruct->LastBitClockPulse value.
412  */
413  MODIFY_REG(USARTx->CR2,
414  USART_CR2_CLKEN | USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_LBCL,
415  USART_CR2_CLKEN | USART_ClockInitStruct->ClockPolarity |
416  USART_ClockInitStruct->ClockPhase | USART_ClockInitStruct->LastBitClockPulse);
417  }
418  }
419  /* Else (USART not in Disabled state => return ERROR */
420  else
421  {
422  status = ERROR;
423  }
424 
425  return (status);
426 }
427 
435 {
436  /* Set LL_USART_ClockInitStruct fields with default values */
437  USART_ClockInitStruct->ClockOutput = LL_USART_CLOCK_DISABLE;
438  USART_ClockInitStruct->ClockPolarity = LL_USART_POLARITY_LOW; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
439  USART_ClockInitStruct->ClockPhase = LL_USART_PHASE_1EDGE; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
440  USART_ClockInitStruct->LastBitClockPulse = LL_USART_LASTCLKPULSE_NO_OUTPUT; /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
441 }
442 
455 #endif /* USART1 || USART2 || USART3 || UART4 || UART5 */
456 
461 #endif /* USE_FULL_LL_DRIVER */
462 
463 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
464 
void LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
Set each field of a LL_USART_ClockInitTypeDef type structure to default value.
LL USART Init Structure definition.
__STATIC_INLINE void LL_USART_SetPrescaler(USART_TypeDef *USARTx, uint32_t PrescalerValue)
Configure Clock source prescaler for baudrate generator and oversampling.
LL USART Clock Init Structure definition.
Header file of RCC LL module.
__STATIC_INLINE void LL_APB2_GRP1_ReleaseReset(uint32_t Periphs)
Release APB2 peripherals reset. APB2RSTR SYSCFGRST LL_APB2_GRP1_ReleaseReset APB2RSTR SDMMC1RST LL_...
uint32_t LL_RCC_GetUARTClockFreq(uint32_t UARTxSource)
Return UARTx clock frequency.
void LL_USART_StructInit(LL_USART_InitTypeDef *USART_InitStruct)
Set each LL_USART_InitTypeDef field to default value.
ErrorStatus LL_USART_ClockInit(USART_TypeDef *USARTx, LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
Initialize USART Clock related settings according to the specified parameters in the USART_ClockInitS...
__STATIC_INLINE void LL_APB1_GRP1_ReleaseReset(uint32_t Periphs)
Release APB1 peripherals reset. APB1RSTR1 TIM2RST LL_APB1_GRP1_ReleaseReset APB1RSTR1 TIM3RST LL_AP...
Header file of USART LL module.
__STATIC_INLINE void LL_USART_DisableSCLKOutput(USART_TypeDef *USARTx)
Disable Clock output on SCLK pin.
__STATIC_INLINE void LL_USART_SetStopBitsLength(USART_TypeDef *USARTx, uint32_t StopBits)
Set the length of the stop bits CR2 STOP LL_USART_SetStopBitsLength.
uint32_t LL_RCC_GetUSARTClockFreq(uint32_t USARTxSource)
Return USARTx clock frequency.
__STATIC_INLINE uint32_t LL_USART_IsEnabled(USART_TypeDef *USARTx)
Indicate if USART is enabled CR1 UE LL_USART_IsEnabled.
__STATIC_INLINE void LL_USART_SetHWFlowCtrl(USART_TypeDef *USARTx, uint32_t HardwareFlowControl)
Configure HW Flow Control mode (both CTS and RTS)
__STATIC_INLINE void LL_APB2_GRP1_ForceReset(uint32_t Periphs)
Force APB2 peripherals reset. APB2RSTR SYSCFGRST LL_APB2_GRP1_ForceReset APB2RSTR SDMMC1RST LL_APB2...
Header file of BUS LL module.
__STATIC_INLINE void LL_APB1_GRP1_ForceReset(uint32_t Periphs)
Force APB1 peripherals reset. APB1RSTR1 TIM2RST LL_APB1_GRP1_ForceReset APB1RSTR1 TIM3RST LL_APB1_G...
__STATIC_INLINE void LL_USART_SetBaudRate(USART_TypeDef *USARTx, uint32_t PeriphClk, uint32_t PrescalerValue, uint32_t OverSampling, uint32_t BaudRate) __STATIC_INLINE void LL_USART_SetBaudRate(USART_TypeDef *USARTx
Configure USART BRR register for achieving expected Baud Rate value.
ADC handle Structure definition.
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
ErrorStatus LL_USART_DeInit(USART_TypeDef *USARTx)
De-initialize USART registers (Registers restored to their default values).
ErrorStatus LL_USART_Init(USART_TypeDef *USARTx, LL_USART_InitTypeDef *USART_InitStruct)
Initialize USART registers according to the specified parameters in USART_InitStruct.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))