]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3XX/stm32f30x_usart.c
Add a qwerty layer
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3XX / stm32f30x_usart.c
1 /**
2   ******************************************************************************
3   * @file    stm32f30x_usart.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    27-February-2014
7   * @brief   This file provides firmware functions to manage the following
8   *          functionalities of the Universal synchronous asynchronous receiver
9   *          transmitter (USART):
10   *           + Initialization and Configuration
11   *           + STOP Mode
12   *           + AutoBaudRate
13   *           + Data transfers
14   *           + Multi-Processor Communication
15   *           + LIN mode
16   *           + Half-duplex mode
17   *           + Smartcard mode
18   *           + IrDA mode
19   *           + RS485 mode  
20   *           + DMA transfers management
21   *           + Interrupts and flags management
22   *           
23   *  @verbatim  
24  ===============================================================================
25                       ##### How to use this driver #####
26  ===============================================================================
27       [..]
28           (#) Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)
29              function for USART1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE)
30              function for USART2, USART3, UART4 and UART5.
31           (#)  According to the USART mode, enable the GPIO clocks using
32               RCC_AHBPeriphClockCmd() function. (The I/O can be TX, RX, CTS,
33               or and SCLK).
34           (#) Peripheral's alternate function:
35               (++) Connect the pin to the desired peripherals' Alternate
36                    Function (AF) using GPIO_PinAFConfig() function.
37               (++) Configure the desired pin in alternate function by:
38                    GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF.
39               (++) Select the type, pull-up/pull-down and output speed via
40                    GPIO_PuPd, GPIO_OType and GPIO_Speed members.
41               (++) Call GPIO_Init() function.
42           (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
43              flow control and Mode(Receiver/Transmitter) using the SPI_Init()
44              function.
45           (#) For synchronous mode, enable the clock and program the polarity,
46              phase and last bit using the USART_ClockInit() function.
47           (#) Enable the USART using the USART_Cmd() function.
48           (#) Enable the NVIC and the corresponding interrupt using the function
49              USART_ITConfig() if you need to use interrupt mode.
50           (#) When using the DMA mode:
51               (++) Configure the DMA using DMA_Init() function.
52               (++) Activate the needed channel Request using USART_DMACmd() function.
53           (#) Enable the DMA using the DMA_Cmd() function, when using DMA mode.
54       [..]
55               Refer to Multi-Processor, LIN, half-duplex, Smartcard, IrDA sub-sections
56               for more details.
57
58    @endverbatim
59
60   ******************************************************************************
61   * @attention
62   *
63   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
64   *
65   * Redistribution and use in source and binary forms, with or without modification,
66   * are permitted provided that the following conditions are met:
67   *   1. Redistributions of source code must retain the above copyright notice,
68   *      this list of conditions and the following disclaimer.
69   *   2. Redistributions in binary form must reproduce the above copyright notice,
70   *      this list of conditions and the following disclaimer in the documentation
71   *      and/or other materials provided with the distribution.
72   *   3. Neither the name of STMicroelectronics nor the names of its contributors
73   *      may be used to endorse or promote products derived from this software
74   *      without specific prior written permission.
75   *
76   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
77   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
79   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
80   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
81   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
82   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
83   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
84   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
85   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86   *
87   ******************************************************************************
88   */
89
90 /* Includes ------------------------------------------------------------------*/
91 #include "stm32f30x_usart.h"
92 #include "stm32f30x_rcc.h"
93
94 /** @addtogroup STM32F30x_StdPeriph_Driver
95   * @{
96   */
97
98 /** @defgroup USART 
99   * @brief USART driver modules
100   * @{
101   */
102
103 /* Private typedef -----------------------------------------------------------*/
104 /* Private define ------------------------------------------------------------*/
105
106 /*!< USART CR1 register clear Mask ((~(uint32_t)0xFFFFE6F3)) */
107 #define CR1_CLEAR_MASK            ((uint32_t)(USART_CR1_M | USART_CR1_PCE | \
108                                               USART_CR1_PS | USART_CR1_TE | \
109                                               USART_CR1_RE))
110
111 /*!< USART CR2 register clock bits clear Mask ((~(uint32_t)0xFFFFF0FF)) */
112 #define CR2_CLOCK_CLEAR_MASK      ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
113                                               USART_CR2_CPHA | USART_CR2_LBCL))
114
115 /*!< USART CR3 register clear Mask ((~(uint32_t)0xFFFFFCFF)) */
116 #define CR3_CLEAR_MASK            ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE))
117
118 /*!< USART Interrupts mask */
119 #define IT_MASK                   ((uint32_t)0x000000FF)
120
121 /* Private macro -------------------------------------------------------------*/
122 /* Private variables ---------------------------------------------------------*/
123 /* Private function prototypes -----------------------------------------------*/
124 /* Private functions ---------------------------------------------------------*/
125
126 /** @defgroup USART_Private_Functions
127   * @{
128   */
129
130 /** @defgroup USART_Group1 Initialization and Configuration functions
131  *  @brief   Initialization and Configuration functions 
132  *
133 @verbatim 
134  ===============================================================================
135            ##### Initialization and Configuration functions #####
136  ===============================================================================  
137   [..]
138       This subsection provides a set of functions allowing to initialize the USART 
139       in asynchronous and in synchronous modes.
140        (+) For the asynchronous mode only these parameters can be configured: 
141             (++) Baud Rate.
142             (++) Word Length. 
143             (++) Stop Bit.
144             (++) Parity: If the parity is enabled, then the MSB bit of the data written
145                  in the data register is transmitted but is changed by the parity bit.
146                  Depending on the frame length defined by the M bit (8-bits or 9-bits),
147                  the possible USART frame formats are as listed in the following table:
148     [..]
149    +-------------------------------------------------------------+     
150    |   M bit |  PCE bit  |            USART frame                |
151    |---------------------|---------------------------------------|             
152    |    0    |    0      |    | SB | 8 bit data | STB |          |
153    |---------|-----------|---------------------------------------|  
154    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
155    |---------|-----------|---------------------------------------|  
156    |    1    |    0      |    | SB | 9 bit data | STB |          |
157    |---------|-----------|---------------------------------------|  
158    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
159    +-------------------------------------------------------------+            
160     [..]
161            (++) Hardware flow control.
162            (++) Receiver/transmitter modes.
163     [..] The USART_Init() function follows the USART  asynchronous configuration 
164          procedure(details for the procedure are available in reference manual.
165         (+) For the synchronous mode in addition to the asynchronous mode parameters
166             these parameters should be also configured:
167             (++) USART Clock Enabled.
168             (++) USART polarity.
169             (++) USART phase.
170             (++) USART LastBit.
171     [..] These parameters can be configured using the USART_ClockInit() function.
172
173 @endverbatim
174   * @{
175   */
176   
177 /**
178   * @brief  Deinitializes the USARTx peripheral registers to their default reset values.
179   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
180   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
181   * @retval None
182   */
183 void USART_DeInit(USART_TypeDef* USARTx)
184 {
185   /* Check the parameters */
186   assert_param(IS_USART_ALL_PERIPH(USARTx));
187
188   if (USARTx == USART1)
189   {
190     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
191     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
192   }
193   else if (USARTx == USART2)
194   {
195     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
196     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
197   }
198   else if (USARTx == USART3)
199   {
200     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
201     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
202   }
203   else if (USARTx == UART4)
204   {
205     RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
206     RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
207   }
208   else
209   {
210     if  (USARTx == UART5)
211     {
212       RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
213       RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
214     }
215   }
216 }
217
218 /**
219   * @brief  Initializes the USARTx peripheral according to the specified
220   *         parameters in the USART_InitStruct .
221   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
222   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
223   * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure
224   *         that contains the configuration information for the specified USART peripheral.
225   * @retval None
226   */
227 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
228 {
229   uint32_t divider = 0, apbclock = 0, tmpreg = 0;
230   RCC_ClocksTypeDef RCC_ClocksStatus;
231   
232   /* Check the parameters */
233   assert_param(IS_USART_ALL_PERIPH(USARTx));
234   assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
235   assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
236   assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
237   assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
238   assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
239   assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
240   
241   /* Disable USART */
242   USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
243   
244   /*---------------------------- USART CR2 Configuration -----------------------*/
245   tmpreg = USARTx->CR2;
246   /* Clear STOP[13:12] bits */
247   tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
248   
249   /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
250   /* Set STOP[13:12] bits according to USART_StopBits value */
251   tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
252   
253   /* Write to USART CR2 */
254   USARTx->CR2 = tmpreg;
255   
256   /*---------------------------- USART CR1 Configuration -----------------------*/
257   tmpreg = USARTx->CR1;
258   /* Clear M, PCE, PS, TE and RE bits */
259   tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);
260   
261   /* Configure the USART Word Length, Parity and mode ----------------------- */
262   /* Set the M bits according to USART_WordLength value */
263   /* Set PCE and PS bits according to USART_Parity value */
264   /* Set TE and RE bits according to USART_Mode value */
265   tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
266     USART_InitStruct->USART_Mode;
267   
268   /* Write to USART CR1 */
269   USARTx->CR1 = tmpreg;
270   
271   /*---------------------------- USART CR3 Configuration -----------------------*/
272   tmpreg = USARTx->CR3;
273   /* Clear CTSE and RTSE bits */
274   tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);
275   
276   /* Configure the USART HFC -------------------------------------------------*/
277   /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
278   tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
279   
280   /* Write to USART CR3 */
281   USARTx->CR3 = tmpreg;
282   
283   /*---------------------------- USART BRR Configuration -----------------------*/
284   /* Configure the USART Baud Rate -------------------------------------------*/
285   RCC_GetClocksFreq(&RCC_ClocksStatus);
286   
287   if (USARTx == USART1)
288   {
289     apbclock = RCC_ClocksStatus.USART1CLK_Frequency;
290   }
291   else if (USARTx == USART2)
292   {
293     apbclock = RCC_ClocksStatus.USART2CLK_Frequency;
294   }
295   else if (USARTx == USART3)
296   {
297     apbclock = RCC_ClocksStatus.USART3CLK_Frequency;
298   }
299   else if (USARTx == UART4)
300   {
301     apbclock = RCC_ClocksStatus.UART4CLK_Frequency;
302   }
303   else 
304   {
305     apbclock = RCC_ClocksStatus.UART5CLK_Frequency;
306   }  
307   
308   /* Determine the integer part */
309   if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
310   {
311     /* (divider * 10) computing in case Oversampling mode is 8 Samples */
312     divider = (uint32_t)((2 * apbclock) / (USART_InitStruct->USART_BaudRate));
313     tmpreg  = (uint32_t)((2 * apbclock) % (USART_InitStruct->USART_BaudRate));
314   }
315   else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
316   {
317     /* (divider * 10) computing in case Oversampling mode is 16 Samples */
318     divider = (uint32_t)((apbclock) / (USART_InitStruct->USART_BaudRate));
319     tmpreg  = (uint32_t)((apbclock) % (USART_InitStruct->USART_BaudRate));
320   }
321   
322   /* round the divider : if fractional part i greater than 0.5 increment divider */
323   if (tmpreg >=  (USART_InitStruct->USART_BaudRate) / 2)
324   {
325     divider++;
326   } 
327   
328   /* Implement the divider in case Oversampling mode is 8 Samples */
329   if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
330   {
331     /* get the LSB of divider and shift it to the right by 1 bit */
332     tmpreg = (divider & (uint16_t)0x000F) >> 1;
333     
334     /* update the divider value */
335     divider = (divider & (uint16_t)0xFFF0) | tmpreg;
336   }
337   
338   /* Write to USART BRR */
339   USARTx->BRR = (uint16_t)divider;
340 }
341
342 /**
343   * @brief  Fills each USART_InitStruct member with its default value.
344   * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure
345   *         which will be initialized.
346   * @retval None
347   */
348 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
349 {
350   /* USART_InitStruct members default value */
351   USART_InitStruct->USART_BaudRate = 9600;
352   USART_InitStruct->USART_WordLength = USART_WordLength_8b;
353   USART_InitStruct->USART_StopBits = USART_StopBits_1;
354   USART_InitStruct->USART_Parity = USART_Parity_No ;
355   USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
356   USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
357 }
358
359 /**
360   * @brief  Initializes the USARTx peripheral Clock according to the
361   *         specified parameters in the USART_ClockInitStruct.
362   * @param  USARTx: Select the USART peripheral. This parameter can be one of the
363   *         following values: USART1 or USART2 or USART3.
364   * @param  USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
365   *         structure that contains the configuration information for the specified
366   *         USART peripheral.
367   * @retval None
368   */
369 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
370 {
371   uint32_t tmpreg = 0;
372   /* Check the parameters */
373   assert_param(IS_USART_123_PERIPH(USARTx));
374   assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
375   assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
376   assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
377   assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
378 /*---------------------------- USART CR2 Configuration -----------------------*/
379   tmpreg = USARTx->CR2;
380   /* Clear CLKEN, CPOL, CPHA, LBCL and SSM bits */
381   tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);
382   /* Configure the USART Clock, CPOL, CPHA, LastBit and SSM ------------*/
383   /* Set CLKEN bit according to USART_Clock value */
384   /* Set CPOL bit according to USART_CPOL value */
385   /* Set CPHA bit according to USART_CPHA value */
386   /* Set LBCL bit according to USART_LastBit value */
387   tmpreg |= (uint32_t)(USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL | 
388                        USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit);
389   /* Write to USART CR2 */
390   USARTx->CR2 = tmpreg;
391 }
392
393 /**
394   * @brief  Fills each USART_ClockInitStruct member with its default value.
395   * @param  USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
396   *         structure which will be initialized.
397   * @retval None
398   */
399 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
400 {
401   /* USART_ClockInitStruct members default value */
402   USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
403   USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
404   USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
405   USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
406 }
407
408 /**
409   * @brief  Enables or disables the specified USART peripheral.
410   * @param  USARTx: Select the USART peripheral. This parameter can be one of the
411   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
412   * @param  NewState: new state of the USARTx peripheral.
413   *         This parameter can be: ENABLE or DISABLE.
414   * @retval None
415   */
416 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
417 {
418   /* Check the parameters */
419   assert_param(IS_USART_ALL_PERIPH(USARTx));
420   assert_param(IS_FUNCTIONAL_STATE(NewState));
421   
422   if (NewState != DISABLE)
423   {
424     /* Enable the selected USART by setting the UE bit in the CR1 register */
425     USARTx->CR1 |= USART_CR1_UE;
426   }
427   else
428   {
429     /* Disable the selected USART by clearing the UE bit in the CR1 register */
430     USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
431   }
432 }
433
434 /**
435   * @brief  Enables or disables the USART's transmitter or receiver.
436   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
437   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
438   * @param  USART_Direction: specifies the USART direction.
439   *         This parameter can be any combination of the following values:
440   *         @arg USART_Mode_Tx: USART Transmitter
441   *         @arg USART_Mode_Rx: USART Receiver
442   * @param  NewState: new state of the USART transfer direction.
443   *         This parameter can be: ENABLE or DISABLE.  
444   * @retval None
445   */
446 void USART_DirectionModeCmd(USART_TypeDef* USARTx, uint32_t USART_DirectionMode, FunctionalState NewState)
447 {
448   /* Check the parameters */
449   assert_param(IS_USART_ALL_PERIPH(USARTx));
450   assert_param(IS_USART_MODE(USART_DirectionMode));
451   assert_param(IS_FUNCTIONAL_STATE(NewState)); 
452
453   if (NewState != DISABLE)
454   {
455     /* Enable the USART's transfer interface by setting the TE and/or RE bits 
456        in the USART CR1 register */
457     USARTx->CR1 |= USART_DirectionMode;
458   }
459   else
460   {
461     /* Disable the USART's transfer interface by clearing the TE and/or RE bits
462        in the USART CR3 register */
463     USARTx->CR1 &= (uint32_t)~USART_DirectionMode;
464   }
465 }
466
467 /**
468   * @brief  Enables or disables the USART's 8x oversampling mode.
469   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
470   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
471   * @param  NewState: new state of the USART 8x oversampling mode.
472   *         This parameter can be: ENABLE or DISABLE.
473   * @note
474   *   This function has to be called before calling USART_Init()
475   *   function in order to have correct baudrate Divider value.
476   * @retval None
477   */
478 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
479 {
480   /* Check the parameters */
481   assert_param(IS_USART_ALL_PERIPH(USARTx));
482   assert_param(IS_FUNCTIONAL_STATE(NewState));
483   
484   if (NewState != DISABLE)
485   {
486     /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
487     USARTx->CR1 |= USART_CR1_OVER8;
488   }
489   else
490   {
491     /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
492     USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_OVER8);
493   }
494 }
495
496 /**
497   * @brief  Enables or disables the USART's one bit sampling method.
498   * @param  USARTx: Select the USART peripheral. This parameter can be one of the
499   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
500   * @param  NewState: new state of the USART one bit sampling method.
501   *         This parameter can be: ENABLE or DISABLE.
502   * @note
503   *   This function has to be called before calling USART_Cmd() function.
504   * @retval None
505   */
506 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
507 {
508   /* Check the parameters */
509   assert_param(IS_USART_ALL_PERIPH(USARTx));
510   assert_param(IS_FUNCTIONAL_STATE(NewState));
511
512   if (NewState != DISABLE)
513   {
514     /* Enable the one bit method by setting the ONEBIT bit in the CR3 register */
515     USARTx->CR3 |= USART_CR3_ONEBIT;
516   }
517   else
518   {
519     /* Disable the one bit method by clearing the ONEBIT bit in the CR3 register */
520     USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_ONEBIT);
521   }
522 }
523
524 /**
525   * @brief  Enables or disables the USART's most significant bit first 
526   *         transmitted/received following the start bit.
527   * @param  USARTx: Select the USART peripheral. This parameter can be one of the
528   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
529   * @param  NewState: new state of the USART most significant bit first
530   *         transmitted/received following the start bit.
531   *         This parameter can be: ENABLE or DISABLE.
532   * @note
533   *   This function has to be called before calling USART_Cmd() function. 
534   * @retval None
535   */
536 void USART_MSBFirstCmd(USART_TypeDef* USARTx, FunctionalState NewState)
537 {
538   /* Check the parameters */
539   assert_param(IS_USART_ALL_PERIPH(USARTx));
540   assert_param(IS_FUNCTIONAL_STATE(NewState));
541
542   if (NewState != DISABLE)
543   {
544     /* Enable the most significant bit first transmitted/received following the
545        start bit by setting the MSBFIRST bit in the CR2 register */
546     USARTx->CR2 |= USART_CR2_MSBFIRST;
547   }
548   else
549   {
550     /* Disable the most significant bit first transmitted/received following the
551        start bit by clearing the MSBFIRST bit in the CR2 register */
552     USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_MSBFIRST);
553   }
554 }
555
556 /**
557   * @brief  Enables or disables the binary data inversion.
558   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
559   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
560   * @param  NewState: new defined levels for the USART data.
561   *         This parameter can be: ENABLE or DISABLE.
562   *   @arg  ENABLE: Logical data from the data register are send/received in negative
563   *         logic. (1=L, 0=H). The parity bit is also inverted.
564   *   @arg  DISABLE: Logical data from the data register are send/received in positive
565   *         logic. (1=H, 0=L) 
566   * @note
567   *   This function has to be called before calling USART_Cmd() function. 
568   * @retval None
569   */
570 void USART_DataInvCmd(USART_TypeDef* USARTx, FunctionalState NewState)
571 {
572   /* Check the parameters */
573   assert_param(IS_USART_ALL_PERIPH(USARTx));
574   assert_param(IS_FUNCTIONAL_STATE(NewState));
575
576   if (NewState != DISABLE)
577   {
578     /* Enable the binary data inversion feature by setting the DATAINV bit in
579        the CR2 register */
580     USARTx->CR2 |= USART_CR2_DATAINV;
581   }
582   else
583   {
584     /* Disable the binary data inversion feature by clearing the DATAINV bit in
585        the CR2 register */
586     USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_DATAINV);
587   }
588 }
589
590 /**
591   * @brief  Enables or disables the Pin(s) active level inversion.
592   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
593   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
594   * @param  USART_InvPin: specifies the USART pin(s) to invert.
595   *         This parameter can be any combination of the following values:
596   *         @arg USART_InvPin_Tx: USART Tx pin active level inversion.
597   *         @arg USART_InvPin_Rx: USART Rx pin active level inversion.
598   * @param  NewState: new active level status for the USART pin(s).
599   *         This parameter can be: ENABLE or DISABLE.
600   *          - ENABLE: pin(s) signal values are inverted (Vdd =0, Gnd =1).
601   *          - DISABLE: pin(s) signal works using the standard logic levels (Vdd =1, Gnd =0).
602   * @note
603   *   This function has to be called before calling USART_Cmd() function.  
604   * @retval None
605   */
606 void USART_InvPinCmd(USART_TypeDef* USARTx, uint32_t USART_InvPin, FunctionalState NewState)
607 {
608   /* Check the parameters */
609   assert_param(IS_USART_ALL_PERIPH(USARTx));
610   assert_param(IS_USART_INVERSTION_PIN(USART_InvPin));
611   assert_param(IS_FUNCTIONAL_STATE(NewState)); 
612
613   if (NewState != DISABLE)
614   {
615     /* Enable the active level inversion for selected pins by setting the TXINV 
616        and/or RXINV bits in the USART CR2 register */
617     USARTx->CR2 |= USART_InvPin;
618   }
619   else
620   {
621     /* Disable the active level inversion for selected requests by clearing the 
622        TXINV and/or RXINV bits in the USART CR2 register */
623     USARTx->CR2 &= (uint32_t)~USART_InvPin;
624   }
625 }
626
627 /**
628   * @brief  Enables or disables the swap Tx/Rx pins.
629   * @param  USARTx: Select the USART peripheral. This parameter can be one of the
630   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
631   * @param  NewState: new state of the USARTx TX/RX pins pinout.
632   *         This parameter can be: ENABLE or DISABLE.
633   *         @arg ENABLE: The TX and RX pins functions are swapped.
634   *         @arg DISABLE: TX/RX pins are used as defined in standard pinout
635   * @note
636   *   This function has to be called before calling USART_Cmd() function.
637   * @retval None
638   */
639 void USART_SWAPPinCmd(USART_TypeDef* USARTx, FunctionalState NewState)
640 {
641   /* Check the parameters */
642   assert_param(IS_USART_ALL_PERIPH(USARTx));
643   assert_param(IS_FUNCTIONAL_STATE(NewState));
644
645   if (NewState != DISABLE)
646   {
647     /* Enable the SWAP feature by setting the SWAP bit in the CR2 register */
648     USARTx->CR2 |= USART_CR2_SWAP;
649   }
650   else
651   {
652     /* Disable the SWAP feature by clearing the SWAP bit in the CR2 register */
653     USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_SWAP);
654   }
655 }
656
657 /**
658   * @brief  Enables or disables the receiver Time Out feature.
659   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
660   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
661   * @param  NewState: new state of the USARTx receiver Time Out.
662   *         This parameter can be: ENABLE or DISABLE.
663   * @retval None
664   */
665 void USART_ReceiverTimeOutCmd(USART_TypeDef* USARTx, FunctionalState NewState)
666 {
667   /* Check the parameters */
668   assert_param(IS_USART_ALL_PERIPH(USARTx));
669   assert_param(IS_FUNCTIONAL_STATE(NewState));
670
671   if (NewState != DISABLE)
672   {
673     /* Enable the receiver time out feature by setting the RTOEN bit in the CR2 
674        register */
675     USARTx->CR2 |= USART_CR2_RTOEN;
676   }
677   else
678   {
679     /* Disable the receiver time out feature by clearing the RTOEN bit in the CR2 
680        register */
681     USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_RTOEN);
682   }
683 }
684
685 /**
686   * @brief  Sets the receiver Time Out value.
687   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
688   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
689   * @param  USART_ReceiverTimeOut: specifies the Receiver Time Out value.
690   * @retval None
691   */
692 void USART_SetReceiverTimeOut(USART_TypeDef* USARTx, uint32_t USART_ReceiverTimeOut)
693 {    
694   /* Check the parameters */
695   assert_param(IS_USART_ALL_PERIPH(USARTx));
696   assert_param(IS_USART_TIMEOUT(USART_ReceiverTimeOut));
697
698   /* Clear the receiver Time Out value by clearing the RTO[23:0] bits in the RTOR
699      register  */
700   USARTx->RTOR &= (uint32_t)~((uint32_t)USART_RTOR_RTO);
701   /* Set the receiver Time Out value by setting the RTO[23:0] bits in the RTOR
702      register  */
703   USARTx->RTOR |= USART_ReceiverTimeOut;
704 }
705
706 /**
707   * @brief  Sets the system clock prescaler.
708   * @param  USARTx: Select the USART peripheral. This parameter can be one of the
709   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
710   * @param  USART_Prescaler: specifies the prescaler clock.  
711   * @note
712   *   This function has to be called before calling USART_Cmd() function.  
713   * @retval None
714   */
715 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
716 {
717   /* Check the parameters */
718   assert_param(IS_USART_ALL_PERIPH(USARTx));
719
720   /* Clear the USART prescaler */
721   USARTx->GTPR &= USART_GTPR_GT;
722   /* Set the USART prescaler */
723   USARTx->GTPR |= USART_Prescaler;
724 }
725
726 /**
727   * @}
728   */
729
730
731 /** @defgroup USART_Group2 STOP Mode functions
732  *  @brief   STOP Mode functions
733  *
734 @verbatim
735  ===============================================================================
736                         ##### STOP Mode functions #####
737  ===============================================================================
738     [..] This subsection provides a set of functions allowing to manage 
739          WakeUp from STOP mode.
740
741     [..] The USART is able to WakeUp from Stop Mode if USART clock is set to HSI
742          or LSI.
743          
744     [..] The WakeUp source is configured by calling USART_StopModeWakeUpSourceConfig()
745          function.
746          
747     [..] After configuring the source of WakeUp and before entering in Stop Mode 
748          USART_STOPModeCmd() function should be called to allow USART WakeUp.
749                            
750 @endverbatim
751   * @{
752   */
753   
754 /**
755   * @brief  Enables or disables the specified USART peripheral in STOP Mode.
756   * @param  USARTx: Select the USART peripheral. This parameter can be one of the
757   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
758   * @param  NewState: new state of the USARTx peripheral state in stop mode.
759   *         This parameter can be: ENABLE or DISABLE.
760   * @note
761   *   This function has to be called when USART clock is set to HSI or LSE.
762   * @retval None
763   */
764 void USART_STOPModeCmd(USART_TypeDef* USARTx, FunctionalState NewState)
765 {
766   /* Check the parameters */
767   assert_param(IS_USART_ALL_PERIPH(USARTx));
768   assert_param(IS_FUNCTIONAL_STATE(NewState));
769   
770   if (NewState != DISABLE)
771   {
772     /* Enable the selected USART in STOP mode by setting the UESM bit in the CR1
773        register */
774     USARTx->CR1 |= USART_CR1_UESM;
775   }
776   else
777   {
778     /* Disable the selected USART in STOP mode by clearing the UE bit in the CR1
779        register */
780     USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UESM);
781   }
782 }
783
784 /**
785   * @brief  Selects the USART WakeUp method form stop mode.
786   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
787   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
788   * @param  USART_WakeUp: specifies the selected USART wakeup method.
789   *         This parameter can be one of the following values:
790   *         @arg USART_WakeUpSource_AddressMatch: WUF active on address match.
791   *         @arg USART_WakeUpSource_StartBit: WUF active on Start bit detection.
792   *         @arg USART_WakeUpSource_RXNE: WUF active on RXNE.
793   * @note
794   *   This function has to be called before calling USART_Cmd() function.  
795   * @retval None
796   */
797 void USART_StopModeWakeUpSourceConfig(USART_TypeDef* USARTx, uint32_t USART_WakeUpSource)
798 {
799   /* Check the parameters */
800   assert_param(IS_USART_ALL_PERIPH(USARTx));
801   assert_param(IS_USART_STOPMODE_WAKEUPSOURCE(USART_WakeUpSource));
802
803   USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_WUS);
804   USARTx->CR3 |= USART_WakeUpSource;
805 }
806
807 /**
808   * @}
809   */
810
811
812 /** @defgroup USART_Group3 AutoBaudRate functions
813  *  @brief   AutoBaudRate functions 
814  *
815 @verbatim
816  ===============================================================================
817                        ##### AutoBaudRate functions #####
818  ===============================================================================
819     [..] This subsection provides a set of functions allowing to manage 
820          the AutoBaudRate detections.
821          
822     [..] Before Enabling AutoBaudRate detection using USART_AutoBaudRateCmd ()
823          The character patterns used to calculate baudrate must be chosen by calling 
824          USART_AutoBaudRateConfig() function. These function take as parameter :
825         (#)USART_AutoBaudRate_StartBit : any character starting with a bit 1.
826         (#)USART_AutoBaudRate_FallingEdge : any character starting with a 10xx bit pattern. 
827                           
828     [..] At any later time, another request for AutoBaudRate detection can be performed
829          using USART_RequestCmd() function.
830          
831     [..] The AutoBaudRate detection is monitored by the status of ABRF flag which indicate
832          that the AutoBaudRate detection is completed. In addition to ABRF flag, the ABRE flag
833          indicate that this procedure is completed without success. USART_GetFlagStatus () 
834          function should be used to monitor the status of these flags.  
835              
836 @endverbatim
837   * @{
838   */
839   
840 /**
841   * @brief  Enables or disables the Auto Baud Rate.
842   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
843   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
844   * @param  NewState: new state of the USARTx auto baud rate.
845   *         This parameter can be: ENABLE or DISABLE.
846   * @retval None
847   */
848 void USART_AutoBaudRateCmd(USART_TypeDef* USARTx, FunctionalState NewState)
849 {
850   /* Check the parameters */
851   assert_param(IS_USART_ALL_PERIPH(USARTx));
852   assert_param(IS_FUNCTIONAL_STATE(NewState));
853
854   if (NewState != DISABLE)
855   {
856     /* Enable the auto baud rate feature by setting the ABREN bit in the CR2 
857        register */
858     USARTx->CR2 |= USART_CR2_ABREN;
859   }
860   else
861   {
862     /* Disable the auto baud rate feature by clearing the ABREN bit in the CR2 
863        register */
864     USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABREN);
865   }
866 }
867
868 /**
869   * @brief  Selects the USART auto baud rate method.
870   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
871   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
872   * @param  USART_AutoBaudRate: specifies the selected USART auto baud rate method.
873   *         This parameter can be one of the following values:
874   *         @arg USART_AutoBaudRate_StartBit: Start Bit duration measurement.
875   *         @arg USART_AutoBaudRate_FallingEdge: Falling edge to falling edge measurement.
876   *         @arg USART_AutoBaudRate_0x7FFrame: 0x7F frame.
877   *         @arg USART_AutoBaudRate_0x55Frame: 0x55 frame.
878   * @note
879   *   This function has to be called before calling USART_Cmd() function. 
880   * @retval None
881   */
882 void USART_AutoBaudRateConfig(USART_TypeDef* USARTx, uint32_t USART_AutoBaudRate)
883 {
884   /* Check the parameters */
885   assert_param(IS_USART_ALL_PERIPH(USARTx));
886   assert_param(IS_USART_AUTOBAUDRATE_MODE(USART_AutoBaudRate));
887
888   USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABRMODE);
889   USARTx->CR2 |= USART_AutoBaudRate;
890 }
891
892 /**
893   * @}
894   */
895
896
897 /** @defgroup USART_Group4 Data transfers functions
898  *  @brief   Data transfers functions 
899  *
900 @verbatim
901  ===============================================================================
902                     ##### Data transfers functions #####
903  ===============================================================================
904     [..] This subsection provides a set of functions allowing to manage 
905          the USART data transfers.
906     [..] During an USART reception, data shifts in least significant bit first 
907          through the RX pin. When a transmission is taking place, a write instruction to 
908          the USART_TDR register stores the data in the shift register.
909     [..] The read access of the USART_RDR register can be done using 
910          the USART_ReceiveData() function and returns the RDR value.
911          Whereas a write access to the USART_TDR can be done using USART_SendData()
912          function and stores the written data into TDR.
913
914 @endverbatim
915   * @{
916   */
917
918 /**
919   * @brief  Transmits single data through the USARTx peripheral.
920   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
921   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
922   * @param  Data: the data to transmit.
923   * @retval None
924   */
925 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
926 {
927   /* Check the parameters */
928   assert_param(IS_USART_ALL_PERIPH(USARTx));
929   assert_param(IS_USART_DATA(Data)); 
930
931   /* Transmit Data */
932   USARTx->TDR = (Data & (uint16_t)0x01FF);
933 }
934
935 /**
936   * @brief  Returns the most recent received data by the USARTx peripheral.
937   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
938   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
939   * @retval The received data.
940   */
941 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
942 {
943   /* Check the parameters */
944   assert_param(IS_USART_ALL_PERIPH(USARTx));
945
946   /* Receive Data */
947   return (uint16_t)(USARTx->RDR & (uint16_t)0x01FF);
948 }
949
950 /**
951   * @}
952   */
953
954 /** @defgroup USART_Group5 MultiProcessor Communication functions
955  *  @brief   Multi-Processor Communication functions 
956  *
957 @verbatim   
958  ===============================================================================
959              ##### Multi-Processor Communication functions #####
960  ===============================================================================
961     [..] This subsection provides a set of functions allowing to manage the USART
962          multiprocessor communication.
963     [..] For instance one of the USARTs can be the master, its TX output is
964          connected to the RX input of the other USART. The others are slaves,
965          their respective TX outputs are logically ANDed together and connected 
966          to the RX input of the master. USART multiprocessor communication is 
967          possible through the following procedure:
968          (#) Program the Baud rate, Word length = 9 bits, Stop bits, Parity, 
969              Mode transmitter or Mode receiver and hardware flow control values 
970              using the USART_Init() function.
971          (#) Configures the USART address using the USART_SetAddress() function.
972          (#) Configures the wake up methode (USART_WakeUp_IdleLine or 
973              USART_WakeUp_AddressMark) using USART_WakeUpConfig() function only 
974              for the slaves.
975          (#) Enable the USART using the USART_Cmd() function.
976          (#) Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd() 
977              function.
978     [..] The USART Slave exit from mute mode when receive the wake up condition.
979
980 @endverbatim
981   * @{
982   */
983
984 /**
985   * @brief  Sets the address of the USART node.
986   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
987   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
988   * @param  USART_Address: Indicates the address of the USART node.
989   * @retval None
990   */
991 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
992 {
993   /* Check the parameters */
994   assert_param(IS_USART_ALL_PERIPH(USARTx));
995
996   /* Clear the USART address */
997   USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADD);
998   /* Set the USART address node */
999   USARTx->CR2 |=((uint32_t)USART_Address << (uint32_t)0x18);
1000 }
1001
1002 /**
1003   * @brief  Enables or disables the USART's mute mode.
1004   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1005   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1006   * @param  NewState: new state of the USART mute mode.
1007   *         This parameter can be: ENABLE or DISABLE.
1008   * @retval None
1009   */
1010 void USART_MuteModeCmd(USART_TypeDef* USARTx, FunctionalState NewState)
1011 {
1012   /* Check the parameters */
1013   assert_param(IS_USART_ALL_PERIPH(USARTx));
1014   assert_param(IS_FUNCTIONAL_STATE(NewState)); 
1015
1016   if (NewState != DISABLE)
1017   {
1018     /* Enable the USART mute mode by setting the MME bit in the CR1 register */
1019     USARTx->CR1 |= USART_CR1_MME;
1020   }
1021   else
1022   {
1023     /* Disable the USART mute mode by clearing the MME bit in the CR1 register */
1024     USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_MME);
1025   }
1026 }
1027
1028 /**
1029   * @brief  Selects the USART WakeUp method from mute mode.
1030   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1031   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1032   * @param  USART_WakeUp: specifies the USART wakeup method.
1033   *         This parameter can be one of the following values:
1034   *         @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
1035   *         @arg USART_WakeUp_AddressMark: WakeUp by an address mark
1036   * @retval None
1037   */
1038 void USART_MuteModeWakeUpConfig(USART_TypeDef* USARTx, uint32_t USART_WakeUp)
1039 {
1040   /* Check the parameters */
1041   assert_param(IS_USART_ALL_PERIPH(USARTx));
1042   assert_param(IS_USART_MUTEMODE_WAKEUP(USART_WakeUp));
1043
1044   USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_WAKE);
1045   USARTx->CR1 |= USART_WakeUp;
1046 }
1047
1048 /**
1049   * @brief  Configure the the USART Address detection length.
1050   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1051   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1052   * @param  USART_AddressLength: specifies the USART address length detection.
1053   *         This parameter can be one of the following values:
1054   *         @arg USART_AddressLength_4b: 4-bit address length detection 
1055   *         @arg USART_AddressLength_7b: 7-bit address length detection 
1056   * @retval None
1057   */
1058 void USART_AddressDetectionConfig(USART_TypeDef* USARTx, uint32_t USART_AddressLength)
1059 {
1060   /* Check the parameters */
1061   assert_param(IS_USART_ALL_PERIPH(USARTx));
1062   assert_param(IS_USART_ADDRESS_DETECTION(USART_AddressLength));
1063
1064   USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADDM7);
1065   USARTx->CR2 |= USART_AddressLength;
1066 }
1067
1068 /**
1069   * @}
1070   */
1071
1072 /** @defgroup USART_Group6 LIN mode functions
1073  *  @brief   LIN mode functions 
1074  *
1075 @verbatim   
1076  ===============================================================================
1077                        ##### LIN mode functions #####
1078  ===============================================================================
1079     [..] This subsection provides a set of functions allowing to manage the USART 
1080          LIN Mode communication.
1081     [..] In LIN mode, 8-bit data format with 1 stop bit is required in accordance 
1082          with the LIN standard.
1083     [..] Only this LIN Feature is supported by the USART IP:
1084          (+) LIN Master Synchronous Break send capability and LIN slave break 
1085              detection capability :  13-bit break generation and 10/11 bit break 
1086              detection.
1087     [..] USART LIN Master transmitter communication is possible through the 
1088          following procedure:
1089          (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity, 
1090              Mode transmitter or Mode receiver and hardware flow control values 
1091              using the USART_Init() function.
1092          (#) Enable the LIN mode using the USART_LINCmd() function.
1093          (#) Enable the USART using the USART_Cmd() function.
1094          (#) Send the break character using USART_SendBreak() function.
1095     [..] USART LIN Master receiver communication is possible through the 
1096          following procedure:
1097          (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity, 
1098              Mode transmitter or Mode receiver and hardware flow control values 
1099              using the USART_Init() function.
1100          (#) Configures the break detection length 
1101              using the USART_LINBreakDetectLengthConfig() function.
1102          (#) Enable the LIN mode using the USART_LINCmd() function.
1103          (#) Enable the USART using the USART_Cmd() function.
1104          [..]
1105          (@) In LIN mode, the following bits must be kept cleared:
1106              (+@) CLKEN in the USART_CR2 register.
1107              (+@) STOP[1:0], SCEN, HDSEL and IREN in the USART_CR3 register.
1108
1109 @endverbatim
1110   * @{
1111   */
1112
1113 /**
1114   * @brief  Sets the USART LIN Break detection length.
1115   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1116   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1117   * @param  USART_LINBreakDetectLength: specifies the LIN break detection length.
1118   *         This parameter can be one of the following values:
1119   *         @arg USART_LINBreakDetectLength_10b: 10-bit break detection
1120   *         @arg USART_LINBreakDetectLength_11b: 11-bit break detection
1121   * @retval None
1122   */
1123 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint32_t USART_LINBreakDetectLength)
1124 {
1125   /* Check the parameters */
1126   assert_param(IS_USART_ALL_PERIPH(USARTx));
1127   assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
1128
1129   USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_LBDL);
1130   USARTx->CR2 |= USART_LINBreakDetectLength;  
1131 }
1132
1133 /**
1134   * @brief  Enables or disables the USART's LIN mode.
1135   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1136   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1137   * @param  NewState: new state of the USART LIN mode.
1138   *         This parameter can be: ENABLE or DISABLE.
1139   * @retval None
1140   */
1141 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
1142 {
1143   /* Check the parameters */
1144   assert_param(IS_USART_ALL_PERIPH(USARTx));
1145   assert_param(IS_FUNCTIONAL_STATE(NewState));
1146
1147   if (NewState != DISABLE)
1148   {
1149     /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
1150     USARTx->CR2 |= USART_CR2_LINEN;
1151   }
1152   else
1153   {
1154     /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
1155     USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_LINEN);
1156   }
1157 }
1158
1159 /**
1160   * @}
1161   */
1162
1163 /** @defgroup USART_Group7 Halfduplex mode function
1164  *  @brief   Half-duplex mode function 
1165  *
1166 @verbatim   
1167  ===============================================================================
1168                    ##### Half-duplex mode function #####
1169  ===============================================================================
1170     [..] This subsection provides a set of functions allowing to manage the USART
1171          Half-duplex communication.
1172     [..] The USART can be configured to follow a single-wire half-duplex protocol 
1173          where the TX and RX lines are internally connected.
1174     [..] USART Half duplex communication is possible through the following procedure:
1175          (#) Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter 
1176              or Mode receiver and hardware flow control values using the USART_Init()
1177             function.
1178          (#) Configures the USART address using the USART_SetAddress() function.
1179          (#) Enable the half duplex mode using USART_HalfDuplexCmd() function.
1180          (#) Enable the USART using the USART_Cmd() function.
1181          [..]
1182          (@) The RX pin is no longer used.
1183          (@) In Half-duplex mode the following bits must be kept cleared:
1184              (+@) LINEN and CLKEN bits in the USART_CR2 register.
1185              (+@) SCEN and IREN bits in the USART_CR3 register.
1186
1187 @endverbatim
1188   * @{
1189   */
1190
1191 /**
1192   * @brief  Enables or disables the USART's Half Duplex communication.
1193   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1194   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1195   * @param  NewState: new state of the USART Communication.
1196   *         This parameter can be: ENABLE or DISABLE.
1197   * @retval None
1198   */
1199 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
1200 {
1201   /* Check the parameters */
1202   assert_param(IS_USART_ALL_PERIPH(USARTx));
1203   assert_param(IS_FUNCTIONAL_STATE(NewState));
1204   
1205   if (NewState != DISABLE)
1206   {
1207     /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
1208     USARTx->CR3 |= USART_CR3_HDSEL;
1209   }
1210   else
1211   {
1212     /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
1213     USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_HDSEL);
1214   }
1215 }
1216
1217 /**
1218   * @}
1219   */
1220
1221
1222 /** @defgroup USART_Group8 Smartcard mode functions
1223  *  @brief   Smartcard mode functions 
1224  *
1225 @verbatim   
1226  ===============================================================================
1227                      ##### Smartcard mode functions #####
1228  ===============================================================================
1229     [..] This subsection provides a set of functions allowing to manage the USART
1230          Smartcard communication.
1231     [..] The Smartcard interface is designed to support asynchronous protocol 
1232          Smartcards as defined in the ISO 7816-3 standard. The USART can provide 
1233          a clock to the smartcard through the SCLK output. In smartcard mode, 
1234          SCLK is not associated to the communication but is simply derived from 
1235          the internal peripheral input clock through a 5-bit prescaler.
1236     [..] Smartcard communication is possible through the following procedure:
1237          (#) Configures the Smartcard Prsecaler using the USART_SetPrescaler() 
1238              function.
1239          (#) Configures the Smartcard Guard Time using the USART_SetGuardTime() 
1240              function.
1241          (#) Program the USART clock using the USART_ClockInit() function as following:
1242              (++) USART Clock enabled.
1243              (++) USART CPOL Low.
1244              (++) USART CPHA on first edge.
1245              (++) USART Last Bit Clock Enabled.
1246          (#) Program the Smartcard interface using the USART_Init() function as 
1247              following:
1248              (++) Word Length = 9 Bits.
1249              (++) 1.5 Stop Bit.
1250              (++) Even parity.
1251              (++) BaudRate = 12096 baud.
1252              (++) Hardware flow control disabled (RTS and CTS signals).
1253              (++) Tx and Rx enabled
1254          (#) Optionally you can enable the parity error interrupt using 
1255              the USART_ITConfig() function.
1256          (#) Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function.
1257          (#) Enable the Smartcard interface using the USART_SmartCardCmd() function.
1258          (#) Enable the USART using the USART_Cmd() function.
1259     [..] 
1260   Please refer to the ISO 7816-3 specification for more details.
1261     [..] 
1262          (@) It is also possible to choose 0.5 stop bit for receiving but it is 
1263              recommended to use 1.5 stop bits for both transmitting and receiving 
1264              to avoid switching between the two configurations.
1265          (@) In smartcard mode, the following bits must be kept cleared:
1266              (+@) LINEN bit in the USART_CR2 register.
1267              (+@) HDSEL and IREN bits in the USART_CR3 register.
1268
1269 @endverbatim
1270   * @{
1271   */
1272
1273 /**
1274   * @brief  Sets the specified USART guard time.
1275   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1276   *         following values: USART1 or USART2 or USART3.
1277   * @param  USART_GuardTime: specifies the guard time.
1278   * @retval None
1279   */
1280 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
1281 {    
1282   /* Check the parameters */
1283   assert_param(IS_USART_123_PERIPH(USARTx));
1284
1285   /* Clear the USART Guard time */
1286   USARTx->GTPR &= USART_GTPR_PSC;
1287   /* Set the USART guard time */
1288   USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
1289 }
1290
1291 /**
1292   * @brief  Enables or disables the USART's Smart Card mode.
1293   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1294   *         following values: USART1 or USART2 or USART3.
1295   * @param  NewState: new state of the Smart Card mode.
1296   *         This parameter can be: ENABLE or DISABLE.      
1297   * @retval None
1298   */
1299 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
1300 {
1301   /* Check the parameters */
1302   assert_param(IS_USART_123_PERIPH(USARTx));
1303   assert_param(IS_FUNCTIONAL_STATE(NewState));
1304   if (NewState != DISABLE)
1305   {
1306     /* Enable the SC mode by setting the SCEN bit in the CR3 register */
1307     USARTx->CR3 |= USART_CR3_SCEN;
1308   }
1309   else
1310   {
1311     /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
1312     USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_SCEN);
1313   }
1314 }
1315
1316 /**
1317   * @brief  Enables or disables NACK transmission.
1318   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1319   *         following values: USART1 or USART2 or USART3.
1320   * @param  NewState: new state of the NACK transmission.
1321   *         This parameter can be: ENABLE or DISABLE.  
1322   * @retval None
1323   */
1324 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
1325 {
1326   /* Check the parameters */
1327   assert_param(IS_USART_123_PERIPH(USARTx)); 
1328   assert_param(IS_FUNCTIONAL_STATE(NewState));
1329   if (NewState != DISABLE)
1330   {
1331     /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
1332     USARTx->CR3 |= USART_CR3_NACK;
1333   }
1334   else
1335   {
1336     /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
1337     USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_NACK);
1338   }
1339 }
1340
1341 /**
1342   * @brief  Sets the Smart Card number of retries in transmit and receive.
1343   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1344   *         following values: USART1 or USART2 or USART3.
1345   * @param  USART_AutoCount: specifies the Smart Card auto retry count.
1346   * @retval None
1347   */
1348 void USART_SetAutoRetryCount(USART_TypeDef* USARTx, uint8_t USART_AutoCount)
1349 {    
1350   /* Check the parameters */
1351   assert_param(IS_USART_123_PERIPH(USARTx));
1352   assert_param(IS_USART_AUTO_RETRY_COUNTER(USART_AutoCount));
1353   /* Clear the USART auto retry count */
1354   USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_SCARCNT);
1355   /* Set the USART auto retry count*/
1356   USARTx->CR3 |= (uint32_t)((uint32_t)USART_AutoCount << 0x11);
1357 }
1358
1359 /**
1360   * @brief  Sets the Smart Card Block length.
1361   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1362   *         following values: USART1 or USART2 or USART3.
1363   * @param  USART_BlockLength: specifies the Smart Card block length.
1364   * @retval None
1365   */
1366 void USART_SetBlockLength(USART_TypeDef* USARTx, uint8_t USART_BlockLength)
1367 {    
1368   /* Check the parameters */
1369   assert_param(IS_USART_123_PERIPH(USARTx));
1370
1371   /* Clear the Smart card block length */
1372   USARTx->RTOR &= (uint32_t)~((uint32_t)USART_RTOR_BLEN);
1373   /* Set the Smart Card block length */
1374   USARTx->RTOR |= (uint32_t)((uint32_t)USART_BlockLength << 0x18);
1375 }
1376
1377 /**
1378   * @}
1379   */
1380
1381 /** @defgroup USART_Group9 IrDA mode functions
1382  *  @brief   IrDA mode functions 
1383  *
1384 @verbatim   
1385  ===============================================================================
1386                         ##### IrDA mode functions #####
1387  ===============================================================================
1388     [..] This subsection provides a set of functions allowing to manage the USART
1389          IrDA communication.
1390     [..] IrDA is a half duplex communication protocol. If the Transmitter is busy, 
1391          any data on the IrDA receive line will be ignored by the IrDA decoder 
1392          and if the Receiver is busy, data on the TX from the USART to IrDA will 
1393          not be encoded by IrDA. While receiving data, transmission should be 
1394          avoided as the data to be transmitted could be corrupted.
1395     [..] IrDA communication is possible through the following procedure:
1396          (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity, 
1397              Transmitter/Receiver modes and hardware flow control values using 
1398              the USART_Init() function.
1399          (#) Configures the IrDA pulse width by configuring the prescaler using  
1400              the USART_SetPrescaler() function.
1401          (#) Configures the IrDA  USART_IrDAMode_LowPower or USART_IrDAMode_Normal 
1402              mode using the USART_IrDAConfig() function.
1403          (#) Enable the IrDA using the USART_IrDACmd() function.
1404          (#) Enable the USART using the USART_Cmd() function.         
1405     [..]
1406     (@) A pulse of width less than two and greater than one PSC period(s) may or 
1407         may not be rejected.
1408     (@) The receiver set up time should be managed by software. The IrDA physical 
1409         layer specification specifies a minimum of 10 ms delay between 
1410         transmission and reception (IrDA is a half duplex protocol).
1411     (@) In IrDA mode, the following bits must be kept cleared:
1412         (+@) LINEN, STOP and CLKEN bits in the USART_CR2 register.
1413         (+@) SCEN and HDSEL bits in the USART_CR3 register.
1414
1415 @endverbatim
1416   * @{
1417   */
1418
1419 /**
1420   * @brief  Configures the USART's IrDA interface.
1421   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1422   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1423   * @param  USART_IrDAMode: specifies the IrDA mode.
1424   *         This parameter can be one of the following values:
1425   *         @arg USART_IrDAMode_LowPower
1426   *         @arg USART_IrDAMode_Normal
1427   * @retval None
1428   */
1429 void USART_IrDAConfig(USART_TypeDef* USARTx, uint32_t USART_IrDAMode)
1430 {
1431   /* Check the parameters */
1432   assert_param(IS_USART_ALL_PERIPH(USARTx));
1433   assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
1434
1435   USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_IRLP);
1436   USARTx->CR3 |= USART_IrDAMode;
1437 }
1438
1439 /**
1440   * @brief  Enables or disables the USART's IrDA interface. 
1441   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1442   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1443   * @param  NewState: new state of the IrDA mode.
1444   *         This parameter can be: ENABLE or DISABLE.
1445   * @retval None
1446   */
1447 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
1448 {
1449   /* Check the parameters */
1450   assert_param(IS_USART_ALL_PERIPH(USARTx));
1451   assert_param(IS_FUNCTIONAL_STATE(NewState));
1452
1453   if (NewState != DISABLE)
1454   {
1455     /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
1456     USARTx->CR3 |= USART_CR3_IREN;
1457   }
1458   else
1459   {
1460     /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
1461     USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_IREN);
1462   }
1463 }
1464 /**
1465   * @}
1466   */
1467
1468 /** @defgroup USART_Group10 RS485 mode function
1469  *  @brief   RS485 mode function 
1470  *
1471 @verbatim  
1472  ===============================================================================
1473                         ##### RS485 mode functions #####
1474  ===============================================================================
1475     [..] This subsection provides a set of functions allowing to manage the USART
1476          RS485 flow control.
1477     [..] RS485 flow control (Driver enable feature) handling is possible through
1478          the following procedure:
1479          (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity, 
1480              Transmitter/Receiver modes and hardware flow control values using 
1481              the USART_Init() function.
1482          (#) Enable the Driver Enable using the USART_DECmd() function.
1483          (#) Configures the Driver Enable polarity using the USART_DEPolarityConfig()
1484              function.
1485          (#) Configures the Driver Enable assertion time using USART_SetDEAssertionTime() 
1486              function and deassertion time using the USART_SetDEDeassertionTime()
1487              function.    
1488          (#) Enable the USART using the USART_Cmd() function.
1489       [..]  
1490        (@) The assertion and dessertion times are expressed in sample time units (1/8 or 
1491             1/16 bit time, depending on the oversampling rate).
1492        
1493 @endverbatim
1494   * @{
1495   */
1496
1497 /**
1498   * @brief  Enables or disables the USART's DE functionality.
1499   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1500   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1501   * @param  NewState: new state of the driver enable mode.
1502   *         This parameter can be: ENABLE or DISABLE.      
1503   * @retval None
1504   */
1505 void USART_DECmd(USART_TypeDef* USARTx, FunctionalState NewState)
1506 {
1507   /* Check the parameters */
1508   assert_param(IS_USART_ALL_PERIPH(USARTx));
1509   assert_param(IS_FUNCTIONAL_STATE(NewState));
1510   if (NewState != DISABLE)
1511   {
1512     /* Enable the DE functionality by setting the DEM bit in the CR3 register */
1513     USARTx->CR3 |= USART_CR3_DEM;
1514   }
1515   else
1516   {
1517     /* Disable the DE functionality by clearing the DEM bit in the CR3 register */
1518     USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DEM);
1519   }
1520 }
1521
1522 /**
1523   * @brief  Configures the USART's DE polarity
1524   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1525   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1526   * @param  USART_DEPolarity: specifies the DE polarity.
1527   *         This parameter can be one of the following values:
1528   *         @arg USART_DEPolarity_Low
1529   *         @arg USART_DEPolarity_High
1530   * @retval None
1531   */
1532 void USART_DEPolarityConfig(USART_TypeDef* USARTx, uint32_t USART_DEPolarity)
1533 {
1534   /* Check the parameters */
1535   assert_param(IS_USART_ALL_PERIPH(USARTx));
1536   assert_param(IS_USART_DE_POLARITY(USART_DEPolarity));
1537
1538   USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DEP);
1539   USARTx->CR3 |= USART_DEPolarity;
1540 }
1541
1542 /**
1543   * @brief  Sets the specified RS485 DE assertion time
1544   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1545   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1546   * @param  USART_AssertionTime: specifies the time between the activation of the DE
1547   *          signal and the beginning of the start bit
1548   * @retval None
1549   */
1550 void USART_SetDEAssertionTime(USART_TypeDef* USARTx, uint32_t USART_DEAssertionTime)
1551 {
1552   /* Check the parameters */
1553   assert_param(IS_USART_ALL_PERIPH(USARTx));
1554   assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEAssertionTime)); 
1555
1556   /* Clear the DE assertion time */
1557   USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_DEAT);
1558   /* Set the new value for the DE assertion time */
1559   USARTx->CR1 |=((uint32_t)USART_DEAssertionTime << (uint32_t)0x15);
1560 }
1561
1562 /**
1563   * @brief  Sets the specified RS485 DE deassertion time
1564   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1565   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1566   * @param  USART_DeassertionTime: specifies the time between the middle of the last 
1567   *         stop bit in a transmitted message and the de-activation of the DE signal
1568   * @retval None
1569   */
1570 void USART_SetDEDeassertionTime(USART_TypeDef* USARTx, uint32_t USART_DEDeassertionTime)
1571 {
1572   /* Check the parameters */
1573   assert_param(IS_USART_ALL_PERIPH(USARTx));
1574   assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEDeassertionTime)); 
1575
1576   /* Clear the DE deassertion time */
1577   USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_DEDT);
1578   /* Set the new value for the DE deassertion time */
1579   USARTx->CR1 |=((uint32_t)USART_DEDeassertionTime << (uint32_t)0x10);
1580 }
1581
1582 /**
1583   * @}
1584   */
1585
1586 /** @defgroup USART_Group11 DMA transfers management functions
1587  *  @brief   DMA transfers management functions
1588  *
1589 @verbatim   
1590  ===============================================================================
1591                ##### DMA transfers management functions #####
1592  ===============================================================================
1593     [..] This section provides two functions that can be used only in DMA mode.
1594     [..] In DMA Mode, the USART communication can be managed by 2 DMA Channel 
1595          requests:
1596          (#) USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request.
1597          (#) USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request.
1598     [..] In this Mode it is advised to use the following function:
1599          (+) void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, 
1600              FunctionalState NewState).
1601 @endverbatim
1602   * @{
1603   */
1604
1605 /**
1606   * @brief  Enables or disables the USART's DMA interface.
1607   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1608   *         following values: USART1 or USART2 or USART3 or UART4.
1609   * @param  USART_DMAReq: specifies the DMA request.
1610   *         This parameter can be any combination of the following values:
1611   *         @arg USART_DMAReq_Tx: USART DMA transmit request
1612   *         @arg USART_DMAReq_Rx: USART DMA receive request
1613   * @param  NewState: new state of the DMA Request sources.
1614   *         This parameter can be: ENABLE or DISABLE.  
1615   * @retval None
1616   */
1617 void USART_DMACmd(USART_TypeDef* USARTx, uint32_t USART_DMAReq, FunctionalState NewState)
1618 {
1619   /* Check the parameters */
1620   assert_param(IS_USART_1234_PERIPH(USARTx));
1621   assert_param(IS_USART_DMAREQ(USART_DMAReq));  
1622   assert_param(IS_FUNCTIONAL_STATE(NewState)); 
1623
1624   if (NewState != DISABLE)
1625   {
1626     /* Enable the DMA transfer for selected requests by setting the DMAT and/or
1627        DMAR bits in the USART CR3 register */
1628     USARTx->CR3 |= USART_DMAReq;
1629   }
1630   else
1631   {
1632     /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
1633        DMAR bits in the USART CR3 register */
1634     USARTx->CR3 &= (uint32_t)~USART_DMAReq;
1635   }
1636 }
1637
1638 /**
1639   * @brief  Enables or disables the USART's DMA interface when reception error occurs.
1640   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1641   *         following values: USART1 or USART2 or USART3 or UART4.
1642   * @param  USART_DMAOnError: specifies the DMA status in case of reception error.
1643   *         This parameter can be any combination of the following values:
1644   *         @arg USART_DMAOnError_Enable: DMA receive request enabled when the USART DMA  
1645   *          reception error is asserted.
1646   *         @arg USART_DMAOnError_Disable: DMA receive request disabled when the USART DMA 
1647   *          reception error is asserted.
1648   * @retval None
1649   */
1650 void USART_DMAReceptionErrorConfig(USART_TypeDef* USARTx, uint32_t USART_DMAOnError)
1651 {
1652   /* Check the parameters */
1653   assert_param(IS_USART_1234_PERIPH(USARTx));
1654   assert_param(IS_USART_DMAONERROR(USART_DMAOnError)); 
1655   
1656   /* Clear the DMA Reception error detection bit */
1657   USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DDRE);
1658   /* Set the new value for the DMA Reception error detection bit */
1659   USARTx->CR3 |= USART_DMAOnError;
1660 }
1661
1662 /**
1663   * @}
1664   */
1665   
1666 /** @defgroup USART_Group12 Interrupts and flags management functions
1667  *  @brief   Interrupts and flags management functions 
1668  *
1669 @verbatim   
1670  ===============================================================================
1671             ##### Interrupts and flags management functions #####
1672  ===============================================================================
1673     [..] This subsection provides a set of functions allowing to configure the 
1674          USART Interrupts sources, Requests and check or clear the flags or pending bits status. 
1675          The user should identify which mode will be used in his application to 
1676          manage the communication: Polling mode, Interrupt mode.
1677          
1678  *** Polling Mode ***
1679  ====================
1680     [..] In Polling Mode, the SPI communication can be managed by these flags:
1681          (#) USART_FLAG_REACK: to indicate the status of the Receive Enable 
1682              acknowledge flag
1683          (#) USART_FLAG_TEACK: to indicate the status of the Transmit Enable 
1684              acknowledge flag.
1685          (#) USART_FLAG_WUF: to indicate the status of the Wake up flag.
1686          (#) USART_FLAG_RWU: to indicate the status of the Receive Wake up flag.
1687          (#) USART_FLAG_SBK: to indicate the status of the Send Break flag.
1688          (#) USART_FLAG_CMF: to indicate the status of the Character match flag.
1689          (#) USART_FLAG_BUSY: to indicate the status of the Busy flag.
1690          (#) USART_FLAG_ABRF: to indicate the status of the Auto baud rate flag.
1691          (#) USART_FLAG_ABRE: to indicate the status of the Auto baud rate error flag.
1692          (#) USART_FLAG_EOBF: to indicate the status of the End of block flag.
1693          (#) USART_FLAG_RTOF: to indicate the status of the Receive time out flag.
1694          (#) USART_FLAG_nCTSS: to indicate the status of the Inverted nCTS input 
1695              bit status.
1696          (#) USART_FLAG_TXE: to indicate the status of the transmit buffer register.
1697          (#) USART_FLAG_RXNE: to indicate the status of the receive buffer register.
1698          (#) USART_FLAG_TC: to indicate the status of the transmit operation.
1699          (#) USART_FLAG_IDLE: to indicate the status of the Idle Line.
1700          (#) USART_FLAG_CTS: to indicate the status of the nCTS input.
1701          (#) USART_FLAG_LBD: to indicate the status of the LIN break detection.
1702          (#) USART_FLAG_NE: to indicate if a noise error occur.
1703          (#) USART_FLAG_FE: to indicate if a frame error occur.
1704          (#) USART_FLAG_PE: to indicate if a parity error occur.
1705          (#) USART_FLAG_ORE: to indicate if an Overrun error occur.
1706     [..] In this Mode it is advised to use the following functions:
1707          (+) FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG).
1708          (+) void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG).
1709                
1710  *** Interrupt Mode ***
1711  ======================
1712     [..] In Interrupt Mode, the USART communication can be managed by 8 interrupt 
1713          sources and 10 pending bits:
1714          (+) Pending Bits:
1715              (##) USART_IT_WU: to indicate the status of the Wake up interrupt.
1716              (##) USART_IT_CM: to indicate the status of Character match interrupt.
1717              (##) USART_IT_EOB: to indicate the status of End of block interrupt.
1718              (##) USART_IT_RTO: to indicate the status of Receive time out interrupt.
1719              (##) USART_IT_CTS: to indicate the status of CTS change interrupt.
1720              (##) USART_IT_LBD: to indicate the status of LIN Break detection interrupt.
1721              (##) USART_IT_TC: to indicate the status of Transmission complete interrupt.
1722              (##) USART_IT_IDLE: to indicate the status of IDLE line detected interrupt.
1723              (##) USART_IT_ORE: to indicate the status of OverRun Error interrupt.
1724              (##) USART_IT_NE: to indicate the status of Noise Error interrupt.
1725              (##) USART_IT_FE: to indicate the status of Framing Error interrupt.
1726              (##) USART_IT_PE: to indicate the status of Parity Error interrupt.  
1727               
1728          (+) Interrupt Source:
1729              (##) USART_IT_WU: specifies the interrupt source for Wake up interrupt.
1730              (##) USART_IT_CM: specifies the interrupt source for Character match 
1731                   interrupt.
1732              (##) USART_IT_EOB: specifies the interrupt source for End of block
1733                   interrupt.
1734              (##) USART_IT_RTO: specifies the interrupt source for Receive time-out
1735                   interrupt.
1736              (##) USART_IT_CTS: specifies the interrupt source for CTS change interrupt.
1737              (##) USART_IT_LBD: specifies the interrupt source for LIN Break 
1738                   detection interrupt.
1739              (##) USART_IT_TXE: specifies the interrupt source for Tansmit Data 
1740                   Register empty interrupt.
1741              (##) USART_IT_TC: specifies the interrupt source for Transmission 
1742                   complete interrupt.
1743              (##) USART_IT_RXNE: specifies the interrupt source for Receive Data 
1744                   register not empty interrupt.
1745              (##) USART_IT_IDLE: specifies the interrupt source for Idle line 
1746                   detection interrupt.
1747              (##) USART_IT_PE: specifies the interrupt source for Parity Error interrupt.
1748              (##) USART_IT_ERR: specifies the interrupt source for Error interrupt
1749                   (Frame error, noise error, overrun error)
1750              -@@- Some parameters are coded in order to use them as interrupt 
1751                  source or as pending bits.
1752     [..] In this Mode it is advised to use the following functions:
1753          (+) void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState).
1754          (+) ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT).
1755          (+) void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT).
1756
1757 @endverbatim
1758   * @{
1759   */
1760
1761 /**
1762   * @brief  Enables or disables the specified USART interrupts.
1763   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1764   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1765   * @param  USART_IT: specifies the USART interrupt sources to be enabled or disabled.
1766   *         This parameter can be one of the following values:
1767   *         @arg USART_IT_WU:  Wake up interrupt.
1768   *         @arg USART_IT_CM:  Character match interrupt.
1769   *         @arg USART_IT_EOB:  End of block interrupt.
1770   *         @arg USART_IT_RTO:  Receive time out interrupt.
1771   *         @arg USART_IT_CTS:  CTS change interrupt.
1772   *         @arg USART_IT_LBD:  LIN Break detection interrupt.
1773   *         @arg USART_IT_TXE:  Tansmit Data Register empty interrupt.
1774   *         @arg USART_IT_TC:  Transmission complete interrupt.
1775   *         @arg USART_IT_RXNE:  Receive Data register not empty interrupt.
1776   *         @arg USART_IT_IDLE:  Idle line detection interrupt.
1777   *         @arg USART_IT_PE:  Parity Error interrupt.
1778   *         @arg USART_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)
1779   * @param  NewState: new state of the specified USARTx interrupts.
1780   *         This parameter can be: ENABLE or DISABLE.
1781   * @retval None
1782   */
1783 void USART_ITConfig(USART_TypeDef* USARTx, uint32_t USART_IT, FunctionalState NewState)
1784 {
1785   uint32_t usartreg = 0, itpos = 0, itmask = 0;
1786   uint32_t usartxbase = 0;
1787   /* Check the parameters */
1788   assert_param(IS_USART_ALL_PERIPH(USARTx));
1789   assert_param(IS_USART_CONFIG_IT(USART_IT));
1790   assert_param(IS_FUNCTIONAL_STATE(NewState));
1791   
1792   usartxbase = (uint32_t)USARTx;
1793
1794   /* Get the USART register index */
1795   usartreg = (((uint16_t)USART_IT) >> 0x08);
1796
1797   /* Get the interrupt position */
1798   itpos = USART_IT & IT_MASK;
1799   itmask = (((uint32_t)0x01) << itpos);
1800
1801   if (usartreg == 0x02) /* The IT is in CR2 register */
1802   {
1803     usartxbase += 0x04;
1804   }
1805   else if (usartreg == 0x03) /* The IT is in CR3 register */
1806   {
1807     usartxbase += 0x08;
1808   }
1809   else /* The IT is in CR1 register */
1810   {
1811   }
1812   if (NewState != DISABLE)
1813   {
1814     *(__IO uint32_t*)usartxbase  |= itmask;
1815   }
1816   else
1817   {
1818     *(__IO uint32_t*)usartxbase &= ~itmask;
1819   }
1820 }
1821
1822 /**
1823   * @brief  Enables the specified USART's Request.
1824   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1825   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1826   * @param  USART_Request: specifies the USART request.
1827   *         This parameter can be any combination of the following values:
1828   *         @arg USART_Request_TXFRQ: Transmit data flush ReQuest
1829   *         @arg USART_Request_RXFRQ: Receive data flush ReQuest
1830   *         @arg USART_Request_MMRQ: Mute Mode ReQuest
1831   *         @arg USART_Request_SBKRQ: Send Break ReQuest
1832   *         @arg USART_Request_ABRRQ: Auto Baud Rate ReQuest
1833   * @param  NewState: new state of the DMA interface when reception error occurs.
1834   *         This parameter can be: ENABLE or DISABLE.  
1835   * @retval None
1836   */
1837 void USART_RequestCmd(USART_TypeDef* USARTx, uint32_t USART_Request, FunctionalState NewState)
1838 {
1839   /* Check the parameters */
1840   assert_param(IS_USART_ALL_PERIPH(USARTx));
1841   assert_param(IS_USART_REQUEST(USART_Request));
1842   assert_param(IS_FUNCTIONAL_STATE(NewState)); 
1843
1844   if (NewState != DISABLE)
1845   {
1846     /* Enable the USART ReQuest by setting the dedicated request bit in the RQR
1847        register.*/
1848     USARTx->RQR |= USART_Request;
1849   }
1850   else
1851   {
1852     /* Disable the USART ReQuest by clearing the dedicated request bit in the RQR
1853        register.*/
1854     USARTx->RQR &= (uint32_t)~USART_Request;
1855   }
1856 }
1857
1858 /**
1859   * @brief  Enables or disables the USART's Overrun detection.
1860   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1861   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1862   * @param  USART_OVRDetection: specifies the OVR detection status in case of OVR error.
1863   *         This parameter can be any combination of the following values:
1864   *         @arg USART_OVRDetection_Enable: OVR error detection enabled when the USART OVR error 
1865   *          is asserted.
1866   *         @arg USART_OVRDetection_Disable: OVR error detection disabled when the USART OVR error 
1867   *          is asserted.
1868   * @retval None
1869   */
1870 void USART_OverrunDetectionConfig(USART_TypeDef* USARTx, uint32_t USART_OVRDetection)
1871 {
1872   /* Check the parameters */
1873   assert_param(IS_USART_ALL_PERIPH(USARTx));
1874   assert_param(IS_USART_OVRDETECTION(USART_OVRDetection));
1875   
1876   /* Clear the OVR detection bit */
1877   USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_OVRDIS);
1878   /* Set the new value for the OVR detection bit */
1879   USARTx->CR3 |= USART_OVRDetection;
1880 }
1881
1882 /**
1883   * @brief  Checks whether the specified USART flag is set or not.
1884   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1885   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1886   * @param  USART_FLAG: specifies the flag to check.
1887   *         This parameter can be one of the following values:
1888   *         @arg USART_FLAG_REACK:  Receive Enable acknowledge flag.
1889   *         @arg USART_FLAG_TEACK:  Transmit Enable acknowledge flag.
1890   *         @arg USART_FLAG_WUF:  Wake up flag.
1891   *         @arg USART_FLAG_RWU:  Receive Wake up flag.
1892   *         @arg USART_FLAG_SBK:  Send Break flag.
1893   *         @arg USART_FLAG_CMF:  Character match flag.
1894   *         @arg USART_FLAG_BUSY:  Busy flag.
1895   *         @arg USART_FLAG_ABRF:  Auto baud rate flag.
1896   *         @arg USART_FLAG_ABRE:  Auto baud rate error flag.
1897   *         @arg USART_FLAG_EOBF:  End of block flag.
1898   *         @arg USART_FLAG_RTOF:  Receive time out flag.
1899   *         @arg USART_FLAG_nCTSS:  Inverted nCTS input bit status.
1900   *         @arg USART_FLAG_CTS:  CTS Change flag.
1901   *         @arg USART_FLAG_LBD:  LIN Break detection flag.
1902   *         @arg USART_FLAG_TXE:  Transmit data register empty flag.
1903   *         @arg USART_FLAG_TC:  Transmission Complete flag.
1904   *         @arg USART_FLAG_RXNE:  Receive data register not empty flag.
1905   *         @arg USART_FLAG_IDLE:  Idle Line detection flag.
1906   *         @arg USART_FLAG_ORE:  OverRun Error flag.
1907   *         @arg USART_FLAG_NE:  Noise Error flag.
1908   *         @arg USART_FLAG_FE:  Framing Error flag.
1909   *         @arg USART_FLAG_PE:  Parity Error flag.
1910   * @retval The new state of USART_FLAG (SET or RESET).
1911   */
1912 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint32_t USART_FLAG)
1913 {
1914   FlagStatus bitstatus = RESET;
1915   /* Check the parameters */
1916   assert_param(IS_USART_ALL_PERIPH(USARTx));
1917   assert_param(IS_USART_FLAG(USART_FLAG));
1918   
1919   if ((USARTx->ISR & USART_FLAG) != (uint16_t)RESET)
1920   {
1921     bitstatus = SET;
1922   }
1923   else
1924   {
1925     bitstatus = RESET;
1926   }
1927   return bitstatus;
1928 }
1929
1930 /**
1931   * @brief  Clears the USARTx's pending flags.
1932   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1933   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1934   * @param  USART_FLAG: specifies the flag to clear.
1935   *         This parameter can be any combination of the following values:
1936   *         @arg USART_FLAG_WUF:  Wake up flag.
1937   *         @arg USART_FLAG_CMF:  Character match flag.
1938   *         @arg USART_FLAG_EOBF:  End of block flag.
1939   *         @arg USART_FLAG_RTOF:  Receive time out flag.
1940   *         @arg USART_FLAG_CTS:  CTS Change flag.
1941   *         @arg USART_FLAG_LBD:  LIN Break detection flag.
1942   *         @arg USART_FLAG_TC:  Transmission Complete flag.
1943   *         @arg USART_FLAG_IDLE:  IDLE line detected flag.
1944   *         @arg USART_FLAG_ORE:  OverRun Error flag.
1945   *         @arg USART_FLAG_NE: Noise Error flag.
1946   *         @arg USART_FLAG_FE: Framing Error flag.
1947   *         @arg USART_FLAG_PE:   Parity Errorflag.
1948   *
1949   * @note
1950   *   - RXNE pending bit is cleared by a read to the USART_RDR register 
1951   *     (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register USART_RQR
1952   *     (USART_RequestCmd()).
1953   *   - TC flag can be also cleared by software sequence: a read operation to 
1954   *     USART_SR register (USART_GetFlagStatus()) followed by a write operation
1955   *     to USART_TDR register (USART_SendData()).
1956   *   - TXE flag is cleared by a write to the USART_TDR register 
1957   *     (USART_SendData()) or by writing 1 to the TXFRQ in the register USART_RQR
1958   *     (USART_RequestCmd()).
1959   *   - SBKF flag is cleared by 1 to the SBKRQ in the register USART_RQR
1960   *     (USART_RequestCmd()).
1961   * @retval None
1962   */
1963 void USART_ClearFlag(USART_TypeDef* USARTx, uint32_t USART_FLAG)
1964 {
1965   /* Check the parameters */
1966   assert_param(IS_USART_ALL_PERIPH(USARTx));
1967   assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
1968      
1969   USARTx->ICR = USART_FLAG;
1970 }
1971
1972 /**
1973   * @brief  Checks whether the specified USART interrupt has occurred or not.
1974   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
1975   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
1976   * @param  USART_IT: specifies the USART interrupt source to check.
1977   *         This parameter can be one of the following values:
1978   *         @arg USART_IT_WU:  Wake up interrupt.
1979   *         @arg USART_IT_CM:  Character match interrupt.
1980   *         @arg USART_IT_EOB:  End of block interrupt.
1981   *         @arg USART_IT_RTO:  Receive time out interrupt.
1982   *         @arg USART_IT_CTS:  CTS change interrupt.
1983   *         @arg USART_IT_LBD:  LIN Break detection interrupt.
1984   *         @arg USART_IT_TXE:  Tansmit Data Register empty interrupt.
1985   *         @arg USART_IT_TC:  Transmission complete interrupt.
1986   *         @arg USART_IT_RXNE:  Receive Data register not empty interrupt.
1987   *         @arg USART_IT_IDLE:  Idle line detection interrupt.
1988   *         @arg USART_IT_ORE:  OverRun Error interrupt.
1989   *         @arg USART_IT_NE:  Noise Error interrupt.
1990   *         @arg USART_IT_FE:  Framing Error interrupt.
1991   *         @arg USART_IT_PE:  Parity Error interrupt.
1992   * @retval The new state of USART_IT (SET or RESET).
1993   */
1994 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint32_t USART_IT)
1995 {
1996   uint32_t bitpos = 0, itmask = 0, usartreg = 0;
1997   ITStatus bitstatus = RESET;
1998   /* Check the parameters */
1999   assert_param(IS_USART_ALL_PERIPH(USARTx));
2000   assert_param(IS_USART_GET_IT(USART_IT)); 
2001   
2002   /* Get the USART register index */
2003   usartreg = (((uint16_t)USART_IT) >> 0x08);
2004   /* Get the interrupt position */
2005   itmask = USART_IT & IT_MASK;
2006   itmask = (uint32_t)0x01 << itmask;
2007   
2008   if (usartreg == 0x01) /* The IT  is in CR1 register */
2009   {
2010     itmask &= USARTx->CR1;
2011   }
2012   else if (usartreg == 0x02) /* The IT  is in CR2 register */
2013   {
2014     itmask &= USARTx->CR2;
2015   }
2016   else /* The IT  is in CR3 register */
2017   {
2018     itmask &= USARTx->CR3;
2019   }
2020   
2021   bitpos = USART_IT >> 0x10;
2022   bitpos = (uint32_t)0x01 << bitpos;
2023   bitpos &= USARTx->ISR;
2024   if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
2025   {
2026     bitstatus = SET;
2027   }
2028   else
2029   {
2030     bitstatus = RESET;
2031   }
2032   
2033   return bitstatus;  
2034 }
2035
2036 /**
2037   * @brief  Clears the USARTx's interrupt pending bits.
2038   * @param  USARTx: Select the USART peripheral. This parameter can be one of the 
2039   *         following values: USART1 or USART2 or USART3 or UART4 or UART5.
2040   * @param  USART_IT: specifies the interrupt pending bit to clear.
2041   *         This parameter can be one of the following values:
2042   *         @arg USART_IT_WU:  Wake up interrupt.
2043   *         @arg USART_IT_CM:  Character match interrupt.
2044   *         @arg USART_IT_EOB:  End of block interrupt.
2045   *         @arg USART_IT_RTO:  Receive time out interrupt.
2046   *         @arg USART_IT_CTS:  CTS change interrupt.
2047   *         @arg USART_IT_LBD:  LIN Break detection interrupt.
2048   *         @arg USART_IT_TC:  Transmission complete interrupt.
2049   *         @arg USART_IT_IDLE:  IDLE line detected interrupt.
2050   *         @arg USART_IT_ORE:  OverRun Error interrupt.
2051   *         @arg USART_IT_NE:  Noise Error interrupt.
2052   *         @arg USART_IT_FE:  Framing Error interrupt.
2053   *         @arg USART_IT_PE:  Parity Error interrupt.
2054   * @note
2055   *   - RXNE pending bit is cleared by a read to the USART_RDR register 
2056   *     (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register USART_RQR
2057   *     (USART_RequestCmd()).
2058   *   - TC pending bit can be also cleared by software sequence: a read 
2059   *     operation to USART_SR register (USART_GetITStatus()) followed by a write 
2060   *     operation to USART_TDR register (USART_SendData()).
2061   *   - TXE pending bit is cleared by a write to the USART_TDR register 
2062   *     (USART_SendData()) or by writing 1 to the TXFRQ in the register USART_RQR
2063   *     (USART_RequestCmd()).
2064   * @retval None
2065   */
2066 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint32_t USART_IT)
2067 {
2068   uint32_t bitpos = 0, itmask = 0;
2069   /* Check the parameters */
2070   assert_param(IS_USART_ALL_PERIPH(USARTx));
2071   assert_param(IS_USART_CLEAR_IT(USART_IT)); 
2072   
2073   bitpos = USART_IT >> 0x10;
2074   itmask = ((uint32_t)0x01 << (uint32_t)bitpos);
2075   USARTx->ICR = (uint32_t)itmask;
2076 }
2077
2078 /**
2079   * @}
2080   */
2081
2082 /**
2083   * @}
2084   */
2085
2086 /**
2087   * @}
2088   */
2089
2090 /**
2091   * @}
2092   */
2093
2094 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/