Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:09

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_ll_lpuart.c
0004   * @author  MCD Application Team
0005   * @brief   LPUART LL module driver.
0006   ******************************************************************************
0007   * @attention
0008   *
0009   * Copyright (c) 2017 STMicroelectronics.
0010   * All rights reserved.
0011   *
0012   * This software is licensed under terms that can be found in the LICENSE file
0013   * in the root directory of this software component.
0014   * If no LICENSE file comes with this software, it is provided AS-IS.
0015   *
0016   ******************************************************************************
0017   */
0018 #if defined(USE_FULL_LL_DRIVER) || defined(__rtems__)
0019 
0020 /* Includes ------------------------------------------------------------------*/
0021 #include "stm32h7xx_ll_lpuart.h"
0022 #include "stm32h7xx_ll_rcc.h"
0023 #include "stm32h7xx_ll_bus.h"
0024 #ifdef USE_FULL_ASSERT
0025 #include "stm32_assert.h"
0026 #else
0027 #define assert_param(expr) ((void)0U)
0028 #endif /* USE_FULL_ASSERT */
0029 
0030 /** @addtogroup STM32H7xx_LL_Driver
0031   * @{
0032   */
0033 
0034 #if defined (LPUART1)
0035 
0036 /** @addtogroup LPUART_LL
0037   * @{
0038   */
0039 
0040 /* Private types -------------------------------------------------------------*/
0041 /* Private variables ---------------------------------------------------------*/
0042 /* Private constants ---------------------------------------------------------*/
0043 /** @addtogroup LPUART_LL_Private_Constants
0044   * @{
0045   */
0046 
0047 /* Definition of default baudrate value used for LPUART initialisation */
0048 #define LPUART_DEFAULT_BAUDRATE          (9600U)
0049 
0050 /**
0051   * @}
0052   */
0053 
0054 
0055 /* Private macros ------------------------------------------------------------*/
0056 /** @addtogroup LPUART_LL_Private_Macros
0057   * @{
0058   */
0059 
0060 /* Check of parameters for configuration of LPUART registers                  */
0061 
0062 #define IS_LL_LPUART_PRESCALER(__VALUE__)  (((__VALUE__) == LL_LPUART_PRESCALER_DIV1) \
0063                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV2) \
0064                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV4) \
0065                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV6) \
0066                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV8) \
0067                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV10) \
0068                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV12) \
0069                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV16) \
0070                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV32) \
0071                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV64) \
0072                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV128) \
0073                                             || ((__VALUE__) == LL_LPUART_PRESCALER_DIV256))
0074 
0075 /* __BAUDRATE__ Depending on constraints applicable for LPUART BRR register   */
0076 /*              value :                                                       */
0077 /*                - fck must be in the range [3 x baudrate, 4096 x baudrate]  */
0078 /*                - LPUART_BRR register value should be >= 0x300              */
0079 /*                - LPUART_BRR register value should be <= 0xFFFFF (20 bits)  */
0080 /*              Baudrate specified by the user should belong to [8, 33000000].*/
0081 #define IS_LL_LPUART_BAUDRATE(__BAUDRATE__) (((__BAUDRATE__) <= 33000000U) && ((__BAUDRATE__) >= 8U))
0082 
0083 /* __VALUE__ BRR content must be greater than or equal to 0x300. */
0084 #define IS_LL_LPUART_BRR_MIN(__VALUE__)   ((__VALUE__) >= 0x300U)
0085 
0086 /* __VALUE__ BRR content must be lower than or equal to 0xFFFFF. */
0087 #define IS_LL_LPUART_BRR_MAX(__VALUE__)   ((__VALUE__) <= 0x000FFFFFU)
0088 
0089 #define IS_LL_LPUART_DIRECTION(__VALUE__) (((__VALUE__) == LL_LPUART_DIRECTION_NONE) \
0090                                            || ((__VALUE__) == LL_LPUART_DIRECTION_RX) \
0091                                            || ((__VALUE__) == LL_LPUART_DIRECTION_TX) \
0092                                            || ((__VALUE__) == LL_LPUART_DIRECTION_TX_RX))
0093 
0094 #define IS_LL_LPUART_PARITY(__VALUE__) (((__VALUE__) == LL_LPUART_PARITY_NONE) \
0095                                         || ((__VALUE__) == LL_LPUART_PARITY_EVEN) \
0096                                         || ((__VALUE__) == LL_LPUART_PARITY_ODD))
0097 
0098 #define IS_LL_LPUART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_LPUART_DATAWIDTH_7B) \
0099                                            || ((__VALUE__) == LL_LPUART_DATAWIDTH_8B) \
0100                                            || ((__VALUE__) == LL_LPUART_DATAWIDTH_9B))
0101 
0102 #define IS_LL_LPUART_STOPBITS(__VALUE__) (((__VALUE__) == LL_LPUART_STOPBITS_1) \
0103                                           || ((__VALUE__) == LL_LPUART_STOPBITS_2))
0104 
0105 #define IS_LL_LPUART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_LPUART_HWCONTROL_NONE) \
0106                                            || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS) \
0107                                            || ((__VALUE__) == LL_LPUART_HWCONTROL_CTS) \
0108                                            || ((__VALUE__) == LL_LPUART_HWCONTROL_RTS_CTS))
0109 
0110 /**
0111   * @}
0112   */
0113 
0114 /* Private function prototypes -----------------------------------------------*/
0115 
0116 /* Exported functions --------------------------------------------------------*/
0117 /** @addtogroup LPUART_LL_Exported_Functions
0118   * @{
0119   */
0120 
0121 /** @addtogroup LPUART_LL_EF_Init
0122   * @{
0123   */
0124 
0125 /**
0126   * @brief  De-initialize LPUART registers (Registers restored to their default values).
0127   * @param  LPUARTx LPUART Instance
0128   * @retval An ErrorStatus enumeration value:
0129   *          - SUCCESS: LPUART registers are de-initialized
0130   *          - ERROR: not applicable
0131   */
0132 ErrorStatus LL_LPUART_DeInit(const USART_TypeDef *LPUARTx)
0133 {
0134   ErrorStatus status = SUCCESS;
0135 
0136   /* Check the parameters */
0137   assert_param(IS_LPUART_INSTANCE(LPUARTx));
0138 
0139   if (LPUARTx == LPUART1)
0140   {
0141     /* Force reset of LPUART peripheral */
0142     LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPUART1);
0143 
0144     /* Release reset of LPUART peripheral */
0145     LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPUART1);
0146   }
0147   else
0148   {
0149     status = ERROR;
0150   }
0151 
0152   return (status);
0153 }
0154 
0155 /**
0156   * @brief  Initialize LPUART registers according to the specified
0157   *         parameters in LPUART_InitStruct.
0158   * @note   As some bits in LPUART configuration registers can only be written when
0159   *         the LPUART is disabled (USART_CR1_UE bit =0),
0160   *         LPUART Peripheral should be in disabled state prior calling this function.
0161   *         Otherwise, ERROR result will be returned.
0162   * @note   Baud rate value stored in LPUART_InitStruct BaudRate field, should be valid (different from 0).
0163   * @param  LPUARTx LPUART Instance
0164   * @param  LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
0165   *         that contains the configuration information for the specified LPUART peripheral.
0166   * @retval An ErrorStatus enumeration value:
0167   *          - SUCCESS: LPUART registers are initialized according to LPUART_InitStruct content
0168   *          - ERROR: Problem occurred during LPUART Registers initialization
0169   */
0170 ErrorStatus LL_LPUART_Init(USART_TypeDef *LPUARTx, const LL_LPUART_InitTypeDef *LPUART_InitStruct)
0171 {
0172   ErrorStatus status = ERROR;
0173   uint32_t periphclk;
0174 
0175   /* Check the parameters */
0176   assert_param(IS_LPUART_INSTANCE(LPUARTx));
0177   assert_param(IS_LL_LPUART_PRESCALER(LPUART_InitStruct->PrescalerValue));
0178   assert_param(IS_LL_LPUART_BAUDRATE(LPUART_InitStruct->BaudRate));
0179   assert_param(IS_LL_LPUART_DATAWIDTH(LPUART_InitStruct->DataWidth));
0180   assert_param(IS_LL_LPUART_STOPBITS(LPUART_InitStruct->StopBits));
0181   assert_param(IS_LL_LPUART_PARITY(LPUART_InitStruct->Parity));
0182   assert_param(IS_LL_LPUART_DIRECTION(LPUART_InitStruct->TransferDirection));
0183   assert_param(IS_LL_LPUART_HWCONTROL(LPUART_InitStruct->HardwareFlowControl));
0184 
0185   /* LPUART needs to be in disabled state, in order to be able to configure some bits in
0186      CRx registers. Otherwise (LPUART not in Disabled state) => return ERROR */
0187   if (LL_LPUART_IsEnabled(LPUARTx) == 0U)
0188   {
0189     /*---------------------------- LPUART CR1 Configuration -----------------------
0190      * Configure LPUARTx CR1 (LPUART Word Length, Parity and Transfer Direction bits) with parameters:
0191      * - DataWidth:          USART_CR1_M bits according to LPUART_InitStruct->DataWidth value
0192      * - Parity:             USART_CR1_PCE, USART_CR1_PS bits according to LPUART_InitStruct->Parity value
0193      * - TransferDirection:  USART_CR1_TE, USART_CR1_RE bits according to LPUART_InitStruct->TransferDirection value
0194      */
0195     MODIFY_REG(LPUARTx->CR1,
0196                (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
0197                (LPUART_InitStruct->DataWidth | LPUART_InitStruct->Parity | LPUART_InitStruct->TransferDirection));
0198 
0199     /*---------------------------- LPUART CR2 Configuration -----------------------
0200      * Configure LPUARTx CR2 (Stop bits) with parameters:
0201      * - Stop Bits:          USART_CR2_STOP bits according to LPUART_InitStruct->StopBits value.
0202      */
0203     LL_LPUART_SetStopBitsLength(LPUARTx, LPUART_InitStruct->StopBits);
0204 
0205     /*---------------------------- LPUART CR3 Configuration -----------------------
0206      * Configure LPUARTx CR3 (Hardware Flow Control) with parameters:
0207      * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according
0208      *   to LPUART_InitStruct->HardwareFlowControl value.
0209      */
0210     LL_LPUART_SetHWFlowCtrl(LPUARTx, LPUART_InitStruct->HardwareFlowControl);
0211 
0212     /*---------------------------- LPUART BRR Configuration -----------------------
0213      * Retrieve Clock frequency used for LPUART Peripheral
0214      */
0215     periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE);
0216 
0217     /* Configure the LPUART Baud Rate :
0218        - prescaler value is required
0219        - valid baud rate value (different from 0) is required
0220        - Peripheral clock as returned by RCC service, should be valid (different from 0).
0221     */
0222     if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
0223         && (LPUART_InitStruct->BaudRate != 0U))
0224     {
0225       status = SUCCESS;
0226       LL_LPUART_SetBaudRate(LPUARTx,
0227                             periphclk,
0228                             LPUART_InitStruct->PrescalerValue,
0229                             LPUART_InitStruct->BaudRate);
0230 
0231       /* Check BRR is greater than or equal to 0x300 */
0232       assert_param(IS_LL_LPUART_BRR_MIN(LPUARTx->BRR));
0233 
0234       /* Check BRR is lower than or equal to 0xFFFFF */
0235       assert_param(IS_LL_LPUART_BRR_MAX(LPUARTx->BRR));
0236     }
0237 
0238     /*---------------------------- LPUART PRESC Configuration -----------------------
0239      * Configure LPUARTx PRESC (Prescaler) with parameters:
0240      * - PrescalerValue: LPUART_PRESC_PRESCALER bits according to LPUART_InitStruct->PrescalerValue value.
0241      */
0242     LL_LPUART_SetPrescaler(LPUARTx, LPUART_InitStruct->PrescalerValue);
0243   }
0244 
0245   return (status);
0246 }
0247 
0248 /**
0249   * @brief Set each @ref LL_LPUART_InitTypeDef field to default value.
0250   * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
0251   *                          whose fields will be set to default values.
0252   * @retval None
0253   */
0254 
0255 void LL_LPUART_StructInit(LL_LPUART_InitTypeDef *LPUART_InitStruct)
0256 {
0257   /* Set LPUART_InitStruct fields to default values */
0258   LPUART_InitStruct->PrescalerValue      = LL_LPUART_PRESCALER_DIV1;
0259   LPUART_InitStruct->BaudRate            = LPUART_DEFAULT_BAUDRATE;
0260   LPUART_InitStruct->DataWidth           = LL_LPUART_DATAWIDTH_8B;
0261   LPUART_InitStruct->StopBits            = LL_LPUART_STOPBITS_1;
0262   LPUART_InitStruct->Parity              = LL_LPUART_PARITY_NONE ;
0263   LPUART_InitStruct->TransferDirection   = LL_LPUART_DIRECTION_TX_RX;
0264   LPUART_InitStruct->HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
0265 }
0266 
0267 /**
0268   * @}
0269   */
0270 
0271 /**
0272   * @}
0273   */
0274 
0275 /**
0276   * @}
0277   */
0278 
0279 #endif /* LPUART1 */
0280 
0281 /**
0282   * @}
0283   */
0284 
0285 #endif /* USE_FULL_LL_DRIVER */