Merge pull request #93 from zyp/master
[fw/stlink] / example / libs_stm / src / stm32l1xx / stm32l1xx_usart.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_usart.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0\r
6   * @date    31-December-2010\r
7   * @brief   This file provides firmware functions to manage the following \r
8   *          functionalities of the Universal synchronous asynchronous receiver\r
9   *          transmitter (USART):           \r
10   *           - Initialization and Configuration\r
11   *           - Data transfers\r
12   *           - Multi-Processor Communication\r
13   *           - LIN mode\r
14   *           - Half-duplex mode\r
15   *           - Smartcard mode\r
16   *           - IrDA mode\r
17   *           - DMA transfers management\r
18   *           - Interrupts and flags management \r
19   *           \r
20   *  @verbatim\r
21   *      \r
22   *          ===================================================================\r
23   *                                 How to use this driver\r
24   *          ===================================================================\r
25   *          1. Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)\r
26   *             function for USART1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE)\r
27   *             function for USART2 and USART3.\r
28   *\r
29   *          2.  According to the USART mode, enable the GPIO clocks using \r
30   *              RCC_AHBPeriphClockCmd() function. (The I/O can be TX, RX, CTS, \r
31   *              or and SCLK). \r
32   *\r
33   *          3. Peripheral\92s alternate function: \r
34   *                 - Connect the pin to the desired peripherals' Alternate \r
35   *                   Function (AF) using GPIO_PinAFConfig() function\r
36   *                 - Configure the desired pin in alternate function by:\r
37   *                   GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF\r
38   *                 - Select the type, pull-up/pull-down and output speed via \r
39   *                   GPIO_PuPd, GPIO_OType and GPIO_Speed members\r
40   *                 - Call GPIO_Init() function\r
41   *        \r
42   *          4. Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware \r
43   *             flow control and Mode(Receiver/Transmitter) using the SPI_Init()\r
44   *             function.\r
45   *\r
46   *          5. For synchronous mode, enable the clock and program the polarity,\r
47   *             phase and last bit using the USART_ClockInit() function.\r
48   *\r
49   *          5. Enable the NVIC and the corresponding interrupt using the function \r
50   *             USART_ITConfig() if you need to use interrupt mode. \r
51   *\r
52   *          6. When using the DMA mode \r
53   *                   - Configure the DMA using DMA_Init() function\r
54   *                   - Active the needed channel Request using USART_DMACmd() function\r
55   * \r
56   *          7. Enable the USART using the USART_Cmd() function.\r
57   * \r
58   *          8. Enable the DMA using the DMA_Cmd() function, when using DMA mode. \r
59   *\r
60   *          Refer to Multi-Processor, LIN, half-duplex, Smartcard, IrDA sub-sections\r
61   *          for more details\r
62   *          \r
63   *  @endverbatim\r
64   *        \r
65   ******************************************************************************\r
66   * @attention\r
67   *\r
68   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
69   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
70   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
71   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
72   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
73   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
74   *\r
75   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
76   ******************************************************************************  \r
77   */ \r
78 \r
79 /* Includes ------------------------------------------------------------------*/\r
80 #include "stm32l1xx_usart.h"\r
81 #include "stm32l1xx_rcc.h"\r
82 \r
83 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
84   * @{\r
85   */\r
86 \r
87 /** @defgroup USART \r
88   * @brief USART driver modules\r
89   * @{\r
90   */\r
91 \r
92 /* Private typedef -----------------------------------------------------------*/\r
93 /* Private define ------------------------------------------------------------*/\r
94 \r
95 /*!< USART CR1 register clear Mask ((~(uint16_t)0xE9F3)) */\r
96 #define CR1_CLEAR_MASK            ((uint16_t)(USART_CR1_M | USART_CR1_PCE | \\r
97                                               USART_CR1_PS | USART_CR1_TE | \\r
98                                               USART_CR1_RE))\r
99 \r
100 /*!< USART CR2 register clock bits clear Mask ((~(uint16_t)0xF0FF)) */\r
101 #define CR2_CLOCK_CLEAR_MASK      ((uint16_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \\r
102                                               USART_CR2_CPHA | USART_CR2_LBCL))\r
103 \r
104 /*!< USART CR3 register clear Mask ((~(uint16_t)0xFCFF)) */\r
105 #define CR3_CLEAR_MASK            ((uint16_t)(USART_CR3_RTSE | USART_CR3_CTSE))\r
106 \r
107 /*!< USART Interrupts mask */\r
108 #define IT_MASK                   ((uint16_t)0x001F)\r
109 \r
110 /* Private macro -------------------------------------------------------------*/\r
111 /* Private variables ---------------------------------------------------------*/\r
112 /* Private function prototypes -----------------------------------------------*/\r
113 /* Private functions ---------------------------------------------------------*/\r
114 \r
115 /** @defgroup USART_Private_Functions\r
116   * @{\r
117   */\r
118 \r
119 /** @defgroup USART_Group1 Initialization and Configuration functions\r
120  *  @brief   Initialization and Configuration functions \r
121  *\r
122 @verbatim   \r
123  ===============================================================================\r
124                   Initialization and Configuration functions\r
125  ===============================================================================  \r
126 \r
127   This subsection provides a set of functions allowing to initialize the USART \r
128   in asynchronous and in synchronous modes.\r
129    - For the asynchronous mode only these parameters can be configured: \r
130         - Baud Rate\r
131         - Word Length \r
132         - Stop Bit\r
133         - Parity: If the parity is enabled, then the MSB bit of the data written\r
134           in the data register is transmitted but is changed by the parity bit.\r
135           Depending on the frame length defined by the M bit (8-bits or 9-bits),\r
136           the possible USART frame formats are as listed in the following table:\r
137    +-------------------------------------------------------------+     \r
138    |   M bit |  PCE bit  |            USART frame                |\r
139    |---------------------|---------------------------------------|             \r
140    |    0    |    0      |    | SB | 8 bit data | STB |          |\r
141    |---------|-----------|---------------------------------------|  \r
142    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |\r
143    |---------|-----------|---------------------------------------|  \r
144    |    1    |    0      |    | SB | 9 bit data | STB |          |\r
145    |---------|-----------|---------------------------------------|  \r
146    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |\r
147    +-------------------------------------------------------------+            \r
148         - Hardware flow control\r
149         - Receiver/transmitter modes\r
150 \r
151   The USART_Init() function follows the USART  asynchronous configuration procedure\r
152   (details for the procedure are available in reference manual (RM0038)).\r
153 \r
154   - For the synchronous mode in addition to the asynchronous mode parameters these \r
155     parameters should be also configured:\r
156         - USART Clock Enabled\r
157         - USART polarity\r
158         - USART phase\r
159         - USART LastBit\r
160   \r
161   These parameters can be configured using the USART_ClockInit() function.\r
162 \r
163 @endverbatim\r
164   * @{\r
165   */\r
166   \r
167 /**\r
168   * @brief  Deinitializes the USARTx peripheral registers to their default reset values.\r
169   * @param  USARTx: Select the USART peripheral. \r
170   *   This parameter can be one of the following values: USART1, USART2 or USART3.\r
171   * @retval None\r
172   */\r
173 void USART_DeInit(USART_TypeDef* USARTx)\r
174 {\r
175   /* Check the parameters */\r
176   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
177 \r
178   if (USARTx == USART1)\r
179   {\r
180     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);\r
181     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);\r
182   }\r
183   else if (USARTx == USART2)\r
184   {\r
185     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);\r
186     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);\r
187   }\r
188   else \r
189   {\r
190     if (USARTx == USART3)\r
191     {\r
192       RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);\r
193       RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);    \r
194     }\r
195   }    \r
196 }\r
197 \r
198 /**\r
199   * @brief  Initializes the USARTx peripheral according to the specified\r
200   *   parameters in the USART_InitStruct .\r
201   * @param  USARTx: Select the USART peripheral. \r
202   *   This parameter can be one of the following values:\r
203   *   USART1, USART2 or USART3.\r
204   * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure\r
205   *   that contains the configuration information for the specified USART peripheral.\r
206   * @retval None\r
207   */\r
208 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)\r
209 {\r
210   uint32_t tmpreg = 0x00, apbclock = 0x00;\r
211   uint32_t integerdivider = 0x00;\r
212   uint32_t fractionaldivider = 0x00;\r
213   RCC_ClocksTypeDef RCC_ClocksStatus;\r
214 \r
215   /* Check the parameters */\r
216   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
217   assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));  \r
218   assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));\r
219   assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));\r
220   assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));\r
221   assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));\r
222   assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));\r
223   \r
224 /*---------------------------- USART CR2 Configuration -----------------------*/\r
225   tmpreg = USARTx->CR2;\r
226   /* Clear STOP[13:12] bits */\r
227   tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);\r
228 \r
229   /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/\r
230   /* Set STOP[13:12] bits according to USART_StopBits value */\r
231   tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;\r
232   \r
233   /* Write to USART CR2 */\r
234   USARTx->CR2 = (uint16_t)tmpreg;\r
235 \r
236 /*---------------------------- USART CR1 Configuration -----------------------*/\r
237   tmpreg = USARTx->CR1;\r
238   /* Clear M, PCE, PS, TE and RE bits */\r
239   tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);\r
240 \r
241   /* Configure the USART Word Length, Parity and mode ----------------------- */\r
242   /* Set the M bits according to USART_WordLength value */\r
243   /* Set PCE and PS bits according to USART_Parity value */\r
244   /* Set TE and RE bits according to USART_Mode value */\r
245   tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |\r
246             USART_InitStruct->USART_Mode;\r
247 \r
248   /* Write to USART CR1 */\r
249   USARTx->CR1 = (uint16_t)tmpreg;\r
250 \r
251 /*---------------------------- USART CR3 Configuration -----------------------*/  \r
252   tmpreg = USARTx->CR3;\r
253   /* Clear CTSE and RTSE bits */\r
254   tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);\r
255 \r
256   /* Configure the USART HFC -------------------------------------------------*/\r
257   /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */\r
258   tmpreg |= USART_InitStruct->USART_HardwareFlowControl;\r
259 \r
260   /* Write to USART CR3 */\r
261   USARTx->CR3 = (uint16_t)tmpreg;\r
262 \r
263 /*---------------------------- USART BRR Configuration -----------------------*/\r
264   /* Configure the USART Baud Rate -------------------------------------------*/\r
265   RCC_GetClocksFreq(&RCC_ClocksStatus);\r
266   if (USARTx == USART1) \r
267   {\r
268     apbclock = RCC_ClocksStatus.PCLK2_Frequency;\r
269   }\r
270   else\r
271   {\r
272     apbclock = RCC_ClocksStatus.PCLK1_Frequency;\r
273   }\r
274 \r
275   /* Determine the integer part */\r
276   if ((USARTx->CR1 & USART_CR1_OVER8) != 0)\r
277   {\r
278     /* Integer part computing in case Oversampling mode is 8 Samples */\r
279     integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));    \r
280   }\r
281   else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */\r
282   {\r
283     /* Integer part computing in case Oversampling mode is 16 Samples */\r
284     integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));    \r
285   }\r
286   tmpreg = (integerdivider / 100) << 4;\r
287 \r
288   /* Determine the fractional part */\r
289   fractionaldivider = integerdivider - (100 * (tmpreg >> 4));\r
290 \r
291   /* Implement the fractional part in the register */\r
292   if ((USARTx->CR1 & USART_CR1_OVER8) != 0)\r
293   {\r
294     tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);\r
295   }\r
296   else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */\r
297   {\r
298     tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);\r
299   }\r
300  \r
301   /* Write to USART BRR */\r
302   USARTx->BRR = (uint16_t)tmpreg;\r
303 }\r
304 \r
305 /**\r
306   * @brief  Fills each USART_InitStruct member with its default value.\r
307   * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure\r
308   *   which will be initialized.\r
309   * @retval None\r
310   */\r
311 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)\r
312 {\r
313   /* USART_InitStruct members default value */\r
314   USART_InitStruct->USART_BaudRate = 9600;\r
315   USART_InitStruct->USART_WordLength = USART_WordLength_8b;\r
316   USART_InitStruct->USART_StopBits = USART_StopBits_1;\r
317   USART_InitStruct->USART_Parity = USART_Parity_No ;\r
318   USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;\r
319   USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;  \r
320 }\r
321 \r
322 /**\r
323   * @brief  Initializes the USARTx peripheral Clock according to the \r
324   *   specified parameters in the USART_ClockInitStruct .\r
325   * @param  USARTx: where x can be 1, 2, 3 to select the USART peripheral.\r
326   * @param  USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef\r
327   *   structure that contains the configuration information for the specified \r
328   *   USART peripheral.  \r
329   * @retval None\r
330   */\r
331 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)\r
332 {\r
333   uint32_t tmpreg = 0x00;\r
334   /* Check the parameters */\r
335   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
336   assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));\r
337   assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));\r
338   assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));\r
339   assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));\r
340   \r
341 /*---------------------------- USART CR2 Configuration -----------------------*/\r
342   tmpreg = USARTx->CR2;\r
343   /* Clear CLKEN, CPOL, CPHA and LBCL bits */\r
344   tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);\r
345   /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/\r
346   /* Set CLKEN bit according to USART_Clock value */\r
347   /* Set CPOL bit according to USART_CPOL value */\r
348   /* Set CPHA bit according to USART_CPHA value */\r
349   /* Set LBCL bit according to USART_LastBit value */\r
350   tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL | \r
351                  USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;\r
352   /* Write to USART CR2 */\r
353   USARTx->CR2 = (uint16_t)tmpreg;\r
354 }\r
355 \r
356 /**\r
357   * @brief  Fills each USART_ClockInitStruct member with its default value.\r
358   * @param  USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef\r
359   *   structure which will be initialized.\r
360   * @retval None\r
361   */\r
362 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)\r
363 {\r
364   /* USART_ClockInitStruct members default value */\r
365   USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;\r
366   USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;\r
367   USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;\r
368   USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;\r
369 }\r
370 \r
371 /**\r
372   * @brief  Enables or disables the specified USART peripheral.\r
373   * @param  USARTx: Select the USART peripheral. \r
374   *   This parameter can be one of the following values:\r
375   *   USART1, USART2 or USART3.\r
376   * @param  NewState: new state of the USARTx peripheral.\r
377   *   This parameter can be: ENABLE or DISABLE.\r
378   * @retval None\r
379   */\r
380 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
381 {\r
382   /* Check the parameters */\r
383   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
384   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
385   \r
386   if (NewState != DISABLE)\r
387   {\r
388     /* Enable the selected USART by setting the UE bit in the CR1 register */\r
389     USARTx->CR1 |= USART_CR1_UE;\r
390   }\r
391   else\r
392   {\r
393     /* Disable the selected USART by clearing the UE bit in the CR1 register */\r
394     USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_UE);\r
395   }\r
396 }\r
397 \r
398 /**\r
399   * @brief  Sets the system clock prescaler.\r
400   * @param  USARTx: Select the USART peripheral. \r
401   *   This parameter can be one of the following values:\r
402   *   USART1, USART2 or USART3.\r
403   * @param  USART_Prescaler: specifies the prescaler clock.  \r
404   * @retval None\r
405   */\r
406 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)\r
407\r
408   /* Check the parameters */\r
409   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
410   \r
411   /* Clear the USART prescaler */\r
412   USARTx->GTPR &= USART_GTPR_GT;\r
413   /* Set the USART prescaler */\r
414   USARTx->GTPR |= USART_Prescaler;\r
415 }\r
416 \r
417 /**\r
418   * @brief  Enables or disables the USART's 8x oversampling mode.\r
419   * @param USARTx: Select the USART peripheral. \r
420   *   This parameter can be one of the following values:\r
421   *     USART1, USART2, USART3.\r
422   * @param NewState: new state of the USART 8x oversampling mode.\r
423   *   This parameter can be: ENABLE or DISABLE.\r
424   *\r
425   * @note\r
426   *   This function has to be called before calling USART_Init()\r
427   *   function in order to have correct baudrate Divider value.\r
428   * @retval : None\r
429   */\r
430 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
431 {\r
432   /* Check the parameters */\r
433   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
434   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
435   \r
436   if (NewState != DISABLE)\r
437   {\r
438     /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */\r
439     USARTx->CR1 |= USART_CR1_OVER8;\r
440   }\r
441   else\r
442   {\r
443     /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */\r
444     USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_OVER8);\r
445   }\r
446 }  \r
447 \r
448 /**\r
449   * @brief  Enables or disables the USART's one bit sampling method.\r
450   * @param USARTx: Select the USART peripheral. \r
451   *   This parameter can be one of the following values:\r
452   *   USART1, USART2, USART3.\r
453   * @param NewState: new state of the USART one bit sampling method.\r
454   *   This parameter can be: ENABLE or DISABLE.\r
455   * @retval : None\r
456   */\r
457 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
458 {\r
459   /* Check the parameters */\r
460   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
461   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
462   \r
463   if (NewState != DISABLE)\r
464   {\r
465     /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */\r
466     USARTx->CR3 |= USART_CR3_ONEBIT;\r
467   }\r
468   else\r
469   {\r
470     /* Disable the one bit method by clearing the ONEBITE bit in the CR3 register */\r
471     USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT);\r
472   }\r
473 }\r
474 \r
475 /**\r
476   * @}\r
477   */\r
478 \r
479 /** @defgroup USART_Group2 Data transfers functions\r
480  *  @brief   Data transfers functions \r
481  *\r
482 @verbatim   \r
483  ===============================================================================\r
484                             Data transfers functions\r
485  ===============================================================================  \r
486 \r
487   This subsection provides a set of functions allowing to manage the USART data \r
488   transfers.\r
489   \r
490   During an USART reception, data shifts in least significant bit first through \r
491   the RX pin. In this mode, the USART_DR register consists of a buffer (RDR) \r
492   between the internal bus and the received shift register.\r
493 \r
494   When a transmission is taking place, a write instruction to the USART_DR register \r
495   stores the data in the TDR register and which is copied in the shift register \r
496   at the end of the current transmission.\r
497 \r
498   The read access of the USART_DR register can be done using the USART_ReceiveData()\r
499   function and returns the RDR buffered value. Whereas a write access to the USART_DR \r
500   can be done using USART_SendData() function and stores the written data into \r
501   TDR buffer.\r
502 \r
503 @endverbatim\r
504   * @{\r
505   */\r
506 \r
507 /**\r
508   * @brief  Transmits single data through the USARTx peripheral.\r
509   * @param  USARTx: Select the USART peripheral. \r
510   *   This parameter can be one of the following values:\r
511   *   USART1, USART2 or USART3.\r
512   * @param  Data: the data to transmit.\r
513   * @retval None\r
514   */\r
515 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)\r
516 {\r
517   /* Check the parameters */\r
518   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
519   assert_param(IS_USART_DATA(Data)); \r
520     \r
521   /* Transmit Data */\r
522   USARTx->DR = (Data & (uint16_t)0x01FF);\r
523 }\r
524 \r
525 /**\r
526   * @brief  Returns the most recent received data by the USARTx peripheral.\r
527   * @param  USARTx: Select the USART peripheral. \r
528   *   This parameter can be one of the following values:\r
529   *   USART1, USART2 or USART3.\r
530   * @retval The received data.\r
531   */\r
532 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)\r
533 {\r
534   /* Check the parameters */\r
535   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
536   \r
537   /* Receive Data */\r
538   return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);\r
539 }\r
540 \r
541 /**\r
542   * @}\r
543   */\r
544 \r
545 /** @defgroup USART_Group3 MultiProcessor Communication functions\r
546  *  @brief   Multi-Processor Communication functions \r
547  *\r
548 @verbatim   \r
549  ===============================================================================\r
550                     Multi-Processor Communication functions\r
551  ===============================================================================  \r
552 \r
553   This subsection provides a set of functions allowing to manage the USART \r
554   multiprocessor communication.\r
555   \r
556   For instance one of the USARTs can be the master, its TX output is connected to \r
557   the RX input of the other USART. The others are slaves, their respective TX outputs \r
558   are logically ANDed together and connected to the RX input of the master.\r
559 \r
560   USART multiprocessor communication is possible through the following procedure:\r
561      1. Program the Baud rate, Word length = 9 bits, Stop bits, Parity, Mode transmitter \r
562         or Mode receiver and hardware flow control values using the USART_Init()\r
563         function.\r
564      2. Configures the USART address using the USART_SetAddress() function.\r
565      3. Configures the wake up methode (USART_WakeUp_IdleLine or USART_WakeUp_AddressMark)\r
566         using USART_WakeUpConfig() function only for the slaves.\r
567      4. Enable the USART using the USART_Cmd() function.\r
568      5. Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd() function.\r
569 \r
570   The USART Slave exit from mute mode when receive the wake up condition.\r
571 \r
572 @endverbatim\r
573   * @{\r
574   */\r
575 \r
576 /**\r
577   * @brief  Sets the address of the USART node.\r
578   * @param  USARTx: Select the USART peripheral. \r
579   *   This parameter can be one of the following values:\r
580   *   USART1, USART2 or USART3.\r
581   * @param  USART_Address: Indicates the address of the USART node.\r
582   * @retval None\r
583   */\r
584 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)\r
585 {\r
586   /* Check the parameters */\r
587   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
588   assert_param(IS_USART_ADDRESS(USART_Address)); \r
589     \r
590   /* Clear the USART address */\r
591   USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_ADD);\r
592   /* Set the USART address node */\r
593   USARTx->CR2 |= USART_Address;\r
594 }\r
595 \r
596 /**\r
597   * @brief  Determines if the USART is in mute mode or not.\r
598   * @param  USARTx: Select the USART peripheral. \r
599   *   This parameter can be one of the following values:\r
600   *   USART1, USART2 or USART3.\r
601   * @param  NewState: new state of the USART mute mode.\r
602   *   This parameter can be: ENABLE or DISABLE.\r
603   * @retval None\r
604   */\r
605 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
606 {\r
607   /* Check the parameters */\r
608   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
609   assert_param(IS_FUNCTIONAL_STATE(NewState)); \r
610   \r
611   if (NewState != DISABLE)\r
612   {\r
613     /* Enable the USART mute mode  by setting the RWU bit in the CR1 register */\r
614     USARTx->CR1 |= USART_CR1_RWU;\r
615   }\r
616   else\r
617   {\r
618     /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */\r
619     USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_RWU);\r
620   }\r
621 }\r
622 /**\r
623   * @brief  Selects the USART WakeUp method.\r
624   * @param  USARTx: Select the USART peripheral. \r
625   *   This parameter can be one of the following values:\r
626   *   USART1, USART2 or USART3.\r
627   * @param  USART_WakeUp: specifies the USART wakeup method.\r
628   *   This parameter can be one of the following values:\r
629   *     @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection\r
630   *     @arg USART_WakeUp_AddressMark: WakeUp by an address mark\r
631   * @retval None\r
632   */\r
633 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)\r
634 {\r
635   /* Check the parameters */\r
636   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
637   assert_param(IS_USART_WAKEUP(USART_WakeUp));\r
638   \r
639   USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_WAKE);\r
640   USARTx->CR1 |= USART_WakeUp;\r
641 }\r
642 \r
643 /**\r
644   * @}\r
645   */\r
646 \r
647 /** @defgroup USART_Group4 LIN mode functions\r
648  *  @brief   LIN mode functions \r
649  *\r
650 @verbatim   \r
651  ===============================================================================\r
652                                 LIN mode functions\r
653  ===============================================================================  \r
654 \r
655   This subsection provides a set of functions allowing to manage the USART LIN \r
656   Mode communication.\r
657   \r
658   In LIN mode, 8-bit data format with 1 stop bit is required in accordance with \r
659   the LIN standard.\r
660 \r
661   Only this LIN Feature is supported by the USART IP:\r
662     - LIN Master Synchronous Break send capability and LIN slave break detection\r
663       capability :  13-bit break generation and 10/11 bit break detection\r
664 \r
665 \r
666   USART LIN Master transmitter communication is possible through the following procedure:\r
667      1. Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity, \r
668         Mode transmitter or Mode receiver and hardware flow control values using \r
669         the USART_Init() function.\r
670      2. Enable the USART using the USART_Cmd() function.\r
671      3. Enable the LIN mode using the USART_LINCmd() function.\r
672      4. Send the break character using USART_SendBreak() function.\r
673 \r
674   USART LIN Master receiver communication is possible through the following procedure:\r
675      1. Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity, \r
676         Mode transmitter or Mode receiver and hardware flow control values using \r
677         the USART_Init() function.\r
678      2. Enable the USART using the USART_Cmd() function.\r
679      3. Configures the break detection length using the USART_LINBreakDetectLengthConfig()\r
680         function.\r
681      4. Enable the LIN mode using the USART_LINCmd() function.\r
682 \r
683 Note:\r
684 ----\r
685   1. In LIN mode, the following bits must be kept cleared:\r
686      - CLKEN in the USART_CR2 register,\r
687      - STOP[1:0], SCEN, HDSEL and IREN in the USART_CR3 register.\r
688 \r
689 @endverbatim\r
690   * @{\r
691   */\r
692 \r
693 /**\r
694   * @brief  Sets the USART LIN Break detection length.\r
695   * @param  USARTx: Select the USART peripheral. \r
696   *   This parameter can be one of the following values:\r
697   *   USART1, USART2 or USART3.\r
698   * @param  USART_LINBreakDetectLength: specifies the LIN break detection length.\r
699   *   This parameter can be one of the following values:\r
700   *     @arg USART_LINBreakDetectLength_10b: 10-bit break detection\r
701   *     @arg USART_LINBreakDetectLength_11b: 11-bit break detection\r
702   * @retval None\r
703   */\r
704 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)\r
705 {\r
706   /* Check the parameters */\r
707   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
708   assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));\r
709   \r
710   USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LBDL);\r
711   USARTx->CR2 |= USART_LINBreakDetectLength;  \r
712 }\r
713 \r
714 /**\r
715   * @brief  Enables or disables the USART\92s LIN mode.\r
716   * @param  USARTx: Select the USART peripheral. \r
717   *   This parameter can be one of the following values:\r
718   *   USART1, USART2 or USART3.\r
719   * @param  NewState: new state of the USART LIN mode.\r
720   *   This parameter can be: ENABLE or DISABLE.\r
721   * @retval None\r
722   */\r
723 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
724 {\r
725   /* Check the parameters */\r
726   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
727   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
728   \r
729   if (NewState != DISABLE)\r
730   {\r
731     /* Enable the LIN mode by setting the LINEN bit in the CR2 register */\r
732     USARTx->CR2 |= USART_CR2_LINEN;\r
733   }\r
734   else\r
735   {\r
736     /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */\r
737     USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LINEN);\r
738   }\r
739 }\r
740 \r
741 /**\r
742   * @brief  Transmits break characters.\r
743   * @param  USARTx: Select the USART peripheral. \r
744   *   This parameter can be one of the following values:\r
745   *   USART1, USART2 or USART3.\r
746   * @retval None\r
747   */\r
748 void USART_SendBreak(USART_TypeDef* USARTx)\r
749 {\r
750   /* Check the parameters */\r
751   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
752   \r
753   /* Send break characters */\r
754   USARTx->CR1 |= USART_CR1_SBK;\r
755 }\r
756 \r
757 /**\r
758   * @}\r
759   */\r
760 \r
761 /** @defgroup USART_Group5 Halfduplex mode function\r
762  *  @brief   Half-duplex mode function \r
763  *\r
764 @verbatim   \r
765  ===============================================================================\r
766                          Half-duplex mode function\r
767  ===============================================================================  \r
768 \r
769   This subsection provides a set of functions allowing to manage the USART \r
770   Half-duplex communication.\r
771   \r
772   The USART can be configured to follow a single-wire half-duplex protocol where \r
773   the TX and RX lines are internally connected.\r
774 \r
775   USART Half duplex communication is possible through the following procedure:\r
776      1. Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter \r
777         or Mode receiver and hardware flow control values using the USART_Init()\r
778         function.\r
779      2. Configures the USART address using the USART_SetAddress() function.\r
780      3. Enable the USART using the USART_Cmd() function.\r
781      4. Enable the half duplex mode using USART_HalfDuplexCmd() function.\r
782 \r
783 Note:\r
784 ----\r
785   1. The RX pin is no longer used\r
786   2. In Half-duplex mode the following bits must be kept cleared:\r
787        - LINEN and CLKEN bits in the USART_CR2 register.\r
788        - SCEN and IREN bits in the USART_CR3 register.\r
789 \r
790 @endverbatim\r
791   * @{\r
792   */\r
793 \r
794 /**\r
795   * @brief  Enables or disables the USART\92s Half Duplex communication.\r
796   * @param  USARTx: Select the USART peripheral. \r
797   *   This parameter can be one of the following values:\r
798   *   USART1, USART2 or USART3.\r
799   * @param  NewState: new state of the USART Communication.\r
800   *   This parameter can be: ENABLE or DISABLE.\r
801   * @retval None\r
802   */\r
803 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
804 {\r
805   /* Check the parameters */\r
806   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
807   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
808   \r
809   if (NewState != DISABLE)\r
810   {\r
811     /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */\r
812     USARTx->CR3 |= USART_CR3_HDSEL;\r
813   }\r
814   else\r
815   {\r
816     /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */\r
817     USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_HDSEL);\r
818   }\r
819 }\r
820 \r
821 /**\r
822   * @}\r
823   */\r
824 \r
825 \r
826 /** @defgroup USART_Group6 Smartcard mode functions\r
827  *  @brief   Smartcard mode functions \r
828  *\r
829 @verbatim   \r
830  ===============================================================================\r
831                                Smartcard mode functions\r
832  ===============================================================================  \r
833 \r
834   This subsection provides a set of functions allowing to manage the USART \r
835   Smartcard communication.\r
836   \r
837   The Smartcard interface is designed to support asynchronous protocol Smartcards as\r
838   defined in the ISO 7816-3 standard.\r
839 \r
840   The USART can provide a clock to the smartcard through the SCLK output.\r
841   In smartcard mode, SCLK is not associated to the communication but is simply derived \r
842   from the internal peripheral input clock through a 5-bit prescaler.\r
843 \r
844   Smartcard communication is possible through the following procedure:\r
845      1. Configures the Smartcard Prsecaler using the USART_SetPrescaler() function.\r
846      2. Configures the Smartcard Guard Time using the USART_SetGuardTime() function.\r
847      3. Program the USART clock using the USART_ClockInit() function as following:\r
848         - USART Clock enabled\r
849         - USART CPOL Low\r
850         - USART CPHA on first edge\r
851         - USART Last Bit Clock Enabled\r
852      4. Program the Smartcard interface using the USART_Init() function as following:\r
853         - Word Length = 9 Bits\r
854         - 1.5 Stop Bit\r
855         - Even parity\r
856         - BaudRate = 12096 baud\r
857         - Hardware flow control disabled (RTS and CTS signals)\r
858         - Tx and Rx enabled\r
859      5. Optionally you can enable the parity error interrupt using the USART_ITConfig()\r
860         function\r
861      6. Enable the USART using the USART_Cmd() function.\r
862      7. Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function.\r
863      8. Enable the Smartcard interface using the USART_SmartCardCmd() function.\r
864 \r
865   Please refer to the ISO 7816-3 specification for more details.\r
866 \r
867 Note: \r
868 -----\r
869   1.  It is also possible to choose 0.5 stop bit for receiving but it is recommended \r
870       to use 1.5 stop bits for both transmitting and receiving to avoid switching \r
871       between the two configurations.\r
872   2. In smartcard mode, the following bits must be kept cleared:\r
873      - LINEN bit in the USART_CR2 register.\r
874      - HDSEL and IREN bits in the USART_CR3 register.\r
875 \r
876 @endverbatim\r
877   * @{\r
878   */\r
879 \r
880 /**\r
881   * @brief  Sets the specified USART guard time.\r
882   * @param  USARTx: Select the USART peripheral. \r
883   *   This parameter can be one of the following values:\r
884   *   USART1, USART2 or USART3.\r
885   * @param  USART_GuardTime: specifies the guard time.   \r
886   * @retval None\r
887   */\r
888 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)\r
889 {    \r
890   /* Check the parameters */\r
891   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
892   \r
893   /* Clear the USART Guard time */\r
894   USARTx->GTPR &= USART_GTPR_PSC;\r
895   /* Set the USART guard time */\r
896   USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);\r
897 }\r
898 \r
899 /**\r
900   * @brief  Enables or disables the USART\92s Smart Card mode.\r
901   * @param  USARTx: Select the USART peripheral. \r
902   *   This parameter can be one of the following values:\r
903   *   USART1, USART2 or USART3.\r
904   * @param  NewState: new state of the Smart Card mode.\r
905   *   This parameter can be: ENABLE or DISABLE.      \r
906   * @retval None\r
907   */\r
908 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
909 {\r
910   /* Check the parameters */\r
911   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
912   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
913   if (NewState != DISABLE)\r
914   {\r
915     /* Enable the SC mode by setting the SCEN bit in the CR3 register */\r
916     USARTx->CR3 |= USART_CR3_SCEN;\r
917   }\r
918   else\r
919   {\r
920     /* Disable the SC mode by clearing the SCEN bit in the CR3 register */\r
921     USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_SCEN);\r
922   }\r
923 }\r
924 \r
925 /**\r
926   * @brief  Enables or disables NACK transmission.\r
927   * @param  USARTx: Select the USART peripheral. \r
928   *   This parameter can be one of the following values:\r
929   *   USART1, USART2 or USART3.\r
930   * @param  NewState: new state of the NACK transmission.\r
931   *   This parameter can be: ENABLE or DISABLE.  \r
932   * @retval None\r
933   */\r
934 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
935 {\r
936   /* Check the parameters */\r
937   assert_param(IS_USART_ALL_PERIPH(USARTx)); \r
938   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
939   if (NewState != DISABLE)\r
940   {\r
941     /* Enable the NACK transmission by setting the NACK bit in the CR3 register */\r
942     USARTx->CR3 |= USART_CR3_NACK;\r
943   }\r
944   else\r
945   {\r
946     /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */\r
947     USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_NACK);\r
948   }\r
949 }\r
950 \r
951 /**\r
952   * @}\r
953   */\r
954 \r
955 /** @defgroup USART_Group7 IrDA mode functions\r
956  *  @brief   IrDA mode functions \r
957  *\r
958 @verbatim   \r
959  ===============================================================================\r
960                                 IrDA mode functions\r
961  ===============================================================================  \r
962 \r
963   This subsection provides a set of functions allowing to manage the USART \r
964   IrDA communication.\r
965   \r
966   IrDA is a half duplex communication protocol. If the Transmitter is busy, any data\r
967   on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver \r
968   is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.\r
969   While receiving data, transmission should be avoided as the data to be transmitted\r
970   could be corrupted.\r
971 \r
972   IrDA communication is possible through the following procedure:\r
973      1. Program the Baud rate, Word length = 8 bits, Stop bits, Parity, Transmitter/Receiver \r
974         modes and hardware flow control values using the USART_Init() function.\r
975      2. Enable the USART using the USART_Cmd() function.\r
976      3. Configures the IrDA pulse width by configuring the prescaler using  \r
977         the USART_SetPrescaler() function.\r
978      4. Configures the IrDA  USART_IrDAMode_LowPower or USART_IrDAMode_Normal mode\r
979         using the USART_IrDAConfig() function.\r
980      5. Enable the IrDA using the USART_IrDACmd() function.\r
981 \r
982 Note: \r
983 -----\r
984   1. A pulse of width less than two and greater than one PSC period(s) may or may\r
985      not be rejected.\r
986   2. The receiver set up time should be managed by software. The IrDA physical layer\r
987      specification specifies a minimum of 10 ms delay between transmission and \r
988      reception (IrDA is a half duplex protocol).\r
989   3. In IrDA mode, the following bits must be kept cleared:\r
990     - LINEN, STOP and CLKEN bits in the USART_CR2 register.\r
991     - SCEN and HDSEL bits in the USART_CR3 register.\r
992 \r
993 @endverbatim\r
994   * @{\r
995   */\r
996 \r
997 /**\r
998   * @brief  Configures the USART\92s IrDA interface.\r
999   * @param  USARTx: Select the USART peripheral. \r
1000   *   This parameter can be one of the following values:\r
1001   *   USART1, USART2 or USART3.\r
1002   * @param  USART_IrDAMode: specifies the IrDA mode.\r
1003   *   This parameter can be one of the following values:\r
1004   *     @arg USART_IrDAMode_LowPower\r
1005   *     @arg USART_IrDAMode_Normal\r
1006   * @retval None\r
1007   */\r
1008 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)\r
1009 {\r
1010   /* Check the parameters */\r
1011   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
1012   assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));\r
1013     \r
1014   USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IRLP);\r
1015   USARTx->CR3 |= USART_IrDAMode;\r
1016 }\r
1017 \r
1018 /**\r
1019   * @brief  Enables or disables the USART\92s IrDA interface.\r
1020   * @param  USARTx: Select the USART peripheral. \r
1021   *   This parameter can be one of the following values:\r
1022   *   USART1, USART2 or USART3.\r
1023   * @param  NewState: new state of the IrDA mode.\r
1024   *   This parameter can be: ENABLE or DISABLE.\r
1025   * @retval None\r
1026   */\r
1027 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
1028 {\r
1029   /* Check the parameters */\r
1030   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
1031   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1032     \r
1033   if (NewState != DISABLE)\r
1034   {\r
1035     /* Enable the IrDA mode by setting the IREN bit in the CR3 register */\r
1036     USARTx->CR3 |= USART_CR3_IREN;\r
1037   }\r
1038   else\r
1039   {\r
1040     /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */\r
1041     USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IREN);\r
1042   }\r
1043 }\r
1044 \r
1045 /**\r
1046   * @}\r
1047   */\r
1048 \r
1049 /** @defgroup USART_Group8 DMA transfers management functions\r
1050  *  @brief   DMA transfers management functions\r
1051  *\r
1052 @verbatim   \r
1053  ===============================================================================\r
1054                       DMA transfers management functions\r
1055  ===============================================================================  \r
1056 \r
1057 @endverbatim\r
1058   * @{\r
1059   */\r
1060   \r
1061 /**\r
1062   * @brief  Enables or disables the USART\92s DMA interface.\r
1063   * @param  USARTx: Select the USART peripheral. \r
1064   *   This parameter can be one of the following values:\r
1065   *   USART1, USART2 or USART3.\r
1066   * @param  USART_DMAReq: specifies the DMA request.\r
1067   *   This parameter can be any combination of the following values:\r
1068   *     @arg USART_DMAReq_Tx: USART DMA transmit request\r
1069   *     @arg USART_DMAReq_Rx: USART DMA receive request\r
1070   * @param  NewState: new state of the DMA Request sources.\r
1071   *   This parameter can be: ENABLE or DISABLE.  \r
1072   * @retval None\r
1073   */\r
1074 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)\r
1075 {\r
1076   /* Check the parameters */\r
1077   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
1078   assert_param(IS_USART_DMAREQ(USART_DMAReq));  \r
1079   assert_param(IS_FUNCTIONAL_STATE(NewState)); \r
1080 \r
1081   if (NewState != DISABLE)\r
1082   {\r
1083     /* Enable the DMA transfer for selected requests by setting the DMAT and/or\r
1084        DMAR bits in the USART CR3 register */\r
1085     USARTx->CR3 |= USART_DMAReq;\r
1086   }\r
1087   else\r
1088   {\r
1089     /* Disable the DMA transfer for selected requests by clearing the DMAT and/or\r
1090        DMAR bits in the USART CR3 register */\r
1091     USARTx->CR3 &= (uint16_t)~USART_DMAReq;\r
1092   }\r
1093 }\r
1094 \r
1095 /**\r
1096   * @}\r
1097   */\r
1098   \r
1099 /** @defgroup USART_Group9 Interrupts and flags management functions\r
1100  *  @brief   Interrupts and flags management functions \r
1101  *\r
1102 @verbatim   \r
1103  ===============================================================================\r
1104                    Interrupts and flags management functions\r
1105  ===============================================================================  \r
1106 \r
1107   This subsection provides a set of functions allowing to configure the USART \r
1108   Interrupts sources, DMA channels requests and check or clear the flags or \r
1109   pending bits status.\r
1110   The user should identify which mode will be used in his application to manage \r
1111   the communication: Polling mode, Interrupt mode or DMA mode. \r
1112     \r
1113   Polling Mode\r
1114   =============\r
1115   In Polling Mode, the SPI communication can be managed by 10 flags:\r
1116      1. USART_FLAG_TXE : to indicate the status of the transmit buffer register\r
1117      2. USART_FLAG_RXNE : to indicate the status of the receive buffer register\r
1118      3. USART_FLAG_TC : to indicate the status of the transmit operation\r
1119      4. USART_FLAG_IDLE : to indicate the status of the Idle Line             \r
1120      5. USART_FLAG_CTS : to indicate the status of the nCTS input\r
1121      6. USART_FLAG_LBD : to indicate the status of the LIN break detection\r
1122      7. USART_FLAG_NE : to indicate if a noise error occur\r
1123      8. USART_FLAG_FE : to indicate if a frame error occur\r
1124      9. USART_FLAG_PE : to indicate if a parity error occur\r
1125      10. USART_FLAG_ORE : to indicate if an Overrun error occur\r
1126 \r
1127   In this Mode it is advised to use the following functions:\r
1128       - FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);\r
1129       - void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);\r
1130 \r
1131   Interrupt Mode\r
1132   ===============\r
1133   In Interrupt Mode, the USART communication can be managed by 8 interrupt sources\r
1134   and 10 pending bits: \r
1135 \r
1136   Pending Bits:\r
1137   ------------- \r
1138      1. USART_IT_TXE : to indicate the status of the transmit buffer register\r
1139      2. USART_IT_RXNE : to indicate the status of the receive buffer register\r
1140      3. USART_IT_TC : to indicate the status of the transmit operation\r
1141      4. USART_IT_IDLE : to indicate the status of the Idle Line             \r
1142      5. USART_IT_CTS : to indicate the status of the nCTS input\r
1143      6. USART_IT_LBD : to indicate the status of the LIN break detection\r
1144      7. USART_IT_NE : to indicate if a noise error occur\r
1145      8. USART_IT_FE : to indicate if a frame error occur\r
1146      9. USART_IT_PE : to indicate if a parity error occur\r
1147      10. USART_IT_ORE : to indicate if an Overrun error occur\r
1148 \r
1149   Interrupt Source:\r
1150   -----------------\r
1151      1. USART_IT_TXE : specifies the interrupt source for the Tx buffer empty \r
1152                        interrupt. \r
1153      2. USART_IT_RXNE : specifies the interrupt source for the Rx buffer not \r
1154                         empty interrupt.\r
1155      3. USART_IT_TC : specifies the interrupt source for the Transmit complete \r
1156                        interrupt. \r
1157      4. USART_IT_IDLE : specifies the interrupt source for the Idle Line interrupt.             \r
1158      5. USART_IT_CTS : specifies the interrupt source for the CTS interrupt. \r
1159      6. USART_IT_LBD : specifies the interrupt source for the LIN break detection\r
1160                        interrupt. \r
1161      7. USART_IT_PE : specifies the interrupt source for theparity error interrupt. \r
1162      8. USART_IT_ERR :  specifies the interrupt source for the errors interrupt.\r
1163 \r
1164     Note: Some parameters are coded in order to use them as interrupt source or \r
1165     ----  as pending bits.\r
1166 \r
1167   In this Mode it is advised to use the following functions:\r
1168      - void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);\r
1169      - ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);\r
1170      - void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);\r
1171 \r
1172   DMA Mode\r
1173   ========\r
1174   In DMA Mode, the USART communication can be managed by 2 DMA Channel requests:\r
1175      1. USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request\r
1176      2. USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request\r
1177 \r
1178   In this Mode it is advised to use the following function:\r
1179      - void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);\r
1180 \r
1181 @endverbatim\r
1182   * @{\r
1183   */\r
1184 \r
1185 /**\r
1186   * @brief  Enables or disables the specified USART interrupts.\r
1187   * @param  USARTx: Select the USART peripheral. \r
1188   *   This parameter can be one of the following values:\r
1189   *   USART1, USART2 or USART3.\r
1190   * @param  USART_IT: specifies the USART interrupt sources to be enabled or disabled.\r
1191   *   This parameter can be one of the following values:\r
1192   *     @arg USART_IT_CTS:  CTS change interrupt\r
1193   *     @arg USART_IT_LBD:  LIN Break detection interrupt\r
1194   *     @arg USART_IT_TXE:  Tansmit Data Register empty interrupt\r
1195   *     @arg USART_IT_TC:   Transmission complete interrupt\r
1196   *     @arg USART_IT_RXNE: Receive Data register not empty interrupt\r
1197   *     @arg USART_IT_IDLE: Idle line detection interrupt\r
1198   *     @arg USART_IT_PE:   Parity Error interrupt\r
1199   *     @arg USART_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)\r
1200   * @param  NewState: new state of the specified USARTx interrupts.\r
1201   *   This parameter can be: ENABLE or DISABLE.\r
1202   * @retval None\r
1203   */\r
1204 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)\r
1205 {\r
1206   uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;\r
1207   uint32_t usartxbase = 0x00;\r
1208   /* Check the parameters */\r
1209   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
1210   assert_param(IS_USART_CONFIG_IT(USART_IT));\r
1211   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1212   \r
1213   usartxbase = (uint32_t)USARTx;\r
1214 \r
1215   /* Get the USART register index */\r
1216   usartreg = (((uint8_t)USART_IT) >> 0x05);\r
1217 \r
1218   /* Get the interrupt position */\r
1219   itpos = USART_IT & IT_MASK;\r
1220   itmask = (((uint32_t)0x01) << itpos);\r
1221     \r
1222   if (usartreg == 0x01) /* The IT is in CR1 register */\r
1223   {\r
1224     usartxbase += 0x0C;\r
1225   }\r
1226   else if (usartreg == 0x02) /* The IT is in CR2 register */\r
1227   {\r
1228     usartxbase += 0x10;\r
1229   }\r
1230   else /* The IT is in CR3 register */\r
1231   {\r
1232     usartxbase += 0x14; \r
1233   }\r
1234   if (NewState != DISABLE)\r
1235   {\r
1236     *(__IO uint32_t*)usartxbase  |= itmask;\r
1237   }\r
1238   else\r
1239   {\r
1240     *(__IO uint32_t*)usartxbase &= ~itmask;\r
1241   }\r
1242 }\r
1243 \r
1244 /**\r
1245   * @brief  Checks whether the specified USART flag is set or not.\r
1246   * @param  USARTx: Select the USART peripheral. \r
1247   *   This parameter can be one of the following values:\r
1248   *   USART1, USART2 or USART3.\r
1249   * @param  USART_FLAG: specifies the flag to check.\r
1250   *   This parameter can be one of the following values:\r
1251   *     @arg USART_FLAG_CTS:  CTS Change flag\r
1252   *     @arg USART_FLAG_LBD:  LIN Break detection flag\r
1253   *     @arg USART_FLAG_TXE:  Transmit data register empty flag\r
1254   *     @arg USART_FLAG_TC:   Transmission Complete flag\r
1255   *     @arg USART_FLAG_RXNE: Receive data register not empty flag\r
1256   *     @arg USART_FLAG_IDLE: Idle Line detection flag\r
1257   *     @arg USART_FLAG_ORE:  OverRun Error flag\r
1258   *     @arg USART_FLAG_NE:   Noise Error flag\r
1259   *     @arg USART_FLAG_FE:   Framing Error flag\r
1260   *     @arg USART_FLAG_PE:   Parity Error flag\r
1261   * @retval The new state of USART_FLAG (SET or RESET).\r
1262   */\r
1263 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)\r
1264 {\r
1265   FlagStatus bitstatus = RESET;\r
1266   /* Check the parameters */\r
1267   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
1268   assert_param(IS_USART_FLAG(USART_FLAG));\r
1269   \r
1270   if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)\r
1271   {\r
1272     bitstatus = SET;\r
1273   }\r
1274   else\r
1275   {\r
1276     bitstatus = RESET;\r
1277   }\r
1278   return bitstatus;\r
1279 }\r
1280 \r
1281 /**\r
1282   * @brief  Clears the USARTx's pending flags.\r
1283   * @param  USARTx: Select the USART peripheral. \r
1284   *   This parameter can be one of the following values:\r
1285   *   USART1, USART2 or USART3.\r
1286   * @param  USART_FLAG: specifies the flag to clear.\r
1287   *   This parameter can be any combination of the following values:\r
1288   *     @arg USART_FLAG_CTS:  CTS Change flag.\r
1289   *     @arg USART_FLAG_LBD:  LIN Break detection flag.\r
1290   *     @arg USART_FLAG_TC:   Transmission Complete flag.\r
1291   *     @arg USART_FLAG_RXNE: Receive data register not empty flag.\r
1292   *   \r
1293   * @note\r
1294   *   - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun \r
1295   *     error) and IDLE (Idle line detected) flags are cleared by software \r
1296   *     sequence: a read operation to USART_SR register (USART_GetFlagStatus()) \r
1297   *     followed by a read operation to USART_DR register (USART_ReceiveData()).\r
1298   *   - RXNE flag can be also cleared by a read to the USART_DR register \r
1299   *     (USART_ReceiveData()).\r
1300   *   - TC flag can be also cleared by software sequence: a read operation to \r
1301   *     USART_SR register (USART_GetFlagStatus()) followed by a write operation\r
1302   *     to USART_DR register (USART_SendData()).\r
1303   *   - TXE flag is cleared only by a write to the USART_DR register \r
1304   *     (USART_SendData()).\r
1305   * @retval None\r
1306   */\r
1307 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)\r
1308 {\r
1309   /* Check the parameters */\r
1310   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
1311   assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));\r
1312      \r
1313   USARTx->SR = (uint16_t)~USART_FLAG;\r
1314 }\r
1315 \r
1316 /**\r
1317   * @brief  Checks whether the specified USART interrupt has occurred or not.\r
1318   * @param  USARTx: Select the USART peripheral. \r
1319   *   This parameter can be one of the following values:\r
1320   *   USART1, USART2 or USART3.\r
1321   * @param  USART_IT: specifies the USART interrupt source to check.\r
1322   *   This parameter can be one of the following values:\r
1323   *     @arg USART_IT_CTS:  CTS change interrupt\r
1324   *     @arg USART_IT_LBD:  LIN Break detection interrupt\r
1325   *     @arg USART_IT_TXE:  Tansmit Data Register empty interrupt\r
1326   *     @arg USART_IT_TC:   Transmission complete interrupt\r
1327   *     @arg USART_IT_RXNE: Receive Data register not empty interrupt\r
1328   *     @arg USART_IT_IDLE: Idle line detection interrupt\r
1329   *     @arg USART_IT_ORE:  OverRun Error interrupt\r
1330   *     @arg USART_IT_NE:   Noise Error interrupt\r
1331   *     @arg USART_IT_FE:   Framing Error interrupt\r
1332   *     @arg USART_IT_PE:   Parity Error interrupt\r
1333   * @retval The new state of USART_IT (SET or RESET).\r
1334   */\r
1335 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)\r
1336 {\r
1337   uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;\r
1338   ITStatus bitstatus = RESET;\r
1339   /* Check the parameters */\r
1340   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
1341   assert_param(IS_USART_GET_IT(USART_IT)); \r
1342   \r
1343   /* Get the USART register index */\r
1344   usartreg = (((uint8_t)USART_IT) >> 0x05);\r
1345   /* Get the interrupt position */\r
1346   itmask = USART_IT & IT_MASK;\r
1347   itmask = (uint32_t)0x01 << itmask;\r
1348   \r
1349   if (usartreg == 0x01) /* The IT  is in CR1 register */\r
1350   {\r
1351     itmask &= USARTx->CR1;\r
1352   }\r
1353   else if (usartreg == 0x02) /* The IT  is in CR2 register */\r
1354   {\r
1355     itmask &= USARTx->CR2;\r
1356   }\r
1357   else /* The IT  is in CR3 register */\r
1358   {\r
1359     itmask &= USARTx->CR3;\r
1360   }\r
1361   \r
1362   bitpos = USART_IT >> 0x08;\r
1363   bitpos = (uint32_t)0x01 << bitpos;\r
1364   bitpos &= USARTx->SR;\r
1365   if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))\r
1366   {\r
1367     bitstatus = SET;\r
1368   }\r
1369   else\r
1370   {\r
1371     bitstatus = RESET;\r
1372   }\r
1373   \r
1374   return bitstatus;  \r
1375 }\r
1376 \r
1377 /**\r
1378   * @brief  Clears the USARTx\92s interrupt pending bits.\r
1379   * @param  USARTx: Select the USART peripheral. \r
1380   *   This parameter can be one of the following values:\r
1381   *   USART1, USART2 or USART3.\r
1382   * @param  USART_IT: specifies the interrupt pending bit to clear.\r
1383   *   This parameter can be one of the following values:\r
1384   *     @arg USART_IT_CTS:  CTS change interrupt\r
1385   *     @arg USART_IT_LBD:  LIN Break detection interrupt\r
1386   *     @arg USART_IT_TC:   Transmission complete interrupt. \r
1387   *     @arg USART_IT_RXNE: Receive Data register not empty interrupt.\r
1388   *   \r
1389   * @note\r
1390   *   - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun \r
1391   *     error) and IDLE (Idle line detected) pending bits are cleared by \r
1392   *     software sequence: a read operation to USART_SR register \r
1393   *     (USART_GetITStatus()) followed by a read operation to USART_DR register \r
1394   *     (USART_ReceiveData()).\r
1395   *   - RXNE pending bit can be also cleared by a read to the USART_DR register \r
1396   *     (USART_ReceiveData()).\r
1397   *   - TC pending bit can be also cleared by software sequence: a read \r
1398   *     operation to USART_SR register (USART_GetITStatus()) followed by a write \r
1399   *     operation to USART_DR register (USART_SendData()).\r
1400   *   - TXE pending bit is cleared only by a write to the USART_DR register \r
1401   *     (USART_SendData()).\r
1402   * @retval None\r
1403   */\r
1404 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)\r
1405 {\r
1406   uint16_t bitpos = 0x00, itmask = 0x00;\r
1407   /* Check the parameters */\r
1408   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
1409   assert_param(IS_USART_CLEAR_IT(USART_IT)); \r
1410   \r
1411   bitpos = USART_IT >> 0x08;\r
1412   itmask = ((uint16_t)0x01 << (uint16_t)bitpos);\r
1413   USARTx->SR = (uint16_t)~itmask;\r
1414 }\r
1415 \r
1416 /**\r
1417   * @}\r
1418   */\r
1419 \r
1420 /**\r
1421   * @}\r
1422   */\r
1423 \r
1424 /**\r
1425   * @}\r
1426   */\r
1427 \r
1428 /**\r
1429   * @}\r
1430   */\r
1431 \r
1432 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r