STM32F103xB HAL User Manual
stm32f1xx_ll_usart.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_ll_usart.c
00004   * @author  MCD Application Team
00005   * @brief   USART LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00010   * All rights reserved.</center></h2>
00011   *
00012   * This software component is licensed by ST under BSD 3-Clause license,
00013   * the "License"; You may not use this file except in compliance with the
00014   * License. You may obtain a copy of the License at:
00015   *                        opensource.org/licenses/BSD-3-Clause
00016   *
00017   ******************************************************************************
00018   */
00019 
00020 #if defined(USE_FULL_LL_DRIVER)
00021 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f1xx_ll_usart.h"
00024 #include "stm32f1xx_ll_rcc.h"
00025 #include "stm32f1xx_ll_bus.h"
00026 #ifdef  USE_FULL_ASSERT
00027 #include "stm32_assert.h"
00028 #else
00029 #define assert_param(expr) ((void)0U)
00030 #endif
00031 
00032 /** @addtogroup STM32F1xx_LL_Driver
00033   * @{
00034   */
00035 
00036 #if defined (USART1) || defined (USART2) || defined (USART3) || defined (UART4) || defined (UART5)
00037 
00038 /** @addtogroup USART_LL
00039   * @{
00040   */
00041 
00042 /* Private types -------------------------------------------------------------*/
00043 /* Private variables ---------------------------------------------------------*/
00044 /* Private constants ---------------------------------------------------------*/
00045 /** @addtogroup USART_LL_Private_Constants
00046   * @{
00047   */
00048 
00049 /**
00050   * @}
00051   */
00052 
00053 
00054 /* Private macros ------------------------------------------------------------*/
00055 /** @addtogroup USART_LL_Private_Macros
00056   * @{
00057   */
00058 
00059 /* __BAUDRATE__ The maximum Baud Rate is derived from the maximum clock available
00060  *              divided by the smallest oversampling used on the USART (i.e. 8)    */
00061 #define IS_LL_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 4500000U)
00062 
00063 /* __VALUE__ In case of oversampling by 16 and 8, BRR content must be greater than or equal to 16d. */
00064 #define IS_LL_USART_BRR_MIN(__VALUE__) ((__VALUE__) >= 16U)
00065 
00066 #define IS_LL_USART_DIRECTION(__VALUE__) (((__VALUE__) == LL_USART_DIRECTION_NONE) \
00067                                        || ((__VALUE__) == LL_USART_DIRECTION_RX) \
00068                                        || ((__VALUE__) == LL_USART_DIRECTION_TX) \
00069                                        || ((__VALUE__) == LL_USART_DIRECTION_TX_RX))
00070 
00071 #define IS_LL_USART_PARITY(__VALUE__) (((__VALUE__) == LL_USART_PARITY_NONE) \
00072                                     || ((__VALUE__) == LL_USART_PARITY_EVEN) \
00073                                     || ((__VALUE__) == LL_USART_PARITY_ODD))
00074 
00075 #define IS_LL_USART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_USART_DATAWIDTH_8B) \
00076                                        || ((__VALUE__) == LL_USART_DATAWIDTH_9B))
00077 
00078 #define IS_LL_USART_OVERSAMPLING(__VALUE__) (((__VALUE__) == LL_USART_OVERSAMPLING_16) \
00079                                           || ((__VALUE__) == LL_USART_OVERSAMPLING_8))
00080 
00081 #define IS_LL_USART_LASTBITCLKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_LASTCLKPULSE_NO_OUTPUT) \
00082                                               || ((__VALUE__) == LL_USART_LASTCLKPULSE_OUTPUT))
00083 
00084 #define IS_LL_USART_CLOCKPHASE(__VALUE__) (((__VALUE__) == LL_USART_PHASE_1EDGE) \
00085                                         || ((__VALUE__) == LL_USART_PHASE_2EDGE))
00086 
00087 #define IS_LL_USART_CLOCKPOLARITY(__VALUE__) (((__VALUE__) == LL_USART_POLARITY_LOW) \
00088                                            || ((__VALUE__) == LL_USART_POLARITY_HIGH))
00089 
00090 #define IS_LL_USART_CLOCKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_CLOCK_DISABLE) \
00091                                          || ((__VALUE__) == LL_USART_CLOCK_ENABLE))
00092 
00093 #define IS_LL_USART_STOPBITS(__VALUE__) (((__VALUE__) == LL_USART_STOPBITS_0_5) \
00094                                       || ((__VALUE__) == LL_USART_STOPBITS_1) \
00095                                       || ((__VALUE__) == LL_USART_STOPBITS_1_5) \
00096                                       || ((__VALUE__) == LL_USART_STOPBITS_2))
00097 
00098 #define IS_LL_USART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_USART_HWCONTROL_NONE) \
00099                                        || ((__VALUE__) == LL_USART_HWCONTROL_RTS) \
00100                                        || ((__VALUE__) == LL_USART_HWCONTROL_CTS) \
00101                                        || ((__VALUE__) == LL_USART_HWCONTROL_RTS_CTS))
00102 
00103 /**
00104   * @}
00105   */
00106 
00107 /* Private function prototypes -----------------------------------------------*/
00108 
00109 /* Exported functions --------------------------------------------------------*/
00110 /** @addtogroup USART_LL_Exported_Functions
00111   * @{
00112   */
00113 
00114 /** @addtogroup USART_LL_EF_Init
00115   * @{
00116   */
00117 
00118 /**
00119   * @brief  De-initialize USART registers (Registers restored to their default values).
00120   * @param  USARTx USART Instance
00121   * @retval An ErrorStatus enumeration value:
00122   *          - SUCCESS: USART registers are de-initialized
00123   *          - ERROR: USART registers are not de-initialized
00124   */
00125 ErrorStatus LL_USART_DeInit(USART_TypeDef *USARTx)
00126 {
00127   ErrorStatus status = SUCCESS;
00128 
00129   /* Check the parameters */
00130   assert_param(IS_UART_INSTANCE(USARTx));
00131 
00132   if (USARTx == USART1)
00133   {
00134     /* Force reset of USART clock */
00135     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART1);
00136 
00137     /* Release reset of USART clock */
00138     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART1);
00139   }
00140   else if (USARTx == USART2)
00141   {
00142     /* Force reset of USART clock */
00143     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART2);
00144 
00145     /* Release reset of USART clock */
00146     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART2);
00147   }
00148 #if defined(USART3)
00149   else if (USARTx == USART3)
00150   {
00151     /* Force reset of USART clock */
00152     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART3);
00153 
00154     /* Release reset of USART clock */
00155     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART3);
00156   }
00157 #endif /* USART3 */
00158 #if defined(UART4)
00159   else if (USARTx == UART4)
00160   {
00161     /* Force reset of UART clock */
00162     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART4);
00163 
00164     /* Release reset of UART clock */
00165     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART4);
00166   }
00167 #endif /* UART4 */
00168 #if defined(UART5)
00169   else if (USARTx == UART5)
00170   {
00171     /* Force reset of UART clock */
00172     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART5);
00173 
00174     /* Release reset of UART clock */
00175     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART5);
00176   }
00177 #endif /* UART5 */
00178   else
00179   {
00180     status = ERROR;
00181   }
00182 
00183   return (status);
00184 }
00185 
00186 /**
00187   * @brief  Initialize USART registers according to the specified
00188   *         parameters in USART_InitStruct.
00189   * @note   As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
00190   *         USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
00191   * @note   Baud rate value stored in USART_InitStruct BaudRate field, should be valid (different from 0).
00192   * @param  USARTx USART Instance
00193   * @param  USART_InitStruct pointer to a LL_USART_InitTypeDef structure
00194   *         that contains the configuration information for the specified USART peripheral.
00195   * @retval An ErrorStatus enumeration value:
00196   *          - SUCCESS: USART registers are initialized according to USART_InitStruct content
00197   *          - ERROR: Problem occurred during USART Registers initialization
00198   */
00199 ErrorStatus LL_USART_Init(USART_TypeDef *USARTx, LL_USART_InitTypeDef *USART_InitStruct)
00200 {
00201   ErrorStatus status = ERROR;
00202   uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
00203   LL_RCC_ClocksTypeDef rcc_clocks;
00204 
00205   /* Check the parameters */
00206   assert_param(IS_UART_INSTANCE(USARTx));
00207   assert_param(IS_LL_USART_BAUDRATE(USART_InitStruct->BaudRate));
00208   assert_param(IS_LL_USART_DATAWIDTH(USART_InitStruct->DataWidth));
00209   assert_param(IS_LL_USART_STOPBITS(USART_InitStruct->StopBits));
00210   assert_param(IS_LL_USART_PARITY(USART_InitStruct->Parity));
00211   assert_param(IS_LL_USART_DIRECTION(USART_InitStruct->TransferDirection));
00212   assert_param(IS_LL_USART_HWCONTROL(USART_InitStruct->HardwareFlowControl));
00213 #if defined(USART_CR1_OVER8)
00214   assert_param(IS_LL_USART_OVERSAMPLING(USART_InitStruct->OverSampling));
00215 #endif /* USART_OverSampling_Feature */
00216 
00217   /* USART needs to be in disabled state, in order to be able to configure some bits in
00218      CRx registers */
00219   if (LL_USART_IsEnabled(USARTx) == 0U)
00220   {
00221     /*---------------------------- USART CR1 Configuration -----------------------
00222      * Configure USARTx CR1 (USART Word Length, Parity, Mode and Oversampling bits) with parameters:
00223      * - DataWidth:          USART_CR1_M bits according to USART_InitStruct->DataWidth value
00224      * - Parity:             USART_CR1_PCE, USART_CR1_PS bits according to USART_InitStruct->Parity value
00225      * - TransferDirection:  USART_CR1_TE, USART_CR1_RE bits according to USART_InitStruct->TransferDirection value
00226      * - Oversampling:       USART_CR1_OVER8 bit according to USART_InitStruct->OverSampling value.
00227      */
00228 #if defined(USART_CR1_OVER8)
00229     MODIFY_REG(USARTx->CR1,
00230                (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
00231                 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
00232                (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
00233                 USART_InitStruct->TransferDirection | USART_InitStruct->OverSampling));
00234 #else
00235     MODIFY_REG(USARTx->CR1,
00236                (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
00237                 USART_CR1_TE | USART_CR1_RE),
00238                (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
00239                 USART_InitStruct->TransferDirection));
00240 #endif /* USART_OverSampling_Feature */
00241 
00242     /*---------------------------- USART CR2 Configuration -----------------------
00243      * Configure USARTx CR2 (Stop bits) with parameters:
00244      * - Stop Bits:          USART_CR2_STOP bits according to USART_InitStruct->StopBits value.
00245      * - CLKEN, CPOL, CPHA and LBCL bits are to be configured using LL_USART_ClockInit().
00246      */
00247     LL_USART_SetStopBitsLength(USARTx, USART_InitStruct->StopBits);
00248 
00249     /*---------------------------- USART CR3 Configuration -----------------------
00250      * Configure USARTx CR3 (Hardware Flow Control) with parameters:
00251      * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to USART_InitStruct->HardwareFlowControl value.
00252      */
00253     LL_USART_SetHWFlowCtrl(USARTx, USART_InitStruct->HardwareFlowControl);
00254 
00255     /*---------------------------- USART BRR Configuration -----------------------
00256      * Retrieve Clock frequency used for USART Peripheral
00257      */
00258     LL_RCC_GetSystemClocksFreq(&rcc_clocks);
00259     if (USARTx == USART1)
00260     {
00261       periphclk = rcc_clocks.PCLK2_Frequency;
00262     }
00263     else if (USARTx == USART2)
00264     {
00265       periphclk = rcc_clocks.PCLK1_Frequency;
00266     }
00267 #if defined(USART3)
00268     else if (USARTx == USART3)
00269     {
00270       periphclk = rcc_clocks.PCLK1_Frequency;
00271     }
00272 #endif /* USART3 */
00273 #if defined(UART4)
00274     else if (USARTx == UART4)
00275     {
00276       periphclk = rcc_clocks.PCLK1_Frequency;
00277     }
00278 #endif /* UART4 */
00279 #if defined(UART5)
00280     else if (USARTx == UART5)
00281     {
00282       periphclk = rcc_clocks.PCLK1_Frequency;
00283     }
00284 #endif /* UART5 */
00285     else
00286     {
00287       /* Nothing to do, as error code is already assigned to ERROR value */
00288     }
00289 
00290     /* Configure the USART Baud Rate :
00291        - valid baud rate value (different from 0) is required
00292        - Peripheral clock as returned by RCC service, should be valid (different from 0).
00293     */
00294     if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
00295         && (USART_InitStruct->BaudRate != 0U))
00296     {
00297       status = SUCCESS;
00298 #if defined(USART_CR1_OVER8)
00299       LL_USART_SetBaudRate(USARTx,
00300                            periphclk,
00301                            USART_InitStruct->OverSampling,
00302                            USART_InitStruct->BaudRate);
00303 #else
00304       LL_USART_SetBaudRate(USARTx,
00305                            periphclk,
00306                            USART_InitStruct->BaudRate);
00307 #endif /* USART_OverSampling_Feature */
00308 
00309       /* Check BRR is greater than or equal to 16d */
00310       assert_param(IS_LL_USART_BRR_MIN(USARTx->BRR));
00311     }
00312   }
00313   /* Endif (=> USART not in Disabled state => return ERROR) */
00314 
00315   return (status);
00316 }
00317 
00318 /**
00319   * @brief Set each @ref LL_USART_InitTypeDef field to default value.
00320   * @param USART_InitStruct Pointer to a @ref LL_USART_InitTypeDef structure
00321   *                         whose fields will be set to default values.
00322   * @retval None
00323   */
00324 
00325 void LL_USART_StructInit(LL_USART_InitTypeDef *USART_InitStruct)
00326 {
00327   /* Set USART_InitStruct fields to default values */
00328   USART_InitStruct->BaudRate            = 9600U;
00329   USART_InitStruct->DataWidth           = LL_USART_DATAWIDTH_8B;
00330   USART_InitStruct->StopBits            = LL_USART_STOPBITS_1;
00331   USART_InitStruct->Parity              = LL_USART_PARITY_NONE ;
00332   USART_InitStruct->TransferDirection   = LL_USART_DIRECTION_TX_RX;
00333   USART_InitStruct->HardwareFlowControl = LL_USART_HWCONTROL_NONE;
00334 #if defined(USART_CR1_OVER8)
00335   USART_InitStruct->OverSampling        = LL_USART_OVERSAMPLING_16;
00336 #endif /* USART_OverSampling_Feature */
00337 }
00338 
00339 /**
00340   * @brief  Initialize USART Clock related settings according to the
00341   *         specified parameters in the USART_ClockInitStruct.
00342   * @note   As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
00343   *         USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
00344   * @param  USARTx USART Instance
00345   * @param  USART_ClockInitStruct Pointer to a @ref LL_USART_ClockInitTypeDef structure
00346   *         that contains the Clock configuration information for the specified USART peripheral.
00347   * @retval An ErrorStatus enumeration value:
00348   *          - SUCCESS: USART registers related to Clock settings are initialized according to USART_ClockInitStruct content
00349   *          - ERROR: Problem occurred during USART Registers initialization
00350   */
00351 ErrorStatus LL_USART_ClockInit(USART_TypeDef *USARTx, LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
00352 {
00353   ErrorStatus status = SUCCESS;
00354 
00355   /* Check USART Instance and Clock signal output parameters */
00356   assert_param(IS_UART_INSTANCE(USARTx));
00357   assert_param(IS_LL_USART_CLOCKOUTPUT(USART_ClockInitStruct->ClockOutput));
00358 
00359   /* USART needs to be in disabled state, in order to be able to configure some bits in
00360      CRx registers */
00361   if (LL_USART_IsEnabled(USARTx) == 0U)
00362   {
00363     /*---------------------------- USART CR2 Configuration -----------------------*/
00364     /* If Clock signal has to be output */
00365     if (USART_ClockInitStruct->ClockOutput == LL_USART_CLOCK_DISABLE)
00366     {
00367       /* Deactivate Clock signal delivery :
00368        * - Disable Clock Output:        USART_CR2_CLKEN cleared
00369        */
00370       LL_USART_DisableSCLKOutput(USARTx);
00371     }
00372     else
00373     {
00374       /* Ensure USART instance is USART capable */
00375       assert_param(IS_USART_INSTANCE(USARTx));
00376 
00377       /* Check clock related parameters */
00378       assert_param(IS_LL_USART_CLOCKPOLARITY(USART_ClockInitStruct->ClockPolarity));
00379       assert_param(IS_LL_USART_CLOCKPHASE(USART_ClockInitStruct->ClockPhase));
00380       assert_param(IS_LL_USART_LASTBITCLKOUTPUT(USART_ClockInitStruct->LastBitClockPulse));
00381 
00382       /*---------------------------- USART CR2 Configuration -----------------------
00383        * Configure USARTx CR2 (Clock signal related bits) with parameters:
00384        * - Enable Clock Output:         USART_CR2_CLKEN set
00385        * - Clock Polarity:              USART_CR2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
00386        * - Clock Phase:                 USART_CR2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
00387        * - Last Bit Clock Pulse Output: USART_CR2_LBCL bit according to USART_ClockInitStruct->LastBitClockPulse value.
00388        */
00389       MODIFY_REG(USARTx->CR2,
00390                  USART_CR2_CLKEN | USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_LBCL,
00391                  USART_CR2_CLKEN | USART_ClockInitStruct->ClockPolarity |
00392                  USART_ClockInitStruct->ClockPhase | USART_ClockInitStruct->LastBitClockPulse);
00393     }
00394   }
00395   /* Else (USART not in Disabled state => return ERROR */
00396   else
00397   {
00398     status = ERROR;
00399   }
00400 
00401   return (status);
00402 }
00403 
00404 /**
00405   * @brief Set each field of a @ref LL_USART_ClockInitTypeDef type structure to default value.
00406   * @param USART_ClockInitStruct Pointer to a @ref LL_USART_ClockInitTypeDef structure
00407   *                              whose fields will be set to default values.
00408   * @retval None
00409   */
00410 void LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
00411 {
00412   /* Set LL_USART_ClockInitStruct fields with default values */
00413   USART_ClockInitStruct->ClockOutput       = LL_USART_CLOCK_DISABLE;
00414   USART_ClockInitStruct->ClockPolarity     = LL_USART_POLARITY_LOW;            /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
00415   USART_ClockInitStruct->ClockPhase        = LL_USART_PHASE_1EDGE;             /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
00416   USART_ClockInitStruct->LastBitClockPulse = LL_USART_LASTCLKPULSE_NO_OUTPUT;  /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
00417 }
00418 
00419 /**
00420   * @}
00421   */
00422 
00423 /**
00424   * @}
00425   */
00426 
00427 /**
00428   * @}
00429   */
00430 
00431 #endif /* USART1 || USART2 || USART3 || UART4 || UART5 */
00432 
00433 /**
00434   * @}
00435   */
00436 
00437 #endif /* USE_FULL_LL_DRIVER */
00438 
00439 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00440