2 ******************************************************************************
\r
3 * @file stm32f10x_usart.c
\r
4 * @author MCD Application Team
\r
6 * @date 11-March-2011
\r
7 * @brief This file provides all the USART firmware functions.
\r
8 ******************************************************************************
\r
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
\r
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
\r
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
\r
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
\r
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
18 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
\r
19 ******************************************************************************
\r
22 /* Includes ------------------------------------------------------------------*/
\r
23 #include "stm32f10x_usart.h"
\r
24 #include "stm32f10x_rcc.h"
\r
26 /** @addtogroup STM32F10x_StdPeriph_Driver
\r
30 /** @defgroup USART
\r
31 * @brief USART driver modules
\r
35 /** @defgroup USART_Private_TypesDefinitions
\r
43 /** @defgroup USART_Private_Defines
\r
47 #define CR1_UE_Set ((uint16_t)0x2000) /*!< USART Enable Mask */
\r
48 #define CR1_UE_Reset ((uint16_t)0xDFFF) /*!< USART Disable Mask */
\r
50 #define CR1_WAKE_Mask ((uint16_t)0xF7FF) /*!< USART WakeUp Method Mask */
\r
52 #define CR1_RWU_Set ((uint16_t)0x0002) /*!< USART mute mode Enable Mask */
\r
53 #define CR1_RWU_Reset ((uint16_t)0xFFFD) /*!< USART mute mode Enable Mask */
\r
54 #define CR1_SBK_Set ((uint16_t)0x0001) /*!< USART Break Character send Mask */
\r
55 #define CR1_CLEAR_Mask ((uint16_t)0xE9F3) /*!< USART CR1 Mask */
\r
56 #define CR2_Address_Mask ((uint16_t)0xFFF0) /*!< USART address Mask */
\r
58 #define CR2_LINEN_Set ((uint16_t)0x4000) /*!< USART LIN Enable Mask */
\r
59 #define CR2_LINEN_Reset ((uint16_t)0xBFFF) /*!< USART LIN Disable Mask */
\r
61 #define CR2_LBDL_Mask ((uint16_t)0xFFDF) /*!< USART LIN Break detection Mask */
\r
62 #define CR2_STOP_CLEAR_Mask ((uint16_t)0xCFFF) /*!< USART CR2 STOP Bits Mask */
\r
63 #define CR2_CLOCK_CLEAR_Mask ((uint16_t)0xF0FF) /*!< USART CR2 Clock Mask */
\r
65 #define CR3_SCEN_Set ((uint16_t)0x0020) /*!< USART SC Enable Mask */
\r
66 #define CR3_SCEN_Reset ((uint16_t)0xFFDF) /*!< USART SC Disable Mask */
\r
68 #define CR3_NACK_Set ((uint16_t)0x0010) /*!< USART SC NACK Enable Mask */
\r
69 #define CR3_NACK_Reset ((uint16_t)0xFFEF) /*!< USART SC NACK Disable Mask */
\r
71 #define CR3_HDSEL_Set ((uint16_t)0x0008) /*!< USART Half-Duplex Enable Mask */
\r
72 #define CR3_HDSEL_Reset ((uint16_t)0xFFF7) /*!< USART Half-Duplex Disable Mask */
\r
74 #define CR3_IRLP_Mask ((uint16_t)0xFFFB) /*!< USART IrDA LowPower mode Mask */
\r
75 #define CR3_CLEAR_Mask ((uint16_t)0xFCFF) /*!< USART CR3 Mask */
\r
77 #define CR3_IREN_Set ((uint16_t)0x0002) /*!< USART IrDA Enable Mask */
\r
78 #define CR3_IREN_Reset ((uint16_t)0xFFFD) /*!< USART IrDA Disable Mask */
\r
79 #define GTPR_LSB_Mask ((uint16_t)0x00FF) /*!< Guard Time Register LSB Mask */
\r
80 #define GTPR_MSB_Mask ((uint16_t)0xFF00) /*!< Guard Time Register MSB Mask */
\r
81 #define IT_Mask ((uint16_t)0x001F) /*!< USART Interrupt Mask */
\r
83 /* USART OverSampling-8 Mask */
\r
84 #define CR1_OVER8_Set ((u16)0x8000) /* USART OVER8 mode Enable Mask */
\r
85 #define CR1_OVER8_Reset ((u16)0x7FFF) /* USART OVER8 mode Disable Mask */
\r
87 /* USART One Bit Sampling Mask */
\r
88 #define CR3_ONEBITE_Set ((u16)0x0800) /* USART ONEBITE mode Enable Mask */
\r
89 #define CR3_ONEBITE_Reset ((u16)0xF7FF) /* USART ONEBITE mode Disable Mask */
\r
95 /** @defgroup USART_Private_Macros
\r
103 /** @defgroup USART_Private_Variables
\r
111 /** @defgroup USART_Private_FunctionPrototypes
\r
119 /** @defgroup USART_Private_Functions
\r
124 * @brief Deinitializes the USARTx peripheral registers to their default reset values.
\r
125 * @param USARTx: Select the USART or the UART peripheral.
\r
126 * This parameter can be one of the following values:
\r
127 * USART1, USART2, USART3, UART4 or UART5.
\r
130 void USART_DeInit(USART_TypeDef* USARTx)
\r
132 /* Check the parameters */
\r
133 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
135 if (USARTx == USART1)
\r
137 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
\r
138 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
\r
140 else if (USARTx == USART2)
\r
142 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
\r
143 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
\r
145 else if (USARTx == USART3)
\r
147 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
\r
148 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
\r
150 else if (USARTx == UART4)
\r
152 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
\r
153 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
\r
157 if (USARTx == UART5)
\r
159 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
\r
160 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
\r
166 * @brief Initializes the USARTx peripheral according to the specified
\r
167 * parameters in the USART_InitStruct .
\r
168 * @param USARTx: Select the USART or the UART peripheral.
\r
169 * This parameter can be one of the following values:
\r
170 * USART1, USART2, USART3, UART4 or UART5.
\r
171 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
\r
172 * that contains the configuration information for the specified USART
\r
176 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
\r
178 uint32_t tmpreg = 0x00, apbclock = 0x00;
\r
179 uint32_t integerdivider = 0x00;
\r
180 uint32_t fractionaldivider = 0x00;
\r
181 uint32_t usartxbase = 0;
\r
182 RCC_ClocksTypeDef RCC_ClocksStatus;
\r
183 /* Check the parameters */
\r
184 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
185 assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
\r
186 assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
\r
187 assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
\r
188 assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
\r
189 assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
\r
190 assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
\r
191 /* The hardware flow control is available only for USART1, USART2 and USART3 */
\r
192 if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
\r
194 assert_param(IS_USART_123_PERIPH(USARTx));
\r
197 usartxbase = (uint32_t)USARTx;
\r
199 /*---------------------------- USART CR2 Configuration -----------------------*/
\r
200 tmpreg = USARTx->CR2;
\r
201 /* Clear STOP[13:12] bits */
\r
202 tmpreg &= CR2_STOP_CLEAR_Mask;
\r
203 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
\r
204 /* Set STOP[13:12] bits according to USART_StopBits value */
\r
205 tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
\r
207 /* Write to USART CR2 */
\r
208 USARTx->CR2 = (uint16_t)tmpreg;
\r
210 /*---------------------------- USART CR1 Configuration -----------------------*/
\r
211 tmpreg = USARTx->CR1;
\r
212 /* Clear M, PCE, PS, TE and RE bits */
\r
213 tmpreg &= CR1_CLEAR_Mask;
\r
214 /* Configure the USART Word Length, Parity and mode ----------------------- */
\r
215 /* Set the M bits according to USART_WordLength value */
\r
216 /* Set PCE and PS bits according to USART_Parity value */
\r
217 /* Set TE and RE bits according to USART_Mode value */
\r
218 tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
\r
219 USART_InitStruct->USART_Mode;
\r
220 /* Write to USART CR1 */
\r
221 USARTx->CR1 = (uint16_t)tmpreg;
\r
223 /*---------------------------- USART CR3 Configuration -----------------------*/
\r
224 tmpreg = USARTx->CR3;
\r
225 /* Clear CTSE and RTSE bits */
\r
226 tmpreg &= CR3_CLEAR_Mask;
\r
227 /* Configure the USART HFC -------------------------------------------------*/
\r
228 /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
\r
229 tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
\r
230 /* Write to USART CR3 */
\r
231 USARTx->CR3 = (uint16_t)tmpreg;
\r
233 /*---------------------------- USART BRR Configuration -----------------------*/
\r
234 /* Configure the USART Baud Rate -------------------------------------------*/
\r
235 RCC_GetClocksFreq(&RCC_ClocksStatus);
\r
236 if (usartxbase == USART1_BASE)
\r
238 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
\r
242 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
\r
245 /* Determine the integer part */
\r
246 if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
\r
248 /* Integer part computing in case Oversampling mode is 8 Samples */
\r
249 integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
\r
251 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
\r
253 /* Integer part computing in case Oversampling mode is 16 Samples */
\r
254 integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
\r
256 tmpreg = (integerdivider / 100) << 4;
\r
258 /* Determine the fractional part */
\r
259 fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
\r
261 /* Implement the fractional part in the register */
\r
262 if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
\r
264 tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
\r
266 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
\r
268 tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
\r
271 /* Write to USART BRR */
\r
272 USARTx->BRR = (uint16_t)tmpreg;
\r
276 * @brief Fills each USART_InitStruct member with its default value.
\r
277 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
\r
278 * which will be initialized.
\r
281 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
\r
283 /* USART_InitStruct members default value */
\r
284 USART_InitStruct->USART_BaudRate = 9600;
\r
285 USART_InitStruct->USART_WordLength = USART_WordLength_8b;
\r
286 USART_InitStruct->USART_StopBits = USART_StopBits_1;
\r
287 USART_InitStruct->USART_Parity = USART_Parity_No ;
\r
288 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
\r
289 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
\r
293 * @brief Initializes the USARTx peripheral Clock according to the
\r
294 * specified parameters in the USART_ClockInitStruct .
\r
295 * @param USARTx: where x can be 1, 2, 3 to select the USART peripheral.
\r
296 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
\r
297 * structure that contains the configuration information for the specified
\r
298 * USART peripheral.
\r
299 * @note The Smart Card and Synchronous modes are not available for UART4 and UART5.
\r
302 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
\r
304 uint32_t tmpreg = 0x00;
\r
305 /* Check the parameters */
\r
306 assert_param(IS_USART_123_PERIPH(USARTx));
\r
307 assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
\r
308 assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
\r
309 assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
\r
310 assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
\r
312 /*---------------------------- USART CR2 Configuration -----------------------*/
\r
313 tmpreg = USARTx->CR2;
\r
314 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
\r
315 tmpreg &= CR2_CLOCK_CLEAR_Mask;
\r
316 /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
\r
317 /* Set CLKEN bit according to USART_Clock value */
\r
318 /* Set CPOL bit according to USART_CPOL value */
\r
319 /* Set CPHA bit according to USART_CPHA value */
\r
320 /* Set LBCL bit according to USART_LastBit value */
\r
321 tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
\r
322 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
\r
323 /* Write to USART CR2 */
\r
324 USARTx->CR2 = (uint16_t)tmpreg;
\r
328 * @brief Fills each USART_ClockInitStruct member with its default value.
\r
329 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
\r
330 * structure which will be initialized.
\r
333 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
\r
335 /* USART_ClockInitStruct members default value */
\r
336 USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
\r
337 USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
\r
338 USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
\r
339 USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
\r
343 * @brief Enables or disables the specified USART peripheral.
\r
344 * @param USARTx: Select the USART or the UART peripheral.
\r
345 * This parameter can be one of the following values:
\r
346 * USART1, USART2, USART3, UART4 or UART5.
\r
347 * @param NewState: new state of the USARTx peripheral.
\r
348 * This parameter can be: ENABLE or DISABLE.
\r
351 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
353 /* Check the parameters */
\r
354 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
355 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
357 if (NewState != DISABLE)
\r
359 /* Enable the selected USART by setting the UE bit in the CR1 register */
\r
360 USARTx->CR1 |= CR1_UE_Set;
\r
364 /* Disable the selected USART by clearing the UE bit in the CR1 register */
\r
365 USARTx->CR1 &= CR1_UE_Reset;
\r
370 * @brief Enables or disables the specified USART interrupts.
\r
371 * @param USARTx: Select the USART or the UART peripheral.
\r
372 * This parameter can be one of the following values:
\r
373 * USART1, USART2, USART3, UART4 or UART5.
\r
374 * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
\r
375 * This parameter can be one of the following values:
\r
376 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
\r
377 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
378 * @arg USART_IT_TXE: Transmit Data Register empty interrupt
\r
379 * @arg USART_IT_TC: Transmission complete interrupt
\r
380 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
\r
381 * @arg USART_IT_IDLE: Idle line detection interrupt
\r
382 * @arg USART_IT_PE: Parity Error interrupt
\r
383 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
\r
384 * @param NewState: new state of the specified USARTx interrupts.
\r
385 * This parameter can be: ENABLE or DISABLE.
\r
388 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
\r
390 uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
\r
391 uint32_t usartxbase = 0x00;
\r
392 /* Check the parameters */
\r
393 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
394 assert_param(IS_USART_CONFIG_IT(USART_IT));
\r
395 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
396 /* The CTS interrupt is not available for UART4 and UART5 */
\r
397 if (USART_IT == USART_IT_CTS)
\r
399 assert_param(IS_USART_123_PERIPH(USARTx));
\r
402 usartxbase = (uint32_t)USARTx;
\r
404 /* Get the USART register index */
\r
405 usartreg = (((uint8_t)USART_IT) >> 0x05);
\r
407 /* Get the interrupt position */
\r
408 itpos = USART_IT & IT_Mask;
\r
409 itmask = (((uint32_t)0x01) << itpos);
\r
411 if (usartreg == 0x01) /* The IT is in CR1 register */
\r
413 usartxbase += 0x0C;
\r
415 else if (usartreg == 0x02) /* The IT is in CR2 register */
\r
417 usartxbase += 0x10;
\r
419 else /* The IT is in CR3 register */
\r
421 usartxbase += 0x14;
\r
423 if (NewState != DISABLE)
\r
425 *(__IO uint32_t*)usartxbase |= itmask;
\r
429 *(__IO uint32_t*)usartxbase &= ~itmask;
\r
434 * @brief Enables or disables the USART
\92s DMA interface.
\r
435 * @param USARTx: Select the USART or the UART peripheral.
\r
436 * This parameter can be one of the following values:
\r
437 * USART1, USART2, USART3, UART4 or UART5.
\r
438 * @param USART_DMAReq: specifies the DMA request.
\r
439 * This parameter can be any combination of the following values:
\r
440 * @arg USART_DMAReq_Tx: USART DMA transmit request
\r
441 * @arg USART_DMAReq_Rx: USART DMA receive request
\r
442 * @param NewState: new state of the DMA Request sources.
\r
443 * This parameter can be: ENABLE or DISABLE.
\r
444 * @note The DMA mode is not available for UART5 except in the STM32
\r
445 * High density value line devices(STM32F10X_HD_VL).
\r
448 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
\r
450 /* Check the parameters */
\r
451 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
452 assert_param(IS_USART_DMAREQ(USART_DMAReq));
\r
453 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
454 if (NewState != DISABLE)
\r
456 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
\r
457 DMAR bits in the USART CR3 register */
\r
458 USARTx->CR3 |= USART_DMAReq;
\r
462 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
\r
463 DMAR bits in the USART CR3 register */
\r
464 USARTx->CR3 &= (uint16_t)~USART_DMAReq;
\r
469 * @brief Sets the address of the USART node.
\r
470 * @param USARTx: Select the USART or the UART peripheral.
\r
471 * This parameter can be one of the following values:
\r
472 * USART1, USART2, USART3, UART4 or UART5.
\r
473 * @param USART_Address: Indicates the address of the USART node.
\r
476 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
\r
478 /* Check the parameters */
\r
479 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
480 assert_param(IS_USART_ADDRESS(USART_Address));
\r
482 /* Clear the USART address */
\r
483 USARTx->CR2 &= CR2_Address_Mask;
\r
484 /* Set the USART address node */
\r
485 USARTx->CR2 |= USART_Address;
\r
489 * @brief Selects the USART WakeUp method.
\r
490 * @param USARTx: Select the USART or the UART peripheral.
\r
491 * This parameter can be one of the following values:
\r
492 * USART1, USART2, USART3, UART4 or UART5.
\r
493 * @param USART_WakeUp: specifies the USART wakeup method.
\r
494 * This parameter can be one of the following values:
\r
495 * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
\r
496 * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
\r
499 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
\r
501 /* Check the parameters */
\r
502 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
503 assert_param(IS_USART_WAKEUP(USART_WakeUp));
\r
505 USARTx->CR1 &= CR1_WAKE_Mask;
\r
506 USARTx->CR1 |= USART_WakeUp;
\r
510 * @brief Determines if the USART is in mute mode or not.
\r
511 * @param USARTx: Select the USART or the UART peripheral.
\r
512 * This parameter can be one of the following values:
\r
513 * USART1, USART2, USART3, UART4 or UART5.
\r
514 * @param NewState: new state of the USART mute mode.
\r
515 * This parameter can be: ENABLE or DISABLE.
\r
518 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
520 /* Check the parameters */
\r
521 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
522 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
524 if (NewState != DISABLE)
\r
526 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
\r
527 USARTx->CR1 |= CR1_RWU_Set;
\r
531 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
\r
532 USARTx->CR1 &= CR1_RWU_Reset;
\r
537 * @brief Sets the USART LIN Break detection length.
\r
538 * @param USARTx: Select the USART or the UART peripheral.
\r
539 * This parameter can be one of the following values:
\r
540 * USART1, USART2, USART3, UART4 or UART5.
\r
541 * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
\r
542 * This parameter can be one of the following values:
\r
543 * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
\r
544 * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
\r
547 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
\r
549 /* Check the parameters */
\r
550 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
551 assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
\r
553 USARTx->CR2 &= CR2_LBDL_Mask;
\r
554 USARTx->CR2 |= USART_LINBreakDetectLength;
\r
558 * @brief Enables or disables the USART
\92s LIN mode.
\r
559 * @param USARTx: Select the USART or the UART peripheral.
\r
560 * This parameter can be one of the following values:
\r
561 * USART1, USART2, USART3, UART4 or UART5.
\r
562 * @param NewState: new state of the USART LIN mode.
\r
563 * This parameter can be: ENABLE or DISABLE.
\r
566 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
568 /* Check the parameters */
\r
569 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
570 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
572 if (NewState != DISABLE)
\r
574 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
\r
575 USARTx->CR2 |= CR2_LINEN_Set;
\r
579 /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
\r
580 USARTx->CR2 &= CR2_LINEN_Reset;
\r
585 * @brief Transmits single data through the USARTx peripheral.
\r
586 * @param USARTx: Select the USART or the UART peripheral.
\r
587 * This parameter can be one of the following values:
\r
588 * USART1, USART2, USART3, UART4 or UART5.
\r
589 * @param Data: the data to transmit.
\r
592 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
\r
594 /* Check the parameters */
\r
595 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
596 assert_param(IS_USART_DATA(Data));
\r
598 /* Transmit Data */
\r
599 USARTx->DR = (Data & (uint16_t)0x01FF);
\r
603 * @brief Returns the most recent received data by the USARTx peripheral.
\r
604 * @param USARTx: Select the USART or the UART peripheral.
\r
605 * This parameter can be one of the following values:
\r
606 * USART1, USART2, USART3, UART4 or UART5.
\r
607 * @retval The received data.
\r
609 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
\r
611 /* Check the parameters */
\r
612 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
615 return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
\r
619 * @brief Transmits break characters.
\r
620 * @param USARTx: Select the USART or the UART peripheral.
\r
621 * This parameter can be one of the following values:
\r
622 * USART1, USART2, USART3, UART4 or UART5.
\r
625 void USART_SendBreak(USART_TypeDef* USARTx)
\r
627 /* Check the parameters */
\r
628 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
630 /* Send break characters */
\r
631 USARTx->CR1 |= CR1_SBK_Set;
\r
635 * @brief Sets the specified USART guard time.
\r
636 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
\r
637 * @param USART_GuardTime: specifies the guard time.
\r
638 * @note The guard time bits are not available for UART4 and UART5.
\r
641 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
\r
643 /* Check the parameters */
\r
644 assert_param(IS_USART_123_PERIPH(USARTx));
\r
646 /* Clear the USART Guard time */
\r
647 USARTx->GTPR &= GTPR_LSB_Mask;
\r
648 /* Set the USART guard time */
\r
649 USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
\r
653 * @brief Sets the system clock prescaler.
\r
654 * @param USARTx: Select the USART or the UART peripheral.
\r
655 * This parameter can be one of the following values:
\r
656 * USART1, USART2, USART3, UART4 or UART5.
\r
657 * @param USART_Prescaler: specifies the prescaler clock.
\r
658 * @note The function is used for IrDA mode with UART4 and UART5.
\r
661 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
\r
663 /* Check the parameters */
\r
664 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
666 /* Clear the USART prescaler */
\r
667 USARTx->GTPR &= GTPR_MSB_Mask;
\r
668 /* Set the USART prescaler */
\r
669 USARTx->GTPR |= USART_Prescaler;
\r
673 * @brief Enables or disables the USART
\92s Smart Card mode.
\r
674 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
\r
675 * @param NewState: new state of the Smart Card mode.
\r
676 * This parameter can be: ENABLE or DISABLE.
\r
677 * @note The Smart Card mode is not available for UART4 and UART5.
\r
680 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
682 /* Check the parameters */
\r
683 assert_param(IS_USART_123_PERIPH(USARTx));
\r
684 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
685 if (NewState != DISABLE)
\r
687 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
\r
688 USARTx->CR3 |= CR3_SCEN_Set;
\r
692 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
\r
693 USARTx->CR3 &= CR3_SCEN_Reset;
\r
698 * @brief Enables or disables NACK transmission.
\r
699 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
\r
700 * @param NewState: new state of the NACK transmission.
\r
701 * This parameter can be: ENABLE or DISABLE.
\r
702 * @note The Smart Card mode is not available for UART4 and UART5.
\r
705 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
707 /* Check the parameters */
\r
708 assert_param(IS_USART_123_PERIPH(USARTx));
\r
709 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
710 if (NewState != DISABLE)
\r
712 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
\r
713 USARTx->CR3 |= CR3_NACK_Set;
\r
717 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
\r
718 USARTx->CR3 &= CR3_NACK_Reset;
\r
723 * @brief Enables or disables the USART
\92s Half Duplex communication.
\r
724 * @param USARTx: Select the USART or the UART peripheral.
\r
725 * This parameter can be one of the following values:
\r
726 * USART1, USART2, USART3, UART4 or UART5.
\r
727 * @param NewState: new state of the USART Communication.
\r
728 * This parameter can be: ENABLE or DISABLE.
\r
731 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
733 /* Check the parameters */
\r
734 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
735 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
737 if (NewState != DISABLE)
\r
739 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
\r
740 USARTx->CR3 |= CR3_HDSEL_Set;
\r
744 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
\r
745 USARTx->CR3 &= CR3_HDSEL_Reset;
\r
751 * @brief Enables or disables the USART's 8x oversampling mode.
\r
752 * @param USARTx: Select the USART or the UART peripheral.
\r
753 * This parameter can be one of the following values:
\r
754 * USART1, USART2, USART3, UART4 or UART5.
\r
755 * @param NewState: new state of the USART one bit sampling method.
\r
756 * This parameter can be: ENABLE or DISABLE.
\r
758 * This function has to be called before calling USART_Init()
\r
759 * function in order to have correct baudrate Divider value.
\r
762 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
764 /* Check the parameters */
\r
765 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
766 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
768 if (NewState != DISABLE)
\r
770 /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
\r
771 USARTx->CR1 |= CR1_OVER8_Set;
\r
775 /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
\r
776 USARTx->CR1 &= CR1_OVER8_Reset;
\r
781 * @brief Enables or disables the USART's one bit sampling method.
\r
782 * @param USARTx: Select the USART or the UART peripheral.
\r
783 * This parameter can be one of the following values:
\r
784 * USART1, USART2, USART3, UART4 or UART5.
\r
785 * @param NewState: new state of the USART one bit sampling method.
\r
786 * This parameter can be: ENABLE or DISABLE.
\r
789 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
791 /* Check the parameters */
\r
792 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
793 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
795 if (NewState != DISABLE)
\r
797 /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
\r
798 USARTx->CR3 |= CR3_ONEBITE_Set;
\r
802 /* Disable tthe one bit method by clearing the ONEBITE bit in the CR3 register */
\r
803 USARTx->CR3 &= CR3_ONEBITE_Reset;
\r
808 * @brief Configures the USART's IrDA interface.
\r
809 * @param USARTx: Select the USART or the UART peripheral.
\r
810 * This parameter can be one of the following values:
\r
811 * USART1, USART2, USART3, UART4 or UART5.
\r
812 * @param USART_IrDAMode: specifies the IrDA mode.
\r
813 * This parameter can be one of the following values:
\r
814 * @arg USART_IrDAMode_LowPower
\r
815 * @arg USART_IrDAMode_Normal
\r
818 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
\r
820 /* Check the parameters */
\r
821 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
822 assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
\r
824 USARTx->CR3 &= CR3_IRLP_Mask;
\r
825 USARTx->CR3 |= USART_IrDAMode;
\r
829 * @brief Enables or disables the USART's IrDA interface.
\r
830 * @param USARTx: Select the USART or the UART peripheral.
\r
831 * This parameter can be one of the following values:
\r
832 * USART1, USART2, USART3, UART4 or UART5.
\r
833 * @param NewState: new state of the IrDA mode.
\r
834 * This parameter can be: ENABLE or DISABLE.
\r
837 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
839 /* Check the parameters */
\r
840 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
841 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
843 if (NewState != DISABLE)
\r
845 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
\r
846 USARTx->CR3 |= CR3_IREN_Set;
\r
850 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
\r
851 USARTx->CR3 &= CR3_IREN_Reset;
\r
856 * @brief Checks whether the specified USART flag is set or not.
\r
857 * @param USARTx: Select the USART or the UART peripheral.
\r
858 * This parameter can be one of the following values:
\r
859 * USART1, USART2, USART3, UART4 or UART5.
\r
860 * @param USART_FLAG: specifies the flag to check.
\r
861 * This parameter can be one of the following values:
\r
862 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
\r
863 * @arg USART_FLAG_LBD: LIN Break detection flag
\r
864 * @arg USART_FLAG_TXE: Transmit data register empty flag
\r
865 * @arg USART_FLAG_TC: Transmission Complete flag
\r
866 * @arg USART_FLAG_RXNE: Receive data register not empty flag
\r
867 * @arg USART_FLAG_IDLE: Idle Line detection flag
\r
868 * @arg USART_FLAG_ORE: OverRun Error flag
\r
869 * @arg USART_FLAG_NE: Noise Error flag
\r
870 * @arg USART_FLAG_FE: Framing Error flag
\r
871 * @arg USART_FLAG_PE: Parity Error flag
\r
872 * @retval The new state of USART_FLAG (SET or RESET).
\r
874 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
\r
876 FlagStatus bitstatus = RESET;
\r
877 /* Check the parameters */
\r
878 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
879 assert_param(IS_USART_FLAG(USART_FLAG));
\r
880 /* The CTS flag is not available for UART4 and UART5 */
\r
881 if (USART_FLAG == USART_FLAG_CTS)
\r
883 assert_param(IS_USART_123_PERIPH(USARTx));
\r
886 if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
\r
898 * @brief Clears the USARTx's pending flags.
\r
899 * @param USARTx: Select the USART or the UART peripheral.
\r
900 * This parameter can be one of the following values:
\r
901 * USART1, USART2, USART3, UART4 or UART5.
\r
902 * @param USART_FLAG: specifies the flag to clear.
\r
903 * This parameter can be any combination of the following values:
\r
904 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
\r
905 * @arg USART_FLAG_LBD: LIN Break detection flag.
\r
906 * @arg USART_FLAG_TC: Transmission Complete flag.
\r
907 * @arg USART_FLAG_RXNE: Receive data register not empty flag.
\r
910 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
\r
911 * error) and IDLE (Idle line detected) flags are cleared by software
\r
912 * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
\r
913 * followed by a read operation to USART_DR register (USART_ReceiveData()).
\r
914 * - RXNE flag can be also cleared by a read to the USART_DR register
\r
915 * (USART_ReceiveData()).
\r
916 * - TC flag can be also cleared by software sequence: a read operation to
\r
917 * USART_SR register (USART_GetFlagStatus()) followed by a write operation
\r
918 * to USART_DR register (USART_SendData()).
\r
919 * - TXE flag is cleared only by a write to the USART_DR register
\r
920 * (USART_SendData()).
\r
923 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
\r
925 /* Check the parameters */
\r
926 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
927 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
\r
928 /* The CTS flag is not available for UART4 and UART5 */
\r
929 if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
\r
931 assert_param(IS_USART_123_PERIPH(USARTx));
\r
934 USARTx->SR = (uint16_t)~USART_FLAG;
\r
938 * @brief Checks whether the specified USART interrupt has occurred or not.
\r
939 * @param USARTx: Select the USART or the UART peripheral.
\r
940 * This parameter can be one of the following values:
\r
941 * USART1, USART2, USART3, UART4 or UART5.
\r
942 * @param USART_IT: specifies the USART interrupt source to check.
\r
943 * This parameter can be one of the following values:
\r
944 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
\r
945 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
946 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
\r
947 * @arg USART_IT_TC: Transmission complete interrupt
\r
948 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
\r
949 * @arg USART_IT_IDLE: Idle line detection interrupt
\r
950 * @arg USART_IT_ORE: OverRun Error interrupt
\r
951 * @arg USART_IT_NE: Noise Error interrupt
\r
952 * @arg USART_IT_FE: Framing Error interrupt
\r
953 * @arg USART_IT_PE: Parity Error interrupt
\r
954 * @retval The new state of USART_IT (SET or RESET).
\r
956 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
\r
958 uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
\r
959 ITStatus bitstatus = RESET;
\r
960 /* Check the parameters */
\r
961 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
962 assert_param(IS_USART_GET_IT(USART_IT));
\r
963 /* The CTS interrupt is not available for UART4 and UART5 */
\r
964 if (USART_IT == USART_IT_CTS)
\r
966 assert_param(IS_USART_123_PERIPH(USARTx));
\r
969 /* Get the USART register index */
\r
970 usartreg = (((uint8_t)USART_IT) >> 0x05);
\r
971 /* Get the interrupt position */
\r
972 itmask = USART_IT & IT_Mask;
\r
973 itmask = (uint32_t)0x01 << itmask;
\r
975 if (usartreg == 0x01) /* The IT is in CR1 register */
\r
977 itmask &= USARTx->CR1;
\r
979 else if (usartreg == 0x02) /* The IT is in CR2 register */
\r
981 itmask &= USARTx->CR2;
\r
983 else /* The IT is in CR3 register */
\r
985 itmask &= USARTx->CR3;
\r
988 bitpos = USART_IT >> 0x08;
\r
989 bitpos = (uint32_t)0x01 << bitpos;
\r
990 bitpos &= USARTx->SR;
\r
991 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
\r
1000 return bitstatus;
\r
1004 * @brief Clears the USARTx's interrupt pending bits.
\r
1005 * @param USARTx: Select the USART or the UART peripheral.
\r
1006 * This parameter can be one of the following values:
\r
1007 * USART1, USART2, USART3, UART4 or UART5.
\r
1008 * @param USART_IT: specifies the interrupt pending bit to clear.
\r
1009 * This parameter can be one of the following values:
\r
1010 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
\r
1011 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
1012 * @arg USART_IT_TC: Transmission complete interrupt.
\r
1013 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
\r
1016 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
\r
1017 * error) and IDLE (Idle line detected) pending bits are cleared by
\r
1018 * software sequence: a read operation to USART_SR register
\r
1019 * (USART_GetITStatus()) followed by a read operation to USART_DR register
\r
1020 * (USART_ReceiveData()).
\r
1021 * - RXNE pending bit can be also cleared by a read to the USART_DR register
\r
1022 * (USART_ReceiveData()).
\r
1023 * - TC pending bit can be also cleared by software sequence: a read
\r
1024 * operation to USART_SR register (USART_GetITStatus()) followed by a write
\r
1025 * operation to USART_DR register (USART_SendData()).
\r
1026 * - TXE pending bit is cleared only by a write to the USART_DR register
\r
1027 * (USART_SendData()).
\r
1030 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
\r
1032 uint16_t bitpos = 0x00, itmask = 0x00;
\r
1033 /* Check the parameters */
\r
1034 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
1035 assert_param(IS_USART_CLEAR_IT(USART_IT));
\r
1036 /* The CTS interrupt is not available for UART4 and UART5 */
\r
1037 if (USART_IT == USART_IT_CTS)
\r
1039 assert_param(IS_USART_123_PERIPH(USARTx));
\r
1042 bitpos = USART_IT >> 0x08;
\r
1043 itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
\r
1044 USARTx->SR = (uint16_t)~itmask;
\r
1058 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
\r