2 ******************************************************************************
3 * @file stm32f4xx_usart.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Universal synchronous asynchronous receiver
10 * - Initialization and Configuration
12 * - Multi-Processor Communication
17 * - DMA transfers management
18 * - Interrupts and flags management
22 * ===================================================================
23 * How to use this driver
24 * ===================================================================
25 * 1. Enable peripheral clock using the follwoing functions
26 * RCC_APB2PeriphClockCmd(RCC_APB2Periph_USARTx, ENABLE) for USART1 and USART6
27 * RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE) for USART2, USART3, UART4 or UART5.
29 * 2. According to the USART mode, enable the GPIO clocks using
30 * RCC_AHB1PeriphClockCmd() function. (The I/O can be TX, RX, CTS,
33 * 3. Peripheral's alternate function:
34 * - Connect the pin to the desired peripherals' Alternate
35 * Function (AF) using GPIO_PinAFConfig() function
36 * - Configure the desired pin in alternate function by:
37 * GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
38 * - Select the type, pull-up/pull-down and output speed via
39 * GPIO_PuPd, GPIO_OType and GPIO_Speed members
40 * - Call GPIO_Init() function
42 * 4. Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
43 * flow control and Mode(Receiver/Transmitter) using the USART_Init()
46 * 5. For synchronous mode, enable the clock and program the polarity,
47 * phase and last bit using the USART_ClockInit() function.
49 * 5. Enable the NVIC and the corresponding interrupt using the function
50 * USART_ITConfig() if you need to use interrupt mode.
52 * 6. When using the DMA mode
53 * - Configure the DMA using DMA_Init() function
54 * - Active the needed channel Request using USART_DMACmd() function
56 * 7. Enable the USART using the USART_Cmd() function.
58 * 8. Enable the DMA using the DMA_Cmd() function, when using DMA mode.
60 * Refer to Multi-Processor, LIN, half-duplex, Smartcard, IrDA sub-sections
63 * In order to reach higher communication baudrates, it is possible to
64 * enable the oversampling by 8 mode using the function USART_OverSampling8Cmd().
65 * This function should be called after enabling the USART clock (RCC_APBxPeriphClockCmd())
66 * and before calling the function USART_Init().
70 ******************************************************************************
73 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
74 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
75 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
76 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
77 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
78 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
80 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
81 ******************************************************************************
84 /* Includes ------------------------------------------------------------------*/
85 #include "stm32f4xx_usart.h"
86 #include "stm32f4xx_rcc.h"
88 /** @addtogroup STM32F4xx_StdPeriph_Driver
93 * @brief USART driver modules
97 /* Private typedef -----------------------------------------------------------*/
98 /* Private define ------------------------------------------------------------*/
100 /*!< USART CR1 register clear Mask ((~(uint16_t)0xE9F3)) */
101 #define CR1_CLEAR_MASK ((uint16_t)(USART_CR1_M | USART_CR1_PCE | \
102 USART_CR1_PS | USART_CR1_TE | \
105 /*!< USART CR2 register clock bits clear Mask ((~(uint16_t)0xF0FF)) */
106 #define CR2_CLOCK_CLEAR_MASK ((uint16_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
107 USART_CR2_CPHA | USART_CR2_LBCL))
109 /*!< USART CR3 register clear Mask ((~(uint16_t)0xFCFF)) */
110 #define CR3_CLEAR_MASK ((uint16_t)(USART_CR3_RTSE | USART_CR3_CTSE))
112 /*!< USART Interrupts mask */
113 #define IT_MASK ((uint16_t)0x001F)
115 /* Private macro -------------------------------------------------------------*/
116 /* Private variables ---------------------------------------------------------*/
117 /* Private function prototypes -----------------------------------------------*/
118 /* Private functions ---------------------------------------------------------*/
120 /** @defgroup USART_Private_Functions
124 /** @defgroup USART_Group1 Initialization and Configuration functions
125 * @brief Initialization and Configuration functions
128 ===============================================================================
129 Initialization and Configuration functions
130 ===============================================================================
132 This subsection provides a set of functions allowing to initialize the USART
133 in asynchronous and in synchronous modes.
134 - For the asynchronous mode only these parameters can be configured:
138 - Parity: If the parity is enabled, then the MSB bit of the data written
139 in the data register is transmitted but is changed by the parity bit.
140 Depending on the frame length defined by the M bit (8-bits or 9-bits),
141 the possible USART frame formats are as listed in the following table:
142 +-------------------------------------------------------------+
143 | M bit | PCE bit | USART frame |
144 |---------------------|---------------------------------------|
145 | 0 | 0 | | SB | 8 bit data | STB | |
146 |---------|-----------|---------------------------------------|
147 | 0 | 1 | | SB | 7 bit data | PB | STB | |
148 |---------|-----------|---------------------------------------|
149 | 1 | 0 | | SB | 9 bit data | STB | |
150 |---------|-----------|---------------------------------------|
151 | 1 | 1 | | SB | 8 bit data | PB | STB | |
152 +-------------------------------------------------------------+
153 - Hardware flow control
154 - Receiver/transmitter modes
156 The USART_Init() function follows the USART asynchronous configuration procedure
157 (details for the procedure are available in reference manual (RM0090)).
159 - For the synchronous mode in addition to the asynchronous mode parameters these
160 parameters should be also configured:
161 - USART Clock Enabled
166 These parameters can be configured using the USART_ClockInit() function.
173 * @brief Deinitializes the USARTx peripheral registers to their default reset values.
174 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
178 void USART_DeInit(USART_TypeDef* USARTx)
180 /* Check the parameters */
181 assert_param(IS_USART_ALL_PERIPH(USARTx));
183 if (USARTx == USART1)
185 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
186 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
188 else if (USARTx == USART2)
190 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
191 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
193 else if (USARTx == USART3)
195 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
196 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
198 else if (USARTx == UART4)
200 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
201 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
203 else if (USARTx == UART5)
205 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
206 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
210 if (USARTx == USART6)
212 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE);
213 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, DISABLE);
219 * @brief Initializes the USARTx peripheral according to the specified
220 * parameters in the USART_InitStruct .
221 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
223 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure that contains
224 * the configuration information for the specified USART peripheral.
227 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
229 uint32_t tmpreg = 0x00, apbclock = 0x00;
230 uint32_t integerdivider = 0x00;
231 uint32_t fractionaldivider = 0x00;
232 RCC_ClocksTypeDef RCC_ClocksStatus;
234 /* Check the parameters */
235 assert_param(IS_USART_ALL_PERIPH(USARTx));
236 assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
237 assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
238 assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
239 assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
240 assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
241 assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
243 /* The hardware flow control is available only for USART1, USART2, USART3 and USART6 */
244 if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
246 assert_param(IS_USART_1236_PERIPH(USARTx));
249 /*---------------------------- USART CR2 Configuration -----------------------*/
250 tmpreg = USARTx->CR2;
252 /* Clear STOP[13:12] bits */
253 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
255 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit :
256 Set STOP[13:12] bits according to USART_StopBits value */
257 tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
259 /* Write to USART CR2 */
260 USARTx->CR2 = (uint16_t)tmpreg;
262 /*---------------------------- USART CR1 Configuration -----------------------*/
263 tmpreg = USARTx->CR1;
265 /* Clear M, PCE, PS, TE and RE bits */
266 tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);
268 /* Configure the USART Word Length, Parity and mode:
269 Set the M bits according to USART_WordLength value
270 Set PCE and PS bits according to USART_Parity value
271 Set TE and RE bits according to USART_Mode value */
272 tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
273 USART_InitStruct->USART_Mode;
275 /* Write to USART CR1 */
276 USARTx->CR1 = (uint16_t)tmpreg;
278 /*---------------------------- USART CR3 Configuration -----------------------*/
279 tmpreg = USARTx->CR3;
281 /* Clear CTSE and RTSE bits */
282 tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);
284 /* Configure the USART HFC :
285 Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
286 tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
288 /* Write to USART CR3 */
289 USARTx->CR3 = (uint16_t)tmpreg;
291 /*---------------------------- USART BRR Configuration -----------------------*/
292 /* Configure the USART Baud Rate */
293 RCC_GetClocksFreq(&RCC_ClocksStatus);
295 if ((USARTx == USART1) || (USARTx == USART6))
297 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
301 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
304 /* Determine the integer part */
305 if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
307 /* Integer part computing in case Oversampling mode is 8 Samples */
308 integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
310 else /* if ((USARTx->CR1 & USART_CR1_OVER8) == 0) */
312 /* Integer part computing in case Oversampling mode is 16 Samples */
313 integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
315 tmpreg = (integerdivider / 100) << 4;
317 /* Determine the fractional part */
318 fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
320 /* Implement the fractional part in the register */
321 if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
323 tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
325 else /* if ((USARTx->CR1 & USART_CR1_OVER8) == 0) */
327 tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
330 /* Write to USART BRR register */
331 USARTx->BRR = (uint16_t)tmpreg;
335 * @brief Fills each USART_InitStruct member with its default value.
336 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure which will
340 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
342 /* USART_InitStruct members default value */
343 USART_InitStruct->USART_BaudRate = 9600;
344 USART_InitStruct->USART_WordLength = USART_WordLength_8b;
345 USART_InitStruct->USART_StopBits = USART_StopBits_1;
346 USART_InitStruct->USART_Parity = USART_Parity_No ;
347 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
348 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
352 * @brief Initializes the USARTx peripheral Clock according to the
353 * specified parameters in the USART_ClockInitStruct .
354 * @param USARTx: where x can be 1, 2, 3 or 6 to select the USART peripheral.
355 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef structure that
356 * contains the configuration information for the specified USART peripheral.
357 * @note The Smart Card and Synchronous modes are not available for UART4 and UART5.
360 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
362 uint32_t tmpreg = 0x00;
363 /* Check the parameters */
364 assert_param(IS_USART_1236_PERIPH(USARTx));
365 assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
366 assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
367 assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
368 assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
370 /*---------------------------- USART CR2 Configuration -----------------------*/
371 tmpreg = USARTx->CR2;
372 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
373 tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);
374 /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
375 /* Set CLKEN bit according to USART_Clock value */
376 /* Set CPOL bit according to USART_CPOL value */
377 /* Set CPHA bit according to USART_CPHA value */
378 /* Set LBCL bit according to USART_LastBit value */
379 tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
380 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
381 /* Write to USART CR2 */
382 USARTx->CR2 = (uint16_t)tmpreg;
386 * @brief Fills each USART_ClockInitStruct member with its default value.
387 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef structure
388 * which will be initialized.
391 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
393 /* USART_ClockInitStruct members default value */
394 USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
395 USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
396 USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
397 USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
401 * @brief Enables or disables the specified USART peripheral.
402 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
404 * @param NewState: new state of the USARTx peripheral.
405 * This parameter can be: ENABLE or DISABLE.
408 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
410 /* Check the parameters */
411 assert_param(IS_USART_ALL_PERIPH(USARTx));
412 assert_param(IS_FUNCTIONAL_STATE(NewState));
414 if (NewState != DISABLE)
416 /* Enable the selected USART by setting the UE bit in the CR1 register */
417 USARTx->CR1 |= USART_CR1_UE;
421 /* Disable the selected USART by clearing the UE bit in the CR1 register */
422 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_UE);
427 * @brief Sets the system clock prescaler.
428 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
430 * @param USART_Prescaler: specifies the prescaler clock.
431 * @note The function is used for IrDA mode with UART4 and UART5.
434 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
436 /* Check the parameters */
437 assert_param(IS_USART_ALL_PERIPH(USARTx));
439 /* Clear the USART prescaler */
440 USARTx->GTPR &= USART_GTPR_GT;
441 /* Set the USART prescaler */
442 USARTx->GTPR |= USART_Prescaler;
446 * @brief Enables or disables the USART's 8x oversampling mode.
447 * @note This function has to be called before calling USART_Init() function
448 * in order to have correct baudrate Divider value.
449 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
451 * @param NewState: new state of the USART 8x oversampling mode.
452 * This parameter can be: ENABLE or DISABLE.
455 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
457 /* Check the parameters */
458 assert_param(IS_USART_ALL_PERIPH(USARTx));
459 assert_param(IS_FUNCTIONAL_STATE(NewState));
461 if (NewState != DISABLE)
463 /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
464 USARTx->CR1 |= USART_CR1_OVER8;
468 /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
469 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_OVER8);
474 * @brief Enables or disables the USART's one bit sampling method.
475 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
477 * @param NewState: new state of the USART one bit sampling method.
478 * This parameter can be: ENABLE or DISABLE.
481 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
483 /* Check the parameters */
484 assert_param(IS_USART_ALL_PERIPH(USARTx));
485 assert_param(IS_FUNCTIONAL_STATE(NewState));
487 if (NewState != DISABLE)
489 /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
490 USARTx->CR3 |= USART_CR3_ONEBIT;
494 /* Disable the one bit method by clearing the ONEBITE bit in the CR3 register */
495 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT);
503 /** @defgroup USART_Group2 Data transfers functions
504 * @brief Data transfers functions
507 ===============================================================================
508 Data transfers functions
509 ===============================================================================
511 This subsection provides a set of functions allowing to manage the USART data
514 During an USART reception, data shifts in least significant bit first through
515 the RX pin. In this mode, the USART_DR register consists of a buffer (RDR)
516 between the internal bus and the received shift register.
518 When a transmission is taking place, a write instruction to the USART_DR register
519 stores the data in the TDR register and which is copied in the shift register
520 at the end of the current transmission.
522 The read access of the USART_DR register can be done using the USART_ReceiveData()
523 function and returns the RDR buffered value. Whereas a write access to the USART_DR
524 can be done using USART_SendData() function and stores the written data into
532 * @brief Transmits single data through the USARTx peripheral.
533 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
535 * @param Data: the data to transmit.
538 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
540 /* Check the parameters */
541 assert_param(IS_USART_ALL_PERIPH(USARTx));
542 assert_param(IS_USART_DATA(Data));
545 USARTx->DR = (Data & (uint16_t)0x01FF);
549 * @brief Returns the most recent received data by the USARTx peripheral.
550 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
552 * @retval The received data.
554 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
556 /* Check the parameters */
557 assert_param(IS_USART_ALL_PERIPH(USARTx));
560 return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
567 /** @defgroup USART_Group3 MultiProcessor Communication functions
568 * @brief Multi-Processor Communication functions
571 ===============================================================================
572 Multi-Processor Communication functions
573 ===============================================================================
575 This subsection provides a set of functions allowing to manage the USART
576 multiprocessor communication.
578 For instance one of the USARTs can be the master, its TX output is connected to
579 the RX input of the other USART. The others are slaves, their respective TX outputs
580 are logically ANDed together and connected to the RX input of the master.
582 USART multiprocessor communication is possible through the following procedure:
583 1. Program the Baud rate, Word length = 9 bits, Stop bits, Parity, Mode transmitter
584 or Mode receiver and hardware flow control values using the USART_Init()
586 2. Configures the USART address using the USART_SetAddress() function.
587 3. Configures the wake up method (USART_WakeUp_IdleLine or USART_WakeUp_AddressMark)
588 using USART_WakeUpConfig() function only for the slaves.
589 4. Enable the USART using the USART_Cmd() function.
590 5. Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd() function.
592 The USART Slave exit from mute mode when receive the wake up condition.
599 * @brief Sets the address of the USART node.
600 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
602 * @param USART_Address: Indicates the address of the USART node.
605 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
607 /* Check the parameters */
608 assert_param(IS_USART_ALL_PERIPH(USARTx));
609 assert_param(IS_USART_ADDRESS(USART_Address));
611 /* Clear the USART address */
612 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_ADD);
613 /* Set the USART address node */
614 USARTx->CR2 |= USART_Address;
618 * @brief Determines if the USART is in mute mode or not.
619 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
621 * @param NewState: new state of the USART mute mode.
622 * This parameter can be: ENABLE or DISABLE.
625 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
627 /* Check the parameters */
628 assert_param(IS_USART_ALL_PERIPH(USARTx));
629 assert_param(IS_FUNCTIONAL_STATE(NewState));
631 if (NewState != DISABLE)
633 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
634 USARTx->CR1 |= USART_CR1_RWU;
638 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
639 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_RWU);
643 * @brief Selects the USART WakeUp method.
644 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
646 * @param USART_WakeUp: specifies the USART wakeup method.
647 * This parameter can be one of the following values:
648 * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
649 * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
652 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
654 /* Check the parameters */
655 assert_param(IS_USART_ALL_PERIPH(USARTx));
656 assert_param(IS_USART_WAKEUP(USART_WakeUp));
658 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_WAKE);
659 USARTx->CR1 |= USART_WakeUp;
666 /** @defgroup USART_Group4 LIN mode functions
667 * @brief LIN mode functions
670 ===============================================================================
672 ===============================================================================
674 This subsection provides a set of functions allowing to manage the USART LIN
677 In LIN mode, 8-bit data format with 1 stop bit is required in accordance with
680 Only this LIN Feature is supported by the USART IP:
681 - LIN Master Synchronous Break send capability and LIN slave break detection
682 capability : 13-bit break generation and 10/11 bit break detection
685 USART LIN Master transmitter communication is possible through the following procedure:
686 1. Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
687 Mode transmitter or Mode receiver and hardware flow control values using
688 the USART_Init() function.
689 2. Enable the USART using the USART_Cmd() function.
690 3. Enable the LIN mode using the USART_LINCmd() function.
691 4. Send the break character using USART_SendBreak() function.
693 USART LIN Master receiver communication is possible through the following procedure:
694 1. Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
695 Mode transmitter or Mode receiver and hardware flow control values using
696 the USART_Init() function.
697 2. Enable the USART using the USART_Cmd() function.
698 3. Configures the break detection length using the USART_LINBreakDetectLengthConfig()
700 4. Enable the LIN mode using the USART_LINCmd() function.
703 @note In LIN mode, the following bits must be kept cleared:
704 - CLKEN in the USART_CR2 register,
705 - STOP[1:0], SCEN, HDSEL and IREN in the USART_CR3 register.
712 * @brief Sets the USART LIN Break detection length.
713 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
715 * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
716 * This parameter can be one of the following values:
717 * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
718 * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
721 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
723 /* Check the parameters */
724 assert_param(IS_USART_ALL_PERIPH(USARTx));
725 assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
727 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LBDL);
728 USARTx->CR2 |= USART_LINBreakDetectLength;
732 * @brief Enables or disables the USART's LIN mode.
733 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
735 * @param NewState: new state of the USART LIN mode.
736 * This parameter can be: ENABLE or DISABLE.
739 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
741 /* Check the parameters */
742 assert_param(IS_USART_ALL_PERIPH(USARTx));
743 assert_param(IS_FUNCTIONAL_STATE(NewState));
745 if (NewState != DISABLE)
747 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
748 USARTx->CR2 |= USART_CR2_LINEN;
752 /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
753 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LINEN);
758 * @brief Transmits break characters.
759 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
763 void USART_SendBreak(USART_TypeDef* USARTx)
765 /* Check the parameters */
766 assert_param(IS_USART_ALL_PERIPH(USARTx));
768 /* Send break characters */
769 USARTx->CR1 |= USART_CR1_SBK;
776 /** @defgroup USART_Group5 Halfduplex mode function
777 * @brief Half-duplex mode function
780 ===============================================================================
781 Half-duplex mode function
782 ===============================================================================
784 This subsection provides a set of functions allowing to manage the USART
785 Half-duplex communication.
787 The USART can be configured to follow a single-wire half-duplex protocol where
788 the TX and RX lines are internally connected.
790 USART Half duplex communication is possible through the following procedure:
791 1. Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter
792 or Mode receiver and hardware flow control values using the USART_Init()
794 2. Configures the USART address using the USART_SetAddress() function.
795 3. Enable the USART using the USART_Cmd() function.
796 4. Enable the half duplex mode using USART_HalfDuplexCmd() function.
799 @note The RX pin is no longer used
800 @note In Half-duplex mode the following bits must be kept cleared:
801 - LINEN and CLKEN bits in the USART_CR2 register.
802 - SCEN and IREN bits in the USART_CR3 register.
809 * @brief Enables or disables the USART's Half Duplex communication.
810 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
812 * @param NewState: new state of the USART Communication.
813 * This parameter can be: ENABLE or DISABLE.
816 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
818 /* Check the parameters */
819 assert_param(IS_USART_ALL_PERIPH(USARTx));
820 assert_param(IS_FUNCTIONAL_STATE(NewState));
822 if (NewState != DISABLE)
824 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
825 USARTx->CR3 |= USART_CR3_HDSEL;
829 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
830 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_HDSEL);
839 /** @defgroup USART_Group6 Smartcard mode functions
840 * @brief Smartcard mode functions
843 ===============================================================================
844 Smartcard mode functions
845 ===============================================================================
847 This subsection provides a set of functions allowing to manage the USART
848 Smartcard communication.
850 The Smartcard interface is designed to support asynchronous protocol Smartcards as
851 defined in the ISO 7816-3 standard.
853 The USART can provide a clock to the smartcard through the SCLK output.
854 In smartcard mode, SCLK is not associated to the communication but is simply derived
855 from the internal peripheral input clock through a 5-bit prescaler.
857 Smartcard communication is possible through the following procedure:
858 1. Configures the Smartcard Prescaler using the USART_SetPrescaler() function.
859 2. Configures the Smartcard Guard Time using the USART_SetGuardTime() function.
860 3. Program the USART clock using the USART_ClockInit() function as following:
861 - USART Clock enabled
863 - USART CPHA on first edge
864 - USART Last Bit Clock Enabled
865 4. Program the Smartcard interface using the USART_Init() function as following:
866 - Word Length = 9 Bits
869 - BaudRate = 12096 baud
870 - Hardware flow control disabled (RTS and CTS signals)
872 5. Optionally you can enable the parity error interrupt using the USART_ITConfig()
874 6. Enable the USART using the USART_Cmd() function.
875 7. Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function.
876 8. Enable the Smartcard interface using the USART_SmartCardCmd() function.
878 Please refer to the ISO 7816-3 specification for more details.
881 @note It is also possible to choose 0.5 stop bit for receiving but it is recommended
882 to use 1.5 stop bits for both transmitting and receiving to avoid switching
883 between the two configurations.
884 @note In smartcard mode, the following bits must be kept cleared:
885 - LINEN bit in the USART_CR2 register.
886 - HDSEL and IREN bits in the USART_CR3 register.
887 @note Smartcard mode is available on USART peripherals only (not available on UART4
888 and UART5 peripherals).
895 * @brief Sets the specified USART guard time.
896 * @param USARTx: where x can be 1, 2, 3 or 6 to select the USART or
898 * @param USART_GuardTime: specifies the guard time.
901 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
903 /* Check the parameters */
904 assert_param(IS_USART_1236_PERIPH(USARTx));
906 /* Clear the USART Guard time */
907 USARTx->GTPR &= USART_GTPR_PSC;
908 /* Set the USART guard time */
909 USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
913 * @brief Enables or disables the USART's Smart Card mode.
914 * @param USARTx: where x can be 1, 2, 3 or 6 to select the USART or
916 * @param NewState: new state of the Smart Card mode.
917 * This parameter can be: ENABLE or DISABLE.
920 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
922 /* Check the parameters */
923 assert_param(IS_USART_1236_PERIPH(USARTx));
924 assert_param(IS_FUNCTIONAL_STATE(NewState));
925 if (NewState != DISABLE)
927 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
928 USARTx->CR3 |= USART_CR3_SCEN;
932 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
933 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_SCEN);
938 * @brief Enables or disables NACK transmission.
939 * @param USARTx: where x can be 1, 2, 3 or 6 to select the USART or
941 * @param NewState: new state of the NACK transmission.
942 * This parameter can be: ENABLE or DISABLE.
945 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
947 /* Check the parameters */
948 assert_param(IS_USART_1236_PERIPH(USARTx));
949 assert_param(IS_FUNCTIONAL_STATE(NewState));
950 if (NewState != DISABLE)
952 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
953 USARTx->CR3 |= USART_CR3_NACK;
957 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
958 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_NACK);
966 /** @defgroup USART_Group7 IrDA mode functions
967 * @brief IrDA mode functions
970 ===============================================================================
972 ===============================================================================
974 This subsection provides a set of functions allowing to manage the USART
977 IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
978 on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
979 is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
980 While receiving data, transmission should be avoided as the data to be transmitted
983 IrDA communication is possible through the following procedure:
984 1. Program the Baud rate, Word length = 8 bits, Stop bits, Parity, Transmitter/Receiver
985 modes and hardware flow control values using the USART_Init() function.
986 2. Enable the USART using the USART_Cmd() function.
987 3. Configures the IrDA pulse width by configuring the prescaler using
988 the USART_SetPrescaler() function.
989 4. Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal mode
990 using the USART_IrDAConfig() function.
991 5. Enable the IrDA using the USART_IrDACmd() function.
993 @note A pulse of width less than two and greater than one PSC period(s) may or may
995 @note The receiver set up time should be managed by software. The IrDA physical layer
996 specification specifies a minimum of 10 ms delay between transmission and
997 reception (IrDA is a half duplex protocol).
998 @note In IrDA mode, the following bits must be kept cleared:
999 - LINEN, STOP and CLKEN bits in the USART_CR2 register.
1000 - SCEN and HDSEL bits in the USART_CR3 register.
1007 * @brief Configures the USART's IrDA interface.
1008 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
1010 * @param USART_IrDAMode: specifies the IrDA mode.
1011 * This parameter can be one of the following values:
1012 * @arg USART_IrDAMode_LowPower
1013 * @arg USART_IrDAMode_Normal
1016 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
1018 /* Check the parameters */
1019 assert_param(IS_USART_ALL_PERIPH(USARTx));
1020 assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
1022 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IRLP);
1023 USARTx->CR3 |= USART_IrDAMode;
1027 * @brief Enables or disables the USART's IrDA interface.
1028 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
1030 * @param NewState: new state of the IrDA mode.
1031 * This parameter can be: ENABLE or DISABLE.
1034 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
1036 /* Check the parameters */
1037 assert_param(IS_USART_ALL_PERIPH(USARTx));
1038 assert_param(IS_FUNCTIONAL_STATE(NewState));
1040 if (NewState != DISABLE)
1042 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
1043 USARTx->CR3 |= USART_CR3_IREN;
1047 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
1048 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IREN);
1056 /** @defgroup USART_Group8 DMA transfers management functions
1057 * @brief DMA transfers management functions
1060 ===============================================================================
1061 DMA transfers management functions
1062 ===============================================================================
1069 * @brief Enables or disables the USART's DMA interface.
1070 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
1072 * @param USART_DMAReq: specifies the DMA request.
1073 * This parameter can be any combination of the following values:
1074 * @arg USART_DMAReq_Tx: USART DMA transmit request
1075 * @arg USART_DMAReq_Rx: USART DMA receive request
1076 * @param NewState: new state of the DMA Request sources.
1077 * This parameter can be: ENABLE or DISABLE.
1080 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
1082 /* Check the parameters */
1083 assert_param(IS_USART_ALL_PERIPH(USARTx));
1084 assert_param(IS_USART_DMAREQ(USART_DMAReq));
1085 assert_param(IS_FUNCTIONAL_STATE(NewState));
1087 if (NewState != DISABLE)
1089 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
1090 DMAR bits in the USART CR3 register */
1091 USARTx->CR3 |= USART_DMAReq;
1095 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
1096 DMAR bits in the USART CR3 register */
1097 USARTx->CR3 &= (uint16_t)~USART_DMAReq;
1105 /** @defgroup USART_Group9 Interrupts and flags management functions
1106 * @brief Interrupts and flags management functions
1109 ===============================================================================
1110 Interrupts and flags management functions
1111 ===============================================================================
1113 This subsection provides a set of functions allowing to configure the USART
1114 Interrupts sources, DMA channels requests and check or clear the flags or
1115 pending bits status.
1116 The user should identify which mode will be used in his application to manage
1117 the communication: Polling mode, Interrupt mode or DMA mode.
1121 In Polling Mode, the SPI communication can be managed by 10 flags:
1122 1. USART_FLAG_TXE : to indicate the status of the transmit buffer register
1123 2. USART_FLAG_RXNE : to indicate the status of the receive buffer register
1124 3. USART_FLAG_TC : to indicate the status of the transmit operation
1125 4. USART_FLAG_IDLE : to indicate the status of the Idle Line
1126 5. USART_FLAG_CTS : to indicate the status of the nCTS input
1127 6. USART_FLAG_LBD : to indicate the status of the LIN break detection
1128 7. USART_FLAG_NE : to indicate if a noise error occur
1129 8. USART_FLAG_FE : to indicate if a frame error occur
1130 9. USART_FLAG_PE : to indicate if a parity error occur
1131 10. USART_FLAG_ORE : to indicate if an Overrun error occur
1133 In this Mode it is advised to use the following functions:
1134 - FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
1135 - void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);
1139 In Interrupt Mode, the USART communication can be managed by 8 interrupt sources
1140 and 10 pending bits:
1144 1. USART_IT_TXE : to indicate the status of the transmit buffer register
1145 2. USART_IT_RXNE : to indicate the status of the receive buffer register
1146 3. USART_IT_TC : to indicate the status of the transmit operation
1147 4. USART_IT_IDLE : to indicate the status of the Idle Line
1148 5. USART_IT_CTS : to indicate the status of the nCTS input
1149 6. USART_IT_LBD : to indicate the status of the LIN break detection
1150 7. USART_IT_NE : to indicate if a noise error occur
1151 8. USART_IT_FE : to indicate if a frame error occur
1152 9. USART_IT_PE : to indicate if a parity error occur
1153 10. USART_IT_ORE : to indicate if an Overrun error occur
1157 1. USART_IT_TXE : specifies the interrupt source for the Tx buffer empty
1159 2. USART_IT_RXNE : specifies the interrupt source for the Rx buffer not
1161 3. USART_IT_TC : specifies the interrupt source for the Transmit complete
1163 4. USART_IT_IDLE : specifies the interrupt source for the Idle Line interrupt.
1164 5. USART_IT_CTS : specifies the interrupt source for the CTS interrupt.
1165 6. USART_IT_LBD : specifies the interrupt source for the LIN break detection
1167 7. USART_IT_PE : specifies the interrupt source for the parity error interrupt.
1168 8. USART_IT_ERR : specifies the interrupt source for the errors interrupt.
1170 @note Some parameters are coded in order to use them as interrupt source or as pending bits.
1172 In this Mode it is advised to use the following functions:
1173 - void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
1174 - ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
1175 - void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);
1179 In DMA Mode, the USART communication can be managed by 2 DMA Channel requests:
1180 1. USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request
1181 2. USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request
1183 In this Mode it is advised to use the following function:
1184 - void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
1191 * @brief Enables or disables the specified USART interrupts.
1192 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
1194 * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
1195 * This parameter can be one of the following values:
1196 * @arg USART_IT_CTS: CTS change interrupt
1197 * @arg USART_IT_LBD: LIN Break detection interrupt
1198 * @arg USART_IT_TXE: Transmit Data Register empty interrupt
1199 * @arg USART_IT_TC: Transmission complete interrupt
1200 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
1201 * @arg USART_IT_IDLE: Idle line detection interrupt
1202 * @arg USART_IT_PE: Parity Error interrupt
1203 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
1204 * @param NewState: new state of the specified USARTx interrupts.
1205 * This parameter can be: ENABLE or DISABLE.
1208 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
1210 uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
1211 uint32_t usartxbase = 0x00;
1212 /* Check the parameters */
1213 assert_param(IS_USART_ALL_PERIPH(USARTx));
1214 assert_param(IS_USART_CONFIG_IT(USART_IT));
1215 assert_param(IS_FUNCTIONAL_STATE(NewState));
1217 /* The CTS interrupt is not available for UART4 and UART5 */
1218 if (USART_IT == USART_IT_CTS)
1220 assert_param(IS_USART_1236_PERIPH(USARTx));
1223 usartxbase = (uint32_t)USARTx;
1225 /* Get the USART register index */
1226 usartreg = (((uint8_t)USART_IT) >> 0x05);
1228 /* Get the interrupt position */
1229 itpos = USART_IT & IT_MASK;
1230 itmask = (((uint32_t)0x01) << itpos);
1232 if (usartreg == 0x01) /* The IT is in CR1 register */
1236 else if (usartreg == 0x02) /* The IT is in CR2 register */
1240 else /* The IT is in CR3 register */
1244 if (NewState != DISABLE)
1246 *(__IO uint32_t*)usartxbase |= itmask;
1250 *(__IO uint32_t*)usartxbase &= ~itmask;
1255 * @brief Checks whether the specified USART flag is set or not.
1256 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
1258 * @param USART_FLAG: specifies the flag to check.
1259 * This parameter can be one of the following values:
1260 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
1261 * @arg USART_FLAG_LBD: LIN Break detection flag
1262 * @arg USART_FLAG_TXE: Transmit data register empty flag
1263 * @arg USART_FLAG_TC: Transmission Complete flag
1264 * @arg USART_FLAG_RXNE: Receive data register not empty flag
1265 * @arg USART_FLAG_IDLE: Idle Line detection flag
1266 * @arg USART_FLAG_ORE: OverRun Error flag
1267 * @arg USART_FLAG_NE: Noise Error flag
1268 * @arg USART_FLAG_FE: Framing Error flag
1269 * @arg USART_FLAG_PE: Parity Error flag
1270 * @retval The new state of USART_FLAG (SET or RESET).
1272 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
1274 FlagStatus bitstatus = RESET;
1275 /* Check the parameters */
1276 assert_param(IS_USART_ALL_PERIPH(USARTx));
1277 assert_param(IS_USART_FLAG(USART_FLAG));
1279 /* The CTS flag is not available for UART4 and UART5 */
1280 if (USART_FLAG == USART_FLAG_CTS)
1282 assert_param(IS_USART_1236_PERIPH(USARTx));
1285 if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
1297 * @brief Clears the USARTx's pending flags.
1298 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
1300 * @param USART_FLAG: specifies the flag to clear.
1301 * This parameter can be any combination of the following values:
1302 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
1303 * @arg USART_FLAG_LBD: LIN Break detection flag.
1304 * @arg USART_FLAG_TC: Transmission Complete flag.
1305 * @arg USART_FLAG_RXNE: Receive data register not empty flag.
1307 * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
1308 * error) and IDLE (Idle line detected) flags are cleared by software
1309 * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
1310 * followed by a read operation to USART_DR register (USART_ReceiveData()).
1311 * @note RXNE flag can be also cleared by a read to the USART_DR register
1312 * (USART_ReceiveData()).
1313 * @note TC flag can be also cleared by software sequence: a read operation to
1314 * USART_SR register (USART_GetFlagStatus()) followed by a write operation
1315 * to USART_DR register (USART_SendData()).
1316 * @note TXE flag is cleared only by a write to the USART_DR register
1317 * (USART_SendData()).
1321 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
1323 /* Check the parameters */
1324 assert_param(IS_USART_ALL_PERIPH(USARTx));
1325 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
1327 /* The CTS flag is not available for UART4 and UART5 */
1328 if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
1330 assert_param(IS_USART_1236_PERIPH(USARTx));
1333 USARTx->SR = (uint16_t)~USART_FLAG;
1337 * @brief Checks whether the specified USART interrupt has occurred or not.
1338 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
1340 * @param USART_IT: specifies the USART interrupt source to check.
1341 * This parameter can be one of the following values:
1342 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
1343 * @arg USART_IT_LBD: LIN Break detection interrupt
1344 * @arg USART_IT_TXE: Transmit Data Register empty interrupt
1345 * @arg USART_IT_TC: Transmission complete interrupt
1346 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
1347 * @arg USART_IT_IDLE: Idle line detection interrupt
1348 * @arg USART_IT_ORE: OverRun Error interrupt
1349 * @arg USART_IT_NE: Noise Error interrupt
1350 * @arg USART_IT_FE: Framing Error interrupt
1351 * @arg USART_IT_PE: Parity Error interrupt
1352 * @retval The new state of USART_IT (SET or RESET).
1354 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
1356 uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
1357 ITStatus bitstatus = RESET;
1358 /* Check the parameters */
1359 assert_param(IS_USART_ALL_PERIPH(USARTx));
1360 assert_param(IS_USART_GET_IT(USART_IT));
1362 /* The CTS interrupt is not available for UART4 and UART5 */
1363 if (USART_IT == USART_IT_CTS)
1365 assert_param(IS_USART_1236_PERIPH(USARTx));
1368 /* Get the USART register index */
1369 usartreg = (((uint8_t)USART_IT) >> 0x05);
1370 /* Get the interrupt position */
1371 itmask = USART_IT & IT_MASK;
1372 itmask = (uint32_t)0x01 << itmask;
1374 if (usartreg == 0x01) /* The IT is in CR1 register */
1376 itmask &= USARTx->CR1;
1378 else if (usartreg == 0x02) /* The IT is in CR2 register */
1380 itmask &= USARTx->CR2;
1382 else /* The IT is in CR3 register */
1384 itmask &= USARTx->CR3;
1387 bitpos = USART_IT >> 0x08;
1388 bitpos = (uint32_t)0x01 << bitpos;
1389 bitpos &= USARTx->SR;
1390 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
1403 * @brief Clears the USARTx's interrupt pending bits.
1404 * @param USARTx: where x can be 1, 2, 3, 4, 5 or 6 to select the USART or
1406 * @param USART_IT: specifies the interrupt pending bit to clear.
1407 * This parameter can be one of the following values:
1408 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
1409 * @arg USART_IT_LBD: LIN Break detection interrupt
1410 * @arg USART_IT_TC: Transmission complete interrupt.
1411 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
1413 * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
1414 * error) and IDLE (Idle line detected) pending bits are cleared by
1415 * software sequence: a read operation to USART_SR register
1416 * (USART_GetITStatus()) followed by a read operation to USART_DR register
1417 * (USART_ReceiveData()).
1418 * @note RXNE pending bit can be also cleared by a read to the USART_DR register
1419 * (USART_ReceiveData()).
1420 * @note TC pending bit can be also cleared by software sequence: a read
1421 * operation to USART_SR register (USART_GetITStatus()) followed by a write
1422 * operation to USART_DR register (USART_SendData()).
1423 * @note TXE pending bit is cleared only by a write to the USART_DR register
1424 * (USART_SendData()).
1428 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
1430 uint16_t bitpos = 0x00, itmask = 0x00;
1431 /* Check the parameters */
1432 assert_param(IS_USART_ALL_PERIPH(USARTx));
1433 assert_param(IS_USART_CLEAR_IT(USART_IT));
1435 /* The CTS interrupt is not available for UART4 and UART5 */
1436 if (USART_IT == USART_IT_CTS)
1438 assert_param(IS_USART_1236_PERIPH(USARTx));
1441 bitpos = USART_IT >> 0x08;
1442 itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
1443 USARTx->SR = (uint16_t)~itmask;
1462 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/