Subversion Repositories dashGPS

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f1xx_ll_usart.c
  4.   * @author  MCD Application Team
  5.   * @brief   USART LL module driver.
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19.  
  20. #if defined(USE_FULL_LL_DRIVER)
  21.  
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32f1xx_ll_usart.h"
  24. #include "stm32f1xx_ll_rcc.h"
  25. #include "stm32f1xx_ll_bus.h"
  26. #ifdef  USE_FULL_ASSERT
  27. #include "stm32_assert.h"
  28. #else
  29. #define assert_param(expr) ((void)0U)
  30. #endif
  31.  
  32. /** @addtogroup STM32F1xx_LL_Driver
  33.   * @{
  34.   */
  35.  
  36. #if defined (USART1) || defined (USART2) || defined (USART3) || defined (UART4) || defined (UART5)
  37.  
  38. /** @addtogroup USART_LL
  39.   * @{
  40.   */
  41.  
  42. /* Private types -------------------------------------------------------------*/
  43. /* Private variables ---------------------------------------------------------*/
  44. /* Private constants ---------------------------------------------------------*/
  45. /** @addtogroup USART_LL_Private_Constants
  46.   * @{
  47.   */
  48.  
  49. /**
  50.   * @}
  51.   */
  52.  
  53.  
  54. /* Private macros ------------------------------------------------------------*/
  55. /** @addtogroup USART_LL_Private_Macros
  56.   * @{
  57.   */
  58.  
  59. /* __BAUDRATE__ The maximum Baud Rate is derived from the maximum clock available
  60.  *              divided by the smallest oversampling used on the USART (i.e. 8)    */
  61. #define IS_LL_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 4500000U)
  62.  
  63. /* __VALUE__ In case of oversampling by 16 and 8, BRR content must be greater than or equal to 16d. */
  64. #define IS_LL_USART_BRR_MIN(__VALUE__) ((__VALUE__) >= 16U)
  65.  
  66. /* __VALUE__ BRR content must be lower than or equal to 0xFFFF. */
  67. #define IS_LL_USART_BRR_MAX(__VALUE__) ((__VALUE__) <= 0x0000FFFFU)
  68.  
  69. #define IS_LL_USART_DIRECTION(__VALUE__) (((__VALUE__) == LL_USART_DIRECTION_NONE) \
  70.                                        || ((__VALUE__) == LL_USART_DIRECTION_RX) \
  71.                                        || ((__VALUE__) == LL_USART_DIRECTION_TX) \
  72.                                        || ((__VALUE__) == LL_USART_DIRECTION_TX_RX))
  73.  
  74. #define IS_LL_USART_PARITY(__VALUE__) (((__VALUE__) == LL_USART_PARITY_NONE) \
  75.                                     || ((__VALUE__) == LL_USART_PARITY_EVEN) \
  76.                                     || ((__VALUE__) == LL_USART_PARITY_ODD))
  77.  
  78. #define IS_LL_USART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_USART_DATAWIDTH_8B) \
  79.                                        || ((__VALUE__) == LL_USART_DATAWIDTH_9B))
  80.  
  81. #define IS_LL_USART_OVERSAMPLING(__VALUE__) (((__VALUE__) == LL_USART_OVERSAMPLING_16) \
  82.                                           || ((__VALUE__) == LL_USART_OVERSAMPLING_8))
  83.  
  84. #define IS_LL_USART_LASTBITCLKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_LASTCLKPULSE_NO_OUTPUT) \
  85.                                               || ((__VALUE__) == LL_USART_LASTCLKPULSE_OUTPUT))
  86.  
  87. #define IS_LL_USART_CLOCKPHASE(__VALUE__) (((__VALUE__) == LL_USART_PHASE_1EDGE) \
  88.                                         || ((__VALUE__) == LL_USART_PHASE_2EDGE))
  89.  
  90. #define IS_LL_USART_CLOCKPOLARITY(__VALUE__) (((__VALUE__) == LL_USART_POLARITY_LOW) \
  91.                                            || ((__VALUE__) == LL_USART_POLARITY_HIGH))
  92.  
  93. #define IS_LL_USART_CLOCKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_CLOCK_DISABLE) \
  94.                                          || ((__VALUE__) == LL_USART_CLOCK_ENABLE))
  95.  
  96. #define IS_LL_USART_STOPBITS(__VALUE__) (((__VALUE__) == LL_USART_STOPBITS_0_5) \
  97.                                       || ((__VALUE__) == LL_USART_STOPBITS_1) \
  98.                                       || ((__VALUE__) == LL_USART_STOPBITS_1_5) \
  99.                                       || ((__VALUE__) == LL_USART_STOPBITS_2))
  100.  
  101. #define IS_LL_USART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_USART_HWCONTROL_NONE) \
  102.                                        || ((__VALUE__) == LL_USART_HWCONTROL_RTS) \
  103.                                        || ((__VALUE__) == LL_USART_HWCONTROL_CTS) \
  104.                                        || ((__VALUE__) == LL_USART_HWCONTROL_RTS_CTS))
  105.  
  106. /**
  107.   * @}
  108.   */
  109.  
  110. /* Private function prototypes -----------------------------------------------*/
  111.  
  112. /* Exported functions --------------------------------------------------------*/
  113. /** @addtogroup USART_LL_Exported_Functions
  114.   * @{
  115.   */
  116.  
  117. /** @addtogroup USART_LL_EF_Init
  118.   * @{
  119.   */
  120.  
  121. /**
  122.   * @brief  De-initialize USART registers (Registers restored to their default values).
  123.   * @param  USARTx USART Instance
  124.   * @retval An ErrorStatus enumeration value:
  125.   *          - SUCCESS: USART registers are de-initialized
  126.   *          - ERROR: USART registers are not de-initialized
  127.   */
  128. ErrorStatus LL_USART_DeInit(USART_TypeDef *USARTx)
  129. {
  130.   ErrorStatus status = SUCCESS;
  131.  
  132.   /* Check the parameters */
  133.   assert_param(IS_UART_INSTANCE(USARTx));
  134.  
  135.   if (USARTx == USART1)
  136.   {
  137.     /* Force reset of USART clock */
  138.     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART1);
  139.  
  140.     /* Release reset of USART clock */
  141.     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART1);
  142.   }
  143.   else if (USARTx == USART2)
  144.   {
  145.     /* Force reset of USART clock */
  146.     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART2);
  147.  
  148.     /* Release reset of USART clock */
  149.     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART2);
  150.   }
  151. #if defined(USART3)
  152.   else if (USARTx == USART3)
  153.   {
  154.     /* Force reset of USART clock */
  155.     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART3);
  156.  
  157.     /* Release reset of USART clock */
  158.     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART3);
  159.   }
  160. #endif /* USART3 */
  161. #if defined(UART4)
  162.   else if (USARTx == UART4)
  163.   {
  164.     /* Force reset of UART clock */
  165.     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART4);
  166.  
  167.     /* Release reset of UART clock */
  168.     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART4);
  169.   }
  170. #endif /* UART4 */
  171. #if defined(UART5)
  172.   else if (USARTx == UART5)
  173.   {
  174.     /* Force reset of UART clock */
  175.     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART5);
  176.  
  177.     /* Release reset of UART clock */
  178.     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART5);
  179.   }
  180. #endif /* UART5 */
  181.   else
  182.   {
  183.     status = ERROR;
  184.   }
  185.  
  186.   return (status);
  187. }
  188.  
  189. /**
  190.   * @brief  Initialize USART registers according to the specified
  191.   *         parameters in USART_InitStruct.
  192.   * @note   As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
  193.   *         USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  194.   * @note   Baud rate value stored in USART_InitStruct BaudRate field, should be valid (different from 0).
  195.   * @param  USARTx USART Instance
  196.   * @param  USART_InitStruct pointer to a LL_USART_InitTypeDef structure
  197.   *         that contains the configuration information for the specified USART peripheral.
  198.   * @retval An ErrorStatus enumeration value:
  199.   *          - SUCCESS: USART registers are initialized according to USART_InitStruct content
  200.   *          - ERROR: Problem occurred during USART Registers initialization
  201.   */
  202. ErrorStatus LL_USART_Init(USART_TypeDef *USARTx, LL_USART_InitTypeDef *USART_InitStruct)
  203. {
  204.   ErrorStatus status = ERROR;
  205.   uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
  206.   LL_RCC_ClocksTypeDef rcc_clocks;
  207.  
  208.   /* Check the parameters */
  209.   assert_param(IS_UART_INSTANCE(USARTx));
  210.   assert_param(IS_LL_USART_BAUDRATE(USART_InitStruct->BaudRate));
  211.   assert_param(IS_LL_USART_DATAWIDTH(USART_InitStruct->DataWidth));
  212.   assert_param(IS_LL_USART_STOPBITS(USART_InitStruct->StopBits));
  213.   assert_param(IS_LL_USART_PARITY(USART_InitStruct->Parity));
  214.   assert_param(IS_LL_USART_DIRECTION(USART_InitStruct->TransferDirection));
  215.   assert_param(IS_LL_USART_HWCONTROL(USART_InitStruct->HardwareFlowControl));
  216. #if defined(USART_CR1_OVER8)
  217.   assert_param(IS_LL_USART_OVERSAMPLING(USART_InitStruct->OverSampling));
  218. #endif /* USART_OverSampling_Feature */
  219.  
  220.   /* USART needs to be in disabled state, in order to be able to configure some bits in
  221.      CRx registers */
  222.   if (LL_USART_IsEnabled(USARTx) == 0U)
  223.   {
  224.     /*---------------------------- USART CR1 Configuration -----------------------
  225.      * Configure USARTx CR1 (USART Word Length, Parity, Mode and Oversampling bits) with parameters:
  226.      * - DataWidth:          USART_CR1_M bits according to USART_InitStruct->DataWidth value
  227.      * - Parity:             USART_CR1_PCE, USART_CR1_PS bits according to USART_InitStruct->Parity value
  228.      * - TransferDirection:  USART_CR1_TE, USART_CR1_RE bits according to USART_InitStruct->TransferDirection value
  229.      * - Oversampling:       USART_CR1_OVER8 bit according to USART_InitStruct->OverSampling value.
  230.      */
  231. #if defined(USART_CR1_OVER8)
  232.     MODIFY_REG(USARTx->CR1,
  233.                (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
  234.                 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
  235.                (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
  236.                 USART_InitStruct->TransferDirection | USART_InitStruct->OverSampling));
  237. #else
  238.     MODIFY_REG(USARTx->CR1,
  239.                (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
  240.                 USART_CR1_TE | USART_CR1_RE),
  241.                (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
  242.                 USART_InitStruct->TransferDirection));
  243. #endif /* USART_OverSampling_Feature */
  244.  
  245.     /*---------------------------- USART CR2 Configuration -----------------------
  246.      * Configure USARTx CR2 (Stop bits) with parameters:
  247.      * - Stop Bits:          USART_CR2_STOP bits according to USART_InitStruct->StopBits value.
  248.      * - CLKEN, CPOL, CPHA and LBCL bits are to be configured using LL_USART_ClockInit().
  249.      */
  250.     LL_USART_SetStopBitsLength(USARTx, USART_InitStruct->StopBits);
  251.  
  252.     /*---------------------------- USART CR3 Configuration -----------------------
  253.      * Configure USARTx CR3 (Hardware Flow Control) with parameters:
  254.      * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to USART_InitStruct->HardwareFlowControl value.
  255.      */
  256.     LL_USART_SetHWFlowCtrl(USARTx, USART_InitStruct->HardwareFlowControl);
  257.  
  258.     /*---------------------------- USART BRR Configuration -----------------------
  259.      * Retrieve Clock frequency used for USART Peripheral
  260.      */
  261.     LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  262.     if (USARTx == USART1)
  263.     {
  264.       periphclk = rcc_clocks.PCLK2_Frequency;
  265.     }
  266.     else if (USARTx == USART2)
  267.     {
  268.       periphclk = rcc_clocks.PCLK1_Frequency;
  269.     }
  270. #if defined(USART3)
  271.     else if (USARTx == USART3)
  272.     {
  273.       periphclk = rcc_clocks.PCLK1_Frequency;
  274.     }
  275. #endif /* USART3 */
  276. #if defined(UART4)
  277.     else if (USARTx == UART4)
  278.     {
  279.       periphclk = rcc_clocks.PCLK1_Frequency;
  280.     }
  281. #endif /* UART4 */
  282. #if defined(UART5)
  283.     else if (USARTx == UART5)
  284.     {
  285.       periphclk = rcc_clocks.PCLK1_Frequency;
  286.     }
  287. #endif /* UART5 */
  288.     else
  289.     {
  290.       /* Nothing to do, as error code is already assigned to ERROR value */
  291.     }
  292.  
  293.     /* Configure the USART Baud Rate :
  294.        - valid baud rate value (different from 0) is required
  295.        - Peripheral clock as returned by RCC service, should be valid (different from 0).
  296.     */
  297.     if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
  298.         && (USART_InitStruct->BaudRate != 0U))
  299.     {
  300.       status = SUCCESS;
  301. #if defined(USART_CR1_OVER8)
  302.       LL_USART_SetBaudRate(USARTx,
  303.                            periphclk,
  304.                            USART_InitStruct->OverSampling,
  305.                            USART_InitStruct->BaudRate);
  306. #else
  307.       LL_USART_SetBaudRate(USARTx,
  308.                            periphclk,
  309.                            USART_InitStruct->BaudRate);
  310. #endif /* USART_OverSampling_Feature */
  311.  
  312.       /* Check BRR is greater than or equal to 16d */
  313.       assert_param(IS_LL_USART_BRR_MIN(USARTx->BRR));
  314.  
  315.       /* Check BRR is greater than or equal to 16d */
  316.       assert_param(IS_LL_USART_BRR_MAX(USARTx->BRR));
  317.     }
  318.   }
  319.   /* Endif (=> USART not in Disabled state => return ERROR) */
  320.  
  321.   return (status);
  322. }
  323.  
  324. /**
  325.   * @brief Set each @ref LL_USART_InitTypeDef field to default value.
  326.   * @param USART_InitStruct Pointer to a @ref LL_USART_InitTypeDef structure
  327.   *                         whose fields will be set to default values.
  328.   * @retval None
  329.   */
  330.  
  331. void LL_USART_StructInit(LL_USART_InitTypeDef *USART_InitStruct)
  332. {
  333.   /* Set USART_InitStruct fields to default values */
  334.   USART_InitStruct->BaudRate            = 9600U;
  335.   USART_InitStruct->DataWidth           = LL_USART_DATAWIDTH_8B;
  336.   USART_InitStruct->StopBits            = LL_USART_STOPBITS_1;
  337.   USART_InitStruct->Parity              = LL_USART_PARITY_NONE ;
  338.   USART_InitStruct->TransferDirection   = LL_USART_DIRECTION_TX_RX;
  339.   USART_InitStruct->HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  340. #if defined(USART_CR1_OVER8)
  341.   USART_InitStruct->OverSampling        = LL_USART_OVERSAMPLING_16;
  342. #endif /* USART_OverSampling_Feature */
  343. }
  344.  
  345. /**
  346.   * @brief  Initialize USART Clock related settings according to the
  347.   *         specified parameters in the USART_ClockInitStruct.
  348.   * @note   As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
  349.   *         USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
  350.   * @param  USARTx USART Instance
  351.   * @param  USART_ClockInitStruct Pointer to a @ref LL_USART_ClockInitTypeDef structure
  352.   *         that contains the Clock configuration information for the specified USART peripheral.
  353.   * @retval An ErrorStatus enumeration value:
  354.   *          - SUCCESS: USART registers related to Clock settings are initialized according to USART_ClockInitStruct content
  355.   *          - ERROR: Problem occurred during USART Registers initialization
  356.   */
  357. ErrorStatus LL_USART_ClockInit(USART_TypeDef *USARTx, LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
  358. {
  359.   ErrorStatus status = SUCCESS;
  360.  
  361.   /* Check USART Instance and Clock signal output parameters */
  362.   assert_param(IS_UART_INSTANCE(USARTx));
  363.   assert_param(IS_LL_USART_CLOCKOUTPUT(USART_ClockInitStruct->ClockOutput));
  364.  
  365.   /* USART needs to be in disabled state, in order to be able to configure some bits in
  366.      CRx registers */
  367.   if (LL_USART_IsEnabled(USARTx) == 0U)
  368.   {
  369.     /*---------------------------- USART CR2 Configuration -----------------------*/
  370.     /* If Clock signal has to be output */
  371.     if (USART_ClockInitStruct->ClockOutput == LL_USART_CLOCK_DISABLE)
  372.     {
  373.       /* Deactivate Clock signal delivery :
  374.        * - Disable Clock Output:        USART_CR2_CLKEN cleared
  375.        */
  376.       LL_USART_DisableSCLKOutput(USARTx);
  377.     }
  378.     else
  379.     {
  380.       /* Ensure USART instance is USART capable */
  381.       assert_param(IS_USART_INSTANCE(USARTx));
  382.  
  383.       /* Check clock related parameters */
  384.       assert_param(IS_LL_USART_CLOCKPOLARITY(USART_ClockInitStruct->ClockPolarity));
  385.       assert_param(IS_LL_USART_CLOCKPHASE(USART_ClockInitStruct->ClockPhase));
  386.       assert_param(IS_LL_USART_LASTBITCLKOUTPUT(USART_ClockInitStruct->LastBitClockPulse));
  387.  
  388.       /*---------------------------- USART CR2 Configuration -----------------------
  389.        * Configure USARTx CR2 (Clock signal related bits) with parameters:
  390.        * - Enable Clock Output:         USART_CR2_CLKEN set
  391.        * - Clock Polarity:              USART_CR2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
  392.        * - Clock Phase:                 USART_CR2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
  393.        * - Last Bit Clock Pulse Output: USART_CR2_LBCL bit according to USART_ClockInitStruct->LastBitClockPulse value.
  394.        */
  395.       MODIFY_REG(USARTx->CR2,
  396.                  USART_CR2_CLKEN | USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_LBCL,
  397.                  USART_CR2_CLKEN | USART_ClockInitStruct->ClockPolarity |
  398.                  USART_ClockInitStruct->ClockPhase | USART_ClockInitStruct->LastBitClockPulse);
  399.     }
  400.   }
  401.   /* Else (USART not in Disabled state => return ERROR */
  402.   else
  403.   {
  404.     status = ERROR;
  405.   }
  406.  
  407.   return (status);
  408. }
  409.  
  410. /**
  411.   * @brief Set each field of a @ref LL_USART_ClockInitTypeDef type structure to default value.
  412.   * @param USART_ClockInitStruct Pointer to a @ref LL_USART_ClockInitTypeDef structure
  413.   *                              whose fields will be set to default values.
  414.   * @retval None
  415.   */
  416. void LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
  417. {
  418.   /* Set LL_USART_ClockInitStruct fields with default values */
  419.   USART_ClockInitStruct->ClockOutput       = LL_USART_CLOCK_DISABLE;
  420.   USART_ClockInitStruct->ClockPolarity     = LL_USART_POLARITY_LOW;            /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  421.   USART_ClockInitStruct->ClockPhase        = LL_USART_PHASE_1EDGE;             /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  422.   USART_ClockInitStruct->LastBitClockPulse = LL_USART_LASTCLKPULSE_NO_OUTPUT;  /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
  423. }
  424.  
  425. /**
  426.   * @}
  427.   */
  428.  
  429. /**
  430.   * @}
  431.   */
  432.  
  433. /**
  434.   * @}
  435.   */
  436.  
  437. #endif /* USART1 || USART2 || USART3 || UART4 || UART5 */
  438.  
  439. /**
  440.   * @}
  441.   */
  442.  
  443. #endif /* USE_FULL_LL_DRIVER */
  444.  
  445. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  446.  
  447.