2 ******************************************************************************
\r
3 * @file stm32l1xx_usart.c
\r
4 * @author MCD Application Team
\r
6 * @date 31-December-2010
\r
7 * @brief This file provides firmware functions to manage the following
\r
8 * functionalities of the Universal synchronous asynchronous receiver
\r
9 * transmitter (USART):
\r
10 * - Initialization and Configuration
\r
12 * - Multi-Processor Communication
\r
14 * - Half-duplex mode
\r
17 * - DMA transfers management
\r
18 * - Interrupts and flags management
\r
22 * ===================================================================
\r
23 * How to use this driver
\r
24 * ===================================================================
\r
25 * 1. Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)
\r
26 * function for USART1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE)
\r
27 * function for USART2 and USART3.
\r
29 * 2. According to the USART mode, enable the GPIO clocks using
\r
30 * RCC_AHBPeriphClockCmd() function. (The I/O can be TX, RX, CTS,
\r
33 * 3. Peripheral
\92s alternate function:
\r
34 * - Connect the pin to the desired peripherals' Alternate
\r
35 * Function (AF) using GPIO_PinAFConfig() function
\r
36 * - Configure the desired pin in alternate function by:
\r
37 * GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
\r
38 * - Select the type, pull-up/pull-down and output speed via
\r
39 * GPIO_PuPd, GPIO_OType and GPIO_Speed members
\r
40 * - Call GPIO_Init() function
\r
42 * 4. Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
\r
43 * flow control and Mode(Receiver/Transmitter) using the SPI_Init()
\r
46 * 5. For synchronous mode, enable the clock and program the polarity,
\r
47 * phase and last bit using the USART_ClockInit() function.
\r
49 * 5. Enable the NVIC and the corresponding interrupt using the function
\r
50 * USART_ITConfig() if you need to use interrupt mode.
\r
52 * 6. When using the DMA mode
\r
53 * - Configure the DMA using DMA_Init() function
\r
54 * - Active the needed channel Request using USART_DMACmd() function
\r
56 * 7. Enable the USART using the USART_Cmd() function.
\r
58 * 8. Enable the DMA using the DMA_Cmd() function, when using DMA mode.
\r
60 * Refer to Multi-Processor, LIN, half-duplex, Smartcard, IrDA sub-sections
\r
65 ******************************************************************************
\r
68 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
69 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
\r
70 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
\r
71 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
\r
72 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
\r
73 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
75 * <h2><center>© COPYRIGHT 2010 STMicroelectronics</center></h2>
\r
76 ******************************************************************************
\r
79 /* Includes ------------------------------------------------------------------*/
\r
80 #include "stm32l1xx_usart.h"
\r
81 #include "stm32l1xx_rcc.h"
\r
83 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
87 /** @defgroup USART
\r
88 * @brief USART driver modules
\r
92 /* Private typedef -----------------------------------------------------------*/
\r
93 /* Private define ------------------------------------------------------------*/
\r
95 /*!< USART CR1 register clear Mask ((~(uint16_t)0xE9F3)) */
\r
96 #define CR1_CLEAR_MASK ((uint16_t)(USART_CR1_M | USART_CR1_PCE | \
\r
97 USART_CR1_PS | USART_CR1_TE | \
\r
100 /*!< USART CR2 register clock bits clear Mask ((~(uint16_t)0xF0FF)) */
\r
101 #define CR2_CLOCK_CLEAR_MASK ((uint16_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
\r
102 USART_CR2_CPHA | USART_CR2_LBCL))
\r
104 /*!< USART CR3 register clear Mask ((~(uint16_t)0xFCFF)) */
\r
105 #define CR3_CLEAR_MASK ((uint16_t)(USART_CR3_RTSE | USART_CR3_CTSE))
\r
107 /*!< USART Interrupts mask */
\r
108 #define IT_MASK ((uint16_t)0x001F)
\r
110 /* Private macro -------------------------------------------------------------*/
\r
111 /* Private variables ---------------------------------------------------------*/
\r
112 /* Private function prototypes -----------------------------------------------*/
\r
113 /* Private functions ---------------------------------------------------------*/
\r
115 /** @defgroup USART_Private_Functions
\r
119 /** @defgroup USART_Group1 Initialization and Configuration functions
\r
120 * @brief Initialization and Configuration functions
\r
123 ===============================================================================
\r
124 Initialization and Configuration functions
\r
125 ===============================================================================
\r
127 This subsection provides a set of functions allowing to initialize the USART
\r
128 in asynchronous and in synchronous modes.
\r
129 - For the asynchronous mode only these parameters can be configured:
\r
133 - Parity: If the parity is enabled, then the MSB bit of the data written
\r
134 in the data register is transmitted but is changed by the parity bit.
\r
135 Depending on the frame length defined by the M bit (8-bits or 9-bits),
\r
136 the possible USART frame formats are as listed in the following table:
\r
137 +-------------------------------------------------------------+
\r
138 | M bit | PCE bit | USART frame |
\r
139 |---------------------|---------------------------------------|
\r
140 | 0 | 0 | | SB | 8 bit data | STB | |
\r
141 |---------|-----------|---------------------------------------|
\r
142 | 0 | 1 | | SB | 7 bit data | PB | STB | |
\r
143 |---------|-----------|---------------------------------------|
\r
144 | 1 | 0 | | SB | 9 bit data | STB | |
\r
145 |---------|-----------|---------------------------------------|
\r
146 | 1 | 1 | | SB | 8 bit data | PB | STB | |
\r
147 +-------------------------------------------------------------+
\r
148 - Hardware flow control
\r
149 - Receiver/transmitter modes
\r
151 The USART_Init() function follows the USART asynchronous configuration procedure
\r
152 (details for the procedure are available in reference manual (RM0038)).
\r
154 - For the synchronous mode in addition to the asynchronous mode parameters these
\r
155 parameters should be also configured:
\r
156 - USART Clock Enabled
\r
161 These parameters can be configured using the USART_ClockInit() function.
\r
168 * @brief Deinitializes the USARTx peripheral registers to their default reset values.
\r
169 * @param USARTx: Select the USART peripheral.
\r
170 * This parameter can be one of the following values: USART1, USART2 or USART3.
\r
173 void USART_DeInit(USART_TypeDef* USARTx)
\r
175 /* Check the parameters */
\r
176 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
178 if (USARTx == USART1)
\r
180 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
\r
181 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
\r
183 else if (USARTx == USART2)
\r
185 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
\r
186 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
\r
190 if (USARTx == USART3)
\r
192 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
\r
193 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
\r
199 * @brief Initializes the USARTx peripheral according to the specified
\r
200 * parameters in the USART_InitStruct .
\r
201 * @param USARTx: Select the USART peripheral.
\r
202 * This parameter can be one of the following values:
\r
203 * USART1, USART2 or USART3.
\r
204 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
\r
205 * that contains the configuration information for the specified USART peripheral.
\r
208 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
\r
210 uint32_t tmpreg = 0x00, apbclock = 0x00;
\r
211 uint32_t integerdivider = 0x00;
\r
212 uint32_t fractionaldivider = 0x00;
\r
213 RCC_ClocksTypeDef RCC_ClocksStatus;
\r
215 /* Check the parameters */
\r
216 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
217 assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
\r
218 assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
\r
219 assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
\r
220 assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
\r
221 assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
\r
222 assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
\r
224 /*---------------------------- USART CR2 Configuration -----------------------*/
\r
225 tmpreg = USARTx->CR2;
\r
226 /* Clear STOP[13:12] bits */
\r
227 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
\r
229 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
\r
230 /* Set STOP[13:12] bits according to USART_StopBits value */
\r
231 tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
\r
233 /* Write to USART CR2 */
\r
234 USARTx->CR2 = (uint16_t)tmpreg;
\r
236 /*---------------------------- USART CR1 Configuration -----------------------*/
\r
237 tmpreg = USARTx->CR1;
\r
238 /* Clear M, PCE, PS, TE and RE bits */
\r
239 tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);
\r
241 /* Configure the USART Word Length, Parity and mode ----------------------- */
\r
242 /* Set the M bits according to USART_WordLength value */
\r
243 /* Set PCE and PS bits according to USART_Parity value */
\r
244 /* Set TE and RE bits according to USART_Mode value */
\r
245 tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
\r
246 USART_InitStruct->USART_Mode;
\r
248 /* Write to USART CR1 */
\r
249 USARTx->CR1 = (uint16_t)tmpreg;
\r
251 /*---------------------------- USART CR3 Configuration -----------------------*/
\r
252 tmpreg = USARTx->CR3;
\r
253 /* Clear CTSE and RTSE bits */
\r
254 tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);
\r
256 /* Configure the USART HFC -------------------------------------------------*/
\r
257 /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
\r
258 tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
\r
260 /* Write to USART CR3 */
\r
261 USARTx->CR3 = (uint16_t)tmpreg;
\r
263 /*---------------------------- USART BRR Configuration -----------------------*/
\r
264 /* Configure the USART Baud Rate -------------------------------------------*/
\r
265 RCC_GetClocksFreq(&RCC_ClocksStatus);
\r
266 if (USARTx == USART1)
\r
268 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
\r
272 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
\r
275 /* Determine the integer part */
\r
276 if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
\r
278 /* Integer part computing in case Oversampling mode is 8 Samples */
\r
279 integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
\r
281 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
\r
283 /* Integer part computing in case Oversampling mode is 16 Samples */
\r
284 integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
\r
286 tmpreg = (integerdivider / 100) << 4;
\r
288 /* Determine the fractional part */
\r
289 fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
\r
291 /* Implement the fractional part in the register */
\r
292 if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
\r
294 tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
\r
296 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
\r
298 tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
\r
301 /* Write to USART BRR */
\r
302 USARTx->BRR = (uint16_t)tmpreg;
\r
306 * @brief Fills each USART_InitStruct member with its default value.
\r
307 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
\r
308 * which will be initialized.
\r
311 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
\r
313 /* USART_InitStruct members default value */
\r
314 USART_InitStruct->USART_BaudRate = 9600;
\r
315 USART_InitStruct->USART_WordLength = USART_WordLength_8b;
\r
316 USART_InitStruct->USART_StopBits = USART_StopBits_1;
\r
317 USART_InitStruct->USART_Parity = USART_Parity_No ;
\r
318 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
\r
319 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
\r
323 * @brief Initializes the USARTx peripheral Clock according to the
\r
324 * specified parameters in the USART_ClockInitStruct .
\r
325 * @param USARTx: where x can be 1, 2, 3 to select the USART peripheral.
\r
326 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
\r
327 * structure that contains the configuration information for the specified
\r
328 * USART peripheral.
\r
331 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
\r
333 uint32_t tmpreg = 0x00;
\r
334 /* Check the parameters */
\r
335 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
336 assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
\r
337 assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
\r
338 assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
\r
339 assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
\r
341 /*---------------------------- USART CR2 Configuration -----------------------*/
\r
342 tmpreg = USARTx->CR2;
\r
343 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
\r
344 tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);
\r
345 /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
\r
346 /* Set CLKEN bit according to USART_Clock value */
\r
347 /* Set CPOL bit according to USART_CPOL value */
\r
348 /* Set CPHA bit according to USART_CPHA value */
\r
349 /* Set LBCL bit according to USART_LastBit value */
\r
350 tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
\r
351 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
\r
352 /* Write to USART CR2 */
\r
353 USARTx->CR2 = (uint16_t)tmpreg;
\r
357 * @brief Fills each USART_ClockInitStruct member with its default value.
\r
358 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
\r
359 * structure which will be initialized.
\r
362 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
\r
364 /* USART_ClockInitStruct members default value */
\r
365 USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
\r
366 USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
\r
367 USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
\r
368 USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
\r
372 * @brief Enables or disables the specified USART peripheral.
\r
373 * @param USARTx: Select the USART peripheral.
\r
374 * This parameter can be one of the following values:
\r
375 * USART1, USART2 or USART3.
\r
376 * @param NewState: new state of the USARTx peripheral.
\r
377 * This parameter can be: ENABLE or DISABLE.
\r
380 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
382 /* Check the parameters */
\r
383 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
384 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
386 if (NewState != DISABLE)
\r
388 /* Enable the selected USART by setting the UE bit in the CR1 register */
\r
389 USARTx->CR1 |= USART_CR1_UE;
\r
393 /* Disable the selected USART by clearing the UE bit in the CR1 register */
\r
394 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_UE);
\r
399 * @brief Sets the system clock prescaler.
\r
400 * @param USARTx: Select the USART peripheral.
\r
401 * This parameter can be one of the following values:
\r
402 * USART1, USART2 or USART3.
\r
403 * @param USART_Prescaler: specifies the prescaler clock.
\r
406 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
\r
408 /* Check the parameters */
\r
409 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
411 /* Clear the USART prescaler */
\r
412 USARTx->GTPR &= USART_GTPR_GT;
\r
413 /* Set the USART prescaler */
\r
414 USARTx->GTPR |= USART_Prescaler;
\r
418 * @brief Enables or disables the USART's 8x oversampling mode.
\r
419 * @param USARTx: Select the USART peripheral.
\r
420 * This parameter can be one of the following values:
\r
421 * USART1, USART2, USART3.
\r
422 * @param NewState: new state of the USART 8x oversampling mode.
\r
423 * This parameter can be: ENABLE or DISABLE.
\r
426 * This function has to be called before calling USART_Init()
\r
427 * function in order to have correct baudrate Divider value.
\r
430 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
432 /* Check the parameters */
\r
433 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
434 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
436 if (NewState != DISABLE)
\r
438 /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
\r
439 USARTx->CR1 |= USART_CR1_OVER8;
\r
443 /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
\r
444 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_OVER8);
\r
449 * @brief Enables or disables the USART's one bit sampling method.
\r
450 * @param USARTx: Select the USART peripheral.
\r
451 * This parameter can be one of the following values:
\r
452 * USART1, USART2, USART3.
\r
453 * @param NewState: new state of the USART one bit sampling method.
\r
454 * This parameter can be: ENABLE or DISABLE.
\r
457 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
459 /* Check the parameters */
\r
460 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
461 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
463 if (NewState != DISABLE)
\r
465 /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
\r
466 USARTx->CR3 |= USART_CR3_ONEBIT;
\r
470 /* Disable the one bit method by clearing the ONEBITE bit in the CR3 register */
\r
471 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT);
\r
479 /** @defgroup USART_Group2 Data transfers functions
\r
480 * @brief Data transfers functions
\r
483 ===============================================================================
\r
484 Data transfers functions
\r
485 ===============================================================================
\r
487 This subsection provides a set of functions allowing to manage the USART data
\r
490 During an USART reception, data shifts in least significant bit first through
\r
491 the RX pin. In this mode, the USART_DR register consists of a buffer (RDR)
\r
492 between the internal bus and the received shift register.
\r
494 When a transmission is taking place, a write instruction to the USART_DR register
\r
495 stores the data in the TDR register and which is copied in the shift register
\r
496 at the end of the current transmission.
\r
498 The read access of the USART_DR register can be done using the USART_ReceiveData()
\r
499 function and returns the RDR buffered value. Whereas a write access to the USART_DR
\r
500 can be done using USART_SendData() function and stores the written data into
\r
508 * @brief Transmits single data through the USARTx peripheral.
\r
509 * @param USARTx: Select the USART peripheral.
\r
510 * This parameter can be one of the following values:
\r
511 * USART1, USART2 or USART3.
\r
512 * @param Data: the data to transmit.
\r
515 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
\r
517 /* Check the parameters */
\r
518 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
519 assert_param(IS_USART_DATA(Data));
\r
521 /* Transmit Data */
\r
522 USARTx->DR = (Data & (uint16_t)0x01FF);
\r
526 * @brief Returns the most recent received data by the USARTx peripheral.
\r
527 * @param USARTx: Select the USART peripheral.
\r
528 * This parameter can be one of the following values:
\r
529 * USART1, USART2 or USART3.
\r
530 * @retval The received data.
\r
532 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
\r
534 /* Check the parameters */
\r
535 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
538 return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
\r
545 /** @defgroup USART_Group3 MultiProcessor Communication functions
\r
546 * @brief Multi-Processor Communication functions
\r
549 ===============================================================================
\r
550 Multi-Processor Communication functions
\r
551 ===============================================================================
\r
553 This subsection provides a set of functions allowing to manage the USART
\r
554 multiprocessor communication.
\r
556 For instance one of the USARTs can be the master, its TX output is connected to
\r
557 the RX input of the other USART. The others are slaves, their respective TX outputs
\r
558 are logically ANDed together and connected to the RX input of the master.
\r
560 USART multiprocessor communication is possible through the following procedure:
\r
561 1. Program the Baud rate, Word length = 9 bits, Stop bits, Parity, Mode transmitter
\r
562 or Mode receiver and hardware flow control values using the USART_Init()
\r
564 2. Configures the USART address using the USART_SetAddress() function.
\r
565 3. Configures the wake up methode (USART_WakeUp_IdleLine or USART_WakeUp_AddressMark)
\r
566 using USART_WakeUpConfig() function only for the slaves.
\r
567 4. Enable the USART using the USART_Cmd() function.
\r
568 5. Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd() function.
\r
570 The USART Slave exit from mute mode when receive the wake up condition.
\r
577 * @brief Sets the address of the USART node.
\r
578 * @param USARTx: Select the USART peripheral.
\r
579 * This parameter can be one of the following values:
\r
580 * USART1, USART2 or USART3.
\r
581 * @param USART_Address: Indicates the address of the USART node.
\r
584 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
\r
586 /* Check the parameters */
\r
587 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
588 assert_param(IS_USART_ADDRESS(USART_Address));
\r
590 /* Clear the USART address */
\r
591 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_ADD);
\r
592 /* Set the USART address node */
\r
593 USARTx->CR2 |= USART_Address;
\r
597 * @brief Determines if the USART is in mute mode or not.
\r
598 * @param USARTx: Select the USART peripheral.
\r
599 * This parameter can be one of the following values:
\r
600 * USART1, USART2 or USART3.
\r
601 * @param NewState: new state of the USART mute mode.
\r
602 * This parameter can be: ENABLE or DISABLE.
\r
605 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
607 /* Check the parameters */
\r
608 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
609 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
611 if (NewState != DISABLE)
\r
613 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
\r
614 USARTx->CR1 |= USART_CR1_RWU;
\r
618 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
\r
619 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_RWU);
\r
623 * @brief Selects the USART WakeUp method.
\r
624 * @param USARTx: Select the USART peripheral.
\r
625 * This parameter can be one of the following values:
\r
626 * USART1, USART2 or USART3.
\r
627 * @param USART_WakeUp: specifies the USART wakeup method.
\r
628 * This parameter can be one of the following values:
\r
629 * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
\r
630 * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
\r
633 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
\r
635 /* Check the parameters */
\r
636 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
637 assert_param(IS_USART_WAKEUP(USART_WakeUp));
\r
639 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_WAKE);
\r
640 USARTx->CR1 |= USART_WakeUp;
\r
647 /** @defgroup USART_Group4 LIN mode functions
\r
648 * @brief LIN mode functions
\r
651 ===============================================================================
\r
653 ===============================================================================
\r
655 This subsection provides a set of functions allowing to manage the USART LIN
\r
656 Mode communication.
\r
658 In LIN mode, 8-bit data format with 1 stop bit is required in accordance with
\r
661 Only this LIN Feature is supported by the USART IP:
\r
662 - LIN Master Synchronous Break send capability and LIN slave break detection
\r
663 capability : 13-bit break generation and 10/11 bit break detection
\r
666 USART LIN Master transmitter communication is possible through the following procedure:
\r
667 1. Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
\r
668 Mode transmitter or Mode receiver and hardware flow control values using
\r
669 the USART_Init() function.
\r
670 2. Enable the USART using the USART_Cmd() function.
\r
671 3. Enable the LIN mode using the USART_LINCmd() function.
\r
672 4. Send the break character using USART_SendBreak() function.
\r
674 USART LIN Master receiver communication is possible through the following procedure:
\r
675 1. Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
\r
676 Mode transmitter or Mode receiver and hardware flow control values using
\r
677 the USART_Init() function.
\r
678 2. Enable the USART using the USART_Cmd() function.
\r
679 3. Configures the break detection length using the USART_LINBreakDetectLengthConfig()
\r
681 4. Enable the LIN mode using the USART_LINCmd() function.
\r
685 1. In LIN mode, the following bits must be kept cleared:
\r
686 - CLKEN in the USART_CR2 register,
\r
687 - STOP[1:0], SCEN, HDSEL and IREN in the USART_CR3 register.
\r
694 * @brief Sets the USART LIN Break detection length.
\r
695 * @param USARTx: Select the USART peripheral.
\r
696 * This parameter can be one of the following values:
\r
697 * USART1, USART2 or USART3.
\r
698 * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
\r
699 * This parameter can be one of the following values:
\r
700 * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
\r
701 * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
\r
704 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
\r
706 /* Check the parameters */
\r
707 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
708 assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
\r
710 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LBDL);
\r
711 USARTx->CR2 |= USART_LINBreakDetectLength;
\r
715 * @brief Enables or disables the USART
\92s LIN mode.
\r
716 * @param USARTx: Select the USART peripheral.
\r
717 * This parameter can be one of the following values:
\r
718 * USART1, USART2 or USART3.
\r
719 * @param NewState: new state of the USART LIN mode.
\r
720 * This parameter can be: ENABLE or DISABLE.
\r
723 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
725 /* Check the parameters */
\r
726 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
727 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
729 if (NewState != DISABLE)
\r
731 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
\r
732 USARTx->CR2 |= USART_CR2_LINEN;
\r
736 /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
\r
737 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LINEN);
\r
742 * @brief Transmits break characters.
\r
743 * @param USARTx: Select the USART peripheral.
\r
744 * This parameter can be one of the following values:
\r
745 * USART1, USART2 or USART3.
\r
748 void USART_SendBreak(USART_TypeDef* USARTx)
\r
750 /* Check the parameters */
\r
751 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
753 /* Send break characters */
\r
754 USARTx->CR1 |= USART_CR1_SBK;
\r
761 /** @defgroup USART_Group5 Halfduplex mode function
\r
762 * @brief Half-duplex mode function
\r
765 ===============================================================================
\r
766 Half-duplex mode function
\r
767 ===============================================================================
\r
769 This subsection provides a set of functions allowing to manage the USART
\r
770 Half-duplex communication.
\r
772 The USART can be configured to follow a single-wire half-duplex protocol where
\r
773 the TX and RX lines are internally connected.
\r
775 USART Half duplex communication is possible through the following procedure:
\r
776 1. Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter
\r
777 or Mode receiver and hardware flow control values using the USART_Init()
\r
779 2. Configures the USART address using the USART_SetAddress() function.
\r
780 3. Enable the USART using the USART_Cmd() function.
\r
781 4. Enable the half duplex mode using USART_HalfDuplexCmd() function.
\r
785 1. The RX pin is no longer used
\r
786 2. In Half-duplex mode the following bits must be kept cleared:
\r
787 - LINEN and CLKEN bits in the USART_CR2 register.
\r
788 - SCEN and IREN bits in the USART_CR3 register.
\r
795 * @brief Enables or disables the USART
\92s Half Duplex communication.
\r
796 * @param USARTx: Select the USART peripheral.
\r
797 * This parameter can be one of the following values:
\r
798 * USART1, USART2 or USART3.
\r
799 * @param NewState: new state of the USART Communication.
\r
800 * This parameter can be: ENABLE or DISABLE.
\r
803 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
805 /* Check the parameters */
\r
806 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
807 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
809 if (NewState != DISABLE)
\r
811 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
\r
812 USARTx->CR3 |= USART_CR3_HDSEL;
\r
816 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
\r
817 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_HDSEL);
\r
826 /** @defgroup USART_Group6 Smartcard mode functions
\r
827 * @brief Smartcard mode functions
\r
830 ===============================================================================
\r
831 Smartcard mode functions
\r
832 ===============================================================================
\r
834 This subsection provides a set of functions allowing to manage the USART
\r
835 Smartcard communication.
\r
837 The Smartcard interface is designed to support asynchronous protocol Smartcards as
\r
838 defined in the ISO 7816-3 standard.
\r
840 The USART can provide a clock to the smartcard through the SCLK output.
\r
841 In smartcard mode, SCLK is not associated to the communication but is simply derived
\r
842 from the internal peripheral input clock through a 5-bit prescaler.
\r
844 Smartcard communication is possible through the following procedure:
\r
845 1. Configures the Smartcard Prsecaler using the USART_SetPrescaler() function.
\r
846 2. Configures the Smartcard Guard Time using the USART_SetGuardTime() function.
\r
847 3. Program the USART clock using the USART_ClockInit() function as following:
\r
848 - USART Clock enabled
\r
850 - USART CPHA on first edge
\r
851 - USART Last Bit Clock Enabled
\r
852 4. Program the Smartcard interface using the USART_Init() function as following:
\r
853 - Word Length = 9 Bits
\r
856 - BaudRate = 12096 baud
\r
857 - Hardware flow control disabled (RTS and CTS signals)
\r
858 - Tx and Rx enabled
\r
859 5. Optionally you can enable the parity error interrupt using the USART_ITConfig()
\r
861 6. Enable the USART using the USART_Cmd() function.
\r
862 7. Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function.
\r
863 8. Enable the Smartcard interface using the USART_SmartCardCmd() function.
\r
865 Please refer to the ISO 7816-3 specification for more details.
\r
869 1. It is also possible to choose 0.5 stop bit for receiving but it is recommended
\r
870 to use 1.5 stop bits for both transmitting and receiving to avoid switching
\r
871 between the two configurations.
\r
872 2. In smartcard mode, the following bits must be kept cleared:
\r
873 - LINEN bit in the USART_CR2 register.
\r
874 - HDSEL and IREN bits in the USART_CR3 register.
\r
881 * @brief Sets the specified USART guard time.
\r
882 * @param USARTx: Select the USART peripheral.
\r
883 * This parameter can be one of the following values:
\r
884 * USART1, USART2 or USART3.
\r
885 * @param USART_GuardTime: specifies the guard time.
\r
888 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
\r
890 /* Check the parameters */
\r
891 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
893 /* Clear the USART Guard time */
\r
894 USARTx->GTPR &= USART_GTPR_PSC;
\r
895 /* Set the USART guard time */
\r
896 USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
\r
900 * @brief Enables or disables the USART
\92s Smart Card mode.
\r
901 * @param USARTx: Select the USART peripheral.
\r
902 * This parameter can be one of the following values:
\r
903 * USART1, USART2 or USART3.
\r
904 * @param NewState: new state of the Smart Card mode.
\r
905 * This parameter can be: ENABLE or DISABLE.
\r
908 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
910 /* Check the parameters */
\r
911 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
912 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
913 if (NewState != DISABLE)
\r
915 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
\r
916 USARTx->CR3 |= USART_CR3_SCEN;
\r
920 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
\r
921 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_SCEN);
\r
926 * @brief Enables or disables NACK transmission.
\r
927 * @param USARTx: Select the USART peripheral.
\r
928 * This parameter can be one of the following values:
\r
929 * USART1, USART2 or USART3.
\r
930 * @param NewState: new state of the NACK transmission.
\r
931 * This parameter can be: ENABLE or DISABLE.
\r
934 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
936 /* Check the parameters */
\r
937 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
938 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
939 if (NewState != DISABLE)
\r
941 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
\r
942 USARTx->CR3 |= USART_CR3_NACK;
\r
946 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
\r
947 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_NACK);
\r
955 /** @defgroup USART_Group7 IrDA mode functions
\r
956 * @brief IrDA mode functions
\r
959 ===============================================================================
\r
960 IrDA mode functions
\r
961 ===============================================================================
\r
963 This subsection provides a set of functions allowing to manage the USART
\r
964 IrDA communication.
\r
966 IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
\r
967 on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
\r
968 is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
\r
969 While receiving data, transmission should be avoided as the data to be transmitted
\r
970 could be corrupted.
\r
972 IrDA communication is possible through the following procedure:
\r
973 1. Program the Baud rate, Word length = 8 bits, Stop bits, Parity, Transmitter/Receiver
\r
974 modes and hardware flow control values using the USART_Init() function.
\r
975 2. Enable the USART using the USART_Cmd() function.
\r
976 3. Configures the IrDA pulse width by configuring the prescaler using
\r
977 the USART_SetPrescaler() function.
\r
978 4. Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal mode
\r
979 using the USART_IrDAConfig() function.
\r
980 5. Enable the IrDA using the USART_IrDACmd() function.
\r
984 1. A pulse of width less than two and greater than one PSC period(s) may or may
\r
986 2. The receiver set up time should be managed by software. The IrDA physical layer
\r
987 specification specifies a minimum of 10 ms delay between transmission and
\r
988 reception (IrDA is a half duplex protocol).
\r
989 3. In IrDA mode, the following bits must be kept cleared:
\r
990 - LINEN, STOP and CLKEN bits in the USART_CR2 register.
\r
991 - SCEN and HDSEL bits in the USART_CR3 register.
\r
998 * @brief Configures the USART
\92s IrDA interface.
\r
999 * @param USARTx: Select the USART peripheral.
\r
1000 * This parameter can be one of the following values:
\r
1001 * USART1, USART2 or USART3.
\r
1002 * @param USART_IrDAMode: specifies the IrDA mode.
\r
1003 * This parameter can be one of the following values:
\r
1004 * @arg USART_IrDAMode_LowPower
\r
1005 * @arg USART_IrDAMode_Normal
\r
1008 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
\r
1010 /* Check the parameters */
\r
1011 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
1012 assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
\r
1014 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IRLP);
\r
1015 USARTx->CR3 |= USART_IrDAMode;
\r
1019 * @brief Enables or disables the USART
\92s IrDA interface.
\r
1020 * @param USARTx: Select the USART peripheral.
\r
1021 * This parameter can be one of the following values:
\r
1022 * USART1, USART2 or USART3.
\r
1023 * @param NewState: new state of the IrDA mode.
\r
1024 * This parameter can be: ENABLE or DISABLE.
\r
1027 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
1029 /* Check the parameters */
\r
1030 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
1031 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
1033 if (NewState != DISABLE)
\r
1035 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
\r
1036 USARTx->CR3 |= USART_CR3_IREN;
\r
1040 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
\r
1041 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IREN);
\r
1049 /** @defgroup USART_Group8 DMA transfers management functions
\r
1050 * @brief DMA transfers management functions
\r
1053 ===============================================================================
\r
1054 DMA transfers management functions
\r
1055 ===============================================================================
\r
1062 * @brief Enables or disables the USART
\92s DMA interface.
\r
1063 * @param USARTx: Select the USART peripheral.
\r
1064 * This parameter can be one of the following values:
\r
1065 * USART1, USART2 or USART3.
\r
1066 * @param USART_DMAReq: specifies the DMA request.
\r
1067 * This parameter can be any combination of the following values:
\r
1068 * @arg USART_DMAReq_Tx: USART DMA transmit request
\r
1069 * @arg USART_DMAReq_Rx: USART DMA receive request
\r
1070 * @param NewState: new state of the DMA Request sources.
\r
1071 * This parameter can be: ENABLE or DISABLE.
\r
1074 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
\r
1076 /* Check the parameters */
\r
1077 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
1078 assert_param(IS_USART_DMAREQ(USART_DMAReq));
\r
1079 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
1081 if (NewState != DISABLE)
\r
1083 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
\r
1084 DMAR bits in the USART CR3 register */
\r
1085 USARTx->CR3 |= USART_DMAReq;
\r
1089 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
\r
1090 DMAR bits in the USART CR3 register */
\r
1091 USARTx->CR3 &= (uint16_t)~USART_DMAReq;
\r
1099 /** @defgroup USART_Group9 Interrupts and flags management functions
\r
1100 * @brief Interrupts and flags management functions
\r
1103 ===============================================================================
\r
1104 Interrupts and flags management functions
\r
1105 ===============================================================================
\r
1107 This subsection provides a set of functions allowing to configure the USART
\r
1108 Interrupts sources, DMA channels requests and check or clear the flags or
\r
1109 pending bits status.
\r
1110 The user should identify which mode will be used in his application to manage
\r
1111 the communication: Polling mode, Interrupt mode or DMA mode.
\r
1115 In Polling Mode, the SPI communication can be managed by 10 flags:
\r
1116 1. USART_FLAG_TXE : to indicate the status of the transmit buffer register
\r
1117 2. USART_FLAG_RXNE : to indicate the status of the receive buffer register
\r
1118 3. USART_FLAG_TC : to indicate the status of the transmit operation
\r
1119 4. USART_FLAG_IDLE : to indicate the status of the Idle Line
\r
1120 5. USART_FLAG_CTS : to indicate the status of the nCTS input
\r
1121 6. USART_FLAG_LBD : to indicate the status of the LIN break detection
\r
1122 7. USART_FLAG_NE : to indicate if a noise error occur
\r
1123 8. USART_FLAG_FE : to indicate if a frame error occur
\r
1124 9. USART_FLAG_PE : to indicate if a parity error occur
\r
1125 10. USART_FLAG_ORE : to indicate if an Overrun error occur
\r
1127 In this Mode it is advised to use the following functions:
\r
1128 - FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
\r
1129 - void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);
\r
1133 In Interrupt Mode, the USART communication can be managed by 8 interrupt sources
\r
1134 and 10 pending bits:
\r
1138 1. USART_IT_TXE : to indicate the status of the transmit buffer register
\r
1139 2. USART_IT_RXNE : to indicate the status of the receive buffer register
\r
1140 3. USART_IT_TC : to indicate the status of the transmit operation
\r
1141 4. USART_IT_IDLE : to indicate the status of the Idle Line
\r
1142 5. USART_IT_CTS : to indicate the status of the nCTS input
\r
1143 6. USART_IT_LBD : to indicate the status of the LIN break detection
\r
1144 7. USART_IT_NE : to indicate if a noise error occur
\r
1145 8. USART_IT_FE : to indicate if a frame error occur
\r
1146 9. USART_IT_PE : to indicate if a parity error occur
\r
1147 10. USART_IT_ORE : to indicate if an Overrun error occur
\r
1151 1. USART_IT_TXE : specifies the interrupt source for the Tx buffer empty
\r
1153 2. USART_IT_RXNE : specifies the interrupt source for the Rx buffer not
\r
1155 3. USART_IT_TC : specifies the interrupt source for the Transmit complete
\r
1157 4. USART_IT_IDLE : specifies the interrupt source for the Idle Line interrupt.
\r
1158 5. USART_IT_CTS : specifies the interrupt source for the CTS interrupt.
\r
1159 6. USART_IT_LBD : specifies the interrupt source for the LIN break detection
\r
1161 7. USART_IT_PE : specifies the interrupt source for theparity error interrupt.
\r
1162 8. USART_IT_ERR : specifies the interrupt source for the errors interrupt.
\r
1164 Note: Some parameters are coded in order to use them as interrupt source or
\r
1165 ---- as pending bits.
\r
1167 In this Mode it is advised to use the following functions:
\r
1168 - void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
\r
1169 - ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
\r
1170 - void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);
\r
1174 In DMA Mode, the USART communication can be managed by 2 DMA Channel requests:
\r
1175 1. USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request
\r
1176 2. USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request
\r
1178 In this Mode it is advised to use the following function:
\r
1179 - void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
\r
1186 * @brief Enables or disables the specified USART interrupts.
\r
1187 * @param USARTx: Select the USART peripheral.
\r
1188 * This parameter can be one of the following values:
\r
1189 * USART1, USART2 or USART3.
\r
1190 * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
\r
1191 * This parameter can be one of the following values:
\r
1192 * @arg USART_IT_CTS: CTS change interrupt
\r
1193 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
1194 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
\r
1195 * @arg USART_IT_TC: Transmission complete interrupt
\r
1196 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
\r
1197 * @arg USART_IT_IDLE: Idle line detection interrupt
\r
1198 * @arg USART_IT_PE: Parity Error interrupt
\r
1199 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
\r
1200 * @param NewState: new state of the specified USARTx interrupts.
\r
1201 * This parameter can be: ENABLE or DISABLE.
\r
1204 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
\r
1206 uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
\r
1207 uint32_t usartxbase = 0x00;
\r
1208 /* Check the parameters */
\r
1209 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
1210 assert_param(IS_USART_CONFIG_IT(USART_IT));
\r
1211 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
1213 usartxbase = (uint32_t)USARTx;
\r
1215 /* Get the USART register index */
\r
1216 usartreg = (((uint8_t)USART_IT) >> 0x05);
\r
1218 /* Get the interrupt position */
\r
1219 itpos = USART_IT & IT_MASK;
\r
1220 itmask = (((uint32_t)0x01) << itpos);
\r
1222 if (usartreg == 0x01) /* The IT is in CR1 register */
\r
1224 usartxbase += 0x0C;
\r
1226 else if (usartreg == 0x02) /* The IT is in CR2 register */
\r
1228 usartxbase += 0x10;
\r
1230 else /* The IT is in CR3 register */
\r
1232 usartxbase += 0x14;
\r
1234 if (NewState != DISABLE)
\r
1236 *(__IO uint32_t*)usartxbase |= itmask;
\r
1240 *(__IO uint32_t*)usartxbase &= ~itmask;
\r
1245 * @brief Checks whether the specified USART flag is set or not.
\r
1246 * @param USARTx: Select the USART peripheral.
\r
1247 * This parameter can be one of the following values:
\r
1248 * USART1, USART2 or USART3.
\r
1249 * @param USART_FLAG: specifies the flag to check.
\r
1250 * This parameter can be one of the following values:
\r
1251 * @arg USART_FLAG_CTS: CTS Change flag
\r
1252 * @arg USART_FLAG_LBD: LIN Break detection flag
\r
1253 * @arg USART_FLAG_TXE: Transmit data register empty flag
\r
1254 * @arg USART_FLAG_TC: Transmission Complete flag
\r
1255 * @arg USART_FLAG_RXNE: Receive data register not empty flag
\r
1256 * @arg USART_FLAG_IDLE: Idle Line detection flag
\r
1257 * @arg USART_FLAG_ORE: OverRun Error flag
\r
1258 * @arg USART_FLAG_NE: Noise Error flag
\r
1259 * @arg USART_FLAG_FE: Framing Error flag
\r
1260 * @arg USART_FLAG_PE: Parity Error flag
\r
1261 * @retval The new state of USART_FLAG (SET or RESET).
\r
1263 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
\r
1265 FlagStatus bitstatus = RESET;
\r
1266 /* Check the parameters */
\r
1267 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
1268 assert_param(IS_USART_FLAG(USART_FLAG));
\r
1270 if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
\r
1276 bitstatus = RESET;
\r
1282 * @brief Clears the USARTx's pending flags.
\r
1283 * @param USARTx: Select the USART peripheral.
\r
1284 * This parameter can be one of the following values:
\r
1285 * USART1, USART2 or USART3.
\r
1286 * @param USART_FLAG: specifies the flag to clear.
\r
1287 * This parameter can be any combination of the following values:
\r
1288 * @arg USART_FLAG_CTS: CTS Change flag.
\r
1289 * @arg USART_FLAG_LBD: LIN Break detection flag.
\r
1290 * @arg USART_FLAG_TC: Transmission Complete flag.
\r
1291 * @arg USART_FLAG_RXNE: Receive data register not empty flag.
\r
1294 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
\r
1295 * error) and IDLE (Idle line detected) flags are cleared by software
\r
1296 * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
\r
1297 * followed by a read operation to USART_DR register (USART_ReceiveData()).
\r
1298 * - RXNE flag can be also cleared by a read to the USART_DR register
\r
1299 * (USART_ReceiveData()).
\r
1300 * - TC flag can be also cleared by software sequence: a read operation to
\r
1301 * USART_SR register (USART_GetFlagStatus()) followed by a write operation
\r
1302 * to USART_DR register (USART_SendData()).
\r
1303 * - TXE flag is cleared only by a write to the USART_DR register
\r
1304 * (USART_SendData()).
\r
1307 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
\r
1309 /* Check the parameters */
\r
1310 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
1311 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
\r
1313 USARTx->SR = (uint16_t)~USART_FLAG;
\r
1317 * @brief Checks whether the specified USART interrupt has occurred or not.
\r
1318 * @param USARTx: Select the USART peripheral.
\r
1319 * This parameter can be one of the following values:
\r
1320 * USART1, USART2 or USART3.
\r
1321 * @param USART_IT: specifies the USART interrupt source to check.
\r
1322 * This parameter can be one of the following values:
\r
1323 * @arg USART_IT_CTS: CTS change interrupt
\r
1324 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
1325 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
\r
1326 * @arg USART_IT_TC: Transmission complete interrupt
\r
1327 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
\r
1328 * @arg USART_IT_IDLE: Idle line detection interrupt
\r
1329 * @arg USART_IT_ORE: OverRun Error interrupt
\r
1330 * @arg USART_IT_NE: Noise Error interrupt
\r
1331 * @arg USART_IT_FE: Framing Error interrupt
\r
1332 * @arg USART_IT_PE: Parity Error interrupt
\r
1333 * @retval The new state of USART_IT (SET or RESET).
\r
1335 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
\r
1337 uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
\r
1338 ITStatus bitstatus = RESET;
\r
1339 /* Check the parameters */
\r
1340 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
1341 assert_param(IS_USART_GET_IT(USART_IT));
\r
1343 /* Get the USART register index */
\r
1344 usartreg = (((uint8_t)USART_IT) >> 0x05);
\r
1345 /* Get the interrupt position */
\r
1346 itmask = USART_IT & IT_MASK;
\r
1347 itmask = (uint32_t)0x01 << itmask;
\r
1349 if (usartreg == 0x01) /* The IT is in CR1 register */
\r
1351 itmask &= USARTx->CR1;
\r
1353 else if (usartreg == 0x02) /* The IT is in CR2 register */
\r
1355 itmask &= USARTx->CR2;
\r
1357 else /* The IT is in CR3 register */
\r
1359 itmask &= USARTx->CR3;
\r
1362 bitpos = USART_IT >> 0x08;
\r
1363 bitpos = (uint32_t)0x01 << bitpos;
\r
1364 bitpos &= USARTx->SR;
\r
1365 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
\r
1371 bitstatus = RESET;
\r
1374 return bitstatus;
\r
1378 * @brief Clears the USARTx
\92s interrupt pending bits.
\r
1379 * @param USARTx: Select the USART peripheral.
\r
1380 * This parameter can be one of the following values:
\r
1381 * USART1, USART2 or USART3.
\r
1382 * @param USART_IT: specifies the interrupt pending bit to clear.
\r
1383 * This parameter can be one of the following values:
\r
1384 * @arg USART_IT_CTS: CTS change interrupt
\r
1385 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
1386 * @arg USART_IT_TC: Transmission complete interrupt.
\r
1387 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
\r
1390 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
\r
1391 * error) and IDLE (Idle line detected) pending bits are cleared by
\r
1392 * software sequence: a read operation to USART_SR register
\r
1393 * (USART_GetITStatus()) followed by a read operation to USART_DR register
\r
1394 * (USART_ReceiveData()).
\r
1395 * - RXNE pending bit can be also cleared by a read to the USART_DR register
\r
1396 * (USART_ReceiveData()).
\r
1397 * - TC pending bit can be also cleared by software sequence: a read
\r
1398 * operation to USART_SR register (USART_GetITStatus()) followed by a write
\r
1399 * operation to USART_DR register (USART_SendData()).
\r
1400 * - TXE pending bit is cleared only by a write to the USART_DR register
\r
1401 * (USART_SendData()).
\r
1404 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
\r
1406 uint16_t bitpos = 0x00, itmask = 0x00;
\r
1407 /* Check the parameters */
\r
1408 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
1409 assert_param(IS_USART_CLEAR_IT(USART_IT));
\r
1411 bitpos = USART_IT >> 0x08;
\r
1412 itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
\r
1413 USARTx->SR = (uint16_t)~itmask;
\r
1432 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
\r