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