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