STM32F479xx HAL User Manual
stm32f4xx_ll_usart.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_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 "stm32f4xx_ll_usart.h"
00024 #include "stm32f4xx_ll_rcc.h"
00025 #include "stm32f4xx_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 STM32F4xx_LL_Driver
00033   * @{
00034   */
00035 
00036 #if defined (USART1) || defined (USART2) || defined (USART3) || defined (USART6) || defined (UART4) || defined (UART5) || defined (UART7) || defined (UART8) || defined (UART9) || defined (UART10)
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__) <= 12500000U)
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(USART6)
00159   else if (USARTx == USART6)
00160   {
00161     /* Force reset of USART clock */
00162     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART6);
00163 
00164     /* Release reset of USART clock */
00165     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART6);
00166   }
00167 #endif /* USART6 */
00168 #if defined(UART4)
00169   else if (USARTx == UART4)
00170   {
00171     /* Force reset of UART clock */
00172     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART4);
00173 
00174     /* Release reset of UART clock */
00175     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART4);
00176   }
00177 #endif /* UART4 */
00178 #if defined(UART5)
00179   else if (USARTx == UART5)
00180   {
00181     /* Force reset of UART clock */
00182     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART5);
00183 
00184     /* Release reset of UART clock */
00185     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART5);
00186   }
00187 #endif /* UART5 */
00188 #if defined(UART7)
00189   else if (USARTx == UART7)
00190   {
00191     /* Force reset of UART clock */
00192     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART7);
00193 
00194     /* Release reset of UART clock */
00195     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART7);
00196   }
00197 #endif /* UART7 */
00198 #if defined(UART8)
00199   else if (USARTx == UART8)
00200   {
00201     /* Force reset of UART clock */
00202     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART8);
00203 
00204     /* Release reset of UART clock */
00205     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART8);
00206   }
00207 #endif /* UART8 */
00208 #if defined(UART9)
00209   else if (USARTx == UART9)
00210   {
00211     /* Force reset of UART clock */
00212     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_UART9);
00213 
00214     /* Release reset of UART clock */
00215     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_UART9);
00216   }
00217 #endif /* UART9 */
00218 #if defined(UART10)
00219   else if (USARTx == UART10)
00220   {
00221     /* Force reset of UART clock */
00222     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_UART10);
00223 
00224     /* Release reset of UART clock */
00225     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_UART10);
00226   }
00227 #endif /* UART10 */
00228   else
00229   {
00230     status = ERROR;
00231   }
00232 
00233   return (status);
00234 }
00235 
00236 /**
00237   * @brief  Initialize USART registers according to the specified
00238   *         parameters in USART_InitStruct.
00239   * @note   As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
00240   *         USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
00241   * @note   Baud rate value stored in USART_InitStruct BaudRate field, should be valid (different from 0).
00242   * @param  USARTx USART Instance
00243   * @param  USART_InitStruct pointer to a LL_USART_InitTypeDef structure
00244   *         that contains the configuration information for the specified USART peripheral.
00245   * @retval An ErrorStatus enumeration value:
00246   *          - SUCCESS: USART registers are initialized according to USART_InitStruct content
00247   *          - ERROR: Problem occurred during USART Registers initialization
00248   */
00249 ErrorStatus LL_USART_Init(USART_TypeDef *USARTx, LL_USART_InitTypeDef *USART_InitStruct)
00250 {
00251   ErrorStatus status = ERROR;
00252   uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
00253   LL_RCC_ClocksTypeDef rcc_clocks;
00254 
00255   /* Check the parameters */
00256   assert_param(IS_UART_INSTANCE(USARTx));
00257   assert_param(IS_LL_USART_BAUDRATE(USART_InitStruct->BaudRate));
00258   assert_param(IS_LL_USART_DATAWIDTH(USART_InitStruct->DataWidth));
00259   assert_param(IS_LL_USART_STOPBITS(USART_InitStruct->StopBits));
00260   assert_param(IS_LL_USART_PARITY(USART_InitStruct->Parity));
00261   assert_param(IS_LL_USART_DIRECTION(USART_InitStruct->TransferDirection));
00262   assert_param(IS_LL_USART_HWCONTROL(USART_InitStruct->HardwareFlowControl));
00263   assert_param(IS_LL_USART_OVERSAMPLING(USART_InitStruct->OverSampling));
00264 
00265   /* USART needs to be in disabled state, in order to be able to configure some bits in
00266      CRx registers */
00267   if (LL_USART_IsEnabled(USARTx) == 0U)
00268   {
00269     /*---------------------------- USART CR1 Configuration -----------------------
00270      * Configure USARTx CR1 (USART Word Length, Parity, Mode and Oversampling bits) with parameters:
00271      * - DataWidth:          USART_CR1_M bits according to USART_InitStruct->DataWidth value
00272      * - Parity:             USART_CR1_PCE, USART_CR1_PS bits according to USART_InitStruct->Parity value
00273      * - TransferDirection:  USART_CR1_TE, USART_CR1_RE bits according to USART_InitStruct->TransferDirection value
00274      * - Oversampling:       USART_CR1_OVER8 bit according to USART_InitStruct->OverSampling value.
00275      */
00276     MODIFY_REG(USARTx->CR1,
00277                (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
00278                 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
00279                (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
00280                 USART_InitStruct->TransferDirection | USART_InitStruct->OverSampling));
00281 
00282     /*---------------------------- USART CR2 Configuration -----------------------
00283      * Configure USARTx CR2 (Stop bits) with parameters:
00284      * - Stop Bits:          USART_CR2_STOP bits according to USART_InitStruct->StopBits value.
00285      * - CLKEN, CPOL, CPHA and LBCL bits are to be configured using LL_USART_ClockInit().
00286      */
00287     LL_USART_SetStopBitsLength(USARTx, USART_InitStruct->StopBits);
00288 
00289     /*---------------------------- USART CR3 Configuration -----------------------
00290      * Configure USARTx CR3 (Hardware Flow Control) with parameters:
00291      * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to USART_InitStruct->HardwareFlowControl value.
00292      */
00293     LL_USART_SetHWFlowCtrl(USARTx, USART_InitStruct->HardwareFlowControl);
00294 
00295     /*---------------------------- USART BRR Configuration -----------------------
00296      * Retrieve Clock frequency used for USART Peripheral
00297      */
00298     LL_RCC_GetSystemClocksFreq(&rcc_clocks);
00299     if (USARTx == USART1)
00300     {
00301       periphclk = rcc_clocks.PCLK2_Frequency;
00302     }
00303     else if (USARTx == USART2)
00304     {
00305       periphclk = rcc_clocks.PCLK1_Frequency;
00306     }
00307 #if defined(USART3)
00308     else if (USARTx == USART3)
00309     {
00310       periphclk = rcc_clocks.PCLK1_Frequency;
00311     }
00312 #endif /* USART3 */
00313 #if defined(USART6)
00314     else if (USARTx == USART6)
00315     {
00316       periphclk = rcc_clocks.PCLK2_Frequency;
00317     }
00318 #endif /* USART6 */
00319 #if defined(UART4)
00320     else if (USARTx == UART4)
00321     {
00322       periphclk = rcc_clocks.PCLK1_Frequency;
00323     }
00324 #endif /* UART4 */
00325 #if defined(UART5)
00326     else if (USARTx == UART5)
00327     {
00328       periphclk = rcc_clocks.PCLK1_Frequency;
00329     }
00330 #endif /* UART5 */
00331 #if defined(UART7)
00332     else if (USARTx == UART7)
00333     {
00334       periphclk = rcc_clocks.PCLK1_Frequency;
00335     }
00336 #endif /* UART7 */
00337 #if defined(UART8)
00338     else if (USARTx == UART8)
00339     {
00340       periphclk = rcc_clocks.PCLK1_Frequency;
00341     }
00342 #endif /* UART8 */
00343 #if defined(UART9)
00344     else if (USARTx == UART9)
00345     {
00346       periphclk = rcc_clocks.PCLK2_Frequency;
00347     }
00348 #endif /* UART9 */
00349 #if defined(UART10)
00350     else if (USARTx == UART10)
00351     {
00352       periphclk = rcc_clocks.PCLK2_Frequency;
00353     }
00354 #endif /* UART10 */
00355     else
00356     {
00357       /* Nothing to do, as error code is already assigned to ERROR value */
00358     }
00359 
00360     /* Configure the USART Baud Rate :
00361        - valid baud rate value (different from 0) is required
00362        - Peripheral clock as returned by RCC service, should be valid (different from 0).
00363     */
00364     if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
00365         && (USART_InitStruct->BaudRate != 0U))
00366     {
00367       status = SUCCESS;
00368       LL_USART_SetBaudRate(USARTx,
00369                            periphclk,
00370                            USART_InitStruct->OverSampling,
00371                            USART_InitStruct->BaudRate);
00372 
00373       /* Check BRR is greater than or equal to 16d */
00374       assert_param(IS_LL_USART_BRR_MIN(USARTx->BRR));
00375     }
00376   }
00377   /* Endif (=> USART not in Disabled state => return ERROR) */
00378 
00379   return (status);
00380 }
00381 
00382 /**
00383   * @brief Set each @ref LL_USART_InitTypeDef field to default value.
00384   * @param USART_InitStruct Pointer to a @ref LL_USART_InitTypeDef structure
00385   *                         whose fields will be set to default values.
00386   * @retval None
00387   */
00388 
00389 void LL_USART_StructInit(LL_USART_InitTypeDef *USART_InitStruct)
00390 {
00391   /* Set USART_InitStruct fields to default values */
00392   USART_InitStruct->BaudRate            = 9600U;
00393   USART_InitStruct->DataWidth           = LL_USART_DATAWIDTH_8B;
00394   USART_InitStruct->StopBits            = LL_USART_STOPBITS_1;
00395   USART_InitStruct->Parity              = LL_USART_PARITY_NONE ;
00396   USART_InitStruct->TransferDirection   = LL_USART_DIRECTION_TX_RX;
00397   USART_InitStruct->HardwareFlowControl = LL_USART_HWCONTROL_NONE;
00398   USART_InitStruct->OverSampling        = LL_USART_OVERSAMPLING_16;
00399 }
00400 
00401 /**
00402   * @brief  Initialize USART Clock related settings according to the
00403   *         specified parameters in the USART_ClockInitStruct.
00404   * @note   As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
00405   *         USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
00406   * @param  USARTx USART Instance
00407   * @param  USART_ClockInitStruct Pointer to a @ref LL_USART_ClockInitTypeDef structure
00408   *         that contains the Clock configuration information for the specified USART peripheral.
00409   * @retval An ErrorStatus enumeration value:
00410   *          - SUCCESS: USART registers related to Clock settings are initialized according to USART_ClockInitStruct content
00411   *          - ERROR: Problem occurred during USART Registers initialization
00412   */
00413 ErrorStatus LL_USART_ClockInit(USART_TypeDef *USARTx, LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
00414 {
00415   ErrorStatus status = SUCCESS;
00416 
00417   /* Check USART Instance and Clock signal output parameters */
00418   assert_param(IS_UART_INSTANCE(USARTx));
00419   assert_param(IS_LL_USART_CLOCKOUTPUT(USART_ClockInitStruct->ClockOutput));
00420 
00421   /* USART needs to be in disabled state, in order to be able to configure some bits in
00422      CRx registers */
00423   if (LL_USART_IsEnabled(USARTx) == 0U)
00424   {
00425     /*---------------------------- USART CR2 Configuration -----------------------*/
00426     /* If Clock signal has to be output */
00427     if (USART_ClockInitStruct->ClockOutput == LL_USART_CLOCK_DISABLE)
00428     {
00429       /* Deactivate Clock signal delivery :
00430        * - Disable Clock Output:        USART_CR2_CLKEN cleared
00431        */
00432       LL_USART_DisableSCLKOutput(USARTx);
00433     }
00434     else
00435     {
00436       /* Ensure USART instance is USART capable */
00437       assert_param(IS_USART_INSTANCE(USARTx));
00438 
00439       /* Check clock related parameters */
00440       assert_param(IS_LL_USART_CLOCKPOLARITY(USART_ClockInitStruct->ClockPolarity));
00441       assert_param(IS_LL_USART_CLOCKPHASE(USART_ClockInitStruct->ClockPhase));
00442       assert_param(IS_LL_USART_LASTBITCLKOUTPUT(USART_ClockInitStruct->LastBitClockPulse));
00443 
00444       /*---------------------------- USART CR2 Configuration -----------------------
00445        * Configure USARTx CR2 (Clock signal related bits) with parameters:
00446        * - Enable Clock Output:         USART_CR2_CLKEN set
00447        * - Clock Polarity:              USART_CR2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
00448        * - Clock Phase:                 USART_CR2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
00449        * - Last Bit Clock Pulse Output: USART_CR2_LBCL bit according to USART_ClockInitStruct->LastBitClockPulse value.
00450        */
00451       MODIFY_REG(USARTx->CR2,
00452                  USART_CR2_CLKEN | USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_LBCL,
00453                  USART_CR2_CLKEN | USART_ClockInitStruct->ClockPolarity |
00454                  USART_ClockInitStruct->ClockPhase | USART_ClockInitStruct->LastBitClockPulse);
00455     }
00456   }
00457   /* Else (USART not in Disabled state => return ERROR */
00458   else
00459   {
00460     status = ERROR;
00461   }
00462 
00463   return (status);
00464 }
00465 
00466 /**
00467   * @brief Set each field of a @ref LL_USART_ClockInitTypeDef type structure to default value.
00468   * @param USART_ClockInitStruct Pointer to a @ref LL_USART_ClockInitTypeDef structure
00469   *                              whose fields will be set to default values.
00470   * @retval None
00471   */
00472 void LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
00473 {
00474   /* Set LL_USART_ClockInitStruct fields with default values */
00475   USART_ClockInitStruct->ClockOutput       = LL_USART_CLOCK_DISABLE;
00476   USART_ClockInitStruct->ClockPolarity     = LL_USART_POLARITY_LOW;            /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
00477   USART_ClockInitStruct->ClockPhase        = LL_USART_PHASE_1EDGE;             /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
00478   USART_ClockInitStruct->LastBitClockPulse = LL_USART_LASTCLKPULSE_NO_OUTPUT;  /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
00479 }
00480 
00481 /**
00482   * @}
00483   */
00484 
00485 /**
00486   * @}
00487   */
00488 
00489 /**
00490   * @}
00491   */
00492 
00493 #endif /* USART1 || USART2 || USART3 || USART6 || UART4 || UART5 || UART7 || UART8 || UART9 || UART10 */
00494 
00495 /**
00496   * @}
00497   */
00498 
00499 #endif /* USE_FULL_LL_DRIVER */
00500 
00501 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00502