STM8L15x Standard Peripherals Drivers
|
stm8l15x_usart.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file stm8l15x_usart.c 00004 * @author MCD Application Team 00005 * @version V1.5.0 00006 * @date 13-May-2011 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 * - Data transfers 00012 * - Multi-Processor Communication 00013 * - Half-duplex mode 00014 * - Smartcard mode 00015 * - IrDA mode 00016 * - DMA transfers management 00017 * - Interrupts and flags management 00018 * 00019 * @verbatim 00020 * 00021 * =================================================================== 00022 * How to use this driver 00023 * =================================================================== 00024 * 1. Enable peripheral clock using CLK_PeripheralClockConfig(CLK_Peripheral_USARTx, 00025 * ENABLE) function (Refer to the product datasheet for the available USART 00026 * peripherals) 00027 * 00028 * 2. Enable the external Pull-up on the used USART Pins using the 00029 * GPIO_ExternalPullUpConfig() function or an external pull-up equivalent resistor 00030 * (RPU = 45 KOhm typical value). 00031 * 00032 * 3. Program the Baud Rate, Word Length , Stop Bit, Parity and Mode (Receiver/Transmitter) 00033 * using the USART_Init() function. 00034 * 00035 * 4. For synchronous mode, enable the clock and program the polarity, 00036 * phase and last bit using the USART_ClockInit() function. 00037 * 00038 * 5. Enable the corresponding interrupt using the function USART_ITConfig() if you need 00039 * to use interrupt mode. 00040 * 00041 * 6. When using the DMA mode 00042 * - Configure the DMA using DMA_Init() function 00043 * - Activate the needed channel Request using USART_DMACmd() function 00044 * 00045 * 7. Enable the USART using the USART_Cmd() function. 00046 * 00047 * 8. Enable the DMA using the DMA_Cmd() function, when using DMA mode. 00048 * 00049 * Refer to Multi-Processor, half-duplex, Smartcard, IrDA sub-sections for more details. 00050 * 00051 * @endverbatim 00052 * 00053 ****************************************************************************** 00054 * @attention 00055 * 00056 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 00057 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 00058 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 00059 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 00060 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 00061 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 00062 * 00063 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> 00064 ****************************************************************************** 00065 */ 00066 00067 /* Includes ------------------------------------------------------------------*/ 00068 #include "stm8l15x_usart.h" 00069 00070 /** @addtogroup STM8L15x_StdPeriph_Driver 00071 * @{ 00072 */ 00073 00074 /** @defgroup USART 00075 * @brief USART driver modules 00076 * @{ 00077 */ 00078 00079 /* Private typedef -----------------------------------------------------------*/ 00080 /* Private define ------------------------------------------------------------*/ 00081 /* Private macro -------------------------------------------------------------*/ 00082 /* Private variables ---------------------------------------------------------*/ 00083 /* Private function prototypes -----------------------------------------------*/ 00084 /* Private functions ---------------------------------------------------------*/ 00085 00086 /** @defgroup USART_Private_Functions 00087 * @{ 00088 */ 00089 00090 /** @defgroup USART_Group1 Initialization and Configuration functions 00091 * @brief Initialization and Configuration functions 00092 * 00093 @verbatim 00094 =============================================================================== 00095 Initialization and Configuration functions 00096 =============================================================================== 00097 00098 This subsection provides a set of functions allowing to initialize the USART 00099 in asynchronous and in synchronous modes. 00100 - For the asynchronous mode only these parameters can be configured: 00101 - Baud Rate 00102 - Word Length 00103 - Stop Bit 00104 - Parity: If the parity is enabled, then the MSB bit of the data written 00105 in the data register is transmitted but is changed by the parity bit. 00106 Depending on the frame length defined by the M bit (8-bits or 9-bits), 00107 the possible USART frame formats are as listed in the following table: 00108 +-------------------------------------------------------------+ 00109 | M bit | PCE bit | USART frame | 00110 |---------------------|---------------------------------------| 00111 | 0 | 0 | | SB | 8 bit data | STB | | 00112 |---------|-----------|---------------------------------------| 00113 | 0 | 1 | | SB | 7 bit data | PB | STB | | 00114 |---------|-----------|---------------------------------------| 00115 | 1 | 0 | | SB | 9 bit data | STB | | 00116 |---------|-----------|---------------------------------------| 00117 | 1 | 1 | | SB | 8 bit data | PB | STB | | 00118 +-------------------------------------------------------------+ 00119 - Receiver/transmitter modes 00120 00121 The USART_Init() function follows the USART asynchronous configuration procedure 00122 (details for the procedure are available in reference manual (RM0031)). 00123 00124 - For the synchronous mode in addition to the asynchronous mode parameters these 00125 parameters should be also configured: 00126 - USART Clock Enabled 00127 - USART polarity 00128 - USART phase 00129 - USART LastBit 00130 00131 These parameters can be configured using the USART_ClockInit() function. 00132 00133 @endverbatim 00134 * @{ 00135 */ 00136 00137 /** 00138 * @brief Deinitializes the USART peripheral. 00139 * @param USARTx: where x can be 1, 2 or 3 to select the specified USART peripheral. 00140 * @retval None 00141 */ 00142 void USART_DeInit(USART_TypeDef* USARTx) 00143 { 00144 00145 /* Clear the Idle Line Detected bit in the status register by a read 00146 to the USART_SR register followed by a Read to the USART_DR register */ 00147 (void) USARTx->SR; 00148 (void) USARTx->DR; 00149 00150 USARTx->BRR2 = USART_BRR2_RESET_VALUE; /* Set USART_BRR2 to reset value 0x00 */ 00151 USARTx->BRR1 = USART_BRR1_RESET_VALUE; /* Set USART_BRR1 to reset value 0x00 */ 00152 00153 USARTx->CR1 = USART_CR1_RESET_VALUE; /* Set USART_CR1 to reset value 0x00 */ 00154 USARTx->CR2 = USART_CR2_RESET_VALUE; /* Set USART_CR2 to reset value 0x00 */ 00155 USARTx->CR3 = USART_CR3_RESET_VALUE; /* Set USART_CR3 to reset value 0x00 */ 00156 USARTx->CR4 = USART_CR4_RESET_VALUE; /* Set USART_CR4 to reset value 0x00 */ 00157 } 00158 00159 /** 00160 * @brief Initializes the USART according to the specified parameters. 00161 * @note Configure in Push Pull or Open Drain mode the Tx pin by setting the 00162 * correct I/O Port register according the product package and line 00163 * configuration 00164 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00165 * @param BaudRate: The baudrate. 00166 * @param USART_WordLength: the word length 00167 * This parameter can be one of the following values: 00168 * @arg USART_WordLength_8b: 8 bits Data 00169 * @arg USART_WordLength_9b: 9 bits Data 00170 * @param USART_StopBits: Stop Bit 00171 * This parameter can be one of the following values: 00172 * @arg USART_StopBits_1: One stop bit is transmitted at the end of frame 00173 * @arg USART_StopBits_2: Two stop bits are transmitted at the end of frame 00174 * @arg USART_StopBits_1_5: One and half stop bits 00175 * @param USART_Parity: Parity 00176 * This parameter can be one of the following values: 00177 * @arg USART_Parity_No: No Parity 00178 * @arg USART_Parity_Even: Even Parity 00179 * @arg USART_Parity_Odd: Odd Parity 00180 * @param USART_Mode: Mode 00181 * This parameter can be one of the following values: 00182 * @arg USART_Mode_Rx: Receive Enable 00183 * @arg USART_Mode_Tx: Transmit Enable 00184 * @retval None 00185 */ 00186 void USART_Init(USART_TypeDef* USARTx, uint32_t BaudRate, USART_WordLength_TypeDef 00187 USART_WordLength, USART_StopBits_TypeDef USART_StopBits, 00188 USART_Parity_TypeDef USART_Parity, USART_Mode_TypeDef USART_Mode) 00189 { 00190 uint32_t BaudRate_Mantissa = 0; 00191 00192 /* Check the parameters */ 00193 assert_param(IS_USART_BAUDRATE(BaudRate)); 00194 00195 assert_param(IS_USART_WORDLENGTH(USART_WordLength)); 00196 00197 assert_param(IS_USART_STOPBITS(USART_StopBits)); 00198 00199 assert_param(IS_USART_PARITY(USART_Parity)); 00200 00201 assert_param(IS_USART_MODE(USART_Mode)); 00202 00203 /* Clear the word length and Parity Control bits */ 00204 USARTx->CR1 &= (uint8_t)(~(USART_CR1_PCEN | USART_CR1_PS | USART_CR1_M)); 00205 /* Set the word length bit according to USART_WordLength value */ 00206 /* Set the Parity Control bit to USART_Parity value */ 00207 USARTx->CR1 |= (uint8_t)((uint8_t)USART_WordLength | (uint8_t)USART_Parity); 00208 00209 /* Clear the STOP bits */ 00210 USARTx->CR3 &= (uint8_t)(~USART_CR3_STOP); 00211 /* Set the STOP bits number according to USART_StopBits value */ 00212 USARTx->CR3 |= (uint8_t)USART_StopBits; 00213 00214 /* Clear the LSB mantissa of USARTDIV */ 00215 USARTx->BRR1 &= (uint8_t)(~USART_BRR1_DIVM); 00216 /* Clear the MSB mantissa of USARTDIV */ 00217 USARTx->BRR2 &= (uint8_t)(~USART_BRR2_DIVM); 00218 /* Clear the Fraction bits of USARTDIV */ 00219 USARTx->BRR2 &= (uint8_t)(~USART_BRR2_DIVF); 00220 00221 BaudRate_Mantissa = (uint32_t)(CLK_GetClockFreq() / BaudRate ); 00222 /* Set the fraction of USARTDIV */ 00223 USARTx->BRR2 = (uint8_t)((BaudRate_Mantissa >> (uint8_t)8) & (uint8_t)0xF0); 00224 /* Set the MSB mantissa of USARTDIV */ 00225 USARTx->BRR2 |= (uint8_t)(BaudRate_Mantissa & (uint8_t)0x0F); 00226 /* Set the LSB mantissa of USARTDIV */ 00227 USARTx->BRR1 = (uint8_t)(BaudRate_Mantissa >> (uint8_t)4); 00228 00229 /* Disable the Transmitter and Receiver */ 00230 USARTx->CR2 &= (uint8_t)~(USART_CR2_TEN | USART_CR2_REN); 00231 /* Set TEN and REN bits according to USART_Mode value */ 00232 USARTx->CR2 |= (uint8_t)USART_Mode; 00233 } 00234 00235 /** 00236 * @brief Initializes the USART Clock according to the specified parameters. 00237 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00238 * @param USART_Clock: Clock 00239 * This parameter can be one of the following values: 00240 * @arg USART_Clock_Disable: CK pin disabled 00241 * @arg USART_Clock_Enable: CK pin enabled 00242 * @param USART_CPOL: Clock Polarity 00243 * This parameter can be one of the following values: 00244 * @arg USART_CPOL_Low: CK to 0 when idle 00245 * @arg USART_CPOL_High: CK to 1 when idle 00246 * @param USART_CPHA: Clock Phase 00247 * This parameter can be one of the following values: 00248 * @arg USART_CPHA_1Edge: The first clock transition is the first data capture edge 00249 * @arg USART_CPHA_2Edge: The second clock transition is the first data capture edge 00250 * @param USART_LastBit: Last Bit 00251 * This parameter can be one of the following values: 00252 * @arg USART_LastBit_Disable: The clock pulse of the last data bit is 00253 * not output to the SCLK pin 00254 * @arg USART_LastBit_Enable: The clock pulse of the last data bit is 00255 * output to the SCLK pin 00256 * @retval None 00257 */ 00258 void USART_ClockInit(USART_TypeDef* USARTx, USART_Clock_TypeDef USART_Clock, 00259 USART_CPOL_TypeDef USART_CPOL, USART_CPHA_TypeDef USART_CPHA, 00260 USART_LastBit_TypeDef USART_LastBit) 00261 { 00262 /* Check the parameters */ 00263 assert_param(IS_USART_CLOCK(USART_Clock)); 00264 assert_param(IS_USART_CPOL(USART_CPOL)); 00265 assert_param(IS_USART_CPHA(USART_CPHA)); 00266 assert_param(IS_USART_LASTBIT(USART_LastBit)); 00267 00268 /* Clear the Clock Polarity, lock Phase, Last Bit Clock pulse */ 00269 USARTx->CR3 &= (uint8_t)~(USART_CR3_CPOL | USART_CR3_CPHA | USART_CR3_LBCL); 00270 /* Set the Clock Polarity, lock Phase, Last Bit Clock pulse */ 00271 USARTx->CR3 |= (uint8_t)((uint8_t)((uint8_t)(USART_CPOL | (uint8_t)USART_CPHA ) | USART_LastBit)); 00272 00273 if (USART_Clock != USART_Clock_Disable) 00274 { 00275 USARTx->CR3 |= (uint8_t)(USART_CR3_CLKEN); /* Set the Clock Enable bit */ 00276 } 00277 else 00278 { 00279 USARTx->CR3 &= (uint8_t)(~USART_CR3_CLKEN); /* Clear the Clock Enable bit */ 00280 } 00281 } 00282 00283 /** 00284 * @brief Enable the USART peripheral. 00285 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00286 * @param NewState: The new state of the USART Communication. 00287 * This parameter can be: ENABLE or DISABLE. 00288 * @retval None 00289 */ 00290 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState) 00291 { 00292 if (NewState != DISABLE) 00293 { 00294 USARTx->CR1 &= (uint8_t)(~USART_CR1_USARTD); /**< USART Enable */ 00295 } 00296 else 00297 { 00298 USARTx->CR1 |= USART_CR1_USARTD; /**< USART Disable (for low power consumption) */ 00299 } 00300 } 00301 00302 /** 00303 * @brief Sets the system clock prescaler. 00304 * @note IrDA Low Power mode or smartcard mode should be enabled 00305 * @note This function is related to SmartCard and IrDa mode. 00306 * @param USARTx: Select the USARTx peripheral. 00307 * @param USART_Prescaler: specifies the prescaler clock. 00308 * @note In IrDA Low Power Mode the clock source is divided by the value given 00309 * in the register (8 bits) 00310 * - 0000 0000 Reserved 00311 * - 0000 0001 divides the clock source by 1 00312 * - 0000 0010 divides the clock source by 2 00313 * - ... 00314 * @note In Smart Card Mode the clock source is divided by the value given in the register 00315 * (5 significant bits) multiplied by 2 00316 * - 0 0000 Reserved 00317 * - 0 0001 divides the clock source by 2 00318 * - 0 0010 divides the clock source by 4 00319 * - 0 0011 divides the clock source by 6 00320 * - ... 00321 * @retval None 00322 */ 00323 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler) 00324 { 00325 /* Load the USART prescaler value*/ 00326 USARTx->PSCR = USART_Prescaler; 00327 } 00328 00329 /** 00330 * @brief Transmits break characters. 00331 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00332 * @retval None 00333 */ 00334 void USART_SendBreak(USART_TypeDef* USARTx) 00335 { 00336 USARTx->CR2 |= USART_CR2_SBK; 00337 } 00338 00339 /** 00340 * @} 00341 */ 00342 00343 /** @defgroup USART_Group2 Data transfers functions 00344 * @brief Data transfers functions 00345 * 00346 @verbatim 00347 =============================================================================== 00348 Data transfers functions 00349 =============================================================================== 00350 00351 This subsection provides a set of functions allowing to manage the USART data 00352 transfers. 00353 00354 During an USART reception, data shifts in least significant bit first through 00355 the RX pin. In this mode, the USART_DR register is similar to a buffer (RDR) 00356 between the internal bus and the received shift register. 00357 00358 When a transmission is taking place, a write instruction to the USART_DR register 00359 stores the data in the TDR register which is copied in the shift register 00360 at the end of the current transmission. 00361 00362 The read access of the USART_DR register can be done using the USART_ReceiveData8() 00363 or USART_ReceiveData9() functions and returns the RDR buffered value. Whereas a write 00364 access to the USART_DR can be done using USART_SendData8() or USART_SendData9() 00365 functions and stores the written data into TDR buffer. 00366 00367 @endverbatim 00368 * @{ 00369 */ 00370 00371 /** 00372 * @brief Returns the most recent received data by the USART peripheral. 00373 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00374 * @retval The received data. 00375 */ 00376 uint8_t USART_ReceiveData8(USART_TypeDef* USARTx) 00377 { 00378 return USARTx->DR; 00379 } 00380 00381 /** 00382 * @brief Returns the most recent received data by the USART peripheral. 00383 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00384 * @retval The received data. 00385 */ 00386 uint16_t USART_ReceiveData9(USART_TypeDef* USARTx) 00387 { 00388 uint16_t temp = 0; 00389 00390 temp = ((uint16_t)(((uint16_t)((uint16_t)USARTx->CR1 & (uint16_t)USART_CR1_R8)) << 1)); 00391 return (uint16_t)( ((uint16_t)((uint16_t)USARTx->DR) | temp) & ((uint16_t)0x01FF)); 00392 } 00393 00394 /** 00395 * @brief Transmits 8 bit data through the USART peripheral. 00396 * @param Data: The data to transmit. 00397 * @retval None 00398 */ 00399 void USART_SendData8(USART_TypeDef* USARTx, uint8_t Data) 00400 { 00401 /* Transmit Data */ 00402 USARTx->DR = Data; 00403 } 00404 00405 /** 00406 * @brief Transmits 9 bit data through the USART peripheral. 00407 * @param USARTx: Select the USARTx peripheral. 00408 * @param Data: The data to transmit. 00409 * This parameter should be lower than 0x1FF. 00410 * @retval None 00411 */ 00412 void USART_SendData9(USART_TypeDef* USARTx, uint16_t Data) 00413 { 00414 assert_param(IS_USART_DATA_9BITS(Data)); 00415 00416 /* Clear the transmit data bit 8 */ 00417 USARTx->CR1 &= ((uint8_t)~USART_CR1_T8); 00418 00419 /* Write the transmit data bit [8] */ 00420 USARTx->CR1 |= (uint8_t)(((uint8_t)(Data >> 2)) & USART_CR1_T8); 00421 00422 /* Write the transmit data bit [0:7] */ 00423 USARTx->DR = (uint8_t)(Data); 00424 } 00425 /** 00426 * @} 00427 */ 00428 00429 /** @defgroup USART_Group3 MultiProcessor Communication functions 00430 * @brief Multi-Processor Communication functions 00431 * 00432 @verbatim 00433 =============================================================================== 00434 Multi-Processor Communication functions 00435 =============================================================================== 00436 00437 This subsection provides a set of functions allowing to manage the USART 00438 multiprocessor communication. 00439 00440 For instance one of the USARTs can be the master, its TX output is connected to 00441 the RX input of the other USART. The others are slaves, their respective TX outputs 00442 are logically ANDed together and connected to the RX input of the master. 00443 00444 USART multiprocessor communication is possible through the following procedure: 00445 1. Program the Baud rate, Word length = 9 bits, Stop bits, Parity, Mode transmitter 00446 or Mode receiver and hardware flow control values using the USART_Init() 00447 function. 00448 2. Configures the USART address using the USART_SetAddress() function. 00449 3. Configures the wake up method (USART_WakeUp_IdleLine or USART_WakeUp_AddressMark) 00450 using USART_WakeUpConfig() function only for the slaves. 00451 4. Enable the USART using the USART_Cmd() function. 00452 5. Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd() function. 00453 00454 The USART Slave exit from mute mode when receiving the wake up condition. 00455 00456 @endverbatim 00457 * @{ 00458 */ 00459 00460 /** 00461 * @brief Determines if the USART is in mute mode or not. 00462 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00463 * @param NewState: The new state of the USART mode. 00464 * This parameter can be: ENABLE or DISABLE. 00465 * @retval None 00466 */ 00467 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState) 00468 { 00469 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00470 00471 if (NewState != DISABLE) 00472 { 00473 /* Enable the mute mode USART by setting the RWU bit in the CR2 register */ 00474 USARTx->CR2 |= USART_CR2_RWU; 00475 } 00476 else 00477 { 00478 /* Disable the mute mode USART by clearing the RWU bit in the CR1 register */ 00479 USARTx->CR2 &= ((uint8_t)~USART_CR2_RWU); 00480 } 00481 } 00482 00483 /** 00484 * @brief Sets the address of the USART node. 00485 * @param USARTx: Select the USARTx peripheral. 00486 * @param Address: Indicates the address of the USART node. 00487 * This parameter should be lower than 16 00488 * @retval None 00489 */ 00490 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address) 00491 { 00492 /* assert_param for USART_Address */ 00493 assert_param(IS_USART_ADDRESS(USART_Address)); 00494 00495 /* Clear the USART address */ 00496 USARTx->CR4 &= ((uint8_t)~USART_CR4_ADD); 00497 /* Set the USART address node */ 00498 USARTx->CR4 |= USART_Address; 00499 } 00500 00501 /** 00502 * @brief Selects the USART WakeUp method. 00503 * @param USART_WakeUp: Specifies the USART wakeup method. 00504 * This parameter can be one of the following values: 00505 * @arg USART_WakeUp_IdleLine: 0x01 Idle Line wake up 00506 * @arg USART_WakeUp_AddressMark: 0x02 Address Mark wake up 00507 * @retval None 00508 */ 00509 void USART_WakeUpConfig(USART_TypeDef* USARTx, USART_WakeUp_TypeDef USART_WakeUp) 00510 { 00511 assert_param(IS_USART_WAKEUP(USART_WakeUp)); 00512 00513 USARTx->CR1 &= ((uint8_t)~USART_CR1_WAKE); 00514 USARTx->CR1 |= (uint8_t)USART_WakeUp; 00515 } 00516 /** 00517 * @} 00518 */ 00519 00520 /** @defgroup USART_Group4 Halfduplex mode function 00521 * @brief Half-duplex mode function 00522 * 00523 @verbatim 00524 =============================================================================== 00525 Half-duplex mode function 00526 =============================================================================== 00527 00528 This subsection provides a function allowing to manage the USART 00529 Half-duplex communication. 00530 00531 The USART can be configured to follow a single-wire half-duplex protocol where 00532 the TX and RX lines are internally connected. 00533 00534 USART Half duplex communication is possible through the following procedure: 00535 1. Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter 00536 or Mode receiver and hardware flow control values using the USART_Init() 00537 function. 00538 2. Configures the USART address using the USART_SetAddress() function. 00539 3. Enable the USART using the USART_Cmd() function. 00540 4. Enable the half duplex mode using USART_HalfDuplexCmd() function. 00541 00542 Note: 00543 ---- 00544 1. The RX pin is no longer used 00545 2. In Half-duplex mode the following bits must be kept cleared: 00546 - CLKEN bits in the USART_CR3 register. 00547 - SCEN and IREN bits in the USART_CR5 register. 00548 00549 @endverbatim 00550 * @{ 00551 */ 00552 00553 /** 00554 * @brief Enables or disables the USART�s Half Duplex communication. 00555 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00556 * @param NewState new state of the USART Communication. 00557 * This parameter can be: ENABLE or DISABLE. 00558 * @retval None 00559 */ 00560 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState) 00561 { 00562 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00563 00564 if (NewState != DISABLE) 00565 { 00566 USARTx->CR5 |= USART_CR5_HDSEL; /**< USART Half Duplex Enable */ 00567 } 00568 else 00569 { 00570 USARTx->CR5 &= (uint8_t)~USART_CR5_HDSEL; /**< USART Half Duplex Disable */ 00571 } 00572 } 00573 00574 /** 00575 * @} 00576 */ 00577 00578 /** @defgroup USART_Group5 Smartcard mode functions 00579 * @brief Smartcard mode functions 00580 * 00581 @verbatim 00582 =============================================================================== 00583 Smartcard mode functions 00584 =============================================================================== 00585 00586 This subsection provides a set of functions allowing to manage the USART 00587 Smartcard communication. 00588 00589 The Smartcard interface is designed to support asynchronous protocol Smartcards as 00590 defined in the ISO 7816-3 standard. 00591 00592 The USART can provide a clock to the smartcard through the SCLK output. 00593 In smartcard mode, SCLK is not associated to the communication but is simply derived 00594 from the internal peripheral input clock through a 5-bit prescaler. 00595 00596 Smartcard communication is possible through the following procedure: 00597 1. Configures the Smartcard Prsecaler using the USART_SetPrescaler() function. 00598 2. Configures the Smartcard Guard Time using the USART_SetGuardTime() function. 00599 3. Program the USART clock using the USART_ClockInit() function as following: 00600 - USART Clock enabled 00601 - USART CPOL Low 00602 - USART CPHA on first edge 00603 - USART Last Bit Clock Enabled 00604 4. Program the Smartcard interface using the USART_Init() function as following: 00605 - Word Length = 9 Bits 00606 - 1.5 Stop Bit 00607 - Even parity 00608 - BaudRate = 12096 baud 00609 - Hardware flow control disabled (RTS and CTS signals) 00610 - Tx and Rx enabled 00611 5. Optionally you can enable the parity error interrupt using the USART_ITConfig() 00612 function 00613 6. Enable the USART using the USART_Cmd() function. 00614 7. Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function. 00615 8. Enable the Smartcard interface using the USART_SmartCardCmd() function. 00616 00617 Please refer to the ISO 7816-3 specification for more details. 00618 00619 Note: 00620 ----- 00621 1. It is also possible to choose 0.5 stop bit for receiving but it is recommended 00622 to use 1.5 stop bits for both transmitting and receiving to avoid switching 00623 between the two configurations. 00624 2. In smartcard mode, the following bits must be kept cleared: 00625 - HDSEL and IREN bits in the USART_CR5 register. 00626 00627 @endverbatim 00628 * @{ 00629 */ 00630 00631 /** 00632 * @brief Enables or disables the USART Smart Card mode. 00633 * @param USARTx: Select the USARTx peripheral. 00634 * @param NewState: new state of the Smart Card mode. 00635 * This parameter can be: ENABLE or DISABLE. 00636 * @retval None 00637 */ 00638 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState) 00639 { 00640 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00641 00642 if (NewState != DISABLE) 00643 { 00644 /* Enable the SC mode by setting the SCEN bit in the CR5 register */ 00645 USARTx->CR5 |= USART_CR5_SCEN; 00646 } 00647 else 00648 { 00649 /* Disable the SC mode by clearing the SCEN bit in the CR5 register */ 00650 USARTx->CR5 &= ((uint8_t)(~USART_CR5_SCEN)); 00651 } 00652 } 00653 00654 /** 00655 * @brief Enables or disables NACK transmission. 00656 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00657 * @param NewState: new state of the Smart Card mode. 00658 * This parameter can be: ENABLE or DISABLE. 00659 * @retval None 00660 */ 00661 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState) 00662 { 00663 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00664 00665 if (NewState != DISABLE) 00666 { 00667 /* Enable the NACK transmission by setting the NACK bit in the CR5 register */ 00668 USARTx->CR5 |= USART_CR5_NACK; 00669 } 00670 else 00671 { 00672 /* Disable the NACK transmission by clearing the NACK bit in the CR5 register */ 00673 USARTx->CR5 &= ((uint8_t)~(USART_CR5_NACK)); 00674 } 00675 } 00676 00677 /** 00678 * @brief Sets the specified USART guard time. 00679 * @note SmartCard Mode should be Enabled 00680 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00681 * @param USART_GuardTime: specifies the guard time. 00682 * @retval None 00683 */ 00684 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime) 00685 { 00686 /* Set the USART guard time */ 00687 USARTx->GTR = USART_GuardTime; 00688 } 00689 00690 /** 00691 * @} 00692 */ 00693 00694 /** @defgroup USART_Group6 IrDA mode functions 00695 * @brief IrDA mode functions 00696 * 00697 @verbatim 00698 =============================================================================== 00699 IrDA mode functions 00700 =============================================================================== 00701 00702 This subsection provides a set of functions allowing to manage the USART 00703 IrDA communication. 00704 00705 IrDA is a half duplex communication protocol. If the Transmitter is busy, any data 00706 on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver 00707 is busy, data on the TX from the USART to IrDA will not be encoded by IrDA. 00708 While receiving data, transmission should be avoided as the data to be transmitted 00709 could be corrupted. 00710 00711 IrDA communication is possible through the following procedure: 00712 1. Program the Baud rate, Word length = 8 bits, Stop bits, Parity, Transmitter/Receiver 00713 modes and hardware flow control values using the USART_Init() function. 00714 2. Enable the USART using the USART_Cmd() function. 00715 3. Configures the IrDA pulse width by configuring the prescaler using 00716 the USART_SetPrescaler() function. 00717 4. Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal mode 00718 using the USART_IrDAConfig() function. 00719 5. Enable the IrDA using the USART_IrDACmd() function. 00720 00721 Note: 00722 ----- 00723 1. A pulse of width less than two and greater than one PSC period(s) may or may 00724 not be rejected. 00725 2. The receiver set up time should be managed by software. The IrDA physical layer 00726 specification specifies a minimum of 10 ms delay between transmission and 00727 reception (IrDA is a half duplex protocol). 00728 3. In IrDA mode, the following bits must be kept cleared: 00729 - STOP and CLKEN bits in the USART_CR3 register. 00730 - SCEN and HDSEL bits in the USART_CR5 register. 00731 00732 @endverbatim 00733 * @{ 00734 */ 00735 00736 /** 00737 * @brief Configures the USART�s IrDA interface. 00738 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00739 * @param USART_IrDAMode specifies the IrDA mode. 00740 * This parameter can be one of the following values: 00741 * @arg USART_IrDAMode_Normal: IrDA Normal Mode 00742 * @arg USART_IrDAMode_LowPower: IrDA Low Power Mode 00743 * @retval None 00744 */ 00745 void USART_IrDAConfig(USART_TypeDef* USARTx, USART_IrDAMode_TypeDef USART_IrDAMode) 00746 { 00747 assert_param(IS_USART_IRDAMODE(USART_IrDAMode)); 00748 00749 if (USART_IrDAMode != USART_IrDAMode_Normal) 00750 { 00751 USARTx->CR5 |= USART_CR5_IRLP; 00752 } 00753 else 00754 { 00755 USARTx->CR5 &= ((uint8_t)~USART_CR5_IRLP); 00756 } 00757 } 00758 00759 /** 00760 * @brief Enables or disables the USART�s IrDA interface. 00761 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00762 * @param NewState new state of the IrDA mode. 00763 * This parameter can be: ENABLE or DISABLE. 00764 * @retval None 00765 */ 00766 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState) 00767 { 00768 00769 /* Check parameters */ 00770 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00771 00772 if (NewState != DISABLE) 00773 { 00774 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */ 00775 USARTx->CR5 |= USART_CR5_IREN; 00776 } 00777 else 00778 { 00779 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */ 00780 USARTx->CR5 &= ((uint8_t)~USART_CR5_IREN); 00781 } 00782 } 00783 00784 /** 00785 * @} 00786 */ 00787 00788 /** @defgroup USART_Group7 DMA transfers management functions 00789 * @brief DMA transfers management functions 00790 * 00791 @verbatim 00792 =============================================================================== 00793 DMA transfers management functions 00794 =============================================================================== 00795 00796 @endverbatim 00797 * @{ 00798 */ 00799 00800 00801 /** 00802 * @brief Enables or disables the USART DMA interface. 00803 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00804 * @param USART_DMAReq Specifies the USART DMA transfer request to be enabled or disabled. 00805 * This parameter can be one of the following values: 00806 * @arg USART_DMAReq_TX: Receive DMA request Enable 00807 * @arg USART_DMAReq_RX: Transmit DMA request Enable 00808 * @param NewState Indicates the new state of the USART DMA request. 00809 * This parameter can be: ENABLE or DISABLE. 00810 * @retval None 00811 */ 00812 void USART_DMACmd(USART_TypeDef* USARTx, USART_DMAReq_TypeDef USART_DMAReq, 00813 FunctionalState NewState) 00814 { 00815 /* Check the parameters */ 00816 assert_param(IS_USART_DMAREQ(USART_DMAReq)); 00817 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00818 00819 if (NewState != DISABLE) 00820 { 00821 /* Enable the DMA transfer for selected requests by setting the DMAT and/or 00822 DMAR bits in the USART CR5 register */ 00823 USARTx->CR5 |= (uint8_t) USART_DMAReq; 00824 } 00825 else 00826 { 00827 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or 00828 DMAR bits in the USART CR5 register */ 00829 USARTx->CR5 &= (uint8_t)~USART_DMAReq; 00830 } 00831 } 00832 00833 /** 00834 * @} 00835 */ 00836 00837 /** @defgroup USART_Group8 Interrupts and flags management functions 00838 * @brief Interrupts and flags management functions 00839 * 00840 @verbatim 00841 =============================================================================== 00842 Interrupts and flags management functions 00843 =============================================================================== 00844 00845 This subsection provides a set of functions allowing to configure the USART 00846 Interrupts sources, DMA channels requests and check or clear the flags or 00847 pending bits status. 00848 The user should identify which mode will be used in his application to manage 00849 the communication: Polling mode, Interrupt mode or DMA mode. 00850 00851 Polling Mode 00852 ============= 00853 In Polling Mode, the USART communication can be managed by 9 flags: 00854 1. USART_FLAG_TXE: to indicate the status of the transmit buffer register 00855 2. USART_FLAG_RXNE: to indicate the status of the receive buffer register 00856 3. USART_FLAG_TC: to indicate the status of the transmit operation 00857 4. USART_FLAG_IDLE: to indicate the status of the Idle Line 00858 5. USART_FLAG_SBK: to indicate the status of the Send Break characters 00859 6. USART_FLAG_NE: to indicate if a noise error occur 00860 7. USART_FLAG_FE: to indicate if a frame error occur 00861 8. USART_FLAG_PE: to indicate if a parity error occur 00862 9. USART_FLAG_ORE: to indicate if an Overrun error occur 00863 00864 In this Mode it is advised to use the following functions: 00865 - FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG); 00866 - void USART_ClearFlag(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG); 00867 00868 Interrupt Mode 00869 =============== 00870 In Interrupt Mode, the USART communication can be managed by 7 interrupt sources 00871 and 6 pending bits: 00872 00873 Pending Bits: 00874 ------------- 00875 1. USART_IT_TXE: to indicate the status of the transmit buffer register 00876 2. USART_IT_RXNE: to indicate the status of the receive buffer register 00877 3. USART_IT_TC: to indicate the status of the transmit operation 00878 4. USART_IT_IDLE: to indicate the status of the Idle Line 00879 5. USART_IT_PE: to indicate if a parity error occur 00880 6. USART_IT_OR: to indicate if an Noise flag, overrun error and framing error in 00881 multibuffer communication error occur 00882 00883 Interrupt Source: 00884 ----------------- 00885 1. USART_IT_TXE: specifies the interrupt source for the Tx buffer empty 00886 interrupt. 00887 2. USART_IT_RXNE: specifies the interrupt source for the Rx buffer not 00888 empty interrupt. 00889 3. USART_IT_TC: specifies the interrupt source for the Transmit complete 00890 interrupt. 00891 4. USART_IT_IDLE: specifies the interrupt source for the Idle Line interrupt. 00892 5. USART_IT_PE: specifies the interrupt source for the parity error interrupt. 00893 6. USART_IT_ERR: specifies the interrupt source for the errors interrupt. 00894 7. USART_IT_OR: specifies the interrupt source for the overrun error interrupt. 00895 00896 Note: Some parameters are coded in order to use them as interrupt source or 00897 ---- as pending bits. 00898 00899 In this Mode it is advised to use the following functions: 00900 - void USART_ITConfig(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT, FunctionalState 00901 NewState); 00902 - ITStatus USART_GetITStatus(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT); 00903 - void USART_ClearITPendingBit(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT); 00904 00905 DMA Mode 00906 ======== 00907 In DMA Mode, the USART communication can be managed by 2 DMA Channel requests: 00908 1. USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request 00909 2. USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request 00910 00911 In this Mode it is advised to use the following function: 00912 - void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState); 00913 00914 @endverbatim 00915 * @{ 00916 */ 00917 00918 /** 00919 * @brief Enables or disables the specified USART interrupts. 00920 * @param USARTx: where x can be 1 to select the specified USART peripheral. 00921 * @param USART_IT specifies the USART interrupt sources to be enabled or disabled. 00922 * This parameter can be one of the following values: 00923 * @arg USART_IT_TXE: Transmit Data Register empty interrupt 00924 * @arg USART_IT_TC: Transmission complete interrupt 00925 * @arg USART_IT_RXNE: Receive Data register not empty interrupt 00926 * @arg USART_IT_OR: Overrun error interrupt 00927 * @arg USART_IT_IDLE: Idle line detection interrupt 00928 * @arg USART_IT_ERR: Error interrupt 00929 * @param NewState new state of the specified USART interrupts. 00930 * This parameter can be: ENABLE or DISABLE. 00931 * @retval None 00932 */ 00933 void USART_ITConfig(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT, FunctionalState NewState) 00934 { 00935 uint8_t usartreg, itpos = 0x00; 00936 assert_param(IS_USART_CONFIG_IT(USART_IT)); 00937 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00938 00939 /* Get the USART register index */ 00940 usartreg = (uint8_t)((uint16_t)USART_IT >> 0x08); 00941 /* Get the USART IT index */ 00942 itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)USART_IT & (uint8_t)0x0F)); 00943 00944 if (NewState != DISABLE) 00945 { 00946 /**< Enable the Interrupt bits according to USART_IT mask */ 00947 if (usartreg == 0x01) 00948 { 00949 USARTx->CR1 |= itpos; 00950 } 00951 else if (usartreg == 0x05) 00952 { 00953 USARTx->CR5 |= itpos; 00954 } 00955 /*uartreg =0x02*/ 00956 else 00957 { 00958 USARTx->CR2 |= itpos; 00959 } 00960 } 00961 else 00962 { 00963 /**< Disable the interrupt bits according to USART_IT mask */ 00964 if (usartreg == 0x01) 00965 { 00966 USARTx->CR1 &= (uint8_t)(~itpos); 00967 } 00968 else if (usartreg == 0x05) 00969 { 00970 USARTx->CR5 &= (uint8_t)(~itpos); 00971 } 00972 /*uartreg =0x02*/ 00973 else 00974 { 00975 USARTx->CR2 &= (uint8_t)(~itpos); 00976 } 00977 } 00978 } 00979 00980 /** 00981 * @brief Checks whether the specified USART flag is set or not. 00982 * @param USARTx: Select the USARTx peripheral. 00983 * @param USART_FLAG specifies the flag to check. 00984 * This parameter can be one of the following values: 00985 * @arg USART_FLAG_TXE: Transmit Data Register empty 00986 * @arg USART_FLAG_TC: Transmission Complete 00987 * @arg USART_FLAG_RXNE: Read Data Register Not Empty 00988 * @arg USART_FLAG_IDLE: Idle line detected 00989 * @arg USART_FLAG_OR: OverRun error 00990 * @arg USART_FLAG_NF: Noise error 00991 * @arg USART_FLAG_FE: Framing Error 00992 * @arg USART_FLAG_PE: Parity Error 00993 * @arg USART_FLAG_SBK: Send Break characters 00994 * @retval FlagStatus (SET or RESET) 00995 */ 00996 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG) 00997 { 00998 FlagStatus status = RESET; 00999 01000 /* Check parameters */ 01001 assert_param(IS_USART_FLAG(USART_FLAG)); 01002 01003 if (USART_FLAG == USART_FLAG_SBK) 01004 { 01005 if ((USARTx->CR2 & (uint8_t)USART_FLAG) != (uint8_t)0x00) 01006 { 01007 /* USART_FLAG is set*/ 01008 status = SET; 01009 } 01010 else 01011 { 01012 /* USART_FLAG is reset*/ 01013 status = RESET; 01014 } 01015 } 01016 else 01017 { 01018 if ((USARTx->SR & (uint8_t)USART_FLAG) != (uint8_t)0x00) 01019 { 01020 /* USART_FLAG is set*/ 01021 status = SET; 01022 } 01023 else 01024 { 01025 /* USART_FLAG is reset*/ 01026 status = RESET; 01027 } 01028 } 01029 /* Return the USART_FLAG status*/ 01030 return status; 01031 } 01032 01033 /** 01034 * @brief Clears the USARTx's pending flags. 01035 * @param USARTx: where x can be 1 to select the specified USART peripheral. 01036 * @param USART_FLAG: specifies the flag to clear. 01037 * This parameter can be any combination of the following values: 01038 * @arg USART_FLAG_TC: Transmission Complete flag. 01039 * @arg USART_FLAG_RXNE: Receive data register not empty flag. 01040 * @note PE (Parity error), FE (Framing error), NE (Noise error), OR (OverRun error) 01041 * and IDLE (Idle line detected) flags are cleared by software sequence: a read 01042 * operation to USART_SR register (USART_GetFlagStatus())followed by a read 01043 * operation to USART_DR register(USART_ReceiveData8() or USART_ReceiveData9()). 01044 * @note RXNE flag can be also cleared by a read to the USART_DR register 01045 * (USART_ReceiveData8()or USART_ReceiveData9()). 01046 * @note TC flag can be also cleared by software sequence: a read operation to USART_SR 01047 * register (USART_GetFlagStatus()) followed by a write operation to USART_DR 01048 * register (USART_SendData8() or USART_SendData9()). 01049 * @note TXE flag is cleared only by a write to the USART_DR register 01050 * (USART_SendData8() or USART_SendData9()). 01051 * @note SBK flag is cleared during the stop bit of break. 01052 * @retval None 01053 */ 01054 void USART_ClearFlag(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG) 01055 { 01056 /* Check the parameters */ 01057 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG)); 01058 /*< Clear RXNE or TC flags */ 01059 USARTx->SR = (uint8_t)((uint16_t)~((uint16_t)USART_FLAG)); 01060 } 01061 01062 /** 01063 * @brief Checks whether the specified USART interrupt has occurred or not. 01064 * @param USARTx: where x can be 1 to select the specified USART peripheral. 01065 * @param USART_IT: Specifies the USART interrupt pending bit to check. 01066 * This parameter can be one of the following values: 01067 * @arg USART_IT_TXE: Transmit Data Register empty interrupt 01068 * @arg USART_IT_TC: Transmission complete interrupt 01069 * @arg USART_IT_RXNE: Receive Data register not empty interrupt 01070 * @arg USART_IT_IDLE: Idle line detection interrupt 01071 * @arg USART_IT_OR: OverRun Error interrupt 01072 * @arg USART_IT_PE: Parity Error interrupt 01073 * @arg USART_IT_FE: Frame Error interrupt 01074 * @arg USART_IT_NF: Noise Flag Error interrupt 01075 * @retval ITStatus The new state of USART_IT (SET or RESET). 01076 */ 01077 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT) 01078 { 01079 ITStatus pendingbitstatus = RESET; 01080 uint8_t temp = 0; 01081 uint8_t itpos = 0; 01082 uint8_t itmask1 = 0; 01083 uint8_t itmask2 = 0; 01084 uint8_t enablestatus = 0; 01085 01086 /* Check parameters */ 01087 assert_param(IS_USART_GET_IT(USART_IT)); 01088 01089 /* Get the USART IT index */ 01090 itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)USART_IT & (uint8_t)0x0F)); 01091 /* Get the USART IT index */ 01092 itmask1 = (uint8_t)((uint8_t)USART_IT >> (uint8_t)4); 01093 /* Set the IT mask*/ 01094 itmask2 = (uint8_t)((uint8_t)1 << itmask1); 01095 01096 /* Check the status of the specified USART pending bit*/ 01097 if (USART_IT == USART_IT_PE) 01098 { 01099 /* Get the USART_IT enable bit status*/ 01100 enablestatus = (uint8_t)((uint8_t)USARTx->CR1 & itmask2); 01101 /* Check the status of the specified USART interrupt*/ 01102 01103 if (((USARTx->SR & itpos) != (uint8_t)0x00) && enablestatus) 01104 { 01105 /* Interrupt occurred*/ 01106 pendingbitstatus = SET; 01107 } 01108 else 01109 { 01110 /* Interrupt not occurred*/ 01111 pendingbitstatus = RESET; 01112 } 01113 } 01114 01115 else if (USART_IT == USART_IT_OR) 01116 { 01117 /* Get the USART_IT enable bit status*/ 01118 enablestatus = (uint8_t)((uint8_t)USARTx->CR2 & itmask2); 01119 /* Check the status of the specified USART interrupt*/ 01120 01121 temp = (uint8_t)(USARTx->CR5 & USART_CR5_EIE); 01122 01123 if (( (USARTx->SR & itpos) != 0x00) && ((enablestatus || temp))) 01124 { 01125 /* Interrupt occurred*/ 01126 pendingbitstatus = SET; 01127 } 01128 else 01129 { 01130 /* Interrupt not occurred*/ 01131 pendingbitstatus = RESET; 01132 } 01133 } 01134 01135 else 01136 { 01137 /* Get the USART_IT enable bit status*/ 01138 enablestatus = (uint8_t)((uint8_t)USARTx->CR2 & itmask2); 01139 /* Check the status of the specified USART interrupt*/ 01140 if (((USARTx->SR & itpos) != (uint8_t)0x00) && enablestatus) 01141 { 01142 /* Interrupt occurred*/ 01143 pendingbitstatus = SET; 01144 } 01145 else 01146 { 01147 /* Interrupt not occurred*/ 01148 pendingbitstatus = RESET; 01149 } 01150 } 01151 01152 /* Return the USART_IT status*/ 01153 return pendingbitstatus; 01154 } 01155 01156 /** 01157 * @brief Clears the USARTx�s interrupt pending bits. 01158 * @param USARTx: where x can be 1 to select the specified USART peripheral. 01159 * @param USART_IT: specifies the interrupt pending bit to clear. 01160 * This parameter can be one of the following values: 01161 * @arg USART_IT_RXNE: Receive Data register not empty interrupt. 01162 * @arg USART_IT_TC: Transmission complete interrupt. 01163 * @note PE (Parity error), FE (Framing error), NE (Noise error), 01164 * OR (OverRun error) and IDLE (Idle line detected) pending bits are 01165 * cleared by software sequence: a read operation to USART_SR register 01166 * (USART_GetITStatus()) followed by a read operation to USART_DR 01167 * register (USART_ReceiveData8() or USART_ReceiveData9()). 01168 * @note RXNE pending bit can be also cleared by a read to the USART_DR register 01169 * (USART_ReceiveData8() or USART_ReceiveData9()). 01170 * @note TC (Transmit complete) pending bit can be also cleared by software 01171 * sequence: a read operation to USART_SR register (USART_GetITStatus()) 01172 * followed by a write operation to USART_DR register (USART_SendData8() 01173 * or USART_SendData9()). 01174 * @note TXE pending bit is cleared only by a write to the USART_DR register 01175 * (USART_SendData8() or USART_SendData9()). 01176 * @retval None 01177 */ 01178 void USART_ClearITPendingBit(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT) 01179 { 01180 uint8_t bitpos = 0x00, itmask = 0x00; 01181 assert_param(IS_USART_CLEAR_IT(USART_IT)); 01182 bitpos = (uint8_t)( (uint8_t)((uint8_t)USART_IT & (uint8_t)0xF0) >> 0x04); 01183 itmask = (uint8_t)( (uint8_t)0x01 << bitpos); 01184 /*< Clear RXNE or TC pending bit */ 01185 USARTx->SR = (uint8_t)~itmask; 01186 } 01187 01188 01189 /** 01190 * @} 01191 */ 01192 01193 /** 01194 * @} 01195 */ 01196 01197 /** 01198 * @} 01199 */ 01200 01201 /** 01202 * @} 01203 */ 01204 01205 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/