TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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