2 ******************************************************************************
3 * @file stm32f30x_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
14 * + Multi-Processor Communication
20 * + DMA transfers management
21 * + Interrupts and flags management
24 ===============================================================================
25 ##### How to use this driver #####
26 ===============================================================================
28 (#) Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)
29 function for USART1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE)
30 function for USART2, USART3, UART4 and UART5.
31 (#) According to the USART mode, enable the GPIO clocks using
32 RCC_AHBPeriphClockCmd() function. (The I/O can be TX, RX, CTS,
34 (#) Peripheral's alternate function:
35 (++) Connect the pin to the desired peripherals' Alternate
36 Function (AF) using GPIO_PinAFConfig() function.
37 (++) Configure the desired pin in alternate function by:
38 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF.
39 (++) Select the type, pull-up/pull-down and output speed via
40 GPIO_PuPd, GPIO_OType and GPIO_Speed members.
41 (++) Call GPIO_Init() function.
42 (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
43 flow control and Mode(Receiver/Transmitter) using the SPI_Init()
45 (#) For synchronous mode, enable the clock and program the polarity,
46 phase and last bit using the USART_ClockInit() function.
47 (#) Enable the USART using the USART_Cmd() function.
48 (#) Enable the NVIC and the corresponding interrupt using the function
49 USART_ITConfig() if you need to use interrupt mode.
50 (#) When using the DMA mode:
51 (++) Configure the DMA using DMA_Init() function.
52 (++) Activate the needed channel Request using USART_DMACmd() function.
53 (#) Enable the DMA using the DMA_Cmd() function, when using DMA mode.
55 Refer to Multi-Processor, LIN, half-duplex, Smartcard, IrDA sub-sections
60 ******************************************************************************
63 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2>
65 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
66 * You may not use this file except in compliance with the License.
67 * You may obtain a copy of the License at:
69 * http://www.st.com/software_license_agreement_liberty_v2
71 * Unless required by applicable law or agreed to in writing, software
72 * distributed under the License is distributed on an "AS IS" BASIS,
73 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74 * See the License for the specific language governing permissions and
75 * limitations under the License.
77 ******************************************************************************
80 /* Includes ------------------------------------------------------------------*/
81 #include "stm32f30x_usart.h"
82 #include "stm32f30x_rcc.h"
84 /** @addtogroup STM32F30x_StdPeriph_Driver
89 * @brief USART driver modules
93 /* Private typedef -----------------------------------------------------------*/
94 /* Private define ------------------------------------------------------------*/
96 /*!< USART CR1 register clear Mask ((~(uint32_t)0xFFFFE6F3)) */
97 #define CR1_CLEAR_MASK ((uint32_t)(USART_CR1_M | USART_CR1_PCE | \
98 USART_CR1_PS | USART_CR1_TE | \
101 /*!< USART CR2 register clock bits clear Mask ((~(uint32_t)0xFFFFF0FF)) */
102 #define CR2_CLOCK_CLEAR_MASK ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
103 USART_CR2_CPHA | USART_CR2_LBCL))
105 /*!< USART CR3 register clear Mask ((~(uint32_t)0xFFFFFCFF)) */
106 #define CR3_CLEAR_MASK ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE))
108 /*!< USART Interrupts mask */
109 #define IT_MASK ((uint32_t)0x000000FF)
111 /* Private macro -------------------------------------------------------------*/
112 /* Private variables ---------------------------------------------------------*/
113 /* Private function prototypes -----------------------------------------------*/
114 /* Private functions ---------------------------------------------------------*/
116 /** @defgroup USART_Private_Functions
120 /** @defgroup USART_Group1 Initialization and Configuration functions
121 * @brief Initialization and Configuration functions
124 ===============================================================================
125 ##### Initialization and Configuration functions #####
126 ===============================================================================
128 This subsection provides a set of functions allowing to initialize the USART
129 in asynchronous and in synchronous modes.
130 (+) For the asynchronous mode only these parameters can be configured:
134 (++) Parity: If the parity is enabled, then the MSB bit of the data written
135 in the data register is transmitted but is changed by the parity bit.
136 Depending on the frame length defined by the M bit (8-bits or 9-bits),
137 the possible USART frame formats are as listed in the following table:
139 +-------------------------------------------------------------+
140 | M bit | PCE bit | USART frame |
141 |---------------------|---------------------------------------|
142 | 0 | 0 | | SB | 8 bit data | STB | |
143 |---------|-----------|---------------------------------------|
144 | 0 | 1 | | SB | 7 bit data | PB | STB | |
145 |---------|-----------|---------------------------------------|
146 | 1 | 0 | | SB | 9 bit data | STB | |
147 |---------|-----------|---------------------------------------|
148 | 1 | 1 | | SB | 8 bit data | PB | STB | |
149 +-------------------------------------------------------------+
151 (++) Hardware flow control.
152 (++) Receiver/transmitter modes.
153 [..] The USART_Init() function follows the USART asynchronous configuration
154 procedure(details for the procedure are available in reference manual.
155 (+) For the synchronous mode in addition to the asynchronous mode parameters
156 these parameters should be also configured:
157 (++) USART Clock Enabled.
161 [..] These parameters can be configured using the USART_ClockInit() function.
168 * @brief Deinitializes the USARTx peripheral registers to their default reset values.
169 * @param USARTx: Select the USART peripheral. This parameter can be one of the
170 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
173 void USART_DeInit(USART_TypeDef
* USARTx
)
175 /* Check the parameters */
176 assert_param(IS_USART_ALL_PERIPH(USARTx
));
178 if (USARTx
== USART1
)
180 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1
, ENABLE
);
181 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1
, DISABLE
);
183 else if (USARTx
== USART2
)
185 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2
, ENABLE
);
186 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2
, DISABLE
);
188 else if (USARTx
== USART3
)
190 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3
, ENABLE
);
191 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3
, DISABLE
);
193 else if (USARTx
== UART4
)
195 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4
, ENABLE
);
196 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4
, DISABLE
);
202 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5
, ENABLE
);
203 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5
, DISABLE
);
209 * @brief Initializes the USARTx peripheral according to the specified
210 * parameters in the USART_InitStruct .
211 * @param USARTx: Select the USART peripheral. This parameter can be one of the
212 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
213 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
214 * that contains the configuration information for the specified USART peripheral.
217 void USART_Init(USART_TypeDef
* USARTx
, USART_InitTypeDef
* USART_InitStruct
)
219 uint32_t divider
= 0, apbclock
= 0, tmpreg
= 0;
220 RCC_ClocksTypeDef RCC_ClocksStatus
;
222 /* Check the parameters */
223 assert_param(IS_USART_ALL_PERIPH(USARTx
));
224 assert_param(IS_USART_BAUDRATE(USART_InitStruct
->USART_BaudRate
));
225 assert_param(IS_USART_WORD_LENGTH(USART_InitStruct
->USART_WordLength
));
226 assert_param(IS_USART_STOPBITS(USART_InitStruct
->USART_StopBits
));
227 assert_param(IS_USART_PARITY(USART_InitStruct
->USART_Parity
));
228 assert_param(IS_USART_MODE(USART_InitStruct
->USART_Mode
));
229 assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct
->USART_HardwareFlowControl
));
232 USARTx
->CR1
&= (uint32_t)~((uint32_t)USART_CR1_UE
);
234 /*---------------------------- USART CR2 Configuration -----------------------*/
235 tmpreg
= USARTx
->CR2
;
236 /* Clear STOP[13:12] bits */
237 tmpreg
&= (uint32_t)~((uint32_t)USART_CR2_STOP
);
239 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
240 /* Set STOP[13:12] bits according to USART_StopBits value */
241 tmpreg
|= (uint32_t)USART_InitStruct
->USART_StopBits
;
243 /* Write to USART CR2 */
244 USARTx
->CR2
= tmpreg
;
246 /*---------------------------- USART CR1 Configuration -----------------------*/
247 tmpreg
= USARTx
->CR1
;
248 /* Clear M, PCE, PS, TE and RE bits */
249 tmpreg
&= (uint32_t)~((uint32_t)CR1_CLEAR_MASK
);
251 /* Configure the USART Word Length, Parity and mode ----------------------- */
252 /* Set the M bits according to USART_WordLength value */
253 /* Set PCE and PS bits according to USART_Parity value */
254 /* Set TE and RE bits according to USART_Mode value */
255 tmpreg
|= (uint32_t)USART_InitStruct
->USART_WordLength
| USART_InitStruct
->USART_Parity
|
256 USART_InitStruct
->USART_Mode
;
258 /* Write to USART CR1 */
259 USARTx
->CR1
= tmpreg
;
261 /*---------------------------- USART CR3 Configuration -----------------------*/
262 tmpreg
= USARTx
->CR3
;
263 /* Clear CTSE and RTSE bits */
264 tmpreg
&= (uint32_t)~((uint32_t)CR3_CLEAR_MASK
);
266 /* Configure the USART HFC -------------------------------------------------*/
267 /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
268 tmpreg
|= USART_InitStruct
->USART_HardwareFlowControl
;
270 /* Write to USART CR3 */
271 USARTx
->CR3
= tmpreg
;
273 /*---------------------------- USART BRR Configuration -----------------------*/
274 /* Configure the USART Baud Rate -------------------------------------------*/
275 RCC_GetClocksFreq(&RCC_ClocksStatus
);
277 if (USARTx
== USART1
)
279 apbclock
= RCC_ClocksStatus
.USART1CLK_Frequency
;
281 else if (USARTx
== USART2
)
283 apbclock
= RCC_ClocksStatus
.USART2CLK_Frequency
;
285 else if (USARTx
== USART3
)
287 apbclock
= RCC_ClocksStatus
.USART3CLK_Frequency
;
289 else if (USARTx
== UART4
)
291 apbclock
= RCC_ClocksStatus
.UART4CLK_Frequency
;
295 apbclock
= RCC_ClocksStatus
.UART5CLK_Frequency
;
298 /* Determine the integer part */
299 if ((USARTx
->CR1
& USART_CR1_OVER8
) != 0)
301 /* (divider * 10) computing in case Oversampling mode is 8 Samples */
302 divider
= (uint32_t)((2 * apbclock
) / (USART_InitStruct
->USART_BaudRate
));
303 tmpreg
= (uint32_t)((2 * apbclock
) % (USART_InitStruct
->USART_BaudRate
));
305 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
307 /* (divider * 10) computing in case Oversampling mode is 16 Samples */
308 divider
= (uint32_t)((apbclock
) / (USART_InitStruct
->USART_BaudRate
));
309 tmpreg
= (uint32_t)((apbclock
) % (USART_InitStruct
->USART_BaudRate
));
312 /* round the divider : if fractional part i greater than 0.5 increment divider */
313 if (tmpreg
>= (USART_InitStruct
->USART_BaudRate
) / 2)
318 /* Implement the divider in case Oversampling mode is 8 Samples */
319 if ((USARTx
->CR1
& USART_CR1_OVER8
) != 0)
321 /* get the LSB of divider and shift it to the right by 1 bit */
322 tmpreg
= (divider
& (uint16_t)0x000F) >> 1;
324 /* update the divider value */
325 divider
= (divider
& (uint16_t)0xFFF0) | tmpreg
;
328 /* Write to USART BRR */
329 USARTx
->BRR
= (uint16_t)divider
;
333 * @brief Fills each USART_InitStruct member with its default value.
334 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
335 * which will be initialized.
338 void USART_StructInit(USART_InitTypeDef
* USART_InitStruct
)
340 /* USART_InitStruct members default value */
341 USART_InitStruct
->USART_BaudRate
= 9600;
342 USART_InitStruct
->USART_WordLength
= USART_WordLength_8b
;
343 USART_InitStruct
->USART_StopBits
= USART_StopBits_1
;
344 USART_InitStruct
->USART_Parity
= USART_Parity_No
;
345 USART_InitStruct
->USART_Mode
= USART_Mode_Rx
| USART_Mode_Tx
;
346 USART_InitStruct
->USART_HardwareFlowControl
= USART_HardwareFlowControl_None
;
350 * @brief Initializes the USARTx peripheral Clock according to the
351 * specified parameters in the USART_ClockInitStruct.
352 * @param USARTx: Select the USART peripheral. This parameter can be one of the
353 * following values: USART1 or USART2 or USART3.
354 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
355 * structure that contains the configuration information for the specified
359 void USART_ClockInit(USART_TypeDef
* USARTx
, USART_ClockInitTypeDef
* USART_ClockInitStruct
)
362 /* Check the parameters */
363 assert_param(IS_USART_123_PERIPH(USARTx
));
364 assert_param(IS_USART_CLOCK(USART_ClockInitStruct
->USART_Clock
));
365 assert_param(IS_USART_CPOL(USART_ClockInitStruct
->USART_CPOL
));
366 assert_param(IS_USART_CPHA(USART_ClockInitStruct
->USART_CPHA
));
367 assert_param(IS_USART_LASTBIT(USART_ClockInitStruct
->USART_LastBit
));
368 /*---------------------------- USART CR2 Configuration -----------------------*/
369 tmpreg
= USARTx
->CR2
;
370 /* Clear CLKEN, CPOL, CPHA, LBCL and SSM bits */
371 tmpreg
&= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK
);
372 /* Configure the USART Clock, CPOL, CPHA, LastBit and SSM ------------*/
373 /* Set CLKEN bit according to USART_Clock value */
374 /* Set CPOL bit according to USART_CPOL value */
375 /* Set CPHA bit according to USART_CPHA value */
376 /* Set LBCL bit according to USART_LastBit value */
377 tmpreg
|= (uint32_t)(USART_ClockInitStruct
->USART_Clock
| USART_ClockInitStruct
->USART_CPOL
|
378 USART_ClockInitStruct
->USART_CPHA
| USART_ClockInitStruct
->USART_LastBit
);
379 /* Write to USART CR2 */
380 USARTx
->CR2
= tmpreg
;
384 * @brief Fills each USART_ClockInitStruct member with its default value.
385 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
386 * structure which will be initialized.
389 void USART_ClockStructInit(USART_ClockInitTypeDef
* USART_ClockInitStruct
)
391 /* USART_ClockInitStruct members default value */
392 USART_ClockInitStruct
->USART_Clock
= USART_Clock_Disable
;
393 USART_ClockInitStruct
->USART_CPOL
= USART_CPOL_Low
;
394 USART_ClockInitStruct
->USART_CPHA
= USART_CPHA_1Edge
;
395 USART_ClockInitStruct
->USART_LastBit
= USART_LastBit_Disable
;
399 * @brief Enables or disables the specified USART peripheral.
400 * @param USARTx: Select the USART peripheral. This parameter can be one of the
401 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
402 * @param NewState: new state of the USARTx peripheral.
403 * This parameter can be: ENABLE or DISABLE.
406 void USART_Cmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
408 /* Check the parameters */
409 assert_param(IS_USART_ALL_PERIPH(USARTx
));
410 assert_param(IS_FUNCTIONAL_STATE(NewState
));
412 if (NewState
!= DISABLE
)
414 /* Enable the selected USART by setting the UE bit in the CR1 register */
415 USARTx
->CR1
|= USART_CR1_UE
;
419 /* Disable the selected USART by clearing the UE bit in the CR1 register */
420 USARTx
->CR1
&= (uint32_t)~((uint32_t)USART_CR1_UE
);
425 * @brief Enables or disables the USART's transmitter or receiver.
426 * @param USARTx: Select the USART peripheral. This parameter can be one of the
427 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
428 * @param USART_Direction: specifies the USART direction.
429 * This parameter can be any combination of the following values:
430 * @arg USART_Mode_Tx: USART Transmitter
431 * @arg USART_Mode_Rx: USART Receiver
432 * @param NewState: new state of the USART transfer direction.
433 * This parameter can be: ENABLE or DISABLE.
436 void USART_DirectionModeCmd(USART_TypeDef
* USARTx
, uint32_t USART_DirectionMode
, FunctionalState NewState
)
438 /* Check the parameters */
439 assert_param(IS_USART_ALL_PERIPH(USARTx
));
440 assert_param(IS_USART_MODE(USART_DirectionMode
));
441 assert_param(IS_FUNCTIONAL_STATE(NewState
));
443 if (NewState
!= DISABLE
)
445 /* Enable the USART's transfer interface by setting the TE and/or RE bits
446 in the USART CR1 register */
447 USARTx
->CR1
|= USART_DirectionMode
;
451 /* Disable the USART's transfer interface by clearing the TE and/or RE bits
452 in the USART CR3 register */
453 USARTx
->CR1
&= (uint32_t)~USART_DirectionMode
;
458 * @brief Enables or disables the USART's 8x oversampling mode.
459 * @param USARTx: Select the USART peripheral. This parameter can be one of the
460 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
461 * @param NewState: new state of the USART 8x oversampling mode.
462 * This parameter can be: ENABLE or DISABLE.
464 * This function has to be called before calling USART_Init()
465 * function in order to have correct baudrate Divider value.
468 void USART_OverSampling8Cmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
470 /* Check the parameters */
471 assert_param(IS_USART_ALL_PERIPH(USARTx
));
472 assert_param(IS_FUNCTIONAL_STATE(NewState
));
474 if (NewState
!= DISABLE
)
476 /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
477 USARTx
->CR1
|= USART_CR1_OVER8
;
481 /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
482 USARTx
->CR1
&= (uint32_t)~((uint32_t)USART_CR1_OVER8
);
487 * @brief Enables or disables the USART's one bit sampling method.
488 * @param USARTx: Select the USART peripheral. This parameter can be one of the
489 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
490 * @param NewState: new state of the USART one bit sampling method.
491 * This parameter can be: ENABLE or DISABLE.
493 * This function has to be called before calling USART_Cmd() function.
496 void USART_OneBitMethodCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
498 /* Check the parameters */
499 assert_param(IS_USART_ALL_PERIPH(USARTx
));
500 assert_param(IS_FUNCTIONAL_STATE(NewState
));
502 if (NewState
!= DISABLE
)
504 /* Enable the one bit method by setting the ONEBIT bit in the CR3 register */
505 USARTx
->CR3
|= USART_CR3_ONEBIT
;
509 /* Disable the one bit method by clearing the ONEBIT bit in the CR3 register */
510 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_ONEBIT
);
515 * @brief Enables or disables the USART's most significant bit first
516 * transmitted/received following the start bit.
517 * @param USARTx: Select the USART peripheral. This parameter can be one of the
518 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
519 * @param NewState: new state of the USART most significant bit first
520 * transmitted/received following the start bit.
521 * This parameter can be: ENABLE or DISABLE.
523 * This function has to be called before calling USART_Cmd() function.
526 void USART_MSBFirstCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
528 /* Check the parameters */
529 assert_param(IS_USART_ALL_PERIPH(USARTx
));
530 assert_param(IS_FUNCTIONAL_STATE(NewState
));
532 if (NewState
!= DISABLE
)
534 /* Enable the most significant bit first transmitted/received following the
535 start bit by setting the MSBFIRST bit in the CR2 register */
536 USARTx
->CR2
|= USART_CR2_MSBFIRST
;
540 /* Disable the most significant bit first transmitted/received following the
541 start bit by clearing the MSBFIRST bit in the CR2 register */
542 USARTx
->CR2
&= (uint32_t)~((uint32_t)USART_CR2_MSBFIRST
);
547 * @brief Enables or disables the binary data inversion.
548 * @param USARTx: Select the USART peripheral. This parameter can be one of the
549 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
550 * @param NewState: new defined levels for the USART data.
551 * This parameter can be: ENABLE or DISABLE.
552 * @arg ENABLE: Logical data from the data register are send/received in negative
553 * logic. (1=L, 0=H). The parity bit is also inverted.
554 * @arg DISABLE: Logical data from the data register are send/received in positive
557 * This function has to be called before calling USART_Cmd() function.
560 void USART_DataInvCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
562 /* Check the parameters */
563 assert_param(IS_USART_ALL_PERIPH(USARTx
));
564 assert_param(IS_FUNCTIONAL_STATE(NewState
));
566 if (NewState
!= DISABLE
)
568 /* Enable the binary data inversion feature by setting the DATAINV bit in
570 USARTx
->CR2
|= USART_CR2_DATAINV
;
574 /* Disable the binary data inversion feature by clearing the DATAINV bit in
576 USARTx
->CR2
&= (uint32_t)~((uint32_t)USART_CR2_DATAINV
);
581 * @brief Enables or disables the Pin(s) active level inversion.
582 * @param USARTx: Select the USART peripheral. This parameter can be one of the
583 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
584 * @param USART_InvPin: specifies the USART pin(s) to invert.
585 * This parameter can be any combination of the following values:
586 * @arg USART_InvPin_Tx: USART Tx pin active level inversion.
587 * @arg USART_InvPin_Rx: USART Rx pin active level inversion.
588 * @param NewState: new active level status for the USART pin(s).
589 * This parameter can be: ENABLE or DISABLE.
590 * - ENABLE: pin(s) signal values are inverted (Vdd =0, Gnd =1).
591 * - DISABLE: pin(s) signal works using the standard logic levels (Vdd =1, Gnd =0).
593 * This function has to be called before calling USART_Cmd() function.
596 void USART_InvPinCmd(USART_TypeDef
* USARTx
, uint32_t USART_InvPin
, FunctionalState NewState
)
598 /* Check the parameters */
599 assert_param(IS_USART_ALL_PERIPH(USARTx
));
600 assert_param(IS_USART_INVERSTION_PIN(USART_InvPin
));
601 assert_param(IS_FUNCTIONAL_STATE(NewState
));
603 if (NewState
!= DISABLE
)
605 /* Enable the active level inversion for selected pins by setting the TXINV
606 and/or RXINV bits in the USART CR2 register */
607 USARTx
->CR2
|= USART_InvPin
;
611 /* Disable the active level inversion for selected requests by clearing the
612 TXINV and/or RXINV bits in the USART CR2 register */
613 USARTx
->CR2
&= (uint32_t)~USART_InvPin
;
618 * @brief Enables or disables the swap Tx/Rx pins.
619 * @param USARTx: Select the USART peripheral. This parameter can be one of the
620 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
621 * @param NewState: new state of the USARTx TX/RX pins pinout.
622 * This parameter can be: ENABLE or DISABLE.
623 * @arg ENABLE: The TX and RX pins functions are swapped.
624 * @arg DISABLE: TX/RX pins are used as defined in standard pinout
626 * This function has to be called before calling USART_Cmd() function.
629 void USART_SWAPPinCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
631 /* Check the parameters */
632 assert_param(IS_USART_ALL_PERIPH(USARTx
));
633 assert_param(IS_FUNCTIONAL_STATE(NewState
));
635 if (NewState
!= DISABLE
)
637 /* Enable the SWAP feature by setting the SWAP bit in the CR2 register */
638 USARTx
->CR2
|= USART_CR2_SWAP
;
642 /* Disable the SWAP feature by clearing the SWAP bit in the CR2 register */
643 USARTx
->CR2
&= (uint32_t)~((uint32_t)USART_CR2_SWAP
);
648 * @brief Enables or disables the receiver Time Out feature.
649 * @param USARTx: Select the USART peripheral. This parameter can be one of the
650 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
651 * @param NewState: new state of the USARTx receiver Time Out.
652 * This parameter can be: ENABLE or DISABLE.
655 void USART_ReceiverTimeOutCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
657 /* Check the parameters */
658 assert_param(IS_USART_ALL_PERIPH(USARTx
));
659 assert_param(IS_FUNCTIONAL_STATE(NewState
));
661 if (NewState
!= DISABLE
)
663 /* Enable the receiver time out feature by setting the RTOEN bit in the CR2
665 USARTx
->CR2
|= USART_CR2_RTOEN
;
669 /* Disable the receiver time out feature by clearing the RTOEN bit in the CR2
671 USARTx
->CR2
&= (uint32_t)~((uint32_t)USART_CR2_RTOEN
);
676 * @brief Sets the receiver Time Out value.
677 * @param USARTx: Select the USART peripheral. This parameter can be one of the
678 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
679 * @param USART_ReceiverTimeOut: specifies the Receiver Time Out value.
682 void USART_SetReceiverTimeOut(USART_TypeDef
* USARTx
, uint32_t USART_ReceiverTimeOut
)
684 /* Check the parameters */
685 assert_param(IS_USART_ALL_PERIPH(USARTx
));
686 assert_param(IS_USART_TIMEOUT(USART_ReceiverTimeOut
));
688 /* Clear the receiver Time Out value by clearing the RTO[23:0] bits in the RTOR
690 USARTx
->RTOR
&= (uint32_t)~((uint32_t)USART_RTOR_RTO
);
691 /* Set the receiver Time Out value by setting the RTO[23:0] bits in the RTOR
693 USARTx
->RTOR
|= USART_ReceiverTimeOut
;
697 * @brief Sets the system clock prescaler.
698 * @param USARTx: Select the USART peripheral. This parameter can be one of the
699 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
700 * @param USART_Prescaler: specifies the prescaler clock.
702 * This function has to be called before calling USART_Cmd() function.
705 void USART_SetPrescaler(USART_TypeDef
* USARTx
, uint8_t USART_Prescaler
)
707 /* Check the parameters */
708 assert_param(IS_USART_ALL_PERIPH(USARTx
));
710 /* Clear the USART prescaler */
711 USARTx
->GTPR
&= USART_GTPR_GT
;
712 /* Set the USART prescaler */
713 USARTx
->GTPR
|= USART_Prescaler
;
721 /** @defgroup USART_Group2 STOP Mode functions
722 * @brief STOP Mode functions
725 ===============================================================================
726 ##### STOP Mode functions #####
727 ===============================================================================
728 [..] This subsection provides a set of functions allowing to manage
729 WakeUp from STOP mode.
731 [..] The USART is able to WakeUp from Stop Mode if USART clock is set to HSI
734 [..] The WakeUp source is configured by calling USART_StopModeWakeUpSourceConfig()
737 [..] After configuring the source of WakeUp and before entering in Stop Mode
738 USART_STOPModeCmd() function should be called to allow USART WakeUp.
745 * @brief Enables or disables the specified USART peripheral in STOP Mode.
746 * @param USARTx: Select the USART peripheral. This parameter can be one of the
747 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
748 * @param NewState: new state of the USARTx peripheral state in stop mode.
749 * This parameter can be: ENABLE or DISABLE.
751 * This function has to be called when USART clock is set to HSI or LSE.
754 void USART_STOPModeCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
756 /* Check the parameters */
757 assert_param(IS_USART_ALL_PERIPH(USARTx
));
758 assert_param(IS_FUNCTIONAL_STATE(NewState
));
760 if (NewState
!= DISABLE
)
762 /* Enable the selected USART in STOP mode by setting the UESM bit in the CR1
764 USARTx
->CR1
|= USART_CR1_UESM
;
768 /* Disable the selected USART in STOP mode by clearing the UE bit in the CR1
770 USARTx
->CR1
&= (uint32_t)~((uint32_t)USART_CR1_UESM
);
775 * @brief Selects the USART WakeUp method form stop mode.
776 * @param USARTx: Select the USART peripheral. This parameter can be one of the
777 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
778 * @param USART_WakeUp: specifies the selected USART wakeup method.
779 * This parameter can be one of the following values:
780 * @arg USART_WakeUpSource_AddressMatch: WUF active on address match.
781 * @arg USART_WakeUpSource_StartBit: WUF active on Start bit detection.
782 * @arg USART_WakeUpSource_RXNE: WUF active on RXNE.
784 * This function has to be called before calling USART_Cmd() function.
787 void USART_StopModeWakeUpSourceConfig(USART_TypeDef
* USARTx
, uint32_t USART_WakeUpSource
)
789 /* Check the parameters */
790 assert_param(IS_USART_ALL_PERIPH(USARTx
));
791 assert_param(IS_USART_STOPMODE_WAKEUPSOURCE(USART_WakeUpSource
));
793 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_WUS
);
794 USARTx
->CR3
|= USART_WakeUpSource
;
802 /** @defgroup USART_Group3 AutoBaudRate functions
803 * @brief AutoBaudRate functions
806 ===============================================================================
807 ##### AutoBaudRate functions #####
808 ===============================================================================
809 [..] This subsection provides a set of functions allowing to manage
810 the AutoBaudRate detections.
812 [..] Before Enabling AutoBaudRate detection using USART_AutoBaudRateCmd ()
813 The character patterns used to calculate baudrate must be chosen by calling
814 USART_AutoBaudRateConfig() function. These function take as parameter :
815 (#)USART_AutoBaudRate_StartBit : any character starting with a bit 1.
816 (#)USART_AutoBaudRate_FallingEdge : any character starting with a 10xx bit pattern.
818 [..] At any later time, another request for AutoBaudRate detection can be performed
819 using USART_RequestCmd() function.
821 [..] The AutoBaudRate detection is monitored by the status of ABRF flag which indicate
822 that the AutoBaudRate detection is completed. In addition to ABRF flag, the ABRE flag
823 indicate that this procedure is completed without success. USART_GetFlagStatus ()
824 function should be used to monitor the status of these flags.
831 * @brief Enables or disables the Auto Baud Rate.
832 * @param USARTx: Select the USART peripheral. This parameter can be one of the
833 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
834 * @param NewState: new state of the USARTx auto baud rate.
835 * This parameter can be: ENABLE or DISABLE.
838 void USART_AutoBaudRateCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
840 /* Check the parameters */
841 assert_param(IS_USART_ALL_PERIPH(USARTx
));
842 assert_param(IS_FUNCTIONAL_STATE(NewState
));
844 if (NewState
!= DISABLE
)
846 /* Enable the auto baud rate feature by setting the ABREN bit in the CR2
848 USARTx
->CR2
|= USART_CR2_ABREN
;
852 /* Disable the auto baud rate feature by clearing the ABREN bit in the CR2
854 USARTx
->CR2
&= (uint32_t)~((uint32_t)USART_CR2_ABREN
);
859 * @brief Selects the USART auto baud rate method.
860 * @param USARTx: Select the USART peripheral. This parameter can be one of the
861 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
862 * @param USART_AutoBaudRate: specifies the selected USART auto baud rate method.
863 * This parameter can be one of the following values:
864 * @arg USART_AutoBaudRate_StartBit: Start Bit duration measurement.
865 * @arg USART_AutoBaudRate_FallingEdge: Falling edge to falling edge measurement.
866 * @arg USART_AutoBaudRate_0x7FFrame: 0x7F frame.
867 * @arg USART_AutoBaudRate_0x55Frame: 0x55 frame.
869 * This function has to be called before calling USART_Cmd() function.
872 void USART_AutoBaudRateConfig(USART_TypeDef
* USARTx
, uint32_t USART_AutoBaudRate
)
874 /* Check the parameters */
875 assert_param(IS_USART_ALL_PERIPH(USARTx
));
876 assert_param(IS_USART_AUTOBAUDRATE_MODE(USART_AutoBaudRate
));
878 USARTx
->CR2
&= (uint32_t)~((uint32_t)USART_CR2_ABRMODE
);
879 USARTx
->CR2
|= USART_AutoBaudRate
;
887 /** @defgroup USART_Group4 Data transfers functions
888 * @brief Data transfers functions
891 ===============================================================================
892 ##### Data transfers functions #####
893 ===============================================================================
894 [..] This subsection provides a set of functions allowing to manage
895 the USART data transfers.
896 [..] During an USART reception, data shifts in least significant bit first
897 through the RX pin. When a transmission is taking place, a write instruction to
898 the USART_TDR register stores the data in the shift register.
899 [..] The read access of the USART_RDR register can be done using
900 the USART_ReceiveData() function and returns the RDR value.
901 Whereas a write access to the USART_TDR can be done using USART_SendData()
902 function and stores the written data into TDR.
909 * @brief Transmits single data through the USARTx peripheral.
910 * @param USARTx: Select the USART peripheral. This parameter can be one of the
911 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
912 * @param Data: the data to transmit.
915 void USART_SendData(USART_TypeDef
* USARTx
, uint16_t Data
)
917 /* Check the parameters */
918 assert_param(IS_USART_ALL_PERIPH(USARTx
));
919 assert_param(IS_USART_DATA(Data
));
922 USARTx
->TDR
= (Data
& (uint16_t)0x01FF);
926 * @brief Returns the most recent received data by the USARTx peripheral.
927 * @param USARTx: Select the USART peripheral. This parameter can be one of the
928 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
929 * @retval The received data.
931 uint16_t USART_ReceiveData(USART_TypeDef
* USARTx
)
933 /* Check the parameters */
934 assert_param(IS_USART_ALL_PERIPH(USARTx
));
937 return (uint16_t)(USARTx
->RDR
& (uint16_t)0x01FF);
944 /** @defgroup USART_Group5 MultiProcessor Communication functions
945 * @brief Multi-Processor Communication functions
948 ===============================================================================
949 ##### Multi-Processor Communication functions #####
950 ===============================================================================
951 [..] This subsection provides a set of functions allowing to manage the USART
952 multiprocessor communication.
953 [..] For instance one of the USARTs can be the master, its TX output is
954 connected to the RX input of the other USART. The others are slaves,
955 their respective TX outputs are logically ANDed together and connected
956 to the RX input of the master. USART multiprocessor communication is
957 possible through the following procedure:
958 (#) Program the Baud rate, Word length = 9 bits, Stop bits, Parity,
959 Mode transmitter or Mode receiver and hardware flow control values
960 using the USART_Init() function.
961 (#) Configures the USART address using the USART_SetAddress() function.
962 (#) Configures the wake up methode (USART_WakeUp_IdleLine or
963 USART_WakeUp_AddressMark) using USART_WakeUpConfig() function only
965 (#) Enable the USART using the USART_Cmd() function.
966 (#) Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd()
968 [..] The USART Slave exit from mute mode when receive the wake up condition.
975 * @brief Sets the address of the USART node.
976 * @param USARTx: Select the USART peripheral. This parameter can be one of the
977 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
978 * @param USART_Address: Indicates the address of the USART node.
981 void USART_SetAddress(USART_TypeDef
* USARTx
, uint8_t USART_Address
)
983 /* Check the parameters */
984 assert_param(IS_USART_ALL_PERIPH(USARTx
));
986 /* Clear the USART address */
987 USARTx
->CR2
&= (uint32_t)~((uint32_t)USART_CR2_ADD
);
988 /* Set the USART address node */
989 USARTx
->CR2
|=((uint32_t)USART_Address
<< (uint32_t)0x18);
993 * @brief Enables or disables the USART's mute mode.
994 * @param USARTx: Select the USART peripheral. This parameter can be one of the
995 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
996 * @param NewState: new state of the USART mute mode.
997 * This parameter can be: ENABLE or DISABLE.
1000 void USART_MuteModeCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
1002 /* Check the parameters */
1003 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1004 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1006 if (NewState
!= DISABLE
)
1008 /* Enable the USART mute mode by setting the MME bit in the CR1 register */
1009 USARTx
->CR1
|= USART_CR1_MME
;
1013 /* Disable the USART mute mode by clearing the MME bit in the CR1 register */
1014 USARTx
->CR1
&= (uint32_t)~((uint32_t)USART_CR1_MME
);
1019 * @brief Selects the USART WakeUp method from mute mode.
1020 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1021 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1022 * @param USART_WakeUp: specifies the USART wakeup method.
1023 * This parameter can be one of the following values:
1024 * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
1025 * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
1028 void USART_MuteModeWakeUpConfig(USART_TypeDef
* USARTx
, uint32_t USART_WakeUp
)
1030 /* Check the parameters */
1031 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1032 assert_param(IS_USART_MUTEMODE_WAKEUP(USART_WakeUp
));
1034 USARTx
->CR1
&= (uint32_t)~((uint32_t)USART_CR1_WAKE
);
1035 USARTx
->CR1
|= USART_WakeUp
;
1039 * @brief Configure the the USART Address detection length.
1040 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1041 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1042 * @param USART_AddressLength: specifies the USART address length detection.
1043 * This parameter can be one of the following values:
1044 * @arg USART_AddressLength_4b: 4-bit address length detection
1045 * @arg USART_AddressLength_7b: 7-bit address length detection
1048 void USART_AddressDetectionConfig(USART_TypeDef
* USARTx
, uint32_t USART_AddressLength
)
1050 /* Check the parameters */
1051 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1052 assert_param(IS_USART_ADDRESS_DETECTION(USART_AddressLength
));
1054 USARTx
->CR2
&= (uint32_t)~((uint32_t)USART_CR2_ADDM7
);
1055 USARTx
->CR2
|= USART_AddressLength
;
1062 /** @defgroup USART_Group6 LIN mode functions
1063 * @brief LIN mode functions
1066 ===============================================================================
1067 ##### LIN mode functions #####
1068 ===============================================================================
1069 [..] This subsection provides a set of functions allowing to manage the USART
1070 LIN Mode communication.
1071 [..] In LIN mode, 8-bit data format with 1 stop bit is required in accordance
1072 with the LIN standard.
1073 [..] Only this LIN Feature is supported by the USART IP:
1074 (+) LIN Master Synchronous Break send capability and LIN slave break
1075 detection capability : 13-bit break generation and 10/11 bit break
1077 [..] USART LIN Master transmitter communication is possible through the
1078 following procedure:
1079 (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
1080 Mode transmitter or Mode receiver and hardware flow control values
1081 using the USART_Init() function.
1082 (#) Enable the LIN mode using the USART_LINCmd() function.
1083 (#) Enable the USART using the USART_Cmd() function.
1084 (#) Send the break character using USART_SendBreak() function.
1085 [..] USART LIN Master receiver communication is possible through the
1086 following procedure:
1087 (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
1088 Mode transmitter or Mode receiver and hardware flow control values
1089 using the USART_Init() function.
1090 (#) Configures the break detection length
1091 using the USART_LINBreakDetectLengthConfig() function.
1092 (#) Enable the LIN mode using the USART_LINCmd() function.
1093 (#) Enable the USART using the USART_Cmd() function.
1095 (@) In LIN mode, the following bits must be kept cleared:
1096 (+@) CLKEN in the USART_CR2 register.
1097 (+@) STOP[1:0], SCEN, HDSEL and IREN in the USART_CR3 register.
1104 * @brief Sets the USART LIN Break detection length.
1105 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1106 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1107 * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
1108 * This parameter can be one of the following values:
1109 * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
1110 * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
1113 void USART_LINBreakDetectLengthConfig(USART_TypeDef
* USARTx
, uint32_t USART_LINBreakDetectLength
)
1115 /* Check the parameters */
1116 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1117 assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength
));
1119 USARTx
->CR2
&= (uint32_t)~((uint32_t)USART_CR2_LBDL
);
1120 USARTx
->CR2
|= USART_LINBreakDetectLength
;
1124 * @brief Enables or disables the USART's LIN mode.
1125 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1126 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1127 * @param NewState: new state of the USART LIN mode.
1128 * This parameter can be: ENABLE or DISABLE.
1131 void USART_LINCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
1133 /* Check the parameters */
1134 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1135 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1137 if (NewState
!= DISABLE
)
1139 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
1140 USARTx
->CR2
|= USART_CR2_LINEN
;
1144 /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
1145 USARTx
->CR2
&= (uint32_t)~((uint32_t)USART_CR2_LINEN
);
1153 /** @defgroup USART_Group7 Halfduplex mode function
1154 * @brief Half-duplex mode function
1157 ===============================================================================
1158 ##### Half-duplex mode function #####
1159 ===============================================================================
1160 [..] This subsection provides a set of functions allowing to manage the USART
1161 Half-duplex communication.
1162 [..] The USART can be configured to follow a single-wire half-duplex protocol
1163 where the TX and RX lines are internally connected.
1164 [..] USART Half duplex communication is possible through the following procedure:
1165 (#) Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter
1166 or Mode receiver and hardware flow control values using the USART_Init()
1168 (#) Configures the USART address using the USART_SetAddress() function.
1169 (#) Enable the half duplex mode using USART_HalfDuplexCmd() function.
1170 (#) Enable the USART using the USART_Cmd() function.
1172 (@) The RX pin is no longer used.
1173 (@) In Half-duplex mode the following bits must be kept cleared:
1174 (+@) LINEN and CLKEN bits in the USART_CR2 register.
1175 (+@) SCEN and IREN bits in the USART_CR3 register.
1182 * @brief Enables or disables the USART's Half Duplex communication.
1183 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1184 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1185 * @param NewState: new state of the USART Communication.
1186 * This parameter can be: ENABLE or DISABLE.
1189 void USART_HalfDuplexCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
1191 /* Check the parameters */
1192 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1193 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1195 if (NewState
!= DISABLE
)
1197 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
1198 USARTx
->CR3
|= USART_CR3_HDSEL
;
1202 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
1203 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_HDSEL
);
1212 /** @defgroup USART_Group8 Smartcard mode functions
1213 * @brief Smartcard mode functions
1216 ===============================================================================
1217 ##### Smartcard mode functions #####
1218 ===============================================================================
1219 [..] This subsection provides a set of functions allowing to manage the USART
1220 Smartcard communication.
1221 [..] The Smartcard interface is designed to support asynchronous protocol
1222 Smartcards as defined in the ISO 7816-3 standard. The USART can provide
1223 a clock to the smartcard through the SCLK output. In smartcard mode,
1224 SCLK is not associated to the communication but is simply derived from
1225 the internal peripheral input clock through a 5-bit prescaler.
1226 [..] Smartcard communication is possible through the following procedure:
1227 (#) Configures the Smartcard Prsecaler using the USART_SetPrescaler()
1229 (#) Configures the Smartcard Guard Time using the USART_SetGuardTime()
1231 (#) Program the USART clock using the USART_ClockInit() function as following:
1232 (++) USART Clock enabled.
1233 (++) USART CPOL Low.
1234 (++) USART CPHA on first edge.
1235 (++) USART Last Bit Clock Enabled.
1236 (#) Program the Smartcard interface using the USART_Init() function as
1238 (++) Word Length = 9 Bits.
1241 (++) BaudRate = 12096 baud.
1242 (++) Hardware flow control disabled (RTS and CTS signals).
1243 (++) Tx and Rx enabled
1244 (#) Optionally you can enable the parity error interrupt using
1245 the USART_ITConfig() function.
1246 (#) Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function.
1247 (#) Enable the Smartcard interface using the USART_SmartCardCmd() function.
1248 (#) Enable the USART using the USART_Cmd() function.
1250 Please refer to the ISO 7816-3 specification for more details.
1252 (@) It is also possible to choose 0.5 stop bit for receiving but it is
1253 recommended to use 1.5 stop bits for both transmitting and receiving
1254 to avoid switching between the two configurations.
1255 (@) In smartcard mode, the following bits must be kept cleared:
1256 (+@) LINEN bit in the USART_CR2 register.
1257 (+@) HDSEL and IREN bits in the USART_CR3 register.
1264 * @brief Sets the specified USART guard time.
1265 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1266 * following values: USART1 or USART2 or USART3.
1267 * @param USART_GuardTime: specifies the guard time.
1270 void USART_SetGuardTime(USART_TypeDef
* USARTx
, uint8_t USART_GuardTime
)
1272 /* Check the parameters */
1273 assert_param(IS_USART_123_PERIPH(USARTx
));
1275 /* Clear the USART Guard time */
1276 USARTx
->GTPR
&= USART_GTPR_PSC
;
1277 /* Set the USART guard time */
1278 USARTx
->GTPR
|= (uint16_t)((uint16_t)USART_GuardTime
<< 0x08);
1282 * @brief Enables or disables the USART's Smart Card mode.
1283 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1284 * following values: USART1 or USART2 or USART3.
1285 * @param NewState: new state of the Smart Card mode.
1286 * This parameter can be: ENABLE or DISABLE.
1289 void USART_SmartCardCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
1291 /* Check the parameters */
1292 assert_param(IS_USART_123_PERIPH(USARTx
));
1293 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1294 if (NewState
!= DISABLE
)
1296 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
1297 USARTx
->CR3
|= USART_CR3_SCEN
;
1301 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
1302 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_SCEN
);
1307 * @brief Enables or disables NACK transmission.
1308 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1309 * following values: USART1 or USART2 or USART3.
1310 * @param NewState: new state of the NACK transmission.
1311 * This parameter can be: ENABLE or DISABLE.
1314 void USART_SmartCardNACKCmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
1316 /* Check the parameters */
1317 assert_param(IS_USART_123_PERIPH(USARTx
));
1318 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1319 if (NewState
!= DISABLE
)
1321 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
1322 USARTx
->CR3
|= USART_CR3_NACK
;
1326 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
1327 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_NACK
);
1332 * @brief Sets the Smart Card number of retries in transmit and receive.
1333 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1334 * following values: USART1 or USART2 or USART3.
1335 * @param USART_AutoCount: specifies the Smart Card auto retry count.
1338 void USART_SetAutoRetryCount(USART_TypeDef
* USARTx
, uint8_t USART_AutoCount
)
1340 /* Check the parameters */
1341 assert_param(IS_USART_123_PERIPH(USARTx
));
1342 assert_param(IS_USART_AUTO_RETRY_COUNTER(USART_AutoCount
));
1343 /* Clear the USART auto retry count */
1344 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_SCARCNT
);
1345 /* Set the USART auto retry count*/
1346 USARTx
->CR3
|= (uint32_t)((uint32_t)USART_AutoCount
<< 0x11);
1350 * @brief Sets the Smart Card Block length.
1351 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1352 * following values: USART1 or USART2 or USART3.
1353 * @param USART_BlockLength: specifies the Smart Card block length.
1356 void USART_SetBlockLength(USART_TypeDef
* USARTx
, uint8_t USART_BlockLength
)
1358 /* Check the parameters */
1359 assert_param(IS_USART_123_PERIPH(USARTx
));
1361 /* Clear the Smart card block length */
1362 USARTx
->RTOR
&= (uint32_t)~((uint32_t)USART_RTOR_BLEN
);
1363 /* Set the Smart Card block length */
1364 USARTx
->RTOR
|= (uint32_t)((uint32_t)USART_BlockLength
<< 0x18);
1371 /** @defgroup USART_Group9 IrDA mode functions
1372 * @brief IrDA mode functions
1375 ===============================================================================
1376 ##### IrDA mode functions #####
1377 ===============================================================================
1378 [..] This subsection provides a set of functions allowing to manage the USART
1380 [..] IrDA is a half duplex communication protocol. If the Transmitter is busy,
1381 any data on the IrDA receive line will be ignored by the IrDA decoder
1382 and if the Receiver is busy, data on the TX from the USART to IrDA will
1383 not be encoded by IrDA. While receiving data, transmission should be
1384 avoided as the data to be transmitted could be corrupted.
1385 [..] IrDA communication is possible through the following procedure:
1386 (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity,
1387 Transmitter/Receiver modes and hardware flow control values using
1388 the USART_Init() function.
1389 (#) Configures the IrDA pulse width by configuring the prescaler using
1390 the USART_SetPrescaler() function.
1391 (#) Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal
1392 mode using the USART_IrDAConfig() function.
1393 (#) Enable the IrDA using the USART_IrDACmd() function.
1394 (#) Enable the USART using the USART_Cmd() function.
1396 (@) A pulse of width less than two and greater than one PSC period(s) may or
1397 may not be rejected.
1398 (@) The receiver set up time should be managed by software. The IrDA physical
1399 layer specification specifies a minimum of 10 ms delay between
1400 transmission and reception (IrDA is a half duplex protocol).
1401 (@) In IrDA mode, the following bits must be kept cleared:
1402 (+@) LINEN, STOP and CLKEN bits in the USART_CR2 register.
1403 (+@) SCEN and HDSEL bits in the USART_CR3 register.
1410 * @brief Configures the USART's IrDA interface.
1411 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1412 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1413 * @param USART_IrDAMode: specifies the IrDA mode.
1414 * This parameter can be one of the following values:
1415 * @arg USART_IrDAMode_LowPower
1416 * @arg USART_IrDAMode_Normal
1419 void USART_IrDAConfig(USART_TypeDef
* USARTx
, uint32_t USART_IrDAMode
)
1421 /* Check the parameters */
1422 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1423 assert_param(IS_USART_IRDA_MODE(USART_IrDAMode
));
1425 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_IRLP
);
1426 USARTx
->CR3
|= USART_IrDAMode
;
1430 * @brief Enables or disables the USART's IrDA interface.
1431 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1432 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1433 * @param NewState: new state of the IrDA mode.
1434 * This parameter can be: ENABLE or DISABLE.
1437 void USART_IrDACmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
1439 /* Check the parameters */
1440 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1441 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1443 if (NewState
!= DISABLE
)
1445 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
1446 USARTx
->CR3
|= USART_CR3_IREN
;
1450 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
1451 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_IREN
);
1458 /** @defgroup USART_Group10 RS485 mode function
1459 * @brief RS485 mode function
1462 ===============================================================================
1463 ##### RS485 mode functions #####
1464 ===============================================================================
1465 [..] This subsection provides a set of functions allowing to manage the USART
1467 [..] RS485 flow control (Driver enable feature) handling is possible through
1468 the following procedure:
1469 (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity,
1470 Transmitter/Receiver modes and hardware flow control values using
1471 the USART_Init() function.
1472 (#) Enable the Driver Enable using the USART_DECmd() function.
1473 (#) Configures the Driver Enable polarity using the USART_DEPolarityConfig()
1475 (#) Configures the Driver Enable assertion time using USART_SetDEAssertionTime()
1476 function and deassertion time using the USART_SetDEDeassertionTime()
1478 (#) Enable the USART using the USART_Cmd() function.
1480 (@) The assertion and dessertion times are expressed in sample time units (1/8 or
1481 1/16 bit time, depending on the oversampling rate).
1488 * @brief Enables or disables the USART's DE functionality.
1489 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1490 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1491 * @param NewState: new state of the driver enable mode.
1492 * This parameter can be: ENABLE or DISABLE.
1495 void USART_DECmd(USART_TypeDef
* USARTx
, FunctionalState NewState
)
1497 /* Check the parameters */
1498 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1499 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1500 if (NewState
!= DISABLE
)
1502 /* Enable the DE functionality by setting the DEM bit in the CR3 register */
1503 USARTx
->CR3
|= USART_CR3_DEM
;
1507 /* Disable the DE functionality by clearing the DEM bit in the CR3 register */
1508 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_DEM
);
1513 * @brief Configures the USART's DE polarity
1514 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1515 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1516 * @param USART_DEPolarity: specifies the DE polarity.
1517 * This parameter can be one of the following values:
1518 * @arg USART_DEPolarity_Low
1519 * @arg USART_DEPolarity_High
1522 void USART_DEPolarityConfig(USART_TypeDef
* USARTx
, uint32_t USART_DEPolarity
)
1524 /* Check the parameters */
1525 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1526 assert_param(IS_USART_DE_POLARITY(USART_DEPolarity
));
1528 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_DEP
);
1529 USARTx
->CR3
|= USART_DEPolarity
;
1533 * @brief Sets the specified RS485 DE assertion time
1534 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1535 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1536 * @param USART_AssertionTime: specifies the time between the activation of the DE
1537 * signal and the beginning of the start bit
1540 void USART_SetDEAssertionTime(USART_TypeDef
* USARTx
, uint32_t USART_DEAssertionTime
)
1542 /* Check the parameters */
1543 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1544 assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEAssertionTime
));
1546 /* Clear the DE assertion time */
1547 USARTx
->CR1
&= (uint32_t)~((uint32_t)USART_CR1_DEAT
);
1548 /* Set the new value for the DE assertion time */
1549 USARTx
->CR1
|=((uint32_t)USART_DEAssertionTime
<< (uint32_t)0x15);
1553 * @brief Sets the specified RS485 DE deassertion time
1554 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1555 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1556 * @param USART_DeassertionTime: specifies the time between the middle of the last
1557 * stop bit in a transmitted message and the de-activation of the DE signal
1560 void USART_SetDEDeassertionTime(USART_TypeDef
* USARTx
, uint32_t USART_DEDeassertionTime
)
1562 /* Check the parameters */
1563 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1564 assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEDeassertionTime
));
1566 /* Clear the DE deassertion time */
1567 USARTx
->CR1
&= (uint32_t)~((uint32_t)USART_CR1_DEDT
);
1568 /* Set the new value for the DE deassertion time */
1569 USARTx
->CR1
|=((uint32_t)USART_DEDeassertionTime
<< (uint32_t)0x10);
1576 /** @defgroup USART_Group11 DMA transfers management functions
1577 * @brief DMA transfers management functions
1580 ===============================================================================
1581 ##### DMA transfers management functions #####
1582 ===============================================================================
1583 [..] This section provides two functions that can be used only in DMA mode.
1584 [..] In DMA Mode, the USART communication can be managed by 2 DMA Channel
1586 (#) USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request.
1587 (#) USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request.
1588 [..] In this Mode it is advised to use the following function:
1589 (+) void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq,
1590 FunctionalState NewState).
1596 * @brief Enables or disables the USART's DMA interface.
1597 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1598 * following values: USART1 or USART2 or USART3 or UART4.
1599 * @param USART_DMAReq: specifies the DMA request.
1600 * This parameter can be any combination of the following values:
1601 * @arg USART_DMAReq_Tx: USART DMA transmit request
1602 * @arg USART_DMAReq_Rx: USART DMA receive request
1603 * @param NewState: new state of the DMA Request sources.
1604 * This parameter can be: ENABLE or DISABLE.
1607 void USART_DMACmd(USART_TypeDef
* USARTx
, uint32_t USART_DMAReq
, FunctionalState NewState
)
1609 /* Check the parameters */
1610 assert_param(IS_USART_1234_PERIPH(USARTx
));
1611 assert_param(IS_USART_DMAREQ(USART_DMAReq
));
1612 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1614 if (NewState
!= DISABLE
)
1616 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
1617 DMAR bits in the USART CR3 register */
1618 USARTx
->CR3
|= USART_DMAReq
;
1622 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
1623 DMAR bits in the USART CR3 register */
1624 USARTx
->CR3
&= (uint32_t)~USART_DMAReq
;
1629 * @brief Enables or disables the USART's DMA interface when reception error occurs.
1630 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1631 * following values: USART1 or USART2 or USART3 or UART4.
1632 * @param USART_DMAOnError: specifies the DMA status in case of reception error.
1633 * This parameter can be any combination of the following values:
1634 * @arg USART_DMAOnError_Enable: DMA receive request enabled when the USART DMA
1635 * reception error is asserted.
1636 * @arg USART_DMAOnError_Disable: DMA receive request disabled when the USART DMA
1637 * reception error is asserted.
1640 void USART_DMAReceptionErrorConfig(USART_TypeDef
* USARTx
, uint32_t USART_DMAOnError
)
1642 /* Check the parameters */
1643 assert_param(IS_USART_1234_PERIPH(USARTx
));
1644 assert_param(IS_USART_DMAONERROR(USART_DMAOnError
));
1646 /* Clear the DMA Reception error detection bit */
1647 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_DDRE
);
1648 /* Set the new value for the DMA Reception error detection bit */
1649 USARTx
->CR3
|= USART_DMAOnError
;
1656 /** @defgroup USART_Group12 Interrupts and flags management functions
1657 * @brief Interrupts and flags management functions
1660 ===============================================================================
1661 ##### Interrupts and flags management functions #####
1662 ===============================================================================
1663 [..] This subsection provides a set of functions allowing to configure the
1664 USART Interrupts sources, Requests and check or clear the flags or pending bits status.
1665 The user should identify which mode will be used in his application to
1666 manage the communication: Polling mode, Interrupt mode.
1668 *** Polling Mode ***
1669 ====================
1670 [..] In Polling Mode, the SPI communication can be managed by these flags:
1671 (#) USART_FLAG_REACK: to indicate the status of the Receive Enable
1673 (#) USART_FLAG_TEACK: to indicate the status of the Transmit Enable
1675 (#) USART_FLAG_WUF: to indicate the status of the Wake up flag.
1676 (#) USART_FLAG_RWU: to indicate the status of the Receive Wake up flag.
1677 (#) USART_FLAG_SBK: to indicate the status of the Send Break flag.
1678 (#) USART_FLAG_CMF: to indicate the status of the Character match flag.
1679 (#) USART_FLAG_BUSY: to indicate the status of the Busy flag.
1680 (#) USART_FLAG_ABRF: to indicate the status of the Auto baud rate flag.
1681 (#) USART_FLAG_ABRE: to indicate the status of the Auto baud rate error flag.
1682 (#) USART_FLAG_EOBF: to indicate the status of the End of block flag.
1683 (#) USART_FLAG_RTOF: to indicate the status of the Receive time out flag.
1684 (#) USART_FLAG_nCTSS: to indicate the status of the Inverted nCTS input
1686 (#) USART_FLAG_TXE: to indicate the status of the transmit buffer register.
1687 (#) USART_FLAG_RXNE: to indicate the status of the receive buffer register.
1688 (#) USART_FLAG_TC: to indicate the status of the transmit operation.
1689 (#) USART_FLAG_IDLE: to indicate the status of the Idle Line.
1690 (#) USART_FLAG_CTS: to indicate the status of the nCTS input.
1691 (#) USART_FLAG_LBD: to indicate the status of the LIN break detection.
1692 (#) USART_FLAG_NE: to indicate if a noise error occur.
1693 (#) USART_FLAG_FE: to indicate if a frame error occur.
1694 (#) USART_FLAG_PE: to indicate if a parity error occur.
1695 (#) USART_FLAG_ORE: to indicate if an Overrun error occur.
1696 [..] In this Mode it is advised to use the following functions:
1697 (+) FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG).
1698 (+) void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG).
1700 *** Interrupt Mode ***
1701 ======================
1702 [..] In Interrupt Mode, the USART communication can be managed by 8 interrupt
1703 sources and 10 pending bits:
1705 (##) USART_IT_WU: to indicate the status of the Wake up interrupt.
1706 (##) USART_IT_CM: to indicate the status of Character match interrupt.
1707 (##) USART_IT_EOB: to indicate the status of End of block interrupt.
1708 (##) USART_IT_RTO: to indicate the status of Receive time out interrupt.
1709 (##) USART_IT_CTS: to indicate the status of CTS change interrupt.
1710 (##) USART_IT_LBD: to indicate the status of LIN Break detection interrupt.
1711 (##) USART_IT_TC: to indicate the status of Transmission complete interrupt.
1712 (##) USART_IT_IDLE: to indicate the status of IDLE line detected interrupt.
1713 (##) USART_IT_ORE: to indicate the status of OverRun Error interrupt.
1714 (##) USART_IT_NE: to indicate the status of Noise Error interrupt.
1715 (##) USART_IT_FE: to indicate the status of Framing Error interrupt.
1716 (##) USART_IT_PE: to indicate the status of Parity Error interrupt.
1718 (+) Interrupt Source:
1719 (##) USART_IT_WU: specifies the interrupt source for Wake up interrupt.
1720 (##) USART_IT_CM: specifies the interrupt source for Character match
1722 (##) USART_IT_EOB: specifies the interrupt source for End of block
1724 (##) USART_IT_RTO: specifies the interrupt source for Receive time-out
1726 (##) USART_IT_CTS: specifies the interrupt source for CTS change interrupt.
1727 (##) USART_IT_LBD: specifies the interrupt source for LIN Break
1728 detection interrupt.
1729 (##) USART_IT_TXE: specifies the interrupt source for Tansmit Data
1730 Register empty interrupt.
1731 (##) USART_IT_TC: specifies the interrupt source for Transmission
1733 (##) USART_IT_RXNE: specifies the interrupt source for Receive Data
1734 register not empty interrupt.
1735 (##) USART_IT_IDLE: specifies the interrupt source for Idle line
1736 detection interrupt.
1737 (##) USART_IT_PE: specifies the interrupt source for Parity Error interrupt.
1738 (##) USART_IT_ERR: specifies the interrupt source for Error interrupt
1739 (Frame error, noise error, overrun error)
1740 -@@- Some parameters are coded in order to use them as interrupt
1741 source or as pending bits.
1742 [..] In this Mode it is advised to use the following functions:
1743 (+) void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState).
1744 (+) ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT).
1745 (+) void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT).
1752 * @brief Enables or disables the specified USART interrupts.
1753 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1754 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1755 * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
1756 * This parameter can be one of the following values:
1757 * @arg USART_IT_WU: Wake up interrupt.
1758 * @arg USART_IT_CM: Character match interrupt.
1759 * @arg USART_IT_EOB: End of block interrupt.
1760 * @arg USART_IT_RTO: Receive time out interrupt.
1761 * @arg USART_IT_CTS: CTS change interrupt.
1762 * @arg USART_IT_LBD: LIN Break detection interrupt.
1763 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt.
1764 * @arg USART_IT_TC: Transmission complete interrupt.
1765 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
1766 * @arg USART_IT_IDLE: Idle line detection interrupt.
1767 * @arg USART_IT_PE: Parity Error interrupt.
1768 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
1769 * @param NewState: new state of the specified USARTx interrupts.
1770 * This parameter can be: ENABLE or DISABLE.
1773 void USART_ITConfig(USART_TypeDef
* USARTx
, uint32_t USART_IT
, FunctionalState NewState
)
1775 uint32_t usartreg
= 0, itpos
= 0, itmask
= 0;
1776 uint32_t usartxbase
= 0;
1777 /* Check the parameters */
1778 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1779 assert_param(IS_USART_CONFIG_IT(USART_IT
));
1780 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1782 usartxbase
= (uint32_t)USARTx
;
1784 /* Get the USART register index */
1785 usartreg
= (((uint16_t)USART_IT
) >> 0x08);
1787 /* Get the interrupt position */
1788 itpos
= USART_IT
& IT_MASK
;
1789 itmask
= (((uint32_t)0x01) << itpos
);
1791 if (usartreg
== 0x02) /* The IT is in CR2 register */
1795 else if (usartreg
== 0x03) /* The IT is in CR3 register */
1799 else /* The IT is in CR1 register */
1802 if (NewState
!= DISABLE
)
1804 *(__IO
uint32_t*)usartxbase
|= itmask
;
1808 *(__IO
uint32_t*)usartxbase
&= ~itmask
;
1813 * @brief Enables the specified USART's Request.
1814 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1815 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1816 * @param USART_Request: specifies the USART request.
1817 * This parameter can be any combination of the following values:
1818 * @arg USART_Request_TXFRQ: Transmit data flush ReQuest
1819 * @arg USART_Request_RXFRQ: Receive data flush ReQuest
1820 * @arg USART_Request_MMRQ: Mute Mode ReQuest
1821 * @arg USART_Request_SBKRQ: Send Break ReQuest
1822 * @arg USART_Request_ABRRQ: Auto Baud Rate ReQuest
1823 * @param NewState: new state of the DMA interface when reception error occurs.
1824 * This parameter can be: ENABLE or DISABLE.
1827 void USART_RequestCmd(USART_TypeDef
* USARTx
, uint32_t USART_Request
, FunctionalState NewState
)
1829 /* Check the parameters */
1830 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1831 assert_param(IS_USART_REQUEST(USART_Request
));
1832 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1834 if (NewState
!= DISABLE
)
1836 /* Enable the USART ReQuest by setting the dedicated request bit in the RQR
1838 USARTx
->RQR
|= USART_Request
;
1842 /* Disable the USART ReQuest by clearing the dedicated request bit in the RQR
1844 USARTx
->RQR
&= (uint32_t)~USART_Request
;
1849 * @brief Enables or disables the USART's Overrun detection.
1850 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1851 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1852 * @param USART_OVRDetection: specifies the OVR detection status in case of OVR error.
1853 * This parameter can be any combination of the following values:
1854 * @arg USART_OVRDetection_Enable: OVR error detection enabled when the USART OVR error
1856 * @arg USART_OVRDetection_Disable: OVR error detection disabled when the USART OVR error
1860 void USART_OverrunDetectionConfig(USART_TypeDef
* USARTx
, uint32_t USART_OVRDetection
)
1862 /* Check the parameters */
1863 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1864 assert_param(IS_USART_OVRDETECTION(USART_OVRDetection
));
1866 /* Clear the OVR detection bit */
1867 USARTx
->CR3
&= (uint32_t)~((uint32_t)USART_CR3_OVRDIS
);
1868 /* Set the new value for the OVR detection bit */
1869 USARTx
->CR3
|= USART_OVRDetection
;
1873 * @brief Checks whether the specified USART flag is set or not.
1874 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1875 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1876 * @param USART_FLAG: specifies the flag to check.
1877 * This parameter can be one of the following values:
1878 * @arg USART_FLAG_REACK: Receive Enable acknowledge flag.
1879 * @arg USART_FLAG_TEACK: Transmit Enable acknowledge flag.
1880 * @arg USART_FLAG_WUF: Wake up flag.
1881 * @arg USART_FLAG_RWU: Receive Wake up flag.
1882 * @arg USART_FLAG_SBK: Send Break flag.
1883 * @arg USART_FLAG_CMF: Character match flag.
1884 * @arg USART_FLAG_BUSY: Busy flag.
1885 * @arg USART_FLAG_ABRF: Auto baud rate flag.
1886 * @arg USART_FLAG_ABRE: Auto baud rate error flag.
1887 * @arg USART_FLAG_EOBF: End of block flag.
1888 * @arg USART_FLAG_RTOF: Receive time out flag.
1889 * @arg USART_FLAG_nCTSS: Inverted nCTS input bit status.
1890 * @arg USART_FLAG_CTS: CTS Change flag.
1891 * @arg USART_FLAG_LBD: LIN Break detection flag.
1892 * @arg USART_FLAG_TXE: Transmit data register empty flag.
1893 * @arg USART_FLAG_TC: Transmission Complete flag.
1894 * @arg USART_FLAG_RXNE: Receive data register not empty flag.
1895 * @arg USART_FLAG_IDLE: Idle Line detection flag.
1896 * @arg USART_FLAG_ORE: OverRun Error flag.
1897 * @arg USART_FLAG_NE: Noise Error flag.
1898 * @arg USART_FLAG_FE: Framing Error flag.
1899 * @arg USART_FLAG_PE: Parity Error flag.
1900 * @retval The new state of USART_FLAG (SET or RESET).
1902 FlagStatus
USART_GetFlagStatus(USART_TypeDef
* USARTx
, uint32_t USART_FLAG
)
1904 FlagStatus bitstatus
= RESET
;
1905 /* Check the parameters */
1906 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1907 assert_param(IS_USART_FLAG(USART_FLAG
));
1909 if ((USARTx
->ISR
& USART_FLAG
) != (uint16_t)RESET
)
1921 * @brief Clears the USARTx's pending flags.
1922 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1923 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1924 * @param USART_FLAG: specifies the flag to clear.
1925 * This parameter can be any combination of the following values:
1926 * @arg USART_FLAG_WUF: Wake up flag.
1927 * @arg USART_FLAG_CMF: Character match flag.
1928 * @arg USART_FLAG_EOBF: End of block flag.
1929 * @arg USART_FLAG_RTOF: Receive time out flag.
1930 * @arg USART_FLAG_CTS: CTS Change flag.
1931 * @arg USART_FLAG_LBD: LIN Break detection flag.
1932 * @arg USART_FLAG_TC: Transmission Complete flag.
1933 * @arg USART_FLAG_IDLE: IDLE line detected flag.
1934 * @arg USART_FLAG_ORE: OverRun Error flag.
1935 * @arg USART_FLAG_NE: Noise Error flag.
1936 * @arg USART_FLAG_FE: Framing Error flag.
1937 * @arg USART_FLAG_PE: Parity Errorflag.
1940 * - RXNE pending bit is cleared by a read to the USART_RDR register
1941 * (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register USART_RQR
1942 * (USART_RequestCmd()).
1943 * - TC flag can be also cleared by software sequence: a read operation to
1944 * USART_SR register (USART_GetFlagStatus()) followed by a write operation
1945 * to USART_TDR register (USART_SendData()).
1946 * - TXE flag is cleared by a write to the USART_TDR register
1947 * (USART_SendData()) or by writing 1 to the TXFRQ in the register USART_RQR
1948 * (USART_RequestCmd()).
1949 * - SBKF flag is cleared by 1 to the SBKRQ in the register USART_RQR
1950 * (USART_RequestCmd()).
1953 void USART_ClearFlag(USART_TypeDef
* USARTx
, uint32_t USART_FLAG
)
1955 /* Check the parameters */
1956 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1957 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG
));
1959 USARTx
->ICR
= USART_FLAG
;
1963 * @brief Checks whether the specified USART interrupt has occurred or not.
1964 * @param USARTx: Select the USART peripheral. This parameter can be one of the
1965 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
1966 * @param USART_IT: specifies the USART interrupt source to check.
1967 * This parameter can be one of the following values:
1968 * @arg USART_IT_WU: Wake up interrupt.
1969 * @arg USART_IT_CM: Character match interrupt.
1970 * @arg USART_IT_EOB: End of block interrupt.
1971 * @arg USART_IT_RTO: Receive time out interrupt.
1972 * @arg USART_IT_CTS: CTS change interrupt.
1973 * @arg USART_IT_LBD: LIN Break detection interrupt.
1974 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt.
1975 * @arg USART_IT_TC: Transmission complete interrupt.
1976 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
1977 * @arg USART_IT_IDLE: Idle line detection interrupt.
1978 * @arg USART_IT_ORE: OverRun Error interrupt.
1979 * @arg USART_IT_NE: Noise Error interrupt.
1980 * @arg USART_IT_FE: Framing Error interrupt.
1981 * @arg USART_IT_PE: Parity Error interrupt.
1982 * @retval The new state of USART_IT (SET or RESET).
1984 ITStatus
USART_GetITStatus(USART_TypeDef
* USARTx
, uint32_t USART_IT
)
1986 uint32_t bitpos
= 0, itmask
= 0, usartreg
= 0;
1987 ITStatus bitstatus
= RESET
;
1988 /* Check the parameters */
1989 assert_param(IS_USART_ALL_PERIPH(USARTx
));
1990 assert_param(IS_USART_GET_IT(USART_IT
));
1992 /* Get the USART register index */
1993 usartreg
= (((uint16_t)USART_IT
) >> 0x08);
1994 /* Get the interrupt position */
1995 itmask
= USART_IT
& IT_MASK
;
1996 itmask
= (uint32_t)0x01 << itmask
;
1998 if (usartreg
== 0x01) /* The IT is in CR1 register */
2000 itmask
&= USARTx
->CR1
;
2002 else if (usartreg
== 0x02) /* The IT is in CR2 register */
2004 itmask
&= USARTx
->CR2
;
2006 else /* The IT is in CR3 register */
2008 itmask
&= USARTx
->CR3
;
2011 bitpos
= USART_IT
>> 0x10;
2012 bitpos
= (uint32_t)0x01 << bitpos
;
2013 bitpos
&= USARTx
->ISR
;
2014 if ((itmask
!= (uint16_t)RESET
)&&(bitpos
!= (uint16_t)RESET
))
2027 * @brief Clears the USARTx's interrupt pending bits.
2028 * @param USARTx: Select the USART peripheral. This parameter can be one of the
2029 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
2030 * @param USART_IT: specifies the interrupt pending bit to clear.
2031 * This parameter can be one of the following values:
2032 * @arg USART_IT_WU: Wake up interrupt.
2033 * @arg USART_IT_CM: Character match interrupt.
2034 * @arg USART_IT_EOB: End of block interrupt.
2035 * @arg USART_IT_RTO: Receive time out interrupt.
2036 * @arg USART_IT_CTS: CTS change interrupt.
2037 * @arg USART_IT_LBD: LIN Break detection interrupt.
2038 * @arg USART_IT_TC: Transmission complete interrupt.
2039 * @arg USART_IT_IDLE: IDLE line detected interrupt.
2040 * @arg USART_IT_ORE: OverRun Error interrupt.
2041 * @arg USART_IT_NE: Noise Error interrupt.
2042 * @arg USART_IT_FE: Framing Error interrupt.
2043 * @arg USART_IT_PE: Parity Error interrupt.
2045 * - RXNE pending bit is cleared by a read to the USART_RDR register
2046 * (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register USART_RQR
2047 * (USART_RequestCmd()).
2048 * - TC pending bit can be also cleared by software sequence: a read
2049 * operation to USART_SR register (USART_GetITStatus()) followed by a write
2050 * operation to USART_TDR register (USART_SendData()).
2051 * - TXE pending bit is cleared by a write to the USART_TDR register
2052 * (USART_SendData()) or by writing 1 to the TXFRQ in the register USART_RQR
2053 * (USART_RequestCmd()).
2056 void USART_ClearITPendingBit(USART_TypeDef
* USARTx
, uint32_t USART_IT
)
2058 uint32_t bitpos
= 0, itmask
= 0;
2059 /* Check the parameters */
2060 assert_param(IS_USART_ALL_PERIPH(USARTx
));
2061 assert_param(IS_USART_CLEAR_IT(USART_IT
));
2063 bitpos
= USART_IT
>> 0x10;
2064 itmask
= ((uint32_t)0x01 << (uint32_t)bitpos
);
2065 USARTx
->ICR
= (uint32_t)itmask
;
2084 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/