Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Space_Invaders_Demo neopixels gpio_test_stm32f3_discovery gpio_test_systimer ... more
stm32f30x_usart.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f30x_usart.c 00004 * @author MCD Application Team 00005 * @version V1.2.3 00006 * @date 10-July-2015 00007 * @brief This file provides firmware functions to manage the following 00008 * functionalities of the Universal synchronous asynchronous receiver 00009 * transmitter (USART): 00010 * + Initialization and Configuration 00011 * + STOP Mode 00012 * + AutoBaudRate 00013 * + Data transfers 00014 * + Multi-Processor Communication 00015 * + LIN mode 00016 * + Half-duplex mode 00017 * + Smartcard mode 00018 * + IrDA mode 00019 * + RS485 mode 00020 * + DMA transfers management 00021 * + Interrupts and flags management 00022 * 00023 * @verbatim 00024 =============================================================================== 00025 ##### How to use this driver ##### 00026 =============================================================================== 00027 [..] 00028 (#) Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE) 00029 function for USART1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE) 00030 function for USART2, USART3, UART4 and UART5. 00031 (#) According to the USART mode, enable the GPIO clocks using 00032 RCC_AHBPeriphClockCmd() function. (The I/O can be TX, RX, CTS, 00033 or and SCLK). 00034 (#) Peripheral's alternate function: 00035 (++) Connect the pin to the desired peripherals' Alternate 00036 Function (AF) using GPIO_PinAFConfig() function. 00037 (++) Configure the desired pin in alternate function by: 00038 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF. 00039 (++) Select the type, pull-up/pull-down and output speed via 00040 GPIO_PuPd, GPIO_OType and GPIO_Speed members. 00041 (++) Call GPIO_Init() function. 00042 (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware 00043 flow control and Mode(Receiver/Transmitter) using the SPI_Init() 00044 function. 00045 (#) For synchronous mode, enable the clock and program the polarity, 00046 phase and last bit using the USART_ClockInit() function. 00047 (#) Enable the USART using the USART_Cmd() function. 00048 (#) Enable the NVIC and the corresponding interrupt using the function 00049 USART_ITConfig() if you need to use interrupt mode. 00050 (#) When using the DMA mode: 00051 (++) Configure the DMA using DMA_Init() function. 00052 (++) Activate the needed channel Request using USART_DMACmd() function. 00053 (#) Enable the DMA using the DMA_Cmd() function, when using DMA mode. 00054 [..] 00055 Refer to Multi-Processor, LIN, half-duplex, Smartcard, IrDA sub-sections 00056 for more details. 00057 00058 @endverbatim 00059 00060 ****************************************************************************** 00061 * @attention 00062 * 00063 * <h2><center>© COPYRIGHT 2015 STMicroelectronics</center></h2> 00064 * 00065 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00066 * You may not use this file except in compliance with the License. 00067 * You may obtain a copy of the License at: 00068 * 00069 * http://www.st.com/software_license_agreement_liberty_v2 00070 * 00071 * Unless required by applicable law or agreed to in writing, software 00072 * distributed under the License is distributed on an "AS IS" BASIS, 00073 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00074 * See the License for the specific language governing permissions and 00075 * limitations under the License. 00076 * 00077 ****************************************************************************** 00078 */ 00079 00080 /* Includes ------------------------------------------------------------------*/ 00081 #include "stm32f30x_usart.h" 00082 #include "stm32f30x_rcc.h" 00083 00084 /** @addtogroup STM32F30x_StdPeriph_Driver 00085 * @{ 00086 */ 00087 00088 /** @defgroup USART 00089 * @brief USART driver modules 00090 * @{ 00091 */ 00092 00093 /* Private typedef -----------------------------------------------------------*/ 00094 /* Private define ------------------------------------------------------------*/ 00095 00096 /*!< USART CR1 register clear Mask ((~(uint32_t)0xFFFFE6F3)) */ 00097 #define CR1_CLEAR_MASK ((uint32_t)(USART_CR1_M | USART_CR1_PCE | \ 00098 USART_CR1_PS | USART_CR1_TE | \ 00099 USART_CR1_RE)) 00100 00101 /*!< USART CR2 register clock bits clear Mask ((~(uint32_t)0xFFFFF0FF)) */ 00102 #define CR2_CLOCK_CLEAR_MASK ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \ 00103 USART_CR2_CPHA | USART_CR2_LBCL)) 00104 00105 /*!< USART CR3 register clear Mask ((~(uint32_t)0xFFFFFCFF)) */ 00106 #define CR3_CLEAR_MASK ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE)) 00107 00108 /*!< USART Interrupts mask */ 00109 #define IT_MASK ((uint32_t)0x000000FF) 00110 00111 /* Private macro -------------------------------------------------------------*/ 00112 /* Private variables ---------------------------------------------------------*/ 00113 /* Private function prototypes -----------------------------------------------*/ 00114 /* Private functions ---------------------------------------------------------*/ 00115 00116 /** @defgroup USART_Private_Functions 00117 * @{ 00118 */ 00119 00120 /** @defgroup USART_Group1 Initialization and Configuration functions 00121 * @brief Initialization and Configuration functions 00122 * 00123 @verbatim 00124 =============================================================================== 00125 ##### Initialization and Configuration functions ##### 00126 =============================================================================== 00127 [..] 00128 This subsection provides a set of functions allowing to initialize the USART 00129 in asynchronous and in synchronous modes. 00130 (+) For the asynchronous mode only these parameters can be configured: 00131 (++) Baud Rate. 00132 (++) Word Length. 00133 (++) Stop Bit. 00134 (++) Parity: If the parity is enabled, then the MSB bit of the data written 00135 in the data register is transmitted but is changed by the parity bit. 00136 Depending on the frame length defined by the M bit (8-bits or 9-bits), 00137 the possible USART frame formats are as listed in the following table: 00138 [..] 00139 +-------------------------------------------------------------+ 00140 | M bit | PCE bit | USART frame | 00141 |---------------------|---------------------------------------| 00142 | 0 | 0 | | SB | 8 bit data | STB | | 00143 |---------|-----------|---------------------------------------| 00144 | 0 | 1 | | SB | 7 bit data | PB | STB | | 00145 |---------|-----------|---------------------------------------| 00146 | 1 | 0 | | SB | 9 bit data | STB | | 00147 |---------|-----------|---------------------------------------| 00148 | 1 | 1 | | SB | 8 bit data | PB | STB | | 00149 +-------------------------------------------------------------+ 00150 [..] 00151 (++) Hardware flow control. 00152 (++) Receiver/transmitter modes. 00153 [..] The USART_Init() function follows the USART asynchronous configuration 00154 procedure(details for the procedure are available in reference manual. 00155 (+) For the synchronous mode in addition to the asynchronous mode parameters 00156 these parameters should be also configured: 00157 (++) USART Clock Enabled. 00158 (++) USART polarity. 00159 (++) USART phase. 00160 (++) USART LastBit. 00161 [..] These parameters can be configured using the USART_ClockInit() function. 00162 00163 @endverbatim 00164 * @{ 00165 */ 00166 00167 /** 00168 * @brief Deinitializes the USARTx peripheral registers to their default reset values. 00169 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00170 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00171 * @retval None 00172 */ 00173 void USART_DeInit(USART_TypeDef* USARTx) 00174 { 00175 /* Check the parameters */ 00176 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00177 00178 if (USARTx == USART1) 00179 { 00180 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); 00181 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); 00182 } 00183 else if (USARTx == USART2) 00184 { 00185 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE); 00186 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE); 00187 } 00188 else if (USARTx == USART3) 00189 { 00190 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE); 00191 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE); 00192 } 00193 else if (USARTx == UART4) 00194 { 00195 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE); 00196 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE); 00197 } 00198 else 00199 { 00200 if (USARTx == UART5) 00201 { 00202 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE); 00203 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE); 00204 } 00205 } 00206 } 00207 00208 /** 00209 * @brief Initializes the USARTx peripheral according to the specified 00210 * parameters in the USART_InitStruct . 00211 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00212 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00213 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure 00214 * that contains the configuration information for the specified USART peripheral. 00215 * @retval None 00216 */ 00217 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct) 00218 { 00219 uint32_t divider = 0, apbclock = 0, tmpreg = 0; 00220 RCC_ClocksTypeDef RCC_ClocksStatus; 00221 00222 /* Check the parameters */ 00223 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00224 assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate)); 00225 assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength)); 00226 assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits)); 00227 assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity)); 00228 assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode)); 00229 assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl)); 00230 00231 /* Disable USART */ 00232 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE); 00233 00234 /*---------------------------- USART CR2 Configuration -----------------------*/ 00235 tmpreg = USARTx->CR2; 00236 /* Clear STOP[13:12] bits */ 00237 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP); 00238 00239 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/ 00240 /* Set STOP[13:12] bits according to USART_StopBits value */ 00241 tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits; 00242 00243 /* Write to USART CR2 */ 00244 USARTx->CR2 = tmpreg; 00245 00246 /*---------------------------- USART CR1 Configuration -----------------------*/ 00247 tmpreg = USARTx->CR1; 00248 /* Clear M, PCE, PS, TE and RE bits */ 00249 tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK); 00250 00251 /* Configure the USART Word Length, Parity and mode ----------------------- */ 00252 /* Set the M bits according to USART_WordLength value */ 00253 /* Set PCE and PS bits according to USART_Parity value */ 00254 /* Set TE and RE bits according to USART_Mode value */ 00255 tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity | 00256 USART_InitStruct->USART_Mode; 00257 00258 /* Write to USART CR1 */ 00259 USARTx->CR1 = tmpreg; 00260 00261 /*---------------------------- USART CR3 Configuration -----------------------*/ 00262 tmpreg = USARTx->CR3; 00263 /* Clear CTSE and RTSE bits */ 00264 tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK); 00265 00266 /* Configure the USART HFC -------------------------------------------------*/ 00267 /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */ 00268 tmpreg |= USART_InitStruct->USART_HardwareFlowControl; 00269 00270 /* Write to USART CR3 */ 00271 USARTx->CR3 = tmpreg; 00272 00273 /*---------------------------- USART BRR Configuration -----------------------*/ 00274 /* Configure the USART Baud Rate -------------------------------------------*/ 00275 RCC_GetClocksFreq(&RCC_ClocksStatus); 00276 00277 if (USARTx == USART1) 00278 { 00279 apbclock = RCC_ClocksStatus.USART1CLK_Frequency; 00280 } 00281 else if (USARTx == USART2) 00282 { 00283 apbclock = RCC_ClocksStatus.USART2CLK_Frequency; 00284 } 00285 else if (USARTx == USART3) 00286 { 00287 apbclock = RCC_ClocksStatus.USART3CLK_Frequency; 00288 } 00289 else if (USARTx == UART4) 00290 { 00291 apbclock = RCC_ClocksStatus.UART4CLK_Frequency; 00292 } 00293 else 00294 { 00295 apbclock = RCC_ClocksStatus.UART5CLK_Frequency; 00296 } 00297 00298 /* Determine the integer part */ 00299 if ((USARTx->CR1 & USART_CR1_OVER8) != 0) 00300 { 00301 /* (divider * 10) computing in case Oversampling mode is 8 Samples */ 00302 divider = (uint32_t)((2 * apbclock) / (USART_InitStruct->USART_BaudRate)); 00303 tmpreg = (uint32_t)((2 * apbclock) % (USART_InitStruct->USART_BaudRate)); 00304 } 00305 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */ 00306 { 00307 /* (divider * 10) computing in case Oversampling mode is 16 Samples */ 00308 divider = (uint32_t)((apbclock) / (USART_InitStruct->USART_BaudRate)); 00309 tmpreg = (uint32_t)((apbclock) % (USART_InitStruct->USART_BaudRate)); 00310 } 00311 00312 /* round the divider : if fractional part i greater than 0.5 increment divider */ 00313 if (tmpreg >= (USART_InitStruct->USART_BaudRate) / 2) 00314 { 00315 divider++; 00316 } 00317 00318 /* Implement the divider in case Oversampling mode is 8 Samples */ 00319 if ((USARTx->CR1 & USART_CR1_OVER8) != 0) 00320 { 00321 /* get the LSB of divider and shift it to the right by 1 bit */ 00322 tmpreg = (divider & (uint16_t)0x000F) >> 1; 00323 00324 /* update the divider value */ 00325 divider = (divider & (uint16_t)0xFFF0) | tmpreg; 00326 } 00327 00328 /* Write to USART BRR */ 00329 USARTx->BRR = (uint16_t)divider; 00330 } 00331 00332 /** 00333 * @brief Fills each USART_InitStruct member with its default value. 00334 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure 00335 * which will be initialized. 00336 * @retval None 00337 */ 00338 void USART_StructInit(USART_InitTypeDef* USART_InitStruct) 00339 { 00340 /* USART_InitStruct members default value */ 00341 USART_InitStruct->USART_BaudRate = 9600; 00342 USART_InitStruct->USART_WordLength = USART_WordLength_8b; 00343 USART_InitStruct->USART_StopBits = USART_StopBits_1; 00344 USART_InitStruct->USART_Parity = USART_Parity_No ; 00345 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 00346 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None; 00347 } 00348 00349 /** 00350 * @brief Initializes the USARTx peripheral Clock according to the 00351 * specified parameters in the USART_ClockInitStruct. 00352 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00353 * following values: USART1 or USART2 or USART3. 00354 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef 00355 * structure that contains the configuration information for the specified 00356 * USART peripheral. 00357 * @retval None 00358 */ 00359 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct) 00360 { 00361 uint32_t tmpreg = 0; 00362 /* Check the parameters */ 00363 assert_param(IS_USART_123_PERIPH(USARTx)); 00364 assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock)); 00365 assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL)); 00366 assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA)); 00367 assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit)); 00368 /*---------------------------- USART CR2 Configuration -----------------------*/ 00369 tmpreg = USARTx->CR2; 00370 /* Clear CLKEN, CPOL, CPHA, LBCL and SSM bits */ 00371 tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK); 00372 /* Configure the USART Clock, CPOL, CPHA, LastBit and SSM ------------*/ 00373 /* Set CLKEN bit according to USART_Clock value */ 00374 /* Set CPOL bit according to USART_CPOL value */ 00375 /* Set CPHA bit according to USART_CPHA value */ 00376 /* Set LBCL bit according to USART_LastBit value */ 00377 tmpreg |= (uint32_t)(USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL | 00378 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit); 00379 /* Write to USART CR2 */ 00380 USARTx->CR2 = tmpreg; 00381 } 00382 00383 /** 00384 * @brief Fills each USART_ClockInitStruct member with its default value. 00385 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef 00386 * structure which will be initialized. 00387 * @retval None 00388 */ 00389 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct) 00390 { 00391 /* USART_ClockInitStruct members default value */ 00392 USART_ClockInitStruct->USART_Clock = USART_Clock_Disable; 00393 USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low; 00394 USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge; 00395 USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable; 00396 } 00397 00398 /** 00399 * @brief Enables or disables the specified USART peripheral. 00400 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00401 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00402 * @param NewState: new state of the USARTx peripheral. 00403 * This parameter can be: ENABLE or DISABLE. 00404 * @retval None 00405 */ 00406 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState) 00407 { 00408 /* Check the parameters */ 00409 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00410 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00411 00412 if (NewState != DISABLE) 00413 { 00414 /* Enable the selected USART by setting the UE bit in the CR1 register */ 00415 USARTx->CR1 |= USART_CR1_UE; 00416 } 00417 else 00418 { 00419 /* Disable the selected USART by clearing the UE bit in the CR1 register */ 00420 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE); 00421 } 00422 } 00423 00424 /** 00425 * @brief Enables or disables the USART's transmitter or receiver. 00426 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00427 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00428 * @param USART_Direction: specifies the USART direction. 00429 * This parameter can be any combination of the following values: 00430 * @arg USART_Mode_Tx: USART Transmitter 00431 * @arg USART_Mode_Rx: USART Receiver 00432 * @param NewState: new state of the USART transfer direction. 00433 * This parameter can be: ENABLE or DISABLE. 00434 * @retval None 00435 */ 00436 void USART_DirectionModeCmd(USART_TypeDef* USARTx, uint32_t USART_DirectionMode, FunctionalState NewState) 00437 { 00438 /* Check the parameters */ 00439 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00440 assert_param(IS_USART_MODE(USART_DirectionMode)); 00441 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00442 00443 if (NewState != DISABLE) 00444 { 00445 /* Enable the USART's transfer interface by setting the TE and/or RE bits 00446 in the USART CR1 register */ 00447 USARTx->CR1 |= USART_DirectionMode; 00448 } 00449 else 00450 { 00451 /* Disable the USART's transfer interface by clearing the TE and/or RE bits 00452 in the USART CR3 register */ 00453 USARTx->CR1 &= (uint32_t)~USART_DirectionMode; 00454 } 00455 } 00456 00457 /** 00458 * @brief Enables or disables the USART's 8x oversampling mode. 00459 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00460 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00461 * @param NewState: new state of the USART 8x oversampling mode. 00462 * This parameter can be: ENABLE or DISABLE. 00463 * @note 00464 * This function has to be called before calling USART_Init() 00465 * function in order to have correct baudrate Divider value. 00466 * @retval None 00467 */ 00468 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState) 00469 { 00470 /* Check the parameters */ 00471 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00472 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00473 00474 if (NewState != DISABLE) 00475 { 00476 /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */ 00477 USARTx->CR1 |= USART_CR1_OVER8; 00478 } 00479 else 00480 { 00481 /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */ 00482 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_OVER8); 00483 } 00484 } 00485 00486 /** 00487 * @brief Enables or disables the USART's one bit sampling method. 00488 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00489 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00490 * @param NewState: new state of the USART one bit sampling method. 00491 * This parameter can be: ENABLE or DISABLE. 00492 * @note 00493 * This function has to be called before calling USART_Cmd() function. 00494 * @retval None 00495 */ 00496 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState) 00497 { 00498 /* Check the parameters */ 00499 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00500 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00501 00502 if (NewState != DISABLE) 00503 { 00504 /* Enable the one bit method by setting the ONEBIT bit in the CR3 register */ 00505 USARTx->CR3 |= USART_CR3_ONEBIT; 00506 } 00507 else 00508 { 00509 /* Disable the one bit method by clearing the ONEBIT bit in the CR3 register */ 00510 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_ONEBIT); 00511 } 00512 } 00513 00514 /** 00515 * @brief Enables or disables the USART's most significant bit first 00516 * transmitted/received following the start bit. 00517 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00518 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00519 * @param NewState: new state of the USART most significant bit first 00520 * transmitted/received following the start bit. 00521 * This parameter can be: ENABLE or DISABLE. 00522 * @note 00523 * This function has to be called before calling USART_Cmd() function. 00524 * @retval None 00525 */ 00526 void USART_MSBFirstCmd(USART_TypeDef* USARTx, FunctionalState NewState) 00527 { 00528 /* Check the parameters */ 00529 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00530 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00531 00532 if (NewState != DISABLE) 00533 { 00534 /* Enable the most significant bit first transmitted/received following the 00535 start bit by setting the MSBFIRST bit in the CR2 register */ 00536 USARTx->CR2 |= USART_CR2_MSBFIRST; 00537 } 00538 else 00539 { 00540 /* Disable the most significant bit first transmitted/received following the 00541 start bit by clearing the MSBFIRST bit in the CR2 register */ 00542 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_MSBFIRST); 00543 } 00544 } 00545 00546 /** 00547 * @brief Enables or disables the binary data inversion. 00548 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00549 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00550 * @param NewState: new defined levels for the USART data. 00551 * This parameter can be: ENABLE or DISABLE. 00552 * @arg ENABLE: Logical data from the data register are send/received in negative 00553 * logic. (1=L, 0=H). The parity bit is also inverted. 00554 * @arg DISABLE: Logical data from the data register are send/received in positive 00555 * logic. (1=H, 0=L) 00556 * @note 00557 * This function has to be called before calling USART_Cmd() function. 00558 * @retval None 00559 */ 00560 void USART_DataInvCmd(USART_TypeDef* USARTx, FunctionalState NewState) 00561 { 00562 /* Check the parameters */ 00563 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00564 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00565 00566 if (NewState != DISABLE) 00567 { 00568 /* Enable the binary data inversion feature by setting the DATAINV bit in 00569 the CR2 register */ 00570 USARTx->CR2 |= USART_CR2_DATAINV; 00571 } 00572 else 00573 { 00574 /* Disable the binary data inversion feature by clearing the DATAINV bit in 00575 the CR2 register */ 00576 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_DATAINV); 00577 } 00578 } 00579 00580 /** 00581 * @brief Enables or disables the Pin(s) active level inversion. 00582 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00583 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00584 * @param USART_InvPin: specifies the USART pin(s) to invert. 00585 * This parameter can be any combination of the following values: 00586 * @arg USART_InvPin_Tx: USART Tx pin active level inversion. 00587 * @arg USART_InvPin_Rx: USART Rx pin active level inversion. 00588 * @param NewState: new active level status for the USART pin(s). 00589 * This parameter can be: ENABLE or DISABLE. 00590 * - ENABLE: pin(s) signal values are inverted (Vdd =0, Gnd =1). 00591 * - DISABLE: pin(s) signal works using the standard logic levels (Vdd =1, Gnd =0). 00592 * @note 00593 * This function has to be called before calling USART_Cmd() function. 00594 * @retval None 00595 */ 00596 void USART_InvPinCmd(USART_TypeDef* USARTx, uint32_t USART_InvPin, FunctionalState NewState) 00597 { 00598 /* Check the parameters */ 00599 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00600 assert_param(IS_USART_INVERSTION_PIN(USART_InvPin)); 00601 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00602 00603 if (NewState != DISABLE) 00604 { 00605 /* Enable the active level inversion for selected pins by setting the TXINV 00606 and/or RXINV bits in the USART CR2 register */ 00607 USARTx->CR2 |= USART_InvPin; 00608 } 00609 else 00610 { 00611 /* Disable the active level inversion for selected requests by clearing the 00612 TXINV and/or RXINV bits in the USART CR2 register */ 00613 USARTx->CR2 &= (uint32_t)~USART_InvPin; 00614 } 00615 } 00616 00617 /** 00618 * @brief Enables or disables the swap Tx/Rx pins. 00619 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00620 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00621 * @param NewState: new state of the USARTx TX/RX pins pinout. 00622 * This parameter can be: ENABLE or DISABLE. 00623 * @arg ENABLE: The TX and RX pins functions are swapped. 00624 * @arg DISABLE: TX/RX pins are used as defined in standard pinout 00625 * @note 00626 * This function has to be called before calling USART_Cmd() function. 00627 * @retval None 00628 */ 00629 void USART_SWAPPinCmd(USART_TypeDef* USARTx, FunctionalState NewState) 00630 { 00631 /* Check the parameters */ 00632 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00633 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00634 00635 if (NewState != DISABLE) 00636 { 00637 /* Enable the SWAP feature by setting the SWAP bit in the CR2 register */ 00638 USARTx->CR2 |= USART_CR2_SWAP; 00639 } 00640 else 00641 { 00642 /* Disable the SWAP feature by clearing the SWAP bit in the CR2 register */ 00643 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_SWAP); 00644 } 00645 } 00646 00647 /** 00648 * @brief Enables or disables the receiver Time Out feature. 00649 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00650 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00651 * @param NewState: new state of the USARTx receiver Time Out. 00652 * This parameter can be: ENABLE or DISABLE. 00653 * @retval None 00654 */ 00655 void USART_ReceiverTimeOutCmd(USART_TypeDef* USARTx, FunctionalState NewState) 00656 { 00657 /* Check the parameters */ 00658 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00659 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00660 00661 if (NewState != DISABLE) 00662 { 00663 /* Enable the receiver time out feature by setting the RTOEN bit in the CR2 00664 register */ 00665 USARTx->CR2 |= USART_CR2_RTOEN; 00666 } 00667 else 00668 { 00669 /* Disable the receiver time out feature by clearing the RTOEN bit in the CR2 00670 register */ 00671 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_RTOEN); 00672 } 00673 } 00674 00675 /** 00676 * @brief Sets the receiver Time Out value. 00677 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00678 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00679 * @param USART_ReceiverTimeOut: specifies the Receiver Time Out value. 00680 * @retval None 00681 */ 00682 void USART_SetReceiverTimeOut(USART_TypeDef* USARTx, uint32_t USART_ReceiverTimeOut) 00683 { 00684 /* Check the parameters */ 00685 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00686 assert_param(IS_USART_TIMEOUT(USART_ReceiverTimeOut)); 00687 00688 /* Clear the receiver Time Out value by clearing the RTO[23:0] bits in the RTOR 00689 register */ 00690 USARTx->RTOR &= (uint32_t)~((uint32_t)USART_RTOR_RTO); 00691 /* Set the receiver Time Out value by setting the RTO[23:0] bits in the RTOR 00692 register */ 00693 USARTx->RTOR |= USART_ReceiverTimeOut; 00694 } 00695 00696 /** 00697 * @brief Sets the system clock prescaler. 00698 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00699 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00700 * @param USART_Prescaler: specifies the prescaler clock. 00701 * @note 00702 * This function has to be called before calling USART_Cmd() function. 00703 * @retval None 00704 */ 00705 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler) 00706 { 00707 /* Check the parameters */ 00708 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00709 00710 /* Clear the USART prescaler */ 00711 USARTx->GTPR &= USART_GTPR_GT; 00712 /* Set the USART prescaler */ 00713 USARTx->GTPR |= USART_Prescaler; 00714 } 00715 00716 /** 00717 * @} 00718 */ 00719 00720 00721 /** @defgroup USART_Group2 STOP Mode functions 00722 * @brief STOP Mode functions 00723 * 00724 @verbatim 00725 =============================================================================== 00726 ##### STOP Mode functions ##### 00727 =============================================================================== 00728 [..] This subsection provides a set of functions allowing to manage 00729 WakeUp from STOP mode. 00730 00731 [..] The USART is able to WakeUp from Stop Mode if USART clock is set to HSI 00732 or LSI. 00733 00734 [..] The WakeUp source is configured by calling USART_StopModeWakeUpSourceConfig() 00735 function. 00736 00737 [..] After configuring the source of WakeUp and before entering in Stop Mode 00738 USART_STOPModeCmd() function should be called to allow USART WakeUp. 00739 00740 @endverbatim 00741 * @{ 00742 */ 00743 00744 /** 00745 * @brief Enables or disables the specified USART peripheral in STOP Mode. 00746 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00747 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00748 * @param NewState: new state of the USARTx peripheral state in stop mode. 00749 * This parameter can be: ENABLE or DISABLE. 00750 * @note 00751 * This function has to be called when USART clock is set to HSI or LSE. 00752 * @retval None 00753 */ 00754 void USART_STOPModeCmd(USART_TypeDef* USARTx, FunctionalState NewState) 00755 { 00756 /* Check the parameters */ 00757 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00758 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00759 00760 if (NewState != DISABLE) 00761 { 00762 /* Enable the selected USART in STOP mode by setting the UESM bit in the CR1 00763 register */ 00764 USARTx->CR1 |= USART_CR1_UESM; 00765 } 00766 else 00767 { 00768 /* Disable the selected USART in STOP mode by clearing the UE bit in the CR1 00769 register */ 00770 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UESM); 00771 } 00772 } 00773 00774 /** 00775 * @brief Selects the USART WakeUp method form stop mode. 00776 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00777 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00778 * @param USART_WakeUp: specifies the selected USART wakeup method. 00779 * This parameter can be one of the following values: 00780 * @arg USART_WakeUpSource_AddressMatch: WUF active on address match. 00781 * @arg USART_WakeUpSource_StartBit: WUF active on Start bit detection. 00782 * @arg USART_WakeUpSource_RXNE: WUF active on RXNE. 00783 * @note 00784 * This function has to be called before calling USART_Cmd() function. 00785 * @retval None 00786 */ 00787 void USART_StopModeWakeUpSourceConfig(USART_TypeDef* USARTx, uint32_t USART_WakeUpSource) 00788 { 00789 /* Check the parameters */ 00790 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00791 assert_param(IS_USART_STOPMODE_WAKEUPSOURCE(USART_WakeUpSource)); 00792 00793 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_WUS); 00794 USARTx->CR3 |= USART_WakeUpSource; 00795 } 00796 00797 /** 00798 * @} 00799 */ 00800 00801 00802 /** @defgroup USART_Group3 AutoBaudRate functions 00803 * @brief AutoBaudRate functions 00804 * 00805 @verbatim 00806 =============================================================================== 00807 ##### AutoBaudRate functions ##### 00808 =============================================================================== 00809 [..] This subsection provides a set of functions allowing to manage 00810 the AutoBaudRate detections. 00811 00812 [..] Before Enabling AutoBaudRate detection using USART_AutoBaudRateCmd () 00813 The character patterns used to calculate baudrate must be chosen by calling 00814 USART_AutoBaudRateConfig() function. These function take as parameter : 00815 (#)USART_AutoBaudRate_StartBit : any character starting with a bit 1. 00816 (#)USART_AutoBaudRate_FallingEdge : any character starting with a 10xx bit pattern. 00817 00818 [..] At any later time, another request for AutoBaudRate detection can be performed 00819 using USART_RequestCmd() function. 00820 00821 [..] The AutoBaudRate detection is monitored by the status of ABRF flag which indicate 00822 that the AutoBaudRate detection is completed. In addition to ABRF flag, the ABRE flag 00823 indicate that this procedure is completed without success. USART_GetFlagStatus () 00824 function should be used to monitor the status of these flags. 00825 00826 @endverbatim 00827 * @{ 00828 */ 00829 00830 /** 00831 * @brief Enables or disables the Auto Baud Rate. 00832 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00833 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00834 * @param NewState: new state of the USARTx auto baud rate. 00835 * This parameter can be: ENABLE or DISABLE. 00836 * @retval None 00837 */ 00838 void USART_AutoBaudRateCmd(USART_TypeDef* USARTx, FunctionalState NewState) 00839 { 00840 /* Check the parameters */ 00841 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00842 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00843 00844 if (NewState != DISABLE) 00845 { 00846 /* Enable the auto baud rate feature by setting the ABREN bit in the CR2 00847 register */ 00848 USARTx->CR2 |= USART_CR2_ABREN; 00849 } 00850 else 00851 { 00852 /* Disable the auto baud rate feature by clearing the ABREN bit in the CR2 00853 register */ 00854 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABREN); 00855 } 00856 } 00857 00858 /** 00859 * @brief Selects the USART auto baud rate method. 00860 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00861 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00862 * @param USART_AutoBaudRate: specifies the selected USART auto baud rate method. 00863 * This parameter can be one of the following values: 00864 * @arg USART_AutoBaudRate_StartBit: Start Bit duration measurement. 00865 * @arg USART_AutoBaudRate_FallingEdge: Falling edge to falling edge measurement. 00866 * @arg USART_AutoBaudRate_0x7FFrame: 0x7F frame. 00867 * @arg USART_AutoBaudRate_0x55Frame: 0x55 frame. 00868 * @note 00869 * This function has to be called before calling USART_Cmd() function. 00870 * @retval None 00871 */ 00872 void USART_AutoBaudRateConfig(USART_TypeDef* USARTx, uint32_t USART_AutoBaudRate) 00873 { 00874 /* Check the parameters */ 00875 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00876 assert_param(IS_USART_AUTOBAUDRATE_MODE(USART_AutoBaudRate)); 00877 00878 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABRMODE); 00879 USARTx->CR2 |= USART_AutoBaudRate; 00880 } 00881 00882 /** 00883 * @} 00884 */ 00885 00886 00887 /** @defgroup USART_Group4 Data transfers functions 00888 * @brief Data transfers functions 00889 * 00890 @verbatim 00891 =============================================================================== 00892 ##### Data transfers functions ##### 00893 =============================================================================== 00894 [..] This subsection provides a set of functions allowing to manage 00895 the USART data transfers. 00896 [..] During an USART reception, data shifts in least significant bit first 00897 through the RX pin. When a transmission is taking place, a write instruction to 00898 the USART_TDR register stores the data in the shift register. 00899 [..] The read access of the USART_RDR register can be done using 00900 the USART_ReceiveData() function and returns the RDR value. 00901 Whereas a write access to the USART_TDR can be done using USART_SendData() 00902 function and stores the written data into TDR. 00903 00904 @endverbatim 00905 * @{ 00906 */ 00907 00908 /** 00909 * @brief Transmits single data through the USARTx peripheral. 00910 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00911 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00912 * @param Data: the data to transmit. 00913 * @retval None 00914 */ 00915 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data) 00916 { 00917 /* Check the parameters */ 00918 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00919 assert_param(IS_USART_DATA(Data)); 00920 00921 /* Transmit Data */ 00922 USARTx->TDR = (Data & (uint16_t)0x01FF); 00923 } 00924 00925 /** 00926 * @brief Returns the most recent received data by the USARTx peripheral. 00927 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00928 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00929 * @retval The received data. 00930 */ 00931 uint16_t USART_ReceiveData(USART_TypeDef* USARTx) 00932 { 00933 /* Check the parameters */ 00934 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00935 00936 /* Receive Data */ 00937 return (uint16_t)(USARTx->RDR & (uint16_t)0x01FF); 00938 } 00939 00940 /** 00941 * @} 00942 */ 00943 00944 /** @defgroup USART_Group5 MultiProcessor Communication functions 00945 * @brief Multi-Processor Communication functions 00946 * 00947 @verbatim 00948 =============================================================================== 00949 ##### Multi-Processor Communication functions ##### 00950 =============================================================================== 00951 [..] This subsection provides a set of functions allowing to manage the USART 00952 multiprocessor communication. 00953 [..] For instance one of the USARTs can be the master, its TX output is 00954 connected to the RX input of the other USART. The others are slaves, 00955 their respective TX outputs are logically ANDed together and connected 00956 to the RX input of the master. USART multiprocessor communication is 00957 possible through the following procedure: 00958 (#) Program the Baud rate, Word length = 9 bits, Stop bits, Parity, 00959 Mode transmitter or Mode receiver and hardware flow control values 00960 using the USART_Init() function. 00961 (#) Configures the USART address using the USART_SetAddress() function. 00962 (#) Configures the wake up methode (USART_WakeUp_IdleLine or 00963 USART_WakeUp_AddressMark) using USART_WakeUpConfig() function only 00964 for the slaves. 00965 (#) Enable the USART using the USART_Cmd() function. 00966 (#) Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd() 00967 function. 00968 [..] The USART Slave exit from mute mode when receive the wake up condition. 00969 00970 @endverbatim 00971 * @{ 00972 */ 00973 00974 /** 00975 * @brief Sets the address of the USART node. 00976 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00977 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00978 * @param USART_Address: Indicates the address of the USART node. 00979 * @retval None 00980 */ 00981 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address) 00982 { 00983 /* Check the parameters */ 00984 assert_param(IS_USART_ALL_PERIPH(USARTx)); 00985 00986 /* Clear the USART address */ 00987 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADD); 00988 /* Set the USART address node */ 00989 USARTx->CR2 |=((uint32_t)USART_Address << (uint32_t)0x18); 00990 } 00991 00992 /** 00993 * @brief Enables or disables the USART's mute mode. 00994 * @param USARTx: Select the USART peripheral. This parameter can be one of the 00995 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 00996 * @param NewState: new state of the USART mute mode. 00997 * This parameter can be: ENABLE or DISABLE. 00998 * @retval None 00999 */ 01000 void USART_MuteModeCmd(USART_TypeDef* USARTx, FunctionalState NewState) 01001 { 01002 /* Check the parameters */ 01003 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01004 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01005 01006 if (NewState != DISABLE) 01007 { 01008 /* Enable the USART mute mode by setting the MME bit in the CR1 register */ 01009 USARTx->CR1 |= USART_CR1_MME; 01010 } 01011 else 01012 { 01013 /* Disable the USART mute mode by clearing the MME bit in the CR1 register */ 01014 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_MME); 01015 } 01016 } 01017 01018 /** 01019 * @brief Selects the USART WakeUp method from mute mode. 01020 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01021 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01022 * @param USART_WakeUp: specifies the USART wakeup method. 01023 * This parameter can be one of the following values: 01024 * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection 01025 * @arg USART_WakeUp_AddressMark: WakeUp by an address mark 01026 * @retval None 01027 */ 01028 void USART_MuteModeWakeUpConfig(USART_TypeDef* USARTx, uint32_t USART_WakeUp) 01029 { 01030 /* Check the parameters */ 01031 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01032 assert_param(IS_USART_MUTEMODE_WAKEUP(USART_WakeUp)); 01033 01034 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_WAKE); 01035 USARTx->CR1 |= USART_WakeUp; 01036 } 01037 01038 /** 01039 * @brief Configure the USART Address detection length. 01040 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01041 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01042 * @param USART_AddressLength: specifies the USART address length detection. 01043 * This parameter can be one of the following values: 01044 * @arg USART_AddressLength_4b: 4-bit address length detection 01045 * @arg USART_AddressLength_7b: 7-bit address length detection 01046 * @retval None 01047 */ 01048 void USART_AddressDetectionConfig(USART_TypeDef* USARTx, uint32_t USART_AddressLength) 01049 { 01050 /* Check the parameters */ 01051 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01052 assert_param(IS_USART_ADDRESS_DETECTION(USART_AddressLength)); 01053 01054 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADDM7); 01055 USARTx->CR2 |= USART_AddressLength; 01056 } 01057 01058 /** 01059 * @} 01060 */ 01061 01062 /** @defgroup USART_Group6 LIN mode functions 01063 * @brief LIN mode functions 01064 * 01065 @verbatim 01066 =============================================================================== 01067 ##### LIN mode functions ##### 01068 =============================================================================== 01069 [..] This subsection provides a set of functions allowing to manage the USART 01070 LIN Mode communication. 01071 [..] In LIN mode, 8-bit data format with 1 stop bit is required in accordance 01072 with the LIN standard. 01073 [..] Only this LIN Feature is supported by the USART IP: 01074 (+) LIN Master Synchronous Break send capability and LIN slave break 01075 detection capability : 13-bit break generation and 10/11 bit break 01076 detection. 01077 [..] USART LIN Master transmitter communication is possible through the 01078 following procedure: 01079 (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity, 01080 Mode transmitter or Mode receiver and hardware flow control values 01081 using the USART_Init() function. 01082 (#) Enable the LIN mode using the USART_LINCmd() function. 01083 (#) Enable the USART using the USART_Cmd() function. 01084 (#) Send the break character using USART_SendBreak() function. 01085 [..] USART LIN Master receiver communication is possible through the 01086 following procedure: 01087 (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity, 01088 Mode transmitter or Mode receiver and hardware flow control values 01089 using the USART_Init() function. 01090 (#) Configures the break detection length 01091 using the USART_LINBreakDetectLengthConfig() function. 01092 (#) Enable the LIN mode using the USART_LINCmd() function. 01093 (#) Enable the USART using the USART_Cmd() function. 01094 [..] 01095 (@) In LIN mode, the following bits must be kept cleared: 01096 (+@) CLKEN in the USART_CR2 register. 01097 (+@) STOP[1:0], SCEN, HDSEL and IREN in the USART_CR3 register. 01098 01099 @endverbatim 01100 * @{ 01101 */ 01102 01103 /** 01104 * @brief Sets the USART LIN Break detection length. 01105 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01106 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01107 * @param USART_LINBreakDetectLength: specifies the LIN break detection length. 01108 * This parameter can be one of the following values: 01109 * @arg USART_LINBreakDetectLength_10b: 10-bit break detection 01110 * @arg USART_LINBreakDetectLength_11b: 11-bit break detection 01111 * @retval None 01112 */ 01113 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint32_t USART_LINBreakDetectLength) 01114 { 01115 /* Check the parameters */ 01116 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01117 assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength)); 01118 01119 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_LBDL); 01120 USARTx->CR2 |= USART_LINBreakDetectLength; 01121 } 01122 01123 /** 01124 * @brief Enables or disables the USART's LIN mode. 01125 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01126 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01127 * @param NewState: new state of the USART LIN mode. 01128 * This parameter can be: ENABLE or DISABLE. 01129 * @retval None 01130 */ 01131 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState) 01132 { 01133 /* Check the parameters */ 01134 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01135 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01136 01137 if (NewState != DISABLE) 01138 { 01139 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */ 01140 USARTx->CR2 |= USART_CR2_LINEN; 01141 } 01142 else 01143 { 01144 /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */ 01145 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_LINEN); 01146 } 01147 } 01148 01149 /** 01150 * @} 01151 */ 01152 01153 /** @defgroup USART_Group7 Halfduplex mode function 01154 * @brief Half-duplex mode function 01155 * 01156 @verbatim 01157 =============================================================================== 01158 ##### Half-duplex mode function ##### 01159 =============================================================================== 01160 [..] This subsection provides a set of functions allowing to manage the USART 01161 Half-duplex communication. 01162 [..] The USART can be configured to follow a single-wire half-duplex protocol 01163 where the TX and RX lines are internally connected. 01164 [..] USART Half duplex communication is possible through the following procedure: 01165 (#) Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter 01166 or Mode receiver and hardware flow control values using the USART_Init() 01167 function. 01168 (#) Configures the USART address using the USART_SetAddress() function. 01169 (#) Enable the half duplex mode using USART_HalfDuplexCmd() function. 01170 (#) Enable the USART using the USART_Cmd() function. 01171 [..] 01172 (@) The RX pin is no longer used. 01173 (@) In Half-duplex mode the following bits must be kept cleared: 01174 (+@) LINEN and CLKEN bits in the USART_CR2 register. 01175 (+@) SCEN and IREN bits in the USART_CR3 register. 01176 01177 @endverbatim 01178 * @{ 01179 */ 01180 01181 /** 01182 * @brief Enables or disables the USART's Half Duplex communication. 01183 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01184 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01185 * @param NewState: new state of the USART Communication. 01186 * This parameter can be: ENABLE or DISABLE. 01187 * @retval None 01188 */ 01189 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState) 01190 { 01191 /* Check the parameters */ 01192 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01193 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01194 01195 if (NewState != DISABLE) 01196 { 01197 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */ 01198 USARTx->CR3 |= USART_CR3_HDSEL; 01199 } 01200 else 01201 { 01202 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */ 01203 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_HDSEL); 01204 } 01205 } 01206 01207 /** 01208 * @} 01209 */ 01210 01211 01212 /** @defgroup USART_Group8 Smartcard mode functions 01213 * @brief Smartcard mode functions 01214 * 01215 @verbatim 01216 =============================================================================== 01217 ##### Smartcard mode functions ##### 01218 =============================================================================== 01219 [..] This subsection provides a set of functions allowing to manage the USART 01220 Smartcard communication. 01221 [..] The Smartcard interface is designed to support asynchronous protocol 01222 Smartcards as defined in the ISO 7816-3 standard. The USART can provide 01223 a clock to the smartcard through the SCLK output. In smartcard mode, 01224 SCLK is not associated to the communication but is simply derived from 01225 the internal peripheral input clock through a 5-bit prescaler. 01226 [..] Smartcard communication is possible through the following procedure: 01227 (#) Configures the Smartcard Prescaler using the USART_SetPrescaler() 01228 function. 01229 (#) Configures the Smartcard Guard Time using the USART_SetGuardTime() 01230 function. 01231 (#) Program the USART clock using the USART_ClockInit() function as following: 01232 (++) USART Clock enabled. 01233 (++) USART CPOL Low. 01234 (++) USART CPHA on first edge. 01235 (++) USART Last Bit Clock Enabled. 01236 (#) Program the Smartcard interface using the USART_Init() function as 01237 following: 01238 (++) Word Length = 9 Bits. 01239 (++) 1.5 Stop Bit. 01240 (++) Even parity. 01241 (++) BaudRate = 12096 baud. 01242 (++) Hardware flow control disabled (RTS and CTS signals). 01243 (++) Tx and Rx enabled 01244 (#) Optionally you can enable the parity error interrupt using 01245 the USART_ITConfig() function. 01246 (#) Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function. 01247 (#) Enable the Smartcard interface using the USART_SmartCardCmd() function. 01248 (#) Enable the USART using the USART_Cmd() function. 01249 [..] 01250 Please refer to the ISO 7816-3 specification for more details. 01251 [..] 01252 (@) It is also possible to choose 0.5 stop bit for receiving but it is 01253 recommended to use 1.5 stop bits for both transmitting and receiving 01254 to avoid switching between the two configurations. 01255 (@) In smartcard mode, the following bits must be kept cleared: 01256 (+@) LINEN bit in the USART_CR2 register. 01257 (+@) HDSEL and IREN bits in the USART_CR3 register. 01258 01259 @endverbatim 01260 * @{ 01261 */ 01262 01263 /** 01264 * @brief Sets the specified USART guard time. 01265 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01266 * following values: USART1 or USART2 or USART3. 01267 * @param USART_GuardTime: specifies the guard time. 01268 * @retval None 01269 */ 01270 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime) 01271 { 01272 /* Check the parameters */ 01273 assert_param(IS_USART_123_PERIPH(USARTx)); 01274 01275 /* Clear the USART Guard time */ 01276 USARTx->GTPR &= USART_GTPR_PSC; 01277 /* Set the USART guard time */ 01278 USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08); 01279 } 01280 01281 /** 01282 * @brief Enables or disables the USART's Smart Card mode. 01283 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01284 * following values: USART1 or USART2 or USART3. 01285 * @param NewState: new state of the Smart Card mode. 01286 * This parameter can be: ENABLE or DISABLE. 01287 * @retval None 01288 */ 01289 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState) 01290 { 01291 /* Check the parameters */ 01292 assert_param(IS_USART_123_PERIPH(USARTx)); 01293 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01294 if (NewState != DISABLE) 01295 { 01296 /* Enable the SC mode by setting the SCEN bit in the CR3 register */ 01297 USARTx->CR3 |= USART_CR3_SCEN; 01298 } 01299 else 01300 { 01301 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */ 01302 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_SCEN); 01303 } 01304 } 01305 01306 /** 01307 * @brief Enables or disables NACK transmission. 01308 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01309 * following values: USART1 or USART2 or USART3. 01310 * @param NewState: new state of the NACK transmission. 01311 * This parameter can be: ENABLE or DISABLE. 01312 * @retval None 01313 */ 01314 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState) 01315 { 01316 /* Check the parameters */ 01317 assert_param(IS_USART_123_PERIPH(USARTx)); 01318 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01319 if (NewState != DISABLE) 01320 { 01321 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */ 01322 USARTx->CR3 |= USART_CR3_NACK; 01323 } 01324 else 01325 { 01326 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */ 01327 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_NACK); 01328 } 01329 } 01330 01331 /** 01332 * @brief Sets the Smart Card number of retries in transmit and receive. 01333 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01334 * following values: USART1 or USART2 or USART3. 01335 * @param USART_AutoCount: specifies the Smart Card auto retry count. 01336 * @retval None 01337 */ 01338 void USART_SetAutoRetryCount(USART_TypeDef* USARTx, uint8_t USART_AutoCount) 01339 { 01340 /* Check the parameters */ 01341 assert_param(IS_USART_123_PERIPH(USARTx)); 01342 assert_param(IS_USART_AUTO_RETRY_COUNTER(USART_AutoCount)); 01343 /* Clear the USART auto retry count */ 01344 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_SCARCNT); 01345 /* Set the USART auto retry count*/ 01346 USARTx->CR3 |= (uint32_t)((uint32_t)USART_AutoCount << 0x11); 01347 } 01348 01349 /** 01350 * @brief Sets the Smart Card Block length. 01351 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01352 * following values: USART1 or USART2 or USART3. 01353 * @param USART_BlockLength: specifies the Smart Card block length. 01354 * @retval None 01355 */ 01356 void USART_SetBlockLength(USART_TypeDef* USARTx, uint8_t USART_BlockLength) 01357 { 01358 /* Check the parameters */ 01359 assert_param(IS_USART_123_PERIPH(USARTx)); 01360 01361 /* Clear the Smart card block length */ 01362 USARTx->RTOR &= (uint32_t)~((uint32_t)USART_RTOR_BLEN); 01363 /* Set the Smart Card block length */ 01364 USARTx->RTOR |= (uint32_t)((uint32_t)USART_BlockLength << 0x18); 01365 } 01366 01367 /** 01368 * @} 01369 */ 01370 01371 /** @defgroup USART_Group9 IrDA mode functions 01372 * @brief IrDA mode functions 01373 * 01374 @verbatim 01375 =============================================================================== 01376 ##### IrDA mode functions ##### 01377 =============================================================================== 01378 [..] This subsection provides a set of functions allowing to manage the USART 01379 IrDA communication. 01380 [..] IrDA is a half duplex communication protocol. If the Transmitter is busy, 01381 any data on the IrDA receive line will be ignored by the IrDA decoder 01382 and if the Receiver is busy, data on the TX from the USART to IrDA will 01383 not be encoded by IrDA. While receiving data, transmission should be 01384 avoided as the data to be transmitted could be corrupted. 01385 [..] IrDA communication is possible through the following procedure: 01386 (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity, 01387 Transmitter/Receiver modes and hardware flow control values using 01388 the USART_Init() function. 01389 (#) Configures the IrDA pulse width by configuring the prescaler using 01390 the USART_SetPrescaler() function. 01391 (#) Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal 01392 mode using the USART_IrDAConfig() function. 01393 (#) Enable the IrDA using the USART_IrDACmd() function. 01394 (#) Enable the USART using the USART_Cmd() function. 01395 [..] 01396 (@) A pulse of width less than two and greater than one PSC period(s) may or 01397 may not be rejected. 01398 (@) The receiver set up time should be managed by software. The IrDA physical 01399 layer specification specifies a minimum of 10 ms delay between 01400 transmission and reception (IrDA is a half duplex protocol). 01401 (@) In IrDA mode, the following bits must be kept cleared: 01402 (+@) LINEN, STOP and CLKEN bits in the USART_CR2 register. 01403 (+@) SCEN and HDSEL bits in the USART_CR3 register. 01404 01405 @endverbatim 01406 * @{ 01407 */ 01408 01409 /** 01410 * @brief Configures the USART's IrDA interface. 01411 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01412 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01413 * @param USART_IrDAMode: specifies the IrDA mode. 01414 * This parameter can be one of the following values: 01415 * @arg USART_IrDAMode_LowPower 01416 * @arg USART_IrDAMode_Normal 01417 * @retval None 01418 */ 01419 void USART_IrDAConfig(USART_TypeDef* USARTx, uint32_t USART_IrDAMode) 01420 { 01421 /* Check the parameters */ 01422 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01423 assert_param(IS_USART_IRDA_MODE(USART_IrDAMode)); 01424 01425 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_IRLP); 01426 USARTx->CR3 |= USART_IrDAMode; 01427 } 01428 01429 /** 01430 * @brief Enables or disables the USART's IrDA interface. 01431 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01432 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01433 * @param NewState: new state of the IrDA mode. 01434 * This parameter can be: ENABLE or DISABLE. 01435 * @retval None 01436 */ 01437 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState) 01438 { 01439 /* Check the parameters */ 01440 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01441 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01442 01443 if (NewState != DISABLE) 01444 { 01445 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */ 01446 USARTx->CR3 |= USART_CR3_IREN; 01447 } 01448 else 01449 { 01450 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */ 01451 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_IREN); 01452 } 01453 } 01454 /** 01455 * @} 01456 */ 01457 01458 /** @defgroup USART_Group10 RS485 mode function 01459 * @brief RS485 mode function 01460 * 01461 @verbatim 01462 =============================================================================== 01463 ##### RS485 mode functions ##### 01464 =============================================================================== 01465 [..] This subsection provides a set of functions allowing to manage the USART 01466 RS485 flow control. 01467 [..] RS485 flow control (Driver enable feature) handling is possible through 01468 the following procedure: 01469 (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity, 01470 Transmitter/Receiver modes and hardware flow control values using 01471 the USART_Init() function. 01472 (#) Enable the Driver Enable using the USART_DECmd() function. 01473 (#) Configures the Driver Enable polarity using the USART_DEPolarityConfig() 01474 function. 01475 (#) Configures the Driver Enable assertion time using USART_SetDEAssertionTime() 01476 function and deassertion time using the USART_SetDEDeassertionTime() 01477 function. 01478 (#) Enable the USART using the USART_Cmd() function. 01479 [..] 01480 (@) The assertion and dessertion times are expressed in sample time units (1/8 or 01481 1/16 bit time, depending on the oversampling rate). 01482 01483 @endverbatim 01484 * @{ 01485 */ 01486 01487 /** 01488 * @brief Enables or disables the USART's DE functionality. 01489 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01490 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01491 * @param NewState: new state of the driver enable mode. 01492 * This parameter can be: ENABLE or DISABLE. 01493 * @retval None 01494 */ 01495 void USART_DECmd(USART_TypeDef* USARTx, FunctionalState NewState) 01496 { 01497 /* Check the parameters */ 01498 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01499 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01500 if (NewState != DISABLE) 01501 { 01502 /* Enable the DE functionality by setting the DEM bit in the CR3 register */ 01503 USARTx->CR3 |= USART_CR3_DEM; 01504 } 01505 else 01506 { 01507 /* Disable the DE functionality by clearing the DEM bit in the CR3 register */ 01508 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DEM); 01509 } 01510 } 01511 01512 /** 01513 * @brief Configures the USART's DE polarity 01514 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01515 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01516 * @param USART_DEPolarity: specifies the DE polarity. 01517 * This parameter can be one of the following values: 01518 * @arg USART_DEPolarity_Low 01519 * @arg USART_DEPolarity_High 01520 * @retval None 01521 */ 01522 void USART_DEPolarityConfig(USART_TypeDef* USARTx, uint32_t USART_DEPolarity) 01523 { 01524 /* Check the parameters */ 01525 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01526 assert_param(IS_USART_DE_POLARITY(USART_DEPolarity)); 01527 01528 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DEP); 01529 USARTx->CR3 |= USART_DEPolarity; 01530 } 01531 01532 /** 01533 * @brief Sets the specified RS485 DE assertion time 01534 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01535 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01536 * @param USART_AssertionTime: specifies the time between the activation of the DE 01537 * signal and the beginning of the start bit 01538 * @retval None 01539 */ 01540 void USART_SetDEAssertionTime(USART_TypeDef* USARTx, uint32_t USART_DEAssertionTime) 01541 { 01542 /* Check the parameters */ 01543 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01544 assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEAssertionTime)); 01545 01546 /* Clear the DE assertion time */ 01547 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_DEAT); 01548 /* Set the new value for the DE assertion time */ 01549 USARTx->CR1 |=((uint32_t)USART_DEAssertionTime << (uint32_t)0x15); 01550 } 01551 01552 /** 01553 * @brief Sets the specified RS485 DE deassertion time 01554 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01555 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01556 * @param USART_DeassertionTime: specifies the time between the middle of the last 01557 * stop bit in a transmitted message and the de-activation of the DE signal 01558 * @retval None 01559 */ 01560 void USART_SetDEDeassertionTime(USART_TypeDef* USARTx, uint32_t USART_DEDeassertionTime) 01561 { 01562 /* Check the parameters */ 01563 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01564 assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEDeassertionTime)); 01565 01566 /* Clear the DE deassertion time */ 01567 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_DEDT); 01568 /* Set the new value for the DE deassertion time */ 01569 USARTx->CR1 |=((uint32_t)USART_DEDeassertionTime << (uint32_t)0x10); 01570 } 01571 01572 /** 01573 * @} 01574 */ 01575 01576 /** @defgroup USART_Group11 DMA transfers management functions 01577 * @brief DMA transfers management functions 01578 * 01579 @verbatim 01580 =============================================================================== 01581 ##### DMA transfers management functions ##### 01582 =============================================================================== 01583 [..] This section provides two functions that can be used only in DMA mode. 01584 [..] In DMA Mode, the USART communication can be managed by 2 DMA Channel 01585 requests: 01586 (#) USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request. 01587 (#) USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request. 01588 [..] In this Mode it is advised to use the following function: 01589 (+) void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, 01590 FunctionalState NewState). 01591 @endverbatim 01592 * @{ 01593 */ 01594 01595 /** 01596 * @brief Enables or disables the USART's DMA interface. 01597 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01598 * following values: USART1 or USART2 or USART3 or UART4. 01599 * @param USART_DMAReq: specifies the DMA request. 01600 * This parameter can be any combination of the following values: 01601 * @arg USART_DMAReq_Tx: USART DMA transmit request 01602 * @arg USART_DMAReq_Rx: USART DMA receive request 01603 * @param NewState: new state of the DMA Request sources. 01604 * This parameter can be: ENABLE or DISABLE. 01605 * @retval None 01606 */ 01607 void USART_DMACmd(USART_TypeDef* USARTx, uint32_t USART_DMAReq, FunctionalState NewState) 01608 { 01609 /* Check the parameters */ 01610 assert_param(IS_USART_1234_PERIPH(USARTx)); 01611 assert_param(IS_USART_DMAREQ(USART_DMAReq)); 01612 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01613 01614 if (NewState != DISABLE) 01615 { 01616 /* Enable the DMA transfer for selected requests by setting the DMAT and/or 01617 DMAR bits in the USART CR3 register */ 01618 USARTx->CR3 |= USART_DMAReq; 01619 } 01620 else 01621 { 01622 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or 01623 DMAR bits in the USART CR3 register */ 01624 USARTx->CR3 &= (uint32_t)~USART_DMAReq; 01625 } 01626 } 01627 01628 /** 01629 * @brief Enables or disables the USART's DMA interface when reception error occurs. 01630 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01631 * following values: USART1 or USART2 or USART3 or UART4. 01632 * @param USART_DMAOnError: specifies the DMA status in case of reception error. 01633 * This parameter can be any combination of the following values: 01634 * @arg USART_DMAOnError_Enable: DMA receive request enabled when the USART DMA 01635 * reception error is asserted. 01636 * @arg USART_DMAOnError_Disable: DMA receive request disabled when the USART DMA 01637 * reception error is asserted. 01638 * @retval None 01639 */ 01640 void USART_DMAReceptionErrorConfig(USART_TypeDef* USARTx, uint32_t USART_DMAOnError) 01641 { 01642 /* Check the parameters */ 01643 assert_param(IS_USART_1234_PERIPH(USARTx)); 01644 assert_param(IS_USART_DMAONERROR(USART_DMAOnError)); 01645 01646 /* Clear the DMA Reception error detection bit */ 01647 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DDRE); 01648 /* Set the new value for the DMA Reception error detection bit */ 01649 USARTx->CR3 |= USART_DMAOnError; 01650 } 01651 01652 /** 01653 * @} 01654 */ 01655 01656 /** @defgroup USART_Group12 Interrupts and flags management functions 01657 * @brief Interrupts and flags management functions 01658 * 01659 @verbatim 01660 =============================================================================== 01661 ##### Interrupts and flags management functions ##### 01662 =============================================================================== 01663 [..] This subsection provides a set of functions allowing to configure the 01664 USART Interrupts sources, Requests and check or clear the flags or pending bits status. 01665 The user should identify which mode will be used in his application to 01666 manage the communication: Polling mode, Interrupt mode. 01667 01668 *** Polling Mode *** 01669 ==================== 01670 [..] In Polling Mode, the SPI communication can be managed by these flags: 01671 (#) USART_FLAG_REACK: to indicate the status of the Receive Enable 01672 acknowledge flag 01673 (#) USART_FLAG_TEACK: to indicate the status of the Transmit Enable 01674 acknowledge flag. 01675 (#) USART_FLAG_WUF: to indicate the status of the Wake up flag. 01676 (#) USART_FLAG_RWU: to indicate the status of the Receive Wake up flag. 01677 (#) USART_FLAG_SBK: to indicate the status of the Send Break flag. 01678 (#) USART_FLAG_CMF: to indicate the status of the Character match flag. 01679 (#) USART_FLAG_BUSY: to indicate the status of the Busy flag. 01680 (#) USART_FLAG_ABRF: to indicate the status of the Auto baud rate flag. 01681 (#) USART_FLAG_ABRE: to indicate the status of the Auto baud rate error flag. 01682 (#) USART_FLAG_EOBF: to indicate the status of the End of block flag. 01683 (#) USART_FLAG_RTOF: to indicate the status of the Receive time out flag. 01684 (#) USART_FLAG_nCTSS: to indicate the status of the Inverted nCTS input 01685 bit status. 01686 (#) USART_FLAG_TXE: to indicate the status of the transmit buffer register. 01687 (#) USART_FLAG_RXNE: to indicate the status of the receive buffer register. 01688 (#) USART_FLAG_TC: to indicate the status of the transmit operation. 01689 (#) USART_FLAG_IDLE: to indicate the status of the Idle Line. 01690 (#) USART_FLAG_CTS: to indicate the status of the nCTS input. 01691 (#) USART_FLAG_LBD: to indicate the status of the LIN break detection. 01692 (#) USART_FLAG_NE: to indicate if a noise error occur. 01693 (#) USART_FLAG_FE: to indicate if a frame error occur. 01694 (#) USART_FLAG_PE: to indicate if a parity error occur. 01695 (#) USART_FLAG_ORE: to indicate if an Overrun error occur. 01696 [..] In this Mode it is advised to use the following functions: 01697 (+) FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG). 01698 (+) void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG). 01699 01700 *** Interrupt Mode *** 01701 ====================== 01702 [..] In Interrupt Mode, the USART communication can be managed by 8 interrupt 01703 sources and 10 pending bits: 01704 (+) Pending Bits: 01705 (##) USART_IT_WU: to indicate the status of the Wake up interrupt. 01706 (##) USART_IT_CM: to indicate the status of Character match interrupt. 01707 (##) USART_IT_EOB: to indicate the status of End of block interrupt. 01708 (##) USART_IT_RTO: to indicate the status of Receive time out interrupt. 01709 (##) USART_IT_CTS: to indicate the status of CTS change interrupt. 01710 (##) USART_IT_LBD: to indicate the status of LIN Break detection interrupt. 01711 (##) USART_IT_TC: to indicate the status of Transmission complete interrupt. 01712 (##) USART_IT_IDLE: to indicate the status of IDLE line detected interrupt. 01713 (##) USART_IT_ORE: to indicate the status of OverRun Error interrupt. 01714 (##) USART_IT_NE: to indicate the status of Noise Error interrupt. 01715 (##) USART_IT_FE: to indicate the status of Framing Error interrupt. 01716 (##) USART_IT_PE: to indicate the status of Parity Error interrupt. 01717 01718 (+) Interrupt Source: 01719 (##) USART_IT_WU: specifies the interrupt source for Wake up interrupt. 01720 (##) USART_IT_CM: specifies the interrupt source for Character match 01721 interrupt. 01722 (##) USART_IT_EOB: specifies the interrupt source for End of block 01723 interrupt. 01724 (##) USART_IT_RTO: specifies the interrupt source for Receive time-out 01725 interrupt. 01726 (##) USART_IT_CTS: specifies the interrupt source for CTS change interrupt. 01727 (##) USART_IT_LBD: specifies the interrupt source for LIN Break 01728 detection interrupt. 01729 (##) USART_IT_TXE: specifies the interrupt source for Transmit Data 01730 Register empty interrupt. 01731 (##) USART_IT_TC: specifies the interrupt source for Transmission 01732 complete interrupt. 01733 (##) USART_IT_RXNE: specifies the interrupt source for Receive Data 01734 register not empty interrupt. 01735 (##) USART_IT_IDLE: specifies the interrupt source for Idle line 01736 detection interrupt. 01737 (##) USART_IT_PE: specifies the interrupt source for Parity Error interrupt. 01738 (##) USART_IT_ERR: specifies the interrupt source for Error interrupt 01739 (Frame error, noise error, overrun error) 01740 -@@- Some parameters are coded in order to use them as interrupt 01741 source or as pending bits. 01742 [..] In this Mode it is advised to use the following functions: 01743 (+) void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState). 01744 (+) ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT). 01745 (+) void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT). 01746 01747 @endverbatim 01748 * @{ 01749 */ 01750 01751 /** 01752 * @brief Enables or disables the specified USART interrupts. 01753 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01754 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01755 * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled. 01756 * This parameter can be one of the following values: 01757 * @arg USART_IT_WU: Wake up interrupt. 01758 * @arg USART_IT_CM: Character match interrupt. 01759 * @arg USART_IT_EOB: End of block interrupt. 01760 * @arg USART_IT_RTO: Receive time out interrupt. 01761 * @arg USART_IT_CTS: CTS change interrupt. 01762 * @arg USART_IT_LBD: LIN Break detection interrupt. 01763 * @arg USART_IT_TXE: Transmit Data Register empty interrupt. 01764 * @arg USART_IT_TC: Transmission complete interrupt. 01765 * @arg USART_IT_RXNE: Receive Data register not empty interrupt. 01766 * @arg USART_IT_IDLE: Idle line detection interrupt. 01767 * @arg USART_IT_PE: Parity Error interrupt. 01768 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error) 01769 * @param NewState: new state of the specified USARTx interrupts. 01770 * This parameter can be: ENABLE or DISABLE. 01771 * @retval None 01772 */ 01773 void USART_ITConfig(USART_TypeDef* USARTx, uint32_t USART_IT, FunctionalState NewState) 01774 { 01775 uint32_t usartreg = 0, itpos = 0, itmask = 0; 01776 uint32_t usartxbase = 0; 01777 /* Check the parameters */ 01778 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01779 assert_param(IS_USART_CONFIG_IT(USART_IT)); 01780 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01781 01782 usartxbase = (uint32_t)USARTx; 01783 01784 /* Get the USART register index */ 01785 usartreg = (((uint16_t)USART_IT) >> 0x08); 01786 01787 /* Get the interrupt position */ 01788 itpos = USART_IT & IT_MASK; 01789 itmask = (((uint32_t)0x01) << itpos); 01790 01791 if (usartreg == 0x02) /* The IT is in CR2 register */ 01792 { 01793 usartxbase += 0x04; 01794 } 01795 else if (usartreg == 0x03) /* The IT is in CR3 register */ 01796 { 01797 usartxbase += 0x08; 01798 } 01799 else /* The IT is in CR1 register */ 01800 { 01801 } 01802 if (NewState != DISABLE) 01803 { 01804 *(__IO uint32_t*)usartxbase |= itmask; 01805 } 01806 else 01807 { 01808 *(__IO uint32_t*)usartxbase &= ~itmask; 01809 } 01810 } 01811 01812 /** 01813 * @brief Enables the specified USART's Request. 01814 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01815 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01816 * @param USART_Request: specifies the USART request. 01817 * This parameter can be any combination of the following values: 01818 * @arg USART_Request_TXFRQ: Transmit data flush ReQuest 01819 * @arg USART_Request_RXFRQ: Receive data flush ReQuest 01820 * @arg USART_Request_MMRQ: Mute Mode ReQuest 01821 * @arg USART_Request_SBKRQ: Send Break ReQuest 01822 * @arg USART_Request_ABRRQ: Auto Baud Rate ReQuest 01823 * @param NewState: new state of the DMA interface when reception error occurs. 01824 * This parameter can be: ENABLE or DISABLE. 01825 * @retval None 01826 */ 01827 void USART_RequestCmd(USART_TypeDef* USARTx, uint32_t USART_Request, FunctionalState NewState) 01828 { 01829 /* Check the parameters */ 01830 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01831 assert_param(IS_USART_REQUEST(USART_Request)); 01832 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01833 01834 if (NewState != DISABLE) 01835 { 01836 /* Enable the USART ReQuest by setting the dedicated request bit in the RQR 01837 register.*/ 01838 USARTx->RQR |= USART_Request; 01839 } 01840 else 01841 { 01842 /* Disable the USART ReQuest by clearing the dedicated request bit in the RQR 01843 register.*/ 01844 USARTx->RQR &= (uint32_t)~USART_Request; 01845 } 01846 } 01847 01848 /** 01849 * @brief Enables or disables the USART's Overrun detection. 01850 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01851 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01852 * @param USART_OVRDetection: specifies the OVR detection status in case of OVR error. 01853 * This parameter can be any combination of the following values: 01854 * @arg USART_OVRDetection_Enable: OVR error detection enabled when the USART OVR error 01855 * is asserted. 01856 * @arg USART_OVRDetection_Disable: OVR error detection disabled when the USART OVR error 01857 * is asserted. 01858 * @retval None 01859 */ 01860 void USART_OverrunDetectionConfig(USART_TypeDef* USARTx, uint32_t USART_OVRDetection) 01861 { 01862 /* Check the parameters */ 01863 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01864 assert_param(IS_USART_OVRDETECTION(USART_OVRDetection)); 01865 01866 /* Clear the OVR detection bit */ 01867 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_OVRDIS); 01868 /* Set the new value for the OVR detection bit */ 01869 USARTx->CR3 |= USART_OVRDetection; 01870 } 01871 01872 /** 01873 * @brief Checks whether the specified USART flag is set or not. 01874 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01875 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01876 * @param USART_FLAG: specifies the flag to check. 01877 * This parameter can be one of the following values: 01878 * @arg USART_FLAG_REACK: Receive Enable acknowledge flag. 01879 * @arg USART_FLAG_TEACK: Transmit Enable acknowledge flag. 01880 * @arg USART_FLAG_WUF: Wake up flag. 01881 * @arg USART_FLAG_RWU: Receive Wake up flag. 01882 * @arg USART_FLAG_SBK: Send Break flag. 01883 * @arg USART_FLAG_CMF: Character match flag. 01884 * @arg USART_FLAG_BUSY: Busy flag. 01885 * @arg USART_FLAG_ABRF: Auto baud rate flag. 01886 * @arg USART_FLAG_ABRE: Auto baud rate error flag. 01887 * @arg USART_FLAG_EOBF: End of block flag. 01888 * @arg USART_FLAG_RTOF: Receive time out flag. 01889 * @arg USART_FLAG_nCTSS: Inverted nCTS input bit status. 01890 * @arg USART_FLAG_CTS: CTS Change flag. 01891 * @arg USART_FLAG_LBD: LIN Break detection flag. 01892 * @arg USART_FLAG_TXE: Transmit data register empty flag. 01893 * @arg USART_FLAG_TC: Transmission Complete flag. 01894 * @arg USART_FLAG_RXNE: Receive data register not empty flag. 01895 * @arg USART_FLAG_IDLE: Idle Line detection flag. 01896 * @arg USART_FLAG_ORE: OverRun Error flag. 01897 * @arg USART_FLAG_NE: Noise Error flag. 01898 * @arg USART_FLAG_FE: Framing Error flag. 01899 * @arg USART_FLAG_PE: Parity Error flag. 01900 * @retval The new state of USART_FLAG (SET or RESET). 01901 */ 01902 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint32_t USART_FLAG) 01903 { 01904 FlagStatus bitstatus = RESET; 01905 /* Check the parameters */ 01906 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01907 assert_param(IS_USART_FLAG(USART_FLAG)); 01908 01909 if ((USARTx->ISR & USART_FLAG) != (uint16_t)RESET) 01910 { 01911 bitstatus = SET; 01912 } 01913 else 01914 { 01915 bitstatus = RESET; 01916 } 01917 return bitstatus; 01918 } 01919 01920 /** 01921 * @brief Clears the USARTx's pending flags. 01922 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01923 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01924 * @param USART_FLAG: specifies the flag to clear. 01925 * This parameter can be any combination of the following values: 01926 * @arg USART_FLAG_WUF: Wake up flag. 01927 * @arg USART_FLAG_CMF: Character match flag. 01928 * @arg USART_FLAG_EOBF: End of block flag. 01929 * @arg USART_FLAG_RTOF: Receive time out flag. 01930 * @arg USART_FLAG_CTS: CTS Change flag. 01931 * @arg USART_FLAG_LBD: LIN Break detection flag. 01932 * @arg USART_FLAG_TC: Transmission Complete flag. 01933 * @arg USART_FLAG_IDLE: IDLE line detected flag. 01934 * @arg USART_FLAG_ORE: OverRun Error flag. 01935 * @arg USART_FLAG_NE: Noise Error flag. 01936 * @arg USART_FLAG_FE: Framing Error flag. 01937 * @arg USART_FLAG_PE: Parity Errorflag. 01938 * 01939 * @note 01940 * - RXNE pending bit is cleared by a read to the USART_RDR register 01941 * (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register USART_RQR 01942 * (USART_RequestCmd()). 01943 * - TC flag can be also cleared by software sequence: a read operation to 01944 * USART_SR register (USART_GetFlagStatus()) followed by a write operation 01945 * to USART_TDR register (USART_SendData()). 01946 * - TXE flag is cleared by a write to the USART_TDR register 01947 * (USART_SendData()) or by writing 1 to the TXFRQ in the register USART_RQR 01948 * (USART_RequestCmd()). 01949 * - SBKF flag is cleared by 1 to the SBKRQ in the register USART_RQR 01950 * (USART_RequestCmd()). 01951 * @retval None 01952 */ 01953 void USART_ClearFlag(USART_TypeDef* USARTx, uint32_t USART_FLAG) 01954 { 01955 /* Check the parameters */ 01956 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01957 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG)); 01958 01959 USARTx->ICR = USART_FLAG; 01960 } 01961 01962 /** 01963 * @brief Checks whether the specified USART interrupt has occurred or not. 01964 * @param USARTx: Select the USART peripheral. This parameter can be one of the 01965 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 01966 * @param USART_IT: specifies the USART interrupt source to check. 01967 * This parameter can be one of the following values: 01968 * @arg USART_IT_WU: Wake up interrupt. 01969 * @arg USART_IT_CM: Character match interrupt. 01970 * @arg USART_IT_EOB: End of block interrupt. 01971 * @arg USART_IT_RTO: Receive time out interrupt. 01972 * @arg USART_IT_CTS: CTS change interrupt. 01973 * @arg USART_IT_LBD: LIN Break detection interrupt. 01974 * @arg USART_IT_TXE: Transmit Data Register empty interrupt. 01975 * @arg USART_IT_TC: Transmission complete interrupt. 01976 * @arg USART_IT_RXNE: Receive Data register not empty interrupt. 01977 * @arg USART_IT_IDLE: Idle line detection interrupt. 01978 * @arg USART_IT_ORE: OverRun Error interrupt. 01979 * @arg USART_IT_NE: Noise Error interrupt. 01980 * @arg USART_IT_FE: Framing Error interrupt. 01981 * @arg USART_IT_PE: Parity Error interrupt. 01982 * @retval The new state of USART_IT (SET or RESET). 01983 */ 01984 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint32_t USART_IT) 01985 { 01986 uint32_t bitpos = 0, itmask = 0, usartreg = 0; 01987 ITStatus bitstatus = RESET; 01988 /* Check the parameters */ 01989 assert_param(IS_USART_ALL_PERIPH(USARTx)); 01990 assert_param(IS_USART_GET_IT(USART_IT)); 01991 01992 /* Get the USART register index */ 01993 usartreg = (((uint16_t)USART_IT) >> 0x08); 01994 /* Get the interrupt position */ 01995 itmask = USART_IT & IT_MASK; 01996 itmask = (uint32_t)0x01 << itmask; 01997 01998 if (usartreg == 0x01) /* The IT is in CR1 register */ 01999 { 02000 itmask &= USARTx->CR1; 02001 } 02002 else if (usartreg == 0x02) /* The IT is in CR2 register */ 02003 { 02004 itmask &= USARTx->CR2; 02005 } 02006 else /* The IT is in CR3 register */ 02007 { 02008 itmask &= USARTx->CR3; 02009 } 02010 02011 bitpos = USART_IT >> 0x10; 02012 bitpos = (uint32_t)0x01 << bitpos; 02013 bitpos &= USARTx->ISR; 02014 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET)) 02015 { 02016 bitstatus = SET; 02017 } 02018 else 02019 { 02020 bitstatus = RESET; 02021 } 02022 02023 return bitstatus; 02024 } 02025 02026 /** 02027 * @brief Clears the USARTx's interrupt pending bits. 02028 * @param USARTx: Select the USART peripheral. This parameter can be one of the 02029 * following values: USART1 or USART2 or USART3 or UART4 or UART5. 02030 * @param USART_IT: specifies the interrupt pending bit to clear. 02031 * This parameter can be one of the following values: 02032 * @arg USART_IT_WU: Wake up interrupt. 02033 * @arg USART_IT_CM: Character match interrupt. 02034 * @arg USART_IT_EOB: End of block interrupt. 02035 * @arg USART_IT_RTO: Receive time out interrupt. 02036 * @arg USART_IT_CTS: CTS change interrupt. 02037 * @arg USART_IT_LBD: LIN Break detection interrupt. 02038 * @arg USART_IT_TC: Transmission complete interrupt. 02039 * @arg USART_IT_IDLE: IDLE line detected interrupt. 02040 * @arg USART_IT_ORE: OverRun Error interrupt. 02041 * @arg USART_IT_NE: Noise Error interrupt. 02042 * @arg USART_IT_FE: Framing Error interrupt. 02043 * @arg USART_IT_PE: Parity Error interrupt. 02044 * @note 02045 * - RXNE pending bit is cleared by a read to the USART_RDR register 02046 * (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register USART_RQR 02047 * (USART_RequestCmd()). 02048 * - TC pending bit can be also cleared by software sequence: a read 02049 * operation to USART_SR register (USART_GetITStatus()) followed by a write 02050 * operation to USART_TDR register (USART_SendData()). 02051 * - TXE pending bit is cleared by a write to the USART_TDR register 02052 * (USART_SendData()) or by writing 1 to the TXFRQ in the register USART_RQR 02053 * (USART_RequestCmd()). 02054 * @retval None 02055 */ 02056 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint32_t USART_IT) 02057 { 02058 uint32_t bitpos = 0, itmask = 0; 02059 /* Check the parameters */ 02060 assert_param(IS_USART_ALL_PERIPH(USARTx)); 02061 assert_param(IS_USART_CLEAR_IT(USART_IT)); 02062 02063 bitpos = USART_IT >> 0x10; 02064 itmask = ((uint32_t)0x01 << (uint32_t)bitpos); 02065 USARTx->ICR = (uint32_t)itmask; 02066 } 02067 02068 /** 02069 * @} 02070 */ 02071 02072 /** 02073 * @} 02074 */ 02075 02076 /** 02077 * @} 02078 */ 02079 02080 /** 02081 * @} 02082 */ 02083 02084 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 17:34:45 by
