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