TUKS MCU Introductory course / TUKS-COURSE-THERMOMETER

Fork of TUKS-COURSE-TIMER by TUKS MCU Introductory course

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4xx_ll_lpuart.c Source File

stm32l4xx_ll_lpuart.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_ll_lpuart.c
00004   * @author  MCD Application Team
00005   * @version V1.5.1
00006   * @date    31-May-2016
00007   * @brief   LPUART LL module driver.
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00012   *
00013   * Redistribution and use in source and binary forms, with or without modification,
00014   * are permitted provided that the following conditions are met:
00015   *   1. Redistributions of source code must retain the above copyright notice,
00016   *      this list of conditions and the following disclaimer.
00017   *   2. Redistributions in binary form must reproduce the above copyright notice,
00018   *      this list of conditions and the following disclaimer in the documentation
00019   *      and/or other materials provided with the distribution.
00020   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021   *      may be used to endorse or promote products derived from this software
00022   *      without specific prior written permission.
00023   *
00024   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034   *
00035   ******************************************************************************
00036   */
00037 #if defined(USE_FULL_LL_DRIVER)
00038 
00039 /* Includes ------------------------------------------------------------------*/
00040 #include "stm32l4xx_ll_lpuart.h"
00041 #include "stm32l4xx_ll_rcc.h"
00042 #include "stm32l4xx_ll_bus.h"
00043 #ifdef  USE_FULL_ASSERT
00044 #include "stm32_assert.h"
00045 #else
00046 #define assert_param(expr) ((void)0U)
00047 #endif
00048 
00049 /** @addtogroup STM32L4xx_LL_Driver
00050   * @{
00051   */
00052 
00053 #if defined (LPUART1)
00054 
00055 /** @addtogroup LPUART_LL
00056   * @{
00057   */
00058 
00059 /* Private types -------------------------------------------------------------*/
00060 /* Private variables ---------------------------------------------------------*/
00061 /* Private constants ---------------------------------------------------------*/
00062 /** @addtogroup LPUART_LL_Private_Constants
00063   * @{
00064   */
00065 
00066 /**
00067   * @}
00068   */
00069 
00070 
00071 /* Private macros ------------------------------------------------------------*/
00072 /** @addtogroup LPUART_LL_Private_Macros
00073   * @{
00074   */
00075 
00076 /* Check of parameters for configuration of LPUART registers                  */
00077 
00078 /* __BAUDRATE__ Depending on constraints applicable for LPUART BRR register   */
00079 /*              value :                                                       */
00080 /*                - fck must be in the range [3 x baudrate, 4096 x baudrate]  */
00081 /*                - LPUART_BRR register value should be >= 0x300              */
00082 /*                - LPUART_BRR register value should be <= 0xFFFFF (20 bits)  */
00083 /*              Baudrate specified by the user should belong to [8, 26000000].*/
00084 #define IS_LL_LPUART_BAUDRATE(__BAUDRATE__) (((__BAUDRATE__) <= 26000000U) && ((__BAUDRATE__) >= 8U))
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   /* Force reset of LPUART peripheral */
00137   LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_LPUART1);
00138 
00139   /* Release reset of LPUART peripheral */
00140   LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_LPUART1);
00141 
00142   return (status);
00143 }
00144 
00145 /**
00146   * @brief  Initialize LPUART registers according to the specified
00147   *         parameters in LPUART_InitStruct.
00148   * @note   As some bits in LPUART configuration registers can only be written when the LPUART is disabled (USART_CR1_UE bit =0),
00149   *         LPUART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
00150   * @note   Baud rate value stored in LPUART_InitStruct BaudRate field, should be valid (different from 0).
00151   * @param  LPUARTx LPUART Instance
00152   * @param  LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
00153   *         that contains the configuration information for the specified LPUART peripheral.
00154   * @retval An ErrorStatus enumeration value:
00155   *          - SUCCESS: LPUART registers are initialized according to LPUART_InitStruct content
00156   *          - ERROR: Problem occurred during LPUART Registers initialization
00157   */
00158 ErrorStatus LL_LPUART_Init(USART_TypeDef *LPUARTx, LL_LPUART_InitTypeDef *LPUART_InitStruct)
00159 {
00160   ErrorStatus status = ERROR;
00161   uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
00162 
00163   /* Check the parameters */
00164   assert_param(IS_LPUART_INSTANCE(LPUARTx));
00165   assert_param(IS_LL_LPUART_BAUDRATE(LPUART_InitStruct->BaudRate ));
00166   assert_param(IS_LL_LPUART_DATAWIDTH(LPUART_InitStruct->DataWidth ));
00167   assert_param(IS_LL_LPUART_STOPBITS(LPUART_InitStruct->StopBits ));
00168   assert_param(IS_LL_LPUART_PARITY(LPUART_InitStruct->Parity ));
00169   assert_param(IS_LL_LPUART_DIRECTION(LPUART_InitStruct->TransferDirection ));
00170   assert_param(IS_LL_LPUART_HWCONTROL(LPUART_InitStruct->HardwareFlowControl ));
00171 
00172   /* LPUART needs to be in disabled state, in order to be able to configure some bits in
00173      CRx registers. Otherwise (LPUART not in Disabled state) => return ERROR */
00174   if (LL_LPUART_IsEnabled(LPUARTx) == 0U)
00175   {
00176     /*---------------------------- LPUART CR1 Configuration -----------------------
00177      * Configure LPUARTx CR1 (LPUART Word Length, Parity and Transfer Direction bits) with parameters:
00178      * - DataWidth:          USART_CR1_M bits according to LPUART_InitStruct->DataWidth value
00179      * - Parity:             USART_CR1_PCE, USART_CR1_PS bits according to LPUART_InitStruct->Parity value
00180      * - TransferDirection:  USART_CR1_TE, USART_CR1_RE bits according to LPUART_InitStruct->TransferDirection value
00181      */
00182     MODIFY_REG(LPUARTx->CR1,
00183                (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
00184                (LPUART_InitStruct->DataWidth  | LPUART_InitStruct->Parity  | LPUART_InitStruct->TransferDirection ));
00185 
00186     /*---------------------------- LPUART CR2 Configuration -----------------------
00187      * Configure LPUARTx CR2 (Stop bits) with parameters:
00188      * - Stop Bits:          USART_CR2_STOP bits according to LPUART_InitStruct->StopBits value.
00189      */
00190     LL_LPUART_SetStopBitsLength(LPUARTx, LPUART_InitStruct->StopBits );
00191 
00192     /*---------------------------- LPUART CR3 Configuration -----------------------
00193      * Configure LPUARTx CR3 (Hardware Flow Control) with parameters:
00194      * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to LPUART_InitStruct->HardwareFlowControl value.
00195      */
00196     LL_LPUART_SetHWFlowCtrl(LPUARTx, LPUART_InitStruct->HardwareFlowControl );
00197 
00198     /*---------------------------- LPUART BRR Configuration -----------------------
00199      * Retrieve Clock frequency used for LPUART Peripheral
00200      */
00201     periphclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_LPUART1_CLKSOURCE);
00202 
00203     /* Configure the LPUART Baud Rate :
00204        - valid baud rate value (different from 0) is required
00205        - Peripheral clock as returned by RCC service, should be valid (different from 0).
00206     */
00207     if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
00208         && (LPUART_InitStruct->BaudRate  != 0U))
00209     {
00210       status = SUCCESS;
00211       LL_LPUART_SetBaudRate(LPUARTx,
00212                             periphclk,
00213                             LPUART_InitStruct->BaudRate );
00214     }
00215   }
00216 
00217   return (status);
00218 }
00219 
00220 /**
00221   * @brief Set each @ref LL_LPUART_InitTypeDef field to default value.
00222   * @param LPUART_InitStruct pointer to a @ref LL_LPUART_InitTypeDef structure
00223   *                          whose fields will be set to default values.
00224   * @retval None
00225   */
00226 
00227 void LL_LPUART_StructInit(LL_LPUART_InitTypeDef *LPUART_InitStruct)
00228 {
00229   /* Set LPUART_InitStruct fields to default values */
00230   LPUART_InitStruct->BaudRate             = 9600U;
00231   LPUART_InitStruct->DataWidth            = LL_LPUART_DATAWIDTH_8B;
00232   LPUART_InitStruct->StopBits             = LL_LPUART_STOPBITS_1;
00233   LPUART_InitStruct->Parity               = LL_LPUART_PARITY_NONE ;
00234   LPUART_InitStruct->TransferDirection    = LL_LPUART_DIRECTION_TX_RX;
00235   LPUART_InitStruct->HardwareFlowControl  = LL_LPUART_HWCONTROL_NONE;
00236 }
00237 
00238 /**
00239   * @}
00240   */
00241 
00242 /**
00243   * @}
00244   */
00245 
00246 /**
00247   * @}
00248   */
00249 
00250 #endif /* defined (LPUART1) */
00251 
00252 /**
00253   * @}
00254   */
00255 
00256 #endif /* USE_FULL_LL_DRIVER */
00257 
00258 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00259