STM32H735xx HAL User Manual
stm32h7xx_ll_lpuart.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32h7xx_ll_lpuart.c
00004   * @author  MCD Application Team
00005   * @brief   LPUART LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * Copyright (c) 2017 STMicroelectronics.
00010   * All rights reserved.
00011   *
00012   * This software is licensed under terms that can be found in the LICENSE file
00013   * in the root directory of this software component.
00014   * If no LICENSE file comes with this software, it is provided AS-IS.
00015   *
00016   ******************************************************************************
00017   */
00018 #if defined(USE_FULL_LL_DRIVER)
00019 
00020 /* Includes ------------------------------------------------------------------*/
00021 #include "stm32h7xx_ll_lpuart.h"
00022 #include "stm32h7xx_ll_rcc.h"
00023 #include "stm32h7xx_ll_bus.h"
00024 #ifdef USE_FULL_ASSERT
00025 #include "stm32_assert.h"
00026 #else
00027 #define assert_param(expr) ((void)0U)
00028 #endif /* USE_FULL_ASSERT */
00029 
00030 /** @addtogroup STM32H7xx_LL_Driver
00031   * @{
00032   */
00033 
00034 #if defined (LPUART1)
00035 
00036 /** @addtogroup LPUART_LL
00037   * @{
00038   */
00039 
00040 /* Private types -------------------------------------------------------------*/
00041 /* Private variables ---------------------------------------------------------*/
00042 /* Private constants ---------------------------------------------------------*/
00043 /** @addtogroup LPUART_LL_Private_Constants
00044   * @{
00045   */
00046 
00047 /**
00048   * @}
00049   */
00050 
00051 
00052 /* Private macros ------------------------------------------------------------*/
00053 /** @addtogroup LPUART_LL_Private_Macros
00054   * @{
00055   */
00056 
00057 /* Check of parameters for configuration of LPUART registers                  */
00058 
00059 #define IS_LL_LPUART_PRESCALER(__VALUE__)  (((__VALUE__) == LL_LPUART_PRESCALER_DIV1) \
00060                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV2) \
00061                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV4) \
00062                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV6) \
00063                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV8) \
00064                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV10) \
00065                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV12) \
00066                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV16) \
00067                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV32) \
00068                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV64) \
00069                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV128) \
00070                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV256))
00071 
00072 /* __BAUDRATE__ Depending on constraints applicable for LPUART BRR register   */
00073 /*              value :                                                       */
00074 /*                - fck must be in the range [3 x baudrate, 4096 x baudrate]  */
00075 /*                - LPUART_BRR register value should be >= 0x300              */
00076 /*                - LPUART_BRR register value should be <= 0xFFFFF (20 bits)  */
00077 /*              Baudrate specified by the user should belong to [8, 33000000].*/
00078 #define IS_LL_LPUART_BAUDRATE(__BAUDRATE__) (((__BAUDRATE__) <= 33000000U) && ((__BAUDRATE__) >= 8U))
00079 
00080 /* __VALUE__ BRR content must be greater than or equal to 0x300. */
00081 #define IS_LL_LPUART_BRR_MIN(__VALUE__)   ((__VALUE__) >= 0x300U)
00082 
00083 /* __VALUE__ BRR content must be lower than or equal to 0xFFFFF. */
00084 #define IS_LL_LPUART_BRR_MAX(__VALUE__)   ((__VALUE__) <= 0x000FFFFFU)
00085 
00086 #define IS_LL_LPUART_DIRECTION(__VALUE__) (((__VALUE__) == LL_LPUART_DIRECTION_NONE) \
00087                                            || ((__VALUE__) == LL_LPUART_DIRECTION_RX) \
00088                                            || ((__VALUE__) == LL_LPUART_DIRECTION_TX) \
00089                                            || ((__VALUE__) == LL_LPUART_DIRECTION_TX_RX))
00090 
00091 #define IS_LL_LPUART_PARITY(__VALUE__) (((__VALUE__) == LL_LPUART_PARITY_NONE) \
00092                                         || ((__VALUE__) == LL_LPUART_PARITY_EVEN) \
00093                                         || ((__VALUE__) == LL_LPUART_PARITY_ODD))
00094 
00095 #define IS_LL_LPUART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_LPUART_DATAWIDTH_7B) \
00096                                            || ((__VALUE__) == LL_LPUART_DATAWIDTH_8B) \
00097                                            || ((__VALUE__) == LL_LPUART_DATAWIDTH_9B))
00098 
00099 #define IS_LL_LPUART_STOPBITS(__VALUE__) (((__VALUE__) == LL_LPUART_STOPBITS_1) \
00100                                           || ((__VALUE__) == LL_LPUART_STOPBITS_2))
00101 
00102 #define IS_LL_LPUART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_LPUART_HWCONTROL_NONE) \
00103                                            || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS) \
00104                                            || ((__VALUE__) == LL_LPUART_HWCONTROL_CTS) \
00105                                            || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS_CTS))
00106 
00107 /**
00108   * @}
00109   */
00110 
00111 /* Private function prototypes -----------------------------------------------*/
00112 
00113 /* Exported functions --------------------------------------------------------*/
00114 /** @addtogroup LPUART_LL_Exported_Functions
00115   * @{
00116   */
00117 
00118 /** @addtogroup LPUART_LL_EF_Init
00119   * @{
00120   */
00121 
00122 /**
00123   * @brief  De-initialize LPUART registers (Registers restored to their default values).
00124   * @param  LPUARTx LPUART Instance
00125   * @retval An ErrorStatus enumeration value:
00126   *          - SUCCESS: LPUART registers are de-initialized
00127   *          - ERROR: not applicable
00128   */
00129 ErrorStatus LL_LPUART_DeInit(USART_TypeDef *LPUARTx)
00130 {
00131   ErrorStatus status = SUCCESS;
00132 
00133   /* Check the parameters */
00134   assert_param(IS_LPUART_INSTANCE(LPUARTx));
00135 
00136   if (LPUARTx == LPUART1)
00137   {
00138     /* Force reset of LPUART peripheral */
00139     LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPUART1);
00140 
00141     /* Release reset of LPUART peripheral */
00142     LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPUART1);
00143   }
00144   else
00145   {
00146     status = ERROR;
00147   }
00148 
00149   return (status);
00150 }
00151 
00152 /**
00153   * @brief  Initialize LPUART registers according to the specified
00154   *         parameters in LPUART_InitStruct.
00155   * @note   As some bits in LPUART configuration registers can only be written when
00156   *         the LPUART is disabled (USART_CR1_UE bit =0),
00157   *         LPUART Peripheral should be in disabled state prior calling this function.
00158   *         Otherwise, ERROR result will be returned.
00159   * @note   Baud rate value stored in LPUART_InitStruct BaudRate field, should be valid (different from 0).
00160   * @param  LPUARTx LPUART Instance
00161   * @param  LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
00162   *         that contains the configuration information for the specified LPUART peripheral.
00163   * @retval An ErrorStatus enumeration value:
00164   *          - SUCCESS: LPUART registers are initialized according to LPUART_InitStruct content
00165   *          - ERROR: Problem occurred during LPUART Registers initialization
00166   */
00167 ErrorStatus LL_LPUART_Init(USART_TypeDef *LPUARTx, LL_LPUART_InitTypeDef *LPUART_InitStruct)
00168 {
00169   ErrorStatus status = ERROR;
00170   uint32_t periphclk;
00171 
00172   /* Check the parameters */
00173   assert_param(IS_LPUART_INSTANCE(LPUARTx));
00174   assert_param(IS_LL_LPUART_PRESCALER(LPUART_InitStruct->PrescalerValue));
00175   assert_param(IS_LL_LPUART_BAUDRATE(LPUART_InitStruct->BaudRate));
00176   assert_param(IS_LL_LPUART_DATAWIDTH(LPUART_InitStruct->DataWidth));
00177   assert_param(IS_LL_LPUART_STOPBITS(LPUART_InitStruct->StopBits));
00178   assert_param(IS_LL_LPUART_PARITY(LPUART_InitStruct->Parity));
00179   assert_param(IS_LL_LPUART_DIRECTION(LPUART_InitStruct->TransferDirection));
00180   assert_param(IS_LL_LPUART_HWCONTROL(LPUART_InitStruct->HardwareFlowControl));
00181 
00182   /* LPUART needs to be in disabled state, in order to be able to configure some bits in
00183      CRx registers. Otherwise (LPUART not in Disabled state) => return ERROR */
00184   if (LL_LPUART_IsEnabled(LPUARTx) == 0U)
00185   {
00186     /*---------------------------- LPUART CR1 Configuration -----------------------
00187      * Configure LPUARTx CR1 (LPUART Word Length, Parity and Transfer Direction bits) with parameters:
00188      * - DataWidth:          USART_CR1_M bits according to LPUART_InitStruct->DataWidth value
00189      * - Parity:             USART_CR1_PCE, USART_CR1_PS bits according to LPUART_InitStruct->Parity value
00190      * - TransferDirection:  USART_CR1_TE, USART_CR1_RE bits according to LPUART_InitStruct->TransferDirection value
00191      */
00192     MODIFY_REG(LPUARTx->CR1,
00193                (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
00194                (LPUART_InitStruct->DataWidth | LPUART_InitStruct->Parity | LPUART_InitStruct->TransferDirection));
00195 
00196     /*---------------------------- LPUART CR2 Configuration -----------------------
00197      * Configure LPUARTx CR2 (Stop bits) with parameters:
00198      * - Stop Bits:          USART_CR2_STOP bits according to LPUART_InitStruct->StopBits value.
00199      */
00200     LL_LPUART_SetStopBitsLength(LPUARTx, LPUART_InitStruct->StopBits);
00201 
00202     /*---------------------------- LPUART CR3 Configuration -----------------------
00203      * Configure LPUARTx CR3 (Hardware Flow Control) with parameters:
00204      * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according
00205      *   to LPUART_InitStruct->HardwareFlowControl value.
00206      */
00207     LL_LPUART_SetHWFlowCtrl(LPUARTx, LPUART_InitStruct->HardwareFlowControl);
00208 
00209     /*---------------------------- LPUART BRR Configuration -----------------------
00210      * Retrieve Clock frequency used for LPUART Peripheral
00211      */
00212     periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE);
00213 
00214     /* Configure the LPUART Baud Rate :
00215        - prescaler value is required
00216        - valid baud rate value (different from 0) is required
00217        - Peripheral clock as returned by RCC service, should be valid (different from 0).
00218     */
00219     if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
00220         && (LPUART_InitStruct->BaudRate != 0U))
00221     {
00222       status = SUCCESS;
00223       LL_LPUART_SetBaudRate(LPUARTx,
00224                             periphclk,
00225                             LPUART_InitStruct->PrescalerValue,
00226                             LPUART_InitStruct->BaudRate);
00227 
00228       /* Check BRR is greater than or equal to 0x300 */
00229       assert_param(IS_LL_LPUART_BRR_MIN(LPUARTx->BRR));
00230 
00231       /* Check BRR is lower than or equal to 0xFFFFF */
00232       assert_param(IS_LL_LPUART_BRR_MAX(LPUARTx->BRR));
00233     }
00234 
00235     /*---------------------------- LPUART PRESC Configuration -----------------------
00236      * Configure LPUARTx PRESC (Prescaler) with parameters:
00237      * - PrescalerValue: LPUART_PRESC_PRESCALER bits according to LPUART_InitStruct->PrescalerValue value.
00238      */
00239     LL_LPUART_SetPrescaler(LPUARTx, LPUART_InitStruct->PrescalerValue);
00240   }
00241 
00242   return (status);
00243 }
00244 
00245 /**
00246   * @brief Set each @ref LL_LPUART_InitTypeDef field to default value.
00247   * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
00248   *                          whose fields will be set to default values.
00249   * @retval None
00250   */
00251 
00252 void LL_LPUART_StructInit(LL_LPUART_InitTypeDef *LPUART_InitStruct)
00253 {
00254   /* Set LPUART_InitStruct fields to default values */
00255   LPUART_InitStruct->PrescalerValue      = LL_LPUART_PRESCALER_DIV1;
00256   LPUART_InitStruct->BaudRate            = 9600U;
00257   LPUART_InitStruct->DataWidth           = LL_LPUART_DATAWIDTH_8B;
00258   LPUART_InitStruct->StopBits            = LL_LPUART_STOPBITS_1;
00259   LPUART_InitStruct->Parity              = LL_LPUART_PARITY_NONE ;
00260   LPUART_InitStruct->TransferDirection   = LL_LPUART_DIRECTION_TX_RX;
00261   LPUART_InitStruct->HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
00262 }
00263 
00264 /**
00265   * @}
00266   */
00267 
00268 /**
00269   * @}
00270   */
00271 
00272 /**
00273   * @}
00274   */
00275 
00276 #endif /* LPUART1 */
00277 
00278 /**
00279   * @}
00280   */
00281 
00282 #endif /* USE_FULL_LL_DRIVER */