mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
187:0387e8f68319
mbed library release version 165

Who changed what in which revision?

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