STM32F10x Standard Peripherals Library  3.5.0
/opt/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c
Go to the documentation of this file.
00001 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f10x_usart.h"
00024 #include "stm32f10x_rcc.h"
00025 
00047 #define CR1_UE_Set                ((uint16_t)0x2000)  
00048 #define CR1_UE_Reset              ((uint16_t)0xDFFF)  
00050 #define CR1_WAKE_Mask             ((uint16_t)0xF7FF)  
00052 #define CR1_RWU_Set               ((uint16_t)0x0002)  
00053 #define CR1_RWU_Reset             ((uint16_t)0xFFFD)  
00054 #define CR1_SBK_Set               ((uint16_t)0x0001)  
00055 #define CR1_CLEAR_Mask            ((uint16_t)0xE9F3)  
00056 #define CR2_Address_Mask          ((uint16_t)0xFFF0)  
00058 #define CR2_LINEN_Set              ((uint16_t)0x4000)  
00059 #define CR2_LINEN_Reset            ((uint16_t)0xBFFF)  
00061 #define CR2_LBDL_Mask             ((uint16_t)0xFFDF)  
00062 #define CR2_STOP_CLEAR_Mask       ((uint16_t)0xCFFF)  
00063 #define CR2_CLOCK_CLEAR_Mask      ((uint16_t)0xF0FF)  
00065 #define CR3_SCEN_Set              ((uint16_t)0x0020)  
00066 #define CR3_SCEN_Reset            ((uint16_t)0xFFDF)  
00068 #define CR3_NACK_Set              ((uint16_t)0x0010)  
00069 #define CR3_NACK_Reset            ((uint16_t)0xFFEF)  
00071 #define CR3_HDSEL_Set             ((uint16_t)0x0008)  
00072 #define CR3_HDSEL_Reset           ((uint16_t)0xFFF7)  
00074 #define CR3_IRLP_Mask             ((uint16_t)0xFFFB)  
00075 #define CR3_CLEAR_Mask            ((uint16_t)0xFCFF)  
00077 #define CR3_IREN_Set              ((uint16_t)0x0002)  
00078 #define CR3_IREN_Reset            ((uint16_t)0xFFFD)  
00079 #define GTPR_LSB_Mask             ((uint16_t)0x00FF)  
00080 #define GTPR_MSB_Mask             ((uint16_t)0xFF00)  
00081 #define IT_Mask                   ((uint16_t)0x001F)  
00083 /* USART OverSampling-8 Mask */
00084 #define CR1_OVER8_Set             ((u16)0x8000)  /* USART OVER8 mode Enable Mask */
00085 #define CR1_OVER8_Reset           ((u16)0x7FFF)  /* USART OVER8 mode Disable Mask */
00086 
00087 /* USART One Bit Sampling Mask */
00088 #define CR3_ONEBITE_Set           ((u16)0x0800)  /* USART ONEBITE mode Enable Mask */
00089 #define CR3_ONEBITE_Reset         ((u16)0xF7FF)  /* USART ONEBITE mode Disable Mask */
00090 
00130 void USART_DeInit(USART_TypeDef* USARTx)
00131 {
00132   /* Check the parameters */
00133   assert_param(IS_USART_ALL_PERIPH(USARTx));
00134 
00135   if (USARTx == USART1)
00136   {
00137     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
00138     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
00139   }
00140   else if (USARTx == USART2)
00141   {
00142     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
00143     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
00144   }
00145   else if (USARTx == USART3)
00146   {
00147     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
00148     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
00149   }    
00150   else if (USARTx == UART4)
00151   {
00152     RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
00153     RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
00154   }    
00155   else
00156   {
00157     if (USARTx == UART5)
00158     { 
00159       RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
00160       RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
00161     }
00162   }
00163 }
00164 
00176 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
00177 {
00178   uint32_t tmpreg = 0x00, apbclock = 0x00;
00179   uint32_t integerdivider = 0x00;
00180   uint32_t fractionaldivider = 0x00;
00181   uint32_t usartxbase = 0;
00182   RCC_ClocksTypeDef RCC_ClocksStatus;
00183   /* Check the parameters */
00184   assert_param(IS_USART_ALL_PERIPH(USARTx));
00185   assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));  
00186   assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
00187   assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
00188   assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
00189   assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
00190   assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
00191   /* The hardware flow control is available only for USART1, USART2 and USART3 */
00192   if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
00193   {
00194     assert_param(IS_USART_123_PERIPH(USARTx));
00195   }
00196 
00197   usartxbase = (uint32_t)USARTx;
00198 
00199 /*---------------------------- USART CR2 Configuration -----------------------*/
00200   tmpreg = USARTx->CR2;
00201   /* Clear STOP[13:12] bits */
00202   tmpreg &= CR2_STOP_CLEAR_Mask;
00203   /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
00204   /* Set STOP[13:12] bits according to USART_StopBits value */
00205   tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
00206   
00207   /* Write to USART CR2 */
00208   USARTx->CR2 = (uint16_t)tmpreg;
00209 
00210 /*---------------------------- USART CR1 Configuration -----------------------*/
00211   tmpreg = USARTx->CR1;
00212   /* Clear M, PCE, PS, TE and RE bits */
00213   tmpreg &= CR1_CLEAR_Mask;
00214   /* Configure the USART Word Length, Parity and mode ----------------------- */
00215   /* Set the M bits according to USART_WordLength value */
00216   /* Set PCE and PS bits according to USART_Parity value */
00217   /* Set TE and RE bits according to USART_Mode value */
00218   tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
00219             USART_InitStruct->USART_Mode;
00220   /* Write to USART CR1 */
00221   USARTx->CR1 = (uint16_t)tmpreg;
00222 
00223 /*---------------------------- USART CR3 Configuration -----------------------*/  
00224   tmpreg = USARTx->CR3;
00225   /* Clear CTSE and RTSE bits */
00226   tmpreg &= CR3_CLEAR_Mask;
00227   /* Configure the USART HFC -------------------------------------------------*/
00228   /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
00229   tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
00230   /* Write to USART CR3 */
00231   USARTx->CR3 = (uint16_t)tmpreg;
00232 
00233 /*---------------------------- USART BRR Configuration -----------------------*/
00234   /* Configure the USART Baud Rate -------------------------------------------*/
00235   RCC_GetClocksFreq(&RCC_ClocksStatus);
00236   if (usartxbase == USART1_BASE)
00237   {
00238     apbclock = RCC_ClocksStatus.PCLK2_Frequency;
00239   }
00240   else
00241   {
00242     apbclock = RCC_ClocksStatus.PCLK1_Frequency;
00243   }
00244   
00245   /* Determine the integer part */
00246   if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
00247   {
00248     /* Integer part computing in case Oversampling mode is 8 Samples */
00249     integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));    
00250   }
00251   else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
00252   {
00253     /* Integer part computing in case Oversampling mode is 16 Samples */
00254     integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));    
00255   }
00256   tmpreg = (integerdivider / 100) << 4;
00257 
00258   /* Determine the fractional part */
00259   fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
00260 
00261   /* Implement the fractional part in the register */
00262   if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
00263   {
00264     tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
00265   }
00266   else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
00267   {
00268     tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
00269   }
00270   
00271   /* Write to USART BRR */
00272   USARTx->BRR = (uint16_t)tmpreg;
00273 }
00274 
00281 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
00282 {
00283   /* USART_InitStruct members default value */
00284   USART_InitStruct->USART_BaudRate = 9600;
00285   USART_InitStruct->USART_WordLength = USART_WordLength_8b;
00286   USART_InitStruct->USART_StopBits = USART_StopBits_1;
00287   USART_InitStruct->USART_Parity = USART_Parity_No ;
00288   USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00289   USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;  
00290 }
00291 
00302 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
00303 {
00304   uint32_t tmpreg = 0x00;
00305   /* Check the parameters */
00306   assert_param(IS_USART_123_PERIPH(USARTx));
00307   assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
00308   assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
00309   assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
00310   assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
00311   
00312 /*---------------------------- USART CR2 Configuration -----------------------*/
00313   tmpreg = USARTx->CR2;
00314   /* Clear CLKEN, CPOL, CPHA and LBCL bits */
00315   tmpreg &= CR2_CLOCK_CLEAR_Mask;
00316   /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
00317   /* Set CLKEN bit according to USART_Clock value */
00318   /* Set CPOL bit according to USART_CPOL value */
00319   /* Set CPHA bit according to USART_CPHA value */
00320   /* Set LBCL bit according to USART_LastBit value */
00321   tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL | 
00322                  USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
00323   /* Write to USART CR2 */
00324   USARTx->CR2 = (uint16_t)tmpreg;
00325 }
00326 
00333 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
00334 {
00335   /* USART_ClockInitStruct members default value */
00336   USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
00337   USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
00338   USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
00339   USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
00340 }
00341 
00351 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
00352 {
00353   /* Check the parameters */
00354   assert_param(IS_USART_ALL_PERIPH(USARTx));
00355   assert_param(IS_FUNCTIONAL_STATE(NewState));
00356   
00357   if (NewState != DISABLE)
00358   {
00359     /* Enable the selected USART by setting the UE bit in the CR1 register */
00360     USARTx->CR1 |= CR1_UE_Set;
00361   }
00362   else
00363   {
00364     /* Disable the selected USART by clearing the UE bit in the CR1 register */
00365     USARTx->CR1 &= CR1_UE_Reset;
00366   }
00367 }
00368 
00388 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
00389 {
00390   uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
00391   uint32_t usartxbase = 0x00;
00392   /* Check the parameters */
00393   assert_param(IS_USART_ALL_PERIPH(USARTx));
00394   assert_param(IS_USART_CONFIG_IT(USART_IT));
00395   assert_param(IS_FUNCTIONAL_STATE(NewState));
00396   /* The CTS interrupt is not available for UART4 and UART5 */
00397   if (USART_IT == USART_IT_CTS)
00398   {
00399     assert_param(IS_USART_123_PERIPH(USARTx));
00400   }   
00401   
00402   usartxbase = (uint32_t)USARTx;
00403 
00404   /* Get the USART register index */
00405   usartreg = (((uint8_t)USART_IT) >> 0x05);
00406 
00407   /* Get the interrupt position */
00408   itpos = USART_IT & IT_Mask;
00409   itmask = (((uint32_t)0x01) << itpos);
00410     
00411   if (usartreg == 0x01) /* The IT is in CR1 register */
00412   {
00413     usartxbase += 0x0C;
00414   }
00415   else if (usartreg == 0x02) /* The IT is in CR2 register */
00416   {
00417     usartxbase += 0x10;
00418   }
00419   else /* The IT is in CR3 register */
00420   {
00421     usartxbase += 0x14; 
00422   }
00423   if (NewState != DISABLE)
00424   {
00425     *(__IO uint32_t*)usartxbase  |= itmask;
00426   }
00427   else
00428   {
00429     *(__IO uint32_t*)usartxbase &= ~itmask;
00430   }
00431 }
00432 
00448 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
00449 {
00450   /* Check the parameters */
00451   assert_param(IS_USART_ALL_PERIPH(USARTx));
00452   assert_param(IS_USART_DMAREQ(USART_DMAReq));  
00453   assert_param(IS_FUNCTIONAL_STATE(NewState)); 
00454   if (NewState != DISABLE)
00455   {
00456     /* Enable the DMA transfer for selected requests by setting the DMAT and/or
00457        DMAR bits in the USART CR3 register */
00458     USARTx->CR3 |= USART_DMAReq;
00459   }
00460   else
00461   {
00462     /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
00463        DMAR bits in the USART CR3 register */
00464     USARTx->CR3 &= (uint16_t)~USART_DMAReq;
00465   }
00466 }
00467 
00476 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
00477 {
00478   /* Check the parameters */
00479   assert_param(IS_USART_ALL_PERIPH(USARTx));
00480   assert_param(IS_USART_ADDRESS(USART_Address)); 
00481     
00482   /* Clear the USART address */
00483   USARTx->CR2 &= CR2_Address_Mask;
00484   /* Set the USART address node */
00485   USARTx->CR2 |= USART_Address;
00486 }
00487 
00499 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
00500 {
00501   /* Check the parameters */
00502   assert_param(IS_USART_ALL_PERIPH(USARTx));
00503   assert_param(IS_USART_WAKEUP(USART_WakeUp));
00504   
00505   USARTx->CR1 &= CR1_WAKE_Mask;
00506   USARTx->CR1 |= USART_WakeUp;
00507 }
00508 
00518 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
00519 {
00520   /* Check the parameters */
00521   assert_param(IS_USART_ALL_PERIPH(USARTx));
00522   assert_param(IS_FUNCTIONAL_STATE(NewState)); 
00523   
00524   if (NewState != DISABLE)
00525   {
00526     /* Enable the USART mute mode  by setting the RWU bit in the CR1 register */
00527     USARTx->CR1 |= CR1_RWU_Set;
00528   }
00529   else
00530   {
00531     /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
00532     USARTx->CR1 &= CR1_RWU_Reset;
00533   }
00534 }
00535 
00547 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
00548 {
00549   /* Check the parameters */
00550   assert_param(IS_USART_ALL_PERIPH(USARTx));
00551   assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
00552   
00553   USARTx->CR2 &= CR2_LBDL_Mask;
00554   USARTx->CR2 |= USART_LINBreakDetectLength;  
00555 }
00556 
00566 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
00567 {
00568   /* Check the parameters */
00569   assert_param(IS_USART_ALL_PERIPH(USARTx));
00570   assert_param(IS_FUNCTIONAL_STATE(NewState));
00571   
00572   if (NewState != DISABLE)
00573   {
00574     /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
00575     USARTx->CR2 |= CR2_LINEN_Set;
00576   }
00577   else
00578   {
00579     /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
00580     USARTx->CR2 &= CR2_LINEN_Reset;
00581   }
00582 }
00583 
00592 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
00593 {
00594   /* Check the parameters */
00595   assert_param(IS_USART_ALL_PERIPH(USARTx));
00596   assert_param(IS_USART_DATA(Data)); 
00597     
00598   /* Transmit Data */
00599   USARTx->DR = (Data & (uint16_t)0x01FF);
00600 }
00601 
00609 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
00610 {
00611   /* Check the parameters */
00612   assert_param(IS_USART_ALL_PERIPH(USARTx));
00613   
00614   /* Receive Data */
00615   return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
00616 }
00617 
00625 void USART_SendBreak(USART_TypeDef* USARTx)
00626 {
00627   /* Check the parameters */
00628   assert_param(IS_USART_ALL_PERIPH(USARTx));
00629   
00630   /* Send break characters */
00631   USARTx->CR1 |= CR1_SBK_Set;
00632 }
00633 
00641 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
00642 {    
00643   /* Check the parameters */
00644   assert_param(IS_USART_123_PERIPH(USARTx));
00645   
00646   /* Clear the USART Guard time */
00647   USARTx->GTPR &= GTPR_LSB_Mask;
00648   /* Set the USART guard time */
00649   USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
00650 }
00651 
00661 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
00662 { 
00663   /* Check the parameters */
00664   assert_param(IS_USART_ALL_PERIPH(USARTx));
00665   
00666   /* Clear the USART prescaler */
00667   USARTx->GTPR &= GTPR_MSB_Mask;
00668   /* Set the USART prescaler */
00669   USARTx->GTPR |= USART_Prescaler;
00670 }
00671 
00680 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
00681 {
00682   /* Check the parameters */
00683   assert_param(IS_USART_123_PERIPH(USARTx));
00684   assert_param(IS_FUNCTIONAL_STATE(NewState));
00685   if (NewState != DISABLE)
00686   {
00687     /* Enable the SC mode by setting the SCEN bit in the CR3 register */
00688     USARTx->CR3 |= CR3_SCEN_Set;
00689   }
00690   else
00691   {
00692     /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
00693     USARTx->CR3 &= CR3_SCEN_Reset;
00694   }
00695 }
00696 
00705 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
00706 {
00707   /* Check the parameters */
00708   assert_param(IS_USART_123_PERIPH(USARTx));  
00709   assert_param(IS_FUNCTIONAL_STATE(NewState));
00710   if (NewState != DISABLE)
00711   {
00712     /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
00713     USARTx->CR3 |= CR3_NACK_Set;
00714   }
00715   else
00716   {
00717     /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
00718     USARTx->CR3 &= CR3_NACK_Reset;
00719   }
00720 }
00721 
00731 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
00732 {
00733   /* Check the parameters */
00734   assert_param(IS_USART_ALL_PERIPH(USARTx));
00735   assert_param(IS_FUNCTIONAL_STATE(NewState));
00736   
00737   if (NewState != DISABLE)
00738   {
00739     /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
00740     USARTx->CR3 |= CR3_HDSEL_Set;
00741   }
00742   else
00743   {
00744     /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
00745     USARTx->CR3 &= CR3_HDSEL_Reset;
00746   }
00747 }
00748 
00749 
00762 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
00763 {
00764   /* Check the parameters */
00765   assert_param(IS_USART_ALL_PERIPH(USARTx));
00766   assert_param(IS_FUNCTIONAL_STATE(NewState));
00767   
00768   if (NewState != DISABLE)
00769   {
00770     /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
00771     USARTx->CR1 |= CR1_OVER8_Set;
00772   }
00773   else
00774   {
00775     /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
00776     USARTx->CR1 &= CR1_OVER8_Reset;
00777   }
00778 }
00779 
00789 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
00790 {
00791   /* Check the parameters */
00792   assert_param(IS_USART_ALL_PERIPH(USARTx));
00793   assert_param(IS_FUNCTIONAL_STATE(NewState));
00794   
00795   if (NewState != DISABLE)
00796   {
00797     /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
00798     USARTx->CR3 |= CR3_ONEBITE_Set;
00799   }
00800   else
00801   {
00802     /* Disable tthe one bit method by clearing the ONEBITE bit in the CR3 register */
00803     USARTx->CR3 &= CR3_ONEBITE_Reset;
00804   }
00805 }
00806 
00818 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
00819 {
00820   /* Check the parameters */
00821   assert_param(IS_USART_ALL_PERIPH(USARTx));
00822   assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
00823     
00824   USARTx->CR3 &= CR3_IRLP_Mask;
00825   USARTx->CR3 |= USART_IrDAMode;
00826 }
00827 
00837 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
00838 {
00839   /* Check the parameters */
00840   assert_param(IS_USART_ALL_PERIPH(USARTx));
00841   assert_param(IS_FUNCTIONAL_STATE(NewState));
00842     
00843   if (NewState != DISABLE)
00844   {
00845     /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
00846     USARTx->CR3 |= CR3_IREN_Set;
00847   }
00848   else
00849   {
00850     /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
00851     USARTx->CR3 &= CR3_IREN_Reset;
00852   }
00853 }
00854 
00874 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
00875 {
00876   FlagStatus bitstatus = RESET;
00877   /* Check the parameters */
00878   assert_param(IS_USART_ALL_PERIPH(USARTx));
00879   assert_param(IS_USART_FLAG(USART_FLAG));
00880   /* The CTS flag is not available for UART4 and UART5 */
00881   if (USART_FLAG == USART_FLAG_CTS)
00882   {
00883     assert_param(IS_USART_123_PERIPH(USARTx));
00884   }  
00885   
00886   if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
00887   {
00888     bitstatus = SET;
00889   }
00890   else
00891   {
00892     bitstatus = RESET;
00893   }
00894   return bitstatus;
00895 }
00896 
00923 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
00924 {
00925   /* Check the parameters */
00926   assert_param(IS_USART_ALL_PERIPH(USARTx));
00927   assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
00928   /* The CTS flag is not available for UART4 and UART5 */
00929   if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
00930   {
00931     assert_param(IS_USART_123_PERIPH(USARTx));
00932   } 
00933    
00934   USARTx->SR = (uint16_t)~USART_FLAG;
00935 }
00936 
00956 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
00957 {
00958   uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
00959   ITStatus bitstatus = RESET;
00960   /* Check the parameters */
00961   assert_param(IS_USART_ALL_PERIPH(USARTx));
00962   assert_param(IS_USART_GET_IT(USART_IT));
00963   /* The CTS interrupt is not available for UART4 and UART5 */ 
00964   if (USART_IT == USART_IT_CTS)
00965   {
00966     assert_param(IS_USART_123_PERIPH(USARTx));
00967   }   
00968   
00969   /* Get the USART register index */
00970   usartreg = (((uint8_t)USART_IT) >> 0x05);
00971   /* Get the interrupt position */
00972   itmask = USART_IT & IT_Mask;
00973   itmask = (uint32_t)0x01 << itmask;
00974   
00975   if (usartreg == 0x01) /* The IT  is in CR1 register */
00976   {
00977     itmask &= USARTx->CR1;
00978   }
00979   else if (usartreg == 0x02) /* The IT  is in CR2 register */
00980   {
00981     itmask &= USARTx->CR2;
00982   }
00983   else /* The IT  is in CR3 register */
00984   {
00985     itmask &= USARTx->CR3;
00986   }
00987   
00988   bitpos = USART_IT >> 0x08;
00989   bitpos = (uint32_t)0x01 << bitpos;
00990   bitpos &= USARTx->SR;
00991   if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
00992   {
00993     bitstatus = SET;
00994   }
00995   else
00996   {
00997     bitstatus = RESET;
00998   }
00999   
01000   return bitstatus;  
01001 }
01002 
01030 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
01031 {
01032   uint16_t bitpos = 0x00, itmask = 0x00;
01033   /* Check the parameters */
01034   assert_param(IS_USART_ALL_PERIPH(USARTx));
01035   assert_param(IS_USART_CLEAR_IT(USART_IT));
01036   /* The CTS interrupt is not available for UART4 and UART5 */
01037   if (USART_IT == USART_IT_CTS)
01038   {
01039     assert_param(IS_USART_123_PERIPH(USARTx));
01040   }   
01041   
01042   bitpos = USART_IT >> 0x08;
01043   itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
01044   USARTx->SR = (uint16_t)~itmask;
01045 }
01058 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/