DSO138_SourceCodes_v037.rar
[DSO138] / Libraries / STM32F10x_StdPeriph_Driver / src / stm32f10x_usart.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f10x_usart.c\r
4   * @author  MCD Application Team\r
5   * @version V3.3.0\r
6   * @date    04/16/2010\r
7   * @brief   This file provides all the USART firmware functions.\r
8   ******************************************************************************\r
9   * @copy\r
10   *\r
11   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
12   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
13   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
14   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
15   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
16   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
17   *\r
18   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
19   */ \r
20 \r
21 /* Includes ------------------------------------------------------------------*/\r
22 #include "stm32f10x_usart.h"\r
23 #include "stm32f10x_rcc.h"\r
24 \r
25 /** @addtogroup STM32F10x_StdPeriph_Driver\r
26   * @{\r
27   */\r
28 \r
29 /** @defgroup USART \r
30   * @brief USART driver modules\r
31   * @{\r
32   */\r
33 \r
34 /** @defgroup USART_Private_TypesDefinitions\r
35   * @{\r
36   */\r
37 \r
38 /**\r
39   * @}\r
40   */\r
41 \r
42 /** @defgroup USART_Private_Defines\r
43   * @{\r
44   */\r
45 \r
46 #define CR1_UE_Set                ((uint16_t)0x2000)  /*!< USART Enable Mask */\r
47 #define CR1_UE_Reset              ((uint16_t)0xDFFF)  /*!< USART Disable Mask */\r
48 \r
49 #define CR1_WAKE_Mask             ((uint16_t)0xF7FF)  /*!< USART WakeUp Method Mask */\r
50 \r
51 #define CR1_RWU_Set               ((uint16_t)0x0002)  /*!< USART mute mode Enable Mask */\r
52 #define CR1_RWU_Reset             ((uint16_t)0xFFFD)  /*!< USART mute mode Enable Mask */\r
53 #define CR1_SBK_Set               ((uint16_t)0x0001)  /*!< USART Break Character send Mask */\r
54 #define CR1_CLEAR_Mask            ((uint16_t)0xE9F3)  /*!< USART CR1 Mask */\r
55 #define CR2_Address_Mask          ((uint16_t)0xFFF0)  /*!< USART address Mask */\r
56 \r
57 #define CR2_LINEN_Set              ((uint16_t)0x4000)  /*!< USART LIN Enable Mask */\r
58 #define CR2_LINEN_Reset            ((uint16_t)0xBFFF)  /*!< USART LIN Disable Mask */\r
59 \r
60 #define CR2_LBDL_Mask             ((uint16_t)0xFFDF)  /*!< USART LIN Break detection Mask */\r
61 #define CR2_STOP_CLEAR_Mask       ((uint16_t)0xCFFF)  /*!< USART CR2 STOP Bits Mask */\r
62 #define CR2_CLOCK_CLEAR_Mask      ((uint16_t)0xF0FF)  /*!< USART CR2 Clock Mask */\r
63 \r
64 #define CR3_SCEN_Set              ((uint16_t)0x0020)  /*!< USART SC Enable Mask */\r
65 #define CR3_SCEN_Reset            ((uint16_t)0xFFDF)  /*!< USART SC Disable Mask */\r
66 \r
67 #define CR3_NACK_Set              ((uint16_t)0x0010)  /*!< USART SC NACK Enable Mask */\r
68 #define CR3_NACK_Reset            ((uint16_t)0xFFEF)  /*!< USART SC NACK Disable Mask */\r
69 \r
70 #define CR3_HDSEL_Set             ((uint16_t)0x0008)  /*!< USART Half-Duplex Enable Mask */\r
71 #define CR3_HDSEL_Reset           ((uint16_t)0xFFF7)  /*!< USART Half-Duplex Disable Mask */\r
72 \r
73 #define CR3_IRLP_Mask             ((uint16_t)0xFFFB)  /*!< USART IrDA LowPower mode Mask */\r
74 #define CR3_CLEAR_Mask            ((uint16_t)0xFCFF)  /*!< USART CR3 Mask */\r
75 \r
76 #define CR3_IREN_Set              ((uint16_t)0x0002)  /*!< USART IrDA Enable Mask */\r
77 #define CR3_IREN_Reset            ((uint16_t)0xFFFD)  /*!< USART IrDA Disable Mask */\r
78 #define GTPR_LSB_Mask             ((uint16_t)0x00FF)  /*!< Guard Time Register LSB Mask */\r
79 #define GTPR_MSB_Mask             ((uint16_t)0xFF00)  /*!< Guard Time Register MSB Mask */\r
80 #define IT_Mask                   ((uint16_t)0x001F)  /*!< USART Interrupt Mask */\r
81 \r
82 /* USART OverSampling-8 Mask */\r
83 #define CR1_OVER8_Set             ((u16)0x8000)  /* USART OVER8 mode Enable Mask */\r
84 #define CR1_OVER8_Reset           ((u16)0x7FFF)  /* USART OVER8 mode Disable Mask */\r
85 \r
86 /* USART One Bit Sampling Mask */\r
87 #define CR3_ONEBITE_Set           ((u16)0x0800)  /* USART ONEBITE mode Enable Mask */\r
88 #define CR3_ONEBITE_Reset         ((u16)0xF7FF)  /* USART ONEBITE mode Disable Mask */\r
89 \r
90 /**\r
91   * @}\r
92   */\r
93 \r
94 /** @defgroup USART_Private_Macros\r
95   * @{\r
96   */\r
97 \r
98 /**\r
99   * @}\r
100   */\r
101 \r
102 /** @defgroup USART_Private_Variables\r
103   * @{\r
104   */\r
105 \r
106 /**\r
107   * @}\r
108   */\r
109 \r
110 /** @defgroup USART_Private_FunctionPrototypes\r
111   * @{\r
112   */\r
113 \r
114 /**\r
115   * @}\r
116   */\r
117 \r
118 /** @defgroup USART_Private_Functions\r
119   * @{\r
120   */\r
121 \r
122 /**\r
123   * @brief  Deinitializes the USARTx peripheral registers to their default reset values.\r
124   * @param  USARTx: Select the USART or the UART peripheral. \r
125   *   This parameter can be one of the following values: USART1, USART2, USART3, UART4 or UART5.\r
126   * @retval None\r
127   */\r
128 void USART_DeInit(USART_TypeDef* USARTx)\r
129 {\r
130   /* Check the parameters */\r
131   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
132 \r
133   if (USARTx == USART1)\r
134   {\r
135     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);\r
136     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);\r
137   }\r
138   else if (USARTx == USART2)\r
139   {\r
140     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);\r
141     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);\r
142   }\r
143   else if (USARTx == USART3)\r
144   {\r
145     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);\r
146     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);\r
147   }    \r
148   else if (USARTx == UART4)\r
149   {\r
150     RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);\r
151     RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);\r
152   }    \r
153   else\r
154   {\r
155     if (USARTx == UART5)\r
156     { \r
157       RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);\r
158       RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);\r
159     }\r
160   }\r
161 }\r
162 \r
163 /**\r
164   * @brief  Initializes the USARTx peripheral according to the specified\r
165   *   parameters in the USART_InitStruct .\r
166   * @param  USARTx: Select the USART or the UART peripheral. \r
167   *   This parameter can be one of the following values:\r
168   *   USART1, USART2, USART3, UART4 or UART5.\r
169   * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure\r
170   *   that contains the configuration information for the specified USART peripheral.\r
171   * @retval None\r
172   */\r
173 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)\r
174 {\r
175   uint32_t tmpreg = 0x00, apbclock = 0x00;\r
176   uint32_t integerdivider = 0x00;\r
177   uint32_t fractionaldivider = 0x00;\r
178   uint32_t usartxbase = 0;\r
179   RCC_ClocksTypeDef RCC_ClocksStatus;\r
180   /* Check the parameters */\r
181   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
182   assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));  \r
183   assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));\r
184   assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));\r
185   assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));\r
186   assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));\r
187   assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));\r
188   /* The hardware flow control is available only for USART1, USART2 and USART3 */\r
189   if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)\r
190   {\r
191     assert_param(IS_USART_123_PERIPH(USARTx));\r
192   }\r
193 \r
194   usartxbase = (uint32_t)USARTx;\r
195 \r
196 /*---------------------------- USART CR2 Configuration -----------------------*/\r
197   tmpreg = USARTx->CR2;\r
198   /* Clear STOP[13:12] bits */\r
199   tmpreg &= CR2_STOP_CLEAR_Mask;\r
200   /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/\r
201   /* Set STOP[13:12] bits according to USART_StopBits value */\r
202   tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;\r
203   \r
204   /* Write to USART CR2 */\r
205   USARTx->CR2 = (uint16_t)tmpreg;\r
206 \r
207 /*---------------------------- USART CR1 Configuration -----------------------*/\r
208   tmpreg = USARTx->CR1;\r
209   /* Clear M, PCE, PS, TE and RE bits */\r
210   tmpreg &= CR1_CLEAR_Mask;\r
211   /* Configure the USART Word Length, Parity and mode ----------------------- */\r
212   /* Set the M bits according to USART_WordLength value */\r
213   /* Set PCE and PS bits according to USART_Parity value */\r
214   /* Set TE and RE bits according to USART_Mode value */\r
215   tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |\r
216             USART_InitStruct->USART_Mode;\r
217   /* Write to USART CR1 */\r
218   USARTx->CR1 = (uint16_t)tmpreg;\r
219 \r
220 /*---------------------------- USART CR3 Configuration -----------------------*/  \r
221   tmpreg = USARTx->CR3;\r
222   /* Clear CTSE and RTSE bits */\r
223   tmpreg &= CR3_CLEAR_Mask;\r
224   /* Configure the USART HFC -------------------------------------------------*/\r
225   /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */\r
226   tmpreg |= USART_InitStruct->USART_HardwareFlowControl;\r
227   /* Write to USART CR3 */\r
228   USARTx->CR3 = (uint16_t)tmpreg;\r
229 \r
230 /*---------------------------- USART BRR Configuration -----------------------*/\r
231   /* Configure the USART Baud Rate -------------------------------------------*/\r
232   RCC_GetClocksFreq(&RCC_ClocksStatus);\r
233   if (usartxbase == USART1_BASE)\r
234   {\r
235     apbclock = RCC_ClocksStatus.PCLK2_Frequency;\r
236   }\r
237   else\r
238   {\r
239     apbclock = RCC_ClocksStatus.PCLK1_Frequency;\r
240   }\r
241   \r
242   /* Determine the integer part */\r
243   if ((USARTx->CR1 & CR1_OVER8_Set) != 0)\r
244   {\r
245     /* Integer part computing in case Oversampling mode is 8 Samples */\r
246     integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));    \r
247   }\r
248   else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */\r
249   {\r
250     /* Integer part computing in case Oversampling mode is 16 Samples */\r
251     integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));    \r
252   }\r
253   tmpreg = (integerdivider / 100) << 4;\r
254 \r
255   /* Determine the fractional part */\r
256   fractionaldivider = integerdivider - (100 * (tmpreg >> 4));\r
257 \r
258   /* Implement the fractional part in the register */\r
259   if ((USARTx->CR1 & CR1_OVER8_Set) != 0)\r
260   {\r
261     tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);\r
262   }\r
263   else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */\r
264   {\r
265     tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);\r
266   }\r
267   \r
268   /* Write to USART BRR */\r
269   USARTx->BRR = (uint16_t)tmpreg;\r
270 }\r
271 \r
272 /**\r
273   * @brief  Fills each USART_InitStruct member with its default value.\r
274   * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure\r
275   *   which will be initialized.\r
276   * @retval None\r
277   */\r
278 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)\r
279 {\r
280   /* USART_InitStruct members default value */\r
281   USART_InitStruct->USART_BaudRate = 9600;\r
282   USART_InitStruct->USART_WordLength = USART_WordLength_8b;\r
283   USART_InitStruct->USART_StopBits = USART_StopBits_1;\r
284   USART_InitStruct->USART_Parity = USART_Parity_No ;\r
285   USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;\r
286   USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;  \r
287 }\r
288 \r
289 /**\r
290   * @brief  Initializes the USARTx peripheral Clock according to the \r
291   *   specified parameters in the USART_ClockInitStruct .\r
292   * @param  USARTx: where x can be 1, 2, 3 to select the USART peripheral.\r
293   * @param  USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef\r
294   *   structure that contains the configuration information for the specified \r
295   *   USART peripheral.  \r
296   * @note The Smart Card mode is not available for UART4 and UART5.\r
297   * @retval None\r
298   */\r
299 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)\r
300 {\r
301   uint32_t tmpreg = 0x00;\r
302   /* Check the parameters */\r
303   assert_param(IS_USART_123_PERIPH(USARTx));\r
304   assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));\r
305   assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));\r
306   assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));\r
307   assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));\r
308   \r
309 /*---------------------------- USART CR2 Configuration -----------------------*/\r
310   tmpreg = USARTx->CR2;\r
311   /* Clear CLKEN, CPOL, CPHA and LBCL bits */\r
312   tmpreg &= CR2_CLOCK_CLEAR_Mask;\r
313   /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/\r
314   /* Set CLKEN bit according to USART_Clock value */\r
315   /* Set CPOL bit according to USART_CPOL value */\r
316   /* Set CPHA bit according to USART_CPHA value */\r
317   /* Set LBCL bit according to USART_LastBit value */\r
318   tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL | \r
319                  USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;\r
320   /* Write to USART CR2 */\r
321   USARTx->CR2 = (uint16_t)tmpreg;\r
322 }\r
323 \r
324 /**\r
325   * @brief  Fills each USART_ClockInitStruct member with its default value.\r
326   * @param  USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef\r
327   *   structure which will be initialized.\r
328   * @retval None\r
329   */\r
330 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)\r
331 {\r
332   /* USART_ClockInitStruct members default value */\r
333   USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;\r
334   USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;\r
335   USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;\r
336   USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;\r
337 }\r
338 \r
339 /**\r
340   * @brief  Enables or disables the specified USART peripheral.\r
341   * @param  USARTx: Select the USART or the UART peripheral. \r
342   *   This parameter can be one of the following values:\r
343   *   USART1, USART2, USART3, UART4 or UART5.\r
344   * @param  NewState: new state of the USARTx peripheral.\r
345   *   This parameter can be: ENABLE or DISABLE.\r
346   * @retval None\r
347   */\r
348 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
349 {\r
350   /* Check the parameters */\r
351   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
352   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
353   \r
354   if (NewState != DISABLE)\r
355   {\r
356     /* Enable the selected USART by setting the UE bit in the CR1 register */\r
357     USARTx->CR1 |= CR1_UE_Set;\r
358   }\r
359   else\r
360   {\r
361     /* Disable the selected USART by clearing the UE bit in the CR1 register */\r
362     USARTx->CR1 &= CR1_UE_Reset;\r
363   }\r
364 }\r
365 \r
366 /**\r
367   * @brief  Enables or disables the specified USART interrupts.\r
368   * @param  USARTx: Select the USART or the UART peripheral. \r
369   *   This parameter can be one of the following values:\r
370   *   USART1, USART2, USART3, UART4 or UART5.\r
371   * @param  USART_IT: specifies the USART interrupt sources to be enabled or disabled.\r
372   *   This parameter can be one of the following values:\r
373   *     @arg USART_IT_CTS:  CTS change interrupt (not available for UART4 and UART5)\r
374   *     @arg USART_IT_LBD:  LIN Break detection interrupt\r
375   *     @arg USART_IT_TXE:  Tansmit Data Register empty interrupt\r
376   *     @arg USART_IT_TC:   Transmission complete interrupt\r
377   *     @arg USART_IT_RXNE: Receive Data register not empty interrupt\r
378   *     @arg USART_IT_IDLE: Idle line detection interrupt\r
379   *     @arg USART_IT_PE:   Parity Error interrupt\r
380   *     @arg USART_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)\r
381   * @param  NewState: new state of the specified USARTx interrupts.\r
382   *   This parameter can be: ENABLE or DISABLE.\r
383   * @retval None\r
384   */\r
385 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)\r
386 {\r
387   uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;\r
388   uint32_t usartxbase = 0x00;\r
389   /* Check the parameters */\r
390   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
391   assert_param(IS_USART_CONFIG_IT(USART_IT));\r
392   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
393   /* The CTS interrupt is not available for UART4 and UART5 */\r
394   if (USART_IT == USART_IT_CTS)\r
395   {\r
396     assert_param(IS_USART_123_PERIPH(USARTx));\r
397   }   \r
398   \r
399   usartxbase = (uint32_t)USARTx;\r
400 \r
401   /* Get the USART register index */\r
402   usartreg = (((uint8_t)USART_IT) >> 0x05);\r
403 \r
404   /* Get the interrupt position */\r
405   itpos = USART_IT & IT_Mask;\r
406   itmask = (((uint32_t)0x01) << itpos);\r
407     \r
408   if (usartreg == 0x01) /* The IT is in CR1 register */\r
409   {\r
410     usartxbase += 0x0C;\r
411   }\r
412   else if (usartreg == 0x02) /* The IT is in CR2 register */\r
413   {\r
414     usartxbase += 0x10;\r
415   }\r
416   else /* The IT is in CR3 register */\r
417   {\r
418     usartxbase += 0x14; \r
419   }\r
420   if (NewState != DISABLE)\r
421   {\r
422     *(__IO uint32_t*)usartxbase  |= itmask;\r
423   }\r
424   else\r
425   {\r
426     *(__IO uint32_t*)usartxbase &= ~itmask;\r
427   }\r
428 }\r
429 \r
430 /**\r
431   * @brief  Enables or disables the USART\92s DMA interface.\r
432   * @param  USARTx: Select the USART or the UART peripheral. \r
433   *   This parameter can be one of the following values:\r
434   *   USART1, USART2, USART3 or UART4.  \r
435   * @param  USART_DMAReq: specifies the DMA request.\r
436   *   This parameter can be any combination of the following values:\r
437   *     @arg USART_DMAReq_Tx: USART DMA transmit request\r
438   *     @arg USART_DMAReq_Rx: USART DMA receive request\r
439   * @param  NewState: new state of the DMA Request sources.\r
440   *   This parameter can be: ENABLE or DISABLE.\r
441   * @note The DMA mode is not available for UART5.  \r
442   * @retval None\r
443   */\r
444 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)\r
445 {\r
446   /* Check the parameters */\r
447   assert_param(IS_USART_1234_PERIPH(USARTx));\r
448   assert_param(IS_USART_DMAREQ(USART_DMAReq));  \r
449   assert_param(IS_FUNCTIONAL_STATE(NewState)); \r
450   if (NewState != DISABLE)\r
451   {\r
452     /* Enable the DMA transfer for selected requests by setting the DMAT and/or\r
453        DMAR bits in the USART CR3 register */\r
454     USARTx->CR3 |= USART_DMAReq;\r
455   }\r
456   else\r
457   {\r
458     /* Disable the DMA transfer for selected requests by clearing the DMAT and/or\r
459        DMAR bits in the USART CR3 register */\r
460     USARTx->CR3 &= (uint16_t)~USART_DMAReq;\r
461   }\r
462 }\r
463 \r
464 /**\r
465   * @brief  Sets the address of the USART node.\r
466   * @param  USARTx: Select the USART or the UART peripheral. \r
467   *   This parameter can be one of the following values:\r
468   *   USART1, USART2, USART3, UART4 or UART5.\r
469   * @param  USART_Address: Indicates the address of the USART node.\r
470   * @retval None\r
471   */\r
472 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)\r
473 {\r
474   /* Check the parameters */\r
475   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
476   assert_param(IS_USART_ADDRESS(USART_Address)); \r
477     \r
478   /* Clear the USART address */\r
479   USARTx->CR2 &= CR2_Address_Mask;\r
480   /* Set the USART address node */\r
481   USARTx->CR2 |= USART_Address;\r
482 }\r
483 \r
484 /**\r
485   * @brief  Selects the USART WakeUp method.\r
486   * @param  USARTx: Select the USART or the UART peripheral. \r
487   *   This parameter can be one of the following values:\r
488   *   USART1, USART2, USART3, UART4 or UART5.\r
489   * @param  USART_WakeUp: specifies the USART wakeup method.\r
490   *   This parameter can be one of the following values:\r
491   *     @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection\r
492   *     @arg USART_WakeUp_AddressMark: WakeUp by an address mark\r
493   * @retval None\r
494   */\r
495 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)\r
496 {\r
497   /* Check the parameters */\r
498   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
499   assert_param(IS_USART_WAKEUP(USART_WakeUp));\r
500   \r
501   USARTx->CR1 &= CR1_WAKE_Mask;\r
502   USARTx->CR1 |= USART_WakeUp;\r
503 }\r
504 \r
505 /**\r
506   * @brief  Determines if the USART is in mute mode or not.\r
507   * @param  USARTx: Select the USART or the UART peripheral. \r
508   *   This parameter can be one of the following values:\r
509   *   USART1, USART2, USART3, UART4 or UART5.\r
510   * @param  NewState: new state of the USART mute mode.\r
511   *   This parameter can be: ENABLE or DISABLE.\r
512   * @retval None\r
513   */\r
514 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
515 {\r
516   /* Check the parameters */\r
517   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
518   assert_param(IS_FUNCTIONAL_STATE(NewState)); \r
519   \r
520   if (NewState != DISABLE)\r
521   {\r
522     /* Enable the USART mute mode  by setting the RWU bit in the CR1 register */\r
523     USARTx->CR1 |= CR1_RWU_Set;\r
524   }\r
525   else\r
526   {\r
527     /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */\r
528     USARTx->CR1 &= CR1_RWU_Reset;\r
529   }\r
530 }\r
531 \r
532 /**\r
533   * @brief  Sets the USART LIN Break detection length.\r
534   * @param  USARTx: Select the USART or the UART peripheral. \r
535   *   This parameter can be one of the following values:\r
536   *   USART1, USART2, USART3, UART4 or UART5.\r
537   * @param  USART_LINBreakDetectLength: specifies the LIN break detection length.\r
538   *   This parameter can be one of the following values:\r
539   *     @arg USART_LINBreakDetectLength_10b: 10-bit break detection\r
540   *     @arg USART_LINBreakDetectLength_11b: 11-bit break detection\r
541   * @retval None\r
542   */\r
543 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)\r
544 {\r
545   /* Check the parameters */\r
546   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
547   assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));\r
548   \r
549   USARTx->CR2 &= CR2_LBDL_Mask;\r
550   USARTx->CR2 |= USART_LINBreakDetectLength;  \r
551 }\r
552 \r
553 /**\r
554   * @brief  Enables or disables the USART\92s LIN mode.\r
555   * @param  USARTx: Select the USART or the UART peripheral. \r
556   *   This parameter can be one of the following values:\r
557   *   USART1, USART2, USART3, UART4 or UART5.\r
558   * @param  NewState: new state of the USART LIN mode.\r
559   *   This parameter can be: ENABLE or DISABLE.\r
560   * @retval None\r
561   */\r
562 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
563 {\r
564   /* Check the parameters */\r
565   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
566   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
567   \r
568   if (NewState != DISABLE)\r
569   {\r
570     /* Enable the LIN mode by setting the LINEN bit in the CR2 register */\r
571     USARTx->CR2 |= CR2_LINEN_Set;\r
572   }\r
573   else\r
574   {\r
575     /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */\r
576     USARTx->CR2 &= CR2_LINEN_Reset;\r
577   }\r
578 }\r
579 \r
580 /**\r
581   * @brief  Transmits single data through the USARTx peripheral.\r
582   * @param  USARTx: Select the USART or the UART peripheral. \r
583   *   This parameter can be one of the following values:\r
584   *   USART1, USART2, USART3, UART4 or UART5.\r
585   * @param  Data: the data to transmit.\r
586   * @retval None\r
587   */\r
588 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)\r
589 {\r
590   /* Check the parameters */\r
591   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
592   assert_param(IS_USART_DATA(Data)); \r
593     \r
594   /* Transmit Data */\r
595   USARTx->DR = (Data & (uint16_t)0x01FF);\r
596 }\r
597 \r
598 /**\r
599   * @brief  Returns the most recent received data by the USARTx peripheral.\r
600   * @param  USARTx: Select the USART or the UART peripheral. \r
601   *   This parameter can be one of the following values:\r
602   *   USART1, USART2, USART3, UART4 or UART5.\r
603   * @retval The received data.\r
604   */\r
605 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)\r
606 {\r
607   /* Check the parameters */\r
608   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
609   \r
610   /* Receive Data */\r
611   return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);\r
612 }\r
613 \r
614 /**\r
615   * @brief  Transmits break characters.\r
616   * @param  USARTx: Select the USART or the UART peripheral. \r
617   *   This parameter can be one of the following values:\r
618   *   USART1, USART2, USART3, UART4 or UART5.\r
619   * @retval None\r
620   */\r
621 void USART_SendBreak(USART_TypeDef* USARTx)\r
622 {\r
623   /* Check the parameters */\r
624   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
625   \r
626   /* Send break characters */\r
627   USARTx->CR1 |= CR1_SBK_Set;\r
628 }\r
629 \r
630 /**\r
631   * @brief  Sets the specified USART guard time.\r
632   * @param  USARTx: where x can be 1, 2 or 3 to select the USART peripheral.\r
633   * @param  USART_GuardTime: specifies the guard time.\r
634   * @note The guard time bits are not available for UART4 and UART5.   \r
635   * @retval None\r
636   */\r
637 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)\r
638 {    \r
639   /* Check the parameters */\r
640   assert_param(IS_USART_123_PERIPH(USARTx));\r
641   \r
642   /* Clear the USART Guard time */\r
643   USARTx->GTPR &= GTPR_LSB_Mask;\r
644   /* Set the USART guard time */\r
645   USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);\r
646 }\r
647 \r
648 /**\r
649   * @brief  Sets the system clock prescaler.\r
650   * @param  USARTx: Select the USART or the UART peripheral. \r
651   *   This parameter can be one of the following values:\r
652   *   USART1, USART2, USART3, UART4 or UART5.\r
653   * @param  USART_Prescaler: specifies the prescaler clock.  \r
654   * @note   The function is used for IrDA mode with UART4 and UART5.\r
655   * @retval None\r
656   */\r
657 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)\r
658\r
659   /* Check the parameters */\r
660   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
661   \r
662   /* Clear the USART prescaler */\r
663   USARTx->GTPR &= GTPR_MSB_Mask;\r
664   /* Set the USART prescaler */\r
665   USARTx->GTPR |= USART_Prescaler;\r
666 }\r
667 \r
668 /**\r
669   * @brief  Enables or disables the USART\92s Smart Card mode.\r
670   * @param  USARTx: where x can be 1, 2 or 3 to select the USART peripheral.\r
671   * @param  NewState: new state of the Smart Card mode.\r
672   *   This parameter can be: ENABLE or DISABLE.     \r
673   * @note The Smart Card mode is not available for UART4 and UART5. \r
674   * @retval None\r
675   */\r
676 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
677 {\r
678   /* Check the parameters */\r
679   assert_param(IS_USART_123_PERIPH(USARTx));\r
680   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
681   if (NewState != DISABLE)\r
682   {\r
683     /* Enable the SC mode by setting the SCEN bit in the CR3 register */\r
684     USARTx->CR3 |= CR3_SCEN_Set;\r
685   }\r
686   else\r
687   {\r
688     /* Disable the SC mode by clearing the SCEN bit in the CR3 register */\r
689     USARTx->CR3 &= CR3_SCEN_Reset;\r
690   }\r
691 }\r
692 \r
693 /**\r
694   * @brief  Enables or disables NACK transmission.\r
695   * @param  USARTx: where x can be 1, 2 or 3 to select the USART peripheral. \r
696   * @param  NewState: new state of the NACK transmission.\r
697   *   This parameter can be: ENABLE or DISABLE.  \r
698   * @note The Smart Card mode is not available for UART4 and UART5.\r
699   * @retval None\r
700   */\r
701 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
702 {\r
703   /* Check the parameters */\r
704   assert_param(IS_USART_123_PERIPH(USARTx));  \r
705   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
706   if (NewState != DISABLE)\r
707   {\r
708     /* Enable the NACK transmission by setting the NACK bit in the CR3 register */\r
709     USARTx->CR3 |= CR3_NACK_Set;\r
710   }\r
711   else\r
712   {\r
713     /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */\r
714     USARTx->CR3 &= CR3_NACK_Reset;\r
715   }\r
716 }\r
717 \r
718 /**\r
719   * @brief  Enables or disables the USART\92s Half Duplex communication.\r
720   * @param  USARTx: Select the USART or the UART peripheral. \r
721   *   This parameter can be one of the following values:\r
722   *   USART1, USART2, USART3, UART4 or UART5.\r
723   * @param  NewState: new state of the USART Communication.\r
724   *   This parameter can be: ENABLE or DISABLE.\r
725   * @retval None\r
726   */\r
727 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
728 {\r
729   /* Check the parameters */\r
730   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
731   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
732   \r
733   if (NewState != DISABLE)\r
734   {\r
735     /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */\r
736     USARTx->CR3 |= CR3_HDSEL_Set;\r
737   }\r
738   else\r
739   {\r
740     /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */\r
741     USARTx->CR3 &= CR3_HDSEL_Reset;\r
742   }\r
743 }\r
744 \r
745 \r
746 /**\r
747   * @brief  Enables or disables the USART's 8x oversampling mode.\r
748   * @param  USARTx: Select the USART or the UART peripheral.\r
749   *   This parameter can be one of the following values:\r
750   *   USART1, USART2, USART3, UART4 or UART5.\r
751   * @param  NewState: new state of the USART one bit sampling methode.\r
752   *   This parameter can be: ENABLE or DISABLE.\r
753   * @note\r
754   *     This function has to be called before calling USART_Init()\r
755   *     function in order to have correct baudrate Divider value.   \r
756   * @retval None\r
757   */\r
758 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
759 {\r
760   /* Check the parameters */\r
761   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
762   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
763   \r
764   if (NewState != DISABLE)\r
765   {\r
766     /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */\r
767     USARTx->CR1 |= CR1_OVER8_Set;\r
768   }\r
769   else\r
770   {\r
771     /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */\r
772     USARTx->CR1 &= CR1_OVER8_Reset;\r
773   }\r
774 }\r
775 \r
776 /**\r
777   * @brief  Enables or disables the USART's one bit sampling methode.\r
778   * @param  USARTx: Select the USART or the UART peripheral.\r
779   *   This parameter can be one of the following values:\r
780   *   USART1, USART2, USART3, UART4 or UART5.\r
781   * @param  NewState: new state of the USART one bit sampling methode.\r
782   *   This parameter can be: ENABLE or DISABLE.\r
783   * @retval None\r
784   */\r
785 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
786 {\r
787   /* Check the parameters */\r
788   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
789   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
790   \r
791   if (NewState != DISABLE)\r
792   {\r
793     /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */\r
794     USARTx->CR3 |= CR3_ONEBITE_Set;\r
795   }\r
796   else\r
797   {\r
798     /* Disable tthe one bit method by clearing the ONEBITE bit in the CR3 register */\r
799     USARTx->CR3 &= CR3_ONEBITE_Reset;\r
800   }\r
801 }\r
802 \r
803 /**\r
804   * @brief  Configures the USART\92s IrDA interface.\r
805   * @param  USARTx: Select the USART or the UART peripheral. \r
806   *   This parameter can be one of the following values:\r
807   *   USART1, USART2, USART3, UART4 or UART5.\r
808   * @param  USART_IrDAMode: specifies the IrDA mode.\r
809   *   This parameter can be one of the following values:\r
810   *     @arg USART_IrDAMode_LowPower\r
811   *     @arg USART_IrDAMode_Normal\r
812   * @retval None\r
813   */\r
814 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)\r
815 {\r
816   /* Check the parameters */\r
817   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
818   assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));\r
819     \r
820   USARTx->CR3 &= CR3_IRLP_Mask;\r
821   USARTx->CR3 |= USART_IrDAMode;\r
822 }\r
823 \r
824 /**\r
825   * @brief  Enables or disables the USART\92s IrDA interface.\r
826   * @param  USARTx: Select the USART or the UART peripheral. \r
827   *   This parameter can be one of the following values:\r
828   *   USART1, USART2, USART3, UART4 or UART5.\r
829   * @param  NewState: new state of the IrDA mode.\r
830   *   This parameter can be: ENABLE or DISABLE.\r
831   * @retval None\r
832   */\r
833 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
834 {\r
835   /* Check the parameters */\r
836   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
837   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
838     \r
839   if (NewState != DISABLE)\r
840   {\r
841     /* Enable the IrDA mode by setting the IREN bit in the CR3 register */\r
842     USARTx->CR3 |= CR3_IREN_Set;\r
843   }\r
844   else\r
845   {\r
846     /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */\r
847     USARTx->CR3 &= CR3_IREN_Reset;\r
848   }\r
849 }\r
850 \r
851 /**\r
852   * @brief  Checks whether the specified USART flag is set or not.\r
853   * @param  USARTx: Select the USART or the UART peripheral. \r
854   *   This parameter can be one of the following values:\r
855   *   USART1, USART2, USART3, UART4 or UART5.\r
856   * @param  USART_FLAG: specifies the flag to check.\r
857   *   This parameter can be one of the following values:\r
858   *     @arg USART_FLAG_CTS:  CTS Change flag (not available for UART4 and UART5)\r
859   *     @arg USART_FLAG_LBD:  LIN Break detection flag\r
860   *     @arg USART_FLAG_TXE:  Transmit data register empty flag\r
861   *     @arg USART_FLAG_TC:   Transmission Complete flag\r
862   *     @arg USART_FLAG_RXNE: Receive data register not empty flag\r
863   *     @arg USART_FLAG_IDLE: Idle Line detection flag\r
864   *     @arg USART_FLAG_ORE:  OverRun Error flag\r
865   *     @arg USART_FLAG_NE:   Noise Error flag\r
866   *     @arg USART_FLAG_FE:   Framing Error flag\r
867   *     @arg USART_FLAG_PE:   Parity Error flag\r
868   * @retval The new state of USART_FLAG (SET or RESET).\r
869   */\r
870 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)\r
871 {\r
872   FlagStatus bitstatus = RESET;\r
873   /* Check the parameters */\r
874   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
875   assert_param(IS_USART_FLAG(USART_FLAG));\r
876   /* The CTS flag is not available for UART4 and UART5 */\r
877   if (USART_FLAG == USART_FLAG_CTS)\r
878   {\r
879     assert_param(IS_USART_123_PERIPH(USARTx));\r
880   }  \r
881   \r
882   if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)\r
883   {\r
884     bitstatus = SET;\r
885   }\r
886   else\r
887   {\r
888     bitstatus = RESET;\r
889   }\r
890   return bitstatus;\r
891 }\r
892 \r
893 /**\r
894   * @brief  Clears the USARTx's pending flags.\r
895   * @param  USARTx: Select the USART or the UART peripheral. \r
896   *   This parameter can be one of the following values:\r
897   *   USART1, USART2, USART3, UART4 or UART5.\r
898   * @param  USART_FLAG: specifies the flag to clear.\r
899   *   This parameter can be any combination of the following values:\r
900   *     @arg USART_FLAG_CTS:  CTS Change flag (not available for UART4 and UART5).\r
901   *     @arg USART_FLAG_LBD:  LIN Break detection flag.\r
902   *     @arg USART_FLAG_TC:   Transmission Complete flag.\r
903   *     @arg USART_FLAG_RXNE: Receive data register not empty flag.\r
904   *   \r
905   * @note\r
906   *   - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun \r
907   *     error) and IDLE (Idle line detected) flags are cleared by software \r
908   *     sequence: a read operation to USART_SR register (USART_GetFlagStatus()) \r
909   *     followed by a read operation to USART_DR register (USART_ReceiveData()).\r
910   *   - RXNE flag can be also cleared by a read to the USART_DR register \r
911   *     (USART_ReceiveData()).\r
912   *   - TC flag can be also cleared by software sequence: a read operation to \r
913   *     USART_SR register (USART_GetFlagStatus()) followed by a write operation\r
914   *     to USART_DR register (USART_SendData()).\r
915   *   - TXE flag is cleared only by a write to the USART_DR register \r
916   *     (USART_SendData()).\r
917   * @retval None\r
918   */\r
919 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)\r
920 {\r
921   /* Check the parameters */\r
922   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
923   assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));\r
924   /* The CTS flag is not available for UART4 and UART5 */\r
925   if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)\r
926   {\r
927     assert_param(IS_USART_123_PERIPH(USARTx));\r
928   } \r
929    \r
930   USARTx->SR = (uint16_t)~USART_FLAG;\r
931 }\r
932 \r
933 /**\r
934   * @brief  Checks whether the specified USART interrupt has occurred or not.\r
935   * @param  USARTx: Select the USART or the UART peripheral. \r
936   *   This parameter can be one of the following values:\r
937   *   USART1, USART2, USART3, UART4 or UART5.\r
938   * @param  USART_IT: specifies the USART interrupt source to check.\r
939   *   This parameter can be one of the following values:\r
940   *     @arg USART_IT_CTS:  CTS change interrupt (not available for UART4 and UART5)\r
941   *     @arg USART_IT_LBD:  LIN Break detection interrupt\r
942   *     @arg USART_IT_TXE:  Tansmit Data Register empty interrupt\r
943   *     @arg USART_IT_TC:   Transmission complete interrupt\r
944   *     @arg USART_IT_RXNE: Receive Data register not empty interrupt\r
945   *     @arg USART_IT_IDLE: Idle line detection interrupt\r
946   *     @arg USART_IT_ORE:  OverRun Error interrupt\r
947   *     @arg USART_IT_NE:   Noise Error interrupt\r
948   *     @arg USART_IT_FE:   Framing Error interrupt\r
949   *     @arg USART_IT_PE:   Parity Error interrupt\r
950   * @retval The new state of USART_IT (SET or RESET).\r
951   */\r
952 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)\r
953 {\r
954   uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;\r
955   ITStatus bitstatus = RESET;\r
956   /* Check the parameters */\r
957   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
958   assert_param(IS_USART_GET_IT(USART_IT));\r
959   /* The CTS interrupt is not available for UART4 and UART5 */ \r
960   if (USART_IT == USART_IT_CTS)\r
961   {\r
962     assert_param(IS_USART_123_PERIPH(USARTx));\r
963   }   \r
964   \r
965   /* Get the USART register index */\r
966   usartreg = (((uint8_t)USART_IT) >> 0x05);\r
967   /* Get the interrupt position */\r
968   itmask = USART_IT & IT_Mask;\r
969   itmask = (uint32_t)0x01 << itmask;\r
970   \r
971   if (usartreg == 0x01) /* The IT  is in CR1 register */\r
972   {\r
973     itmask &= USARTx->CR1;\r
974   }\r
975   else if (usartreg == 0x02) /* The IT  is in CR2 register */\r
976   {\r
977     itmask &= USARTx->CR2;\r
978   }\r
979   else /* The IT  is in CR3 register */\r
980   {\r
981     itmask &= USARTx->CR3;\r
982   }\r
983   \r
984   bitpos = USART_IT >> 0x08;\r
985   bitpos = (uint32_t)0x01 << bitpos;\r
986   bitpos &= USARTx->SR;\r
987   if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))\r
988   {\r
989     bitstatus = SET;\r
990   }\r
991   else\r
992   {\r
993     bitstatus = RESET;\r
994   }\r
995   \r
996   return bitstatus;  \r
997 }\r
998 \r
999 /**\r
1000   * @brief  Clears the USARTx\92s interrupt pending bits.\r
1001   * @param  USARTx: Select the USART or the UART peripheral. \r
1002   *   This parameter can be one of the following values:\r
1003   *   USART1, USART2, USART3, UART4 or UART5.\r
1004   * @param  USART_IT: specifies the interrupt pending bit to clear.\r
1005   *   This parameter can be one of the following values:\r
1006   *     @arg USART_IT_CTS:  CTS change interrupt (not available for UART4 and UART5)\r
1007   *     @arg USART_IT_LBD:  LIN Break detection interrupt\r
1008   *     @arg USART_IT_TC:   Transmission complete interrupt. \r
1009   *     @arg USART_IT_RXNE: Receive Data register not empty interrupt.\r
1010   *   \r
1011   * @note\r
1012   *   - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun \r
1013   *     error) and IDLE (Idle line detected) pending bits are cleared by \r
1014   *     software sequence: a read operation to USART_SR register \r
1015   *     (USART_GetITStatus()) followed by a read operation to USART_DR register \r
1016   *     (USART_ReceiveData()).\r
1017   *   - RXNE pending bit can be also cleared by a read to the USART_DR register \r
1018   *     (USART_ReceiveData()).\r
1019   *   - TC pending bit can be also cleared by software sequence: a read \r
1020   *     operation to USART_SR register (USART_GetITStatus()) followed by a write \r
1021   *     operation to USART_DR register (USART_SendData()).\r
1022   *   - TXE pending bit is cleared only by a write to the USART_DR register \r
1023   *     (USART_SendData()).\r
1024   * @retval None\r
1025   */\r
1026 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)\r
1027 {\r
1028   uint16_t bitpos = 0x00, itmask = 0x00;\r
1029   /* Check the parameters */\r
1030   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
1031   assert_param(IS_USART_CLEAR_IT(USART_IT));\r
1032   /* The CTS interrupt is not available for UART4 and UART5 */\r
1033   if (USART_IT == USART_IT_CTS)\r
1034   {\r
1035     assert_param(IS_USART_123_PERIPH(USARTx));\r
1036   }   \r
1037   \r
1038   bitpos = USART_IT >> 0x08;\r
1039   itmask = ((uint16_t)0x01 << (uint16_t)bitpos);\r
1040   USARTx->SR = (uint16_t)~itmask;\r
1041 }\r
1042 /**\r
1043   * @}\r
1044   */\r
1045 \r
1046 /**\r
1047   * @}\r
1048   */\r
1049 \r
1050 /**\r
1051   * @}\r
1052   */\r
1053 \r
1054 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r