2 ******************************************************************************
\r
3 * @file stm32f10x_usart.c
\r
4 * @author MCD Application Team
\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 2010 STMicroelectronics</center></h2>
\r
21 /* Includes ------------------------------------------------------------------*/
\r
22 #include "stm32f10x_usart.h"
\r
23 #include "stm32f10x_rcc.h"
\r
25 /** @addtogroup STM32F10x_StdPeriph_Driver
\r
29 /** @defgroup USART
\r
30 * @brief USART driver modules
\r
34 /** @defgroup USART_Private_TypesDefinitions
\r
42 /** @defgroup USART_Private_Defines
\r
46 #define CR1_UE_Set ((uint16_t)0x2000) /*!< USART Enable Mask */
\r
47 #define CR1_UE_Reset ((uint16_t)0xDFFF) /*!< USART Disable Mask */
\r
49 #define CR1_WAKE_Mask ((uint16_t)0xF7FF) /*!< USART WakeUp Method Mask */
\r
51 #define CR1_RWU_Set ((uint16_t)0x0002) /*!< USART mute mode Enable Mask */
\r
52 #define CR1_RWU_Reset ((uint16_t)0xFFFD) /*!< USART mute mode Enable Mask */
\r
53 #define CR1_SBK_Set ((uint16_t)0x0001) /*!< USART Break Character send Mask */
\r
54 #define CR1_CLEAR_Mask ((uint16_t)0xE9F3) /*!< USART CR1 Mask */
\r
55 #define CR2_Address_Mask ((uint16_t)0xFFF0) /*!< USART address Mask */
\r
57 #define CR2_LINEN_Set ((uint16_t)0x4000) /*!< USART LIN Enable Mask */
\r
58 #define CR2_LINEN_Reset ((uint16_t)0xBFFF) /*!< USART LIN Disable Mask */
\r
60 #define CR2_LBDL_Mask ((uint16_t)0xFFDF) /*!< USART LIN Break detection Mask */
\r
61 #define CR2_STOP_CLEAR_Mask ((uint16_t)0xCFFF) /*!< USART CR2 STOP Bits Mask */
\r
62 #define CR2_CLOCK_CLEAR_Mask ((uint16_t)0xF0FF) /*!< USART CR2 Clock Mask */
\r
64 #define CR3_SCEN_Set ((uint16_t)0x0020) /*!< USART SC Enable Mask */
\r
65 #define CR3_SCEN_Reset ((uint16_t)0xFFDF) /*!< USART SC Disable Mask */
\r
67 #define CR3_NACK_Set ((uint16_t)0x0010) /*!< USART SC NACK Enable Mask */
\r
68 #define CR3_NACK_Reset ((uint16_t)0xFFEF) /*!< USART SC NACK Disable Mask */
\r
70 #define CR3_HDSEL_Set ((uint16_t)0x0008) /*!< USART Half-Duplex Enable Mask */
\r
71 #define CR3_HDSEL_Reset ((uint16_t)0xFFF7) /*!< USART Half-Duplex Disable Mask */
\r
73 #define CR3_IRLP_Mask ((uint16_t)0xFFFB) /*!< USART IrDA LowPower mode Mask */
\r
74 #define CR3_CLEAR_Mask ((uint16_t)0xFCFF) /*!< USART CR3 Mask */
\r
76 #define CR3_IREN_Set ((uint16_t)0x0002) /*!< USART IrDA Enable Mask */
\r
77 #define CR3_IREN_Reset ((uint16_t)0xFFFD) /*!< USART IrDA Disable Mask */
\r
78 #define GTPR_LSB_Mask ((uint16_t)0x00FF) /*!< Guard Time Register LSB Mask */
\r
79 #define GTPR_MSB_Mask ((uint16_t)0xFF00) /*!< Guard Time Register MSB Mask */
\r
80 #define IT_Mask ((uint16_t)0x001F) /*!< USART Interrupt Mask */
\r
82 /* USART OverSampling-8 Mask */
\r
83 #define CR1_OVER8_Set ((u16)0x8000) /* USART OVER8 mode Enable Mask */
\r
84 #define CR1_OVER8_Reset ((u16)0x7FFF) /* USART OVER8 mode Disable Mask */
\r
86 /* USART One Bit Sampling Mask */
\r
87 #define CR3_ONEBITE_Set ((u16)0x0800) /* USART ONEBITE mode Enable Mask */
\r
88 #define CR3_ONEBITE_Reset ((u16)0xF7FF) /* USART ONEBITE mode Disable Mask */
\r
94 /** @defgroup USART_Private_Macros
\r
102 /** @defgroup USART_Private_Variables
\r
110 /** @defgroup USART_Private_FunctionPrototypes
\r
118 /** @defgroup USART_Private_Functions
\r
123 * @brief Deinitializes the USARTx peripheral registers to their default reset values.
\r
124 * @param USARTx: Select the USART or the UART peripheral.
\r
125 * This parameter can be one of the following values: USART1, USART2, USART3, UART4 or UART5.
\r
128 void USART_DeInit(USART_TypeDef* USARTx)
\r
130 /* Check the parameters */
\r
131 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
133 if (USARTx == USART1)
\r
135 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
\r
136 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
\r
138 else if (USARTx == USART2)
\r
140 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
\r
141 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
\r
143 else if (USARTx == USART3)
\r
145 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
\r
146 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
\r
148 else if (USARTx == UART4)
\r
150 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
\r
151 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
\r
155 if (USARTx == UART5)
\r
157 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
\r
158 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
\r
164 * @brief Initializes the USARTx peripheral according to the specified
\r
165 * parameters in the USART_InitStruct .
\r
166 * @param USARTx: Select the USART or the UART peripheral.
\r
167 * This parameter can be one of the following values:
\r
168 * USART1, USART2, USART3, UART4 or UART5.
\r
169 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
\r
170 * that contains the configuration information for the specified USART peripheral.
\r
173 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
\r
175 uint32_t tmpreg = 0x00, apbclock = 0x00;
\r
176 uint32_t integerdivider = 0x00;
\r
177 uint32_t fractionaldivider = 0x00;
\r
178 uint32_t usartxbase = 0;
\r
179 RCC_ClocksTypeDef RCC_ClocksStatus;
\r
180 /* Check the parameters */
\r
181 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
182 assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
\r
183 assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
\r
184 assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
\r
185 assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
\r
186 assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
\r
187 assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
\r
188 /* The hardware flow control is available only for USART1, USART2 and USART3 */
\r
189 if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
\r
191 assert_param(IS_USART_123_PERIPH(USARTx));
\r
194 usartxbase = (uint32_t)USARTx;
\r
196 /*---------------------------- USART CR2 Configuration -----------------------*/
\r
197 tmpreg = USARTx->CR2;
\r
198 /* Clear STOP[13:12] bits */
\r
199 tmpreg &= CR2_STOP_CLEAR_Mask;
\r
200 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
\r
201 /* Set STOP[13:12] bits according to USART_StopBits value */
\r
202 tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
\r
204 /* Write to USART CR2 */
\r
205 USARTx->CR2 = (uint16_t)tmpreg;
\r
207 /*---------------------------- USART CR1 Configuration -----------------------*/
\r
208 tmpreg = USARTx->CR1;
\r
209 /* Clear M, PCE, PS, TE and RE bits */
\r
210 tmpreg &= CR1_CLEAR_Mask;
\r
211 /* Configure the USART Word Length, Parity and mode ----------------------- */
\r
212 /* Set the M bits according to USART_WordLength value */
\r
213 /* Set PCE and PS bits according to USART_Parity value */
\r
214 /* Set TE and RE bits according to USART_Mode value */
\r
215 tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
\r
216 USART_InitStruct->USART_Mode;
\r
217 /* Write to USART CR1 */
\r
218 USARTx->CR1 = (uint16_t)tmpreg;
\r
220 /*---------------------------- USART CR3 Configuration -----------------------*/
\r
221 tmpreg = USARTx->CR3;
\r
222 /* Clear CTSE and RTSE bits */
\r
223 tmpreg &= CR3_CLEAR_Mask;
\r
224 /* Configure the USART HFC -------------------------------------------------*/
\r
225 /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
\r
226 tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
\r
227 /* Write to USART CR3 */
\r
228 USARTx->CR3 = (uint16_t)tmpreg;
\r
230 /*---------------------------- USART BRR Configuration -----------------------*/
\r
231 /* Configure the USART Baud Rate -------------------------------------------*/
\r
232 RCC_GetClocksFreq(&RCC_ClocksStatus);
\r
233 if (usartxbase == USART1_BASE)
\r
235 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
\r
239 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
\r
242 /* Determine the integer part */
\r
243 if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
\r
245 /* Integer part computing in case Oversampling mode is 8 Samples */
\r
246 integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
\r
248 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
\r
250 /* Integer part computing in case Oversampling mode is 16 Samples */
\r
251 integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
\r
253 tmpreg = (integerdivider / 100) << 4;
\r
255 /* Determine the fractional part */
\r
256 fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
\r
258 /* Implement the fractional part in the register */
\r
259 if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
\r
261 tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
\r
263 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
\r
265 tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
\r
268 /* Write to USART BRR */
\r
269 USARTx->BRR = (uint16_t)tmpreg;
\r
273 * @brief Fills each USART_InitStruct member with its default value.
\r
274 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
\r
275 * which will be initialized.
\r
278 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
\r
280 /* USART_InitStruct members default value */
\r
281 USART_InitStruct->USART_BaudRate = 9600;
\r
282 USART_InitStruct->USART_WordLength = USART_WordLength_8b;
\r
283 USART_InitStruct->USART_StopBits = USART_StopBits_1;
\r
284 USART_InitStruct->USART_Parity = USART_Parity_No ;
\r
285 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
\r
286 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
\r
290 * @brief Initializes the USARTx peripheral Clock according to the
\r
291 * specified parameters in the USART_ClockInitStruct .
\r
292 * @param USARTx: where x can be 1, 2, 3 to select the USART peripheral.
\r
293 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
\r
294 * structure that contains the configuration information for the specified
\r
295 * USART peripheral.
\r
296 * @note The Smart Card mode is not available for UART4 and UART5.
\r
299 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
\r
301 uint32_t tmpreg = 0x00;
\r
302 /* Check the parameters */
\r
303 assert_param(IS_USART_123_PERIPH(USARTx));
\r
304 assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
\r
305 assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
\r
306 assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
\r
307 assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
\r
309 /*---------------------------- USART CR2 Configuration -----------------------*/
\r
310 tmpreg = USARTx->CR2;
\r
311 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
\r
312 tmpreg &= CR2_CLOCK_CLEAR_Mask;
\r
313 /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
\r
314 /* Set CLKEN bit according to USART_Clock value */
\r
315 /* Set CPOL bit according to USART_CPOL value */
\r
316 /* Set CPHA bit according to USART_CPHA value */
\r
317 /* Set LBCL bit according to USART_LastBit value */
\r
318 tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
\r
319 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
\r
320 /* Write to USART CR2 */
\r
321 USARTx->CR2 = (uint16_t)tmpreg;
\r
325 * @brief Fills each USART_ClockInitStruct member with its default value.
\r
326 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
\r
327 * structure which will be initialized.
\r
330 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
\r
332 /* USART_ClockInitStruct members default value */
\r
333 USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
\r
334 USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
\r
335 USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
\r
336 USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
\r
340 * @brief Enables or disables the specified USART peripheral.
\r
341 * @param USARTx: Select the USART or the UART peripheral.
\r
342 * This parameter can be one of the following values:
\r
343 * USART1, USART2, USART3, UART4 or UART5.
\r
344 * @param NewState: new state of the USARTx peripheral.
\r
345 * This parameter can be: ENABLE or DISABLE.
\r
348 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
350 /* Check the parameters */
\r
351 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
352 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
354 if (NewState != DISABLE)
\r
356 /* Enable the selected USART by setting the UE bit in the CR1 register */
\r
357 USARTx->CR1 |= CR1_UE_Set;
\r
361 /* Disable the selected USART by clearing the UE bit in the CR1 register */
\r
362 USARTx->CR1 &= CR1_UE_Reset;
\r
367 * @brief Enables or disables the specified USART interrupts.
\r
368 * @param USARTx: Select the USART or the UART peripheral.
\r
369 * This parameter can be one of the following values:
\r
370 * USART1, USART2, USART3, UART4 or UART5.
\r
371 * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
\r
372 * This parameter can be one of the following values:
\r
373 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
\r
374 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
375 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
\r
376 * @arg USART_IT_TC: Transmission complete interrupt
\r
377 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
\r
378 * @arg USART_IT_IDLE: Idle line detection interrupt
\r
379 * @arg USART_IT_PE: Parity Error interrupt
\r
380 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
\r
381 * @param NewState: new state of the specified USARTx interrupts.
\r
382 * This parameter can be: ENABLE or DISABLE.
\r
385 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
\r
387 uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
\r
388 uint32_t usartxbase = 0x00;
\r
389 /* Check the parameters */
\r
390 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
391 assert_param(IS_USART_CONFIG_IT(USART_IT));
\r
392 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
393 /* The CTS interrupt is not available for UART4 and UART5 */
\r
394 if (USART_IT == USART_IT_CTS)
\r
396 assert_param(IS_USART_123_PERIPH(USARTx));
\r
399 usartxbase = (uint32_t)USARTx;
\r
401 /* Get the USART register index */
\r
402 usartreg = (((uint8_t)USART_IT) >> 0x05);
\r
404 /* Get the interrupt position */
\r
405 itpos = USART_IT & IT_Mask;
\r
406 itmask = (((uint32_t)0x01) << itpos);
\r
408 if (usartreg == 0x01) /* The IT is in CR1 register */
\r
410 usartxbase += 0x0C;
\r
412 else if (usartreg == 0x02) /* The IT is in CR2 register */
\r
414 usartxbase += 0x10;
\r
416 else /* The IT is in CR3 register */
\r
418 usartxbase += 0x14;
\r
420 if (NewState != DISABLE)
\r
422 *(__IO uint32_t*)usartxbase |= itmask;
\r
426 *(__IO uint32_t*)usartxbase &= ~itmask;
\r
431 * @brief Enables or disables the USART
\92s DMA interface.
\r
432 * @param USARTx: Select the USART or the UART peripheral.
\r
433 * This parameter can be one of the following values:
\r
434 * USART1, USART2, USART3 or UART4.
\r
435 * @param USART_DMAReq: specifies the DMA request.
\r
436 * This parameter can be any combination of the following values:
\r
437 * @arg USART_DMAReq_Tx: USART DMA transmit request
\r
438 * @arg USART_DMAReq_Rx: USART DMA receive request
\r
439 * @param NewState: new state of the DMA Request sources.
\r
440 * This parameter can be: ENABLE or DISABLE.
\r
441 * @note The DMA mode is not available for UART5.
\r
444 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
\r
446 /* Check the parameters */
\r
447 assert_param(IS_USART_1234_PERIPH(USARTx));
\r
448 assert_param(IS_USART_DMAREQ(USART_DMAReq));
\r
449 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
450 if (NewState != DISABLE)
\r
452 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
\r
453 DMAR bits in the USART CR3 register */
\r
454 USARTx->CR3 |= USART_DMAReq;
\r
458 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
\r
459 DMAR bits in the USART CR3 register */
\r
460 USARTx->CR3 &= (uint16_t)~USART_DMAReq;
\r
465 * @brief Sets the address of the USART node.
\r
466 * @param USARTx: Select the USART or the UART peripheral.
\r
467 * This parameter can be one of the following values:
\r
468 * USART1, USART2, USART3, UART4 or UART5.
\r
469 * @param USART_Address: Indicates the address of the USART node.
\r
472 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
\r
474 /* Check the parameters */
\r
475 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
476 assert_param(IS_USART_ADDRESS(USART_Address));
\r
478 /* Clear the USART address */
\r
479 USARTx->CR2 &= CR2_Address_Mask;
\r
480 /* Set the USART address node */
\r
481 USARTx->CR2 |= USART_Address;
\r
485 * @brief Selects the USART WakeUp method.
\r
486 * @param USARTx: Select the USART or the UART peripheral.
\r
487 * This parameter can be one of the following values:
\r
488 * USART1, USART2, USART3, UART4 or UART5.
\r
489 * @param USART_WakeUp: specifies the USART wakeup method.
\r
490 * This parameter can be one of the following values:
\r
491 * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
\r
492 * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
\r
495 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
\r
497 /* Check the parameters */
\r
498 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
499 assert_param(IS_USART_WAKEUP(USART_WakeUp));
\r
501 USARTx->CR1 &= CR1_WAKE_Mask;
\r
502 USARTx->CR1 |= USART_WakeUp;
\r
506 * @brief Determines if the USART is in mute mode or not.
\r
507 * @param USARTx: Select the USART or the UART peripheral.
\r
508 * This parameter can be one of the following values:
\r
509 * USART1, USART2, USART3, UART4 or UART5.
\r
510 * @param NewState: new state of the USART mute mode.
\r
511 * This parameter can be: ENABLE or DISABLE.
\r
514 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
516 /* Check the parameters */
\r
517 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
518 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
520 if (NewState != DISABLE)
\r
522 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
\r
523 USARTx->CR1 |= CR1_RWU_Set;
\r
527 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
\r
528 USARTx->CR1 &= CR1_RWU_Reset;
\r
533 * @brief Sets the USART LIN Break detection length.
\r
534 * @param USARTx: Select the USART or the UART peripheral.
\r
535 * This parameter can be one of the following values:
\r
536 * USART1, USART2, USART3, UART4 or UART5.
\r
537 * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
\r
538 * This parameter can be one of the following values:
\r
539 * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
\r
540 * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
\r
543 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
\r
545 /* Check the parameters */
\r
546 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
547 assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
\r
549 USARTx->CR2 &= CR2_LBDL_Mask;
\r
550 USARTx->CR2 |= USART_LINBreakDetectLength;
\r
554 * @brief Enables or disables the USART
\92s LIN mode.
\r
555 * @param USARTx: Select the USART or the UART peripheral.
\r
556 * This parameter can be one of the following values:
\r
557 * USART1, USART2, USART3, UART4 or UART5.
\r
558 * @param NewState: new state of the USART LIN mode.
\r
559 * This parameter can be: ENABLE or DISABLE.
\r
562 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
564 /* Check the parameters */
\r
565 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
566 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
568 if (NewState != DISABLE)
\r
570 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
\r
571 USARTx->CR2 |= CR2_LINEN_Set;
\r
575 /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
\r
576 USARTx->CR2 &= CR2_LINEN_Reset;
\r
581 * @brief Transmits single data through the USARTx peripheral.
\r
582 * @param USARTx: Select the USART or the UART peripheral.
\r
583 * This parameter can be one of the following values:
\r
584 * USART1, USART2, USART3, UART4 or UART5.
\r
585 * @param Data: the data to transmit.
\r
588 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
\r
590 /* Check the parameters */
\r
591 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
592 assert_param(IS_USART_DATA(Data));
\r
594 /* Transmit Data */
\r
595 USARTx->DR = (Data & (uint16_t)0x01FF);
\r
599 * @brief Returns the most recent received data by the USARTx peripheral.
\r
600 * @param USARTx: Select the USART or the UART peripheral.
\r
601 * This parameter can be one of the following values:
\r
602 * USART1, USART2, USART3, UART4 or UART5.
\r
603 * @retval The received data.
\r
605 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
\r
607 /* Check the parameters */
\r
608 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
611 return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
\r
615 * @brief Transmits break characters.
\r
616 * @param USARTx: Select the USART or the UART peripheral.
\r
617 * This parameter can be one of the following values:
\r
618 * USART1, USART2, USART3, UART4 or UART5.
\r
621 void USART_SendBreak(USART_TypeDef* USARTx)
\r
623 /* Check the parameters */
\r
624 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
626 /* Send break characters */
\r
627 USARTx->CR1 |= CR1_SBK_Set;
\r
631 * @brief Sets the specified USART guard time.
\r
632 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
\r
633 * @param USART_GuardTime: specifies the guard time.
\r
634 * @note The guard time bits are not available for UART4 and UART5.
\r
637 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
\r
639 /* Check the parameters */
\r
640 assert_param(IS_USART_123_PERIPH(USARTx));
\r
642 /* Clear the USART Guard time */
\r
643 USARTx->GTPR &= GTPR_LSB_Mask;
\r
644 /* Set the USART guard time */
\r
645 USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
\r
649 * @brief Sets the system clock prescaler.
\r
650 * @param USARTx: Select the USART or the UART peripheral.
\r
651 * This parameter can be one of the following values:
\r
652 * USART1, USART2, USART3, UART4 or UART5.
\r
653 * @param USART_Prescaler: specifies the prescaler clock.
\r
654 * @note The function is used for IrDA mode with UART4 and UART5.
\r
657 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
\r
659 /* Check the parameters */
\r
660 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
662 /* Clear the USART prescaler */
\r
663 USARTx->GTPR &= GTPR_MSB_Mask;
\r
664 /* Set the USART prescaler */
\r
665 USARTx->GTPR |= USART_Prescaler;
\r
669 * @brief Enables or disables the USART
\92s Smart Card mode.
\r
670 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
\r
671 * @param NewState: new state of the Smart Card mode.
\r
672 * This parameter can be: ENABLE or DISABLE.
\r
673 * @note The Smart Card mode is not available for UART4 and UART5.
\r
676 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
678 /* Check the parameters */
\r
679 assert_param(IS_USART_123_PERIPH(USARTx));
\r
680 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
681 if (NewState != DISABLE)
\r
683 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
\r
684 USARTx->CR3 |= CR3_SCEN_Set;
\r
688 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
\r
689 USARTx->CR3 &= CR3_SCEN_Reset;
\r
694 * @brief Enables or disables NACK transmission.
\r
695 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
\r
696 * @param NewState: new state of the NACK transmission.
\r
697 * This parameter can be: ENABLE or DISABLE.
\r
698 * @note The Smart Card mode is not available for UART4 and UART5.
\r
701 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
703 /* Check the parameters */
\r
704 assert_param(IS_USART_123_PERIPH(USARTx));
\r
705 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
706 if (NewState != DISABLE)
\r
708 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
\r
709 USARTx->CR3 |= CR3_NACK_Set;
\r
713 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
\r
714 USARTx->CR3 &= CR3_NACK_Reset;
\r
719 * @brief Enables or disables the USART
\92s Half Duplex communication.
\r
720 * @param USARTx: Select the USART or the UART peripheral.
\r
721 * This parameter can be one of the following values:
\r
722 * USART1, USART2, USART3, UART4 or UART5.
\r
723 * @param NewState: new state of the USART Communication.
\r
724 * This parameter can be: ENABLE or DISABLE.
\r
727 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
729 /* Check the parameters */
\r
730 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
731 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
733 if (NewState != DISABLE)
\r
735 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
\r
736 USARTx->CR3 |= CR3_HDSEL_Set;
\r
740 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
\r
741 USARTx->CR3 &= CR3_HDSEL_Reset;
\r
747 * @brief Enables or disables the USART's 8x oversampling mode.
\r
748 * @param USARTx: Select the USART or the UART peripheral.
\r
749 * This parameter can be one of the following values:
\r
750 * USART1, USART2, USART3, UART4 or UART5.
\r
751 * @param NewState: new state of the USART one bit sampling methode.
\r
752 * This parameter can be: ENABLE or DISABLE.
\r
754 * This function has to be called before calling USART_Init()
\r
755 * function in order to have correct baudrate Divider value.
\r
758 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
760 /* Check the parameters */
\r
761 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
762 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
764 if (NewState != DISABLE)
\r
766 /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
\r
767 USARTx->CR1 |= CR1_OVER8_Set;
\r
771 /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
\r
772 USARTx->CR1 &= CR1_OVER8_Reset;
\r
777 * @brief Enables or disables the USART's one bit sampling methode.
\r
778 * @param USARTx: Select the USART or the UART peripheral.
\r
779 * This parameter can be one of the following values:
\r
780 * USART1, USART2, USART3, UART4 or UART5.
\r
781 * @param NewState: new state of the USART one bit sampling methode.
\r
782 * This parameter can be: ENABLE or DISABLE.
\r
785 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
787 /* Check the parameters */
\r
788 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
789 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
791 if (NewState != DISABLE)
\r
793 /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
\r
794 USARTx->CR3 |= CR3_ONEBITE_Set;
\r
798 /* Disable tthe one bit method by clearing the ONEBITE bit in the CR3 register */
\r
799 USARTx->CR3 &= CR3_ONEBITE_Reset;
\r
804 * @brief Configures the USART
\92s IrDA interface.
\r
805 * @param USARTx: Select the USART or the UART peripheral.
\r
806 * This parameter can be one of the following values:
\r
807 * USART1, USART2, USART3, UART4 or UART5.
\r
808 * @param USART_IrDAMode: specifies the IrDA mode.
\r
809 * This parameter can be one of the following values:
\r
810 * @arg USART_IrDAMode_LowPower
\r
811 * @arg USART_IrDAMode_Normal
\r
814 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
\r
816 /* Check the parameters */
\r
817 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
818 assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
\r
820 USARTx->CR3 &= CR3_IRLP_Mask;
\r
821 USARTx->CR3 |= USART_IrDAMode;
\r
825 * @brief Enables or disables the USART
\92s IrDA interface.
\r
826 * @param USARTx: Select the USART or the UART peripheral.
\r
827 * This parameter can be one of the following values:
\r
828 * USART1, USART2, USART3, UART4 or UART5.
\r
829 * @param NewState: new state of the IrDA mode.
\r
830 * This parameter can be: ENABLE or DISABLE.
\r
833 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
835 /* Check the parameters */
\r
836 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
837 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
839 if (NewState != DISABLE)
\r
841 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
\r
842 USARTx->CR3 |= CR3_IREN_Set;
\r
846 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
\r
847 USARTx->CR3 &= CR3_IREN_Reset;
\r
852 * @brief Checks whether the specified USART flag is set or not.
\r
853 * @param USARTx: Select the USART or the UART peripheral.
\r
854 * This parameter can be one of the following values:
\r
855 * USART1, USART2, USART3, UART4 or UART5.
\r
856 * @param USART_FLAG: specifies the flag to check.
\r
857 * This parameter can be one of the following values:
\r
858 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
\r
859 * @arg USART_FLAG_LBD: LIN Break detection flag
\r
860 * @arg USART_FLAG_TXE: Transmit data register empty flag
\r
861 * @arg USART_FLAG_TC: Transmission Complete flag
\r
862 * @arg USART_FLAG_RXNE: Receive data register not empty flag
\r
863 * @arg USART_FLAG_IDLE: Idle Line detection flag
\r
864 * @arg USART_FLAG_ORE: OverRun Error flag
\r
865 * @arg USART_FLAG_NE: Noise Error flag
\r
866 * @arg USART_FLAG_FE: Framing Error flag
\r
867 * @arg USART_FLAG_PE: Parity Error flag
\r
868 * @retval The new state of USART_FLAG (SET or RESET).
\r
870 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
\r
872 FlagStatus bitstatus = RESET;
\r
873 /* Check the parameters */
\r
874 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
875 assert_param(IS_USART_FLAG(USART_FLAG));
\r
876 /* The CTS flag is not available for UART4 and UART5 */
\r
877 if (USART_FLAG == USART_FLAG_CTS)
\r
879 assert_param(IS_USART_123_PERIPH(USARTx));
\r
882 if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
\r
894 * @brief Clears the USARTx's pending flags.
\r
895 * @param USARTx: Select the USART or the UART peripheral.
\r
896 * This parameter can be one of the following values:
\r
897 * USART1, USART2, USART3, UART4 or UART5.
\r
898 * @param USART_FLAG: specifies the flag to clear.
\r
899 * This parameter can be any combination of the following values:
\r
900 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
\r
901 * @arg USART_FLAG_LBD: LIN Break detection flag.
\r
902 * @arg USART_FLAG_TC: Transmission Complete flag.
\r
903 * @arg USART_FLAG_RXNE: Receive data register not empty flag.
\r
906 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
\r
907 * error) and IDLE (Idle line detected) flags are cleared by software
\r
908 * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
\r
909 * followed by a read operation to USART_DR register (USART_ReceiveData()).
\r
910 * - RXNE flag can be also cleared by a read to the USART_DR register
\r
911 * (USART_ReceiveData()).
\r
912 * - TC flag can be also cleared by software sequence: a read operation to
\r
913 * USART_SR register (USART_GetFlagStatus()) followed by a write operation
\r
914 * to USART_DR register (USART_SendData()).
\r
915 * - TXE flag is cleared only by a write to the USART_DR register
\r
916 * (USART_SendData()).
\r
919 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
\r
921 /* Check the parameters */
\r
922 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
923 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
\r
924 /* The CTS flag is not available for UART4 and UART5 */
\r
925 if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
\r
927 assert_param(IS_USART_123_PERIPH(USARTx));
\r
930 USARTx->SR = (uint16_t)~USART_FLAG;
\r
934 * @brief Checks whether the specified USART interrupt has occurred or not.
\r
935 * @param USARTx: Select the USART or the UART peripheral.
\r
936 * This parameter can be one of the following values:
\r
937 * USART1, USART2, USART3, UART4 or UART5.
\r
938 * @param USART_IT: specifies the USART interrupt source to check.
\r
939 * This parameter can be one of the following values:
\r
940 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
\r
941 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
942 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
\r
943 * @arg USART_IT_TC: Transmission complete interrupt
\r
944 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
\r
945 * @arg USART_IT_IDLE: Idle line detection interrupt
\r
946 * @arg USART_IT_ORE: OverRun Error interrupt
\r
947 * @arg USART_IT_NE: Noise Error interrupt
\r
948 * @arg USART_IT_FE: Framing Error interrupt
\r
949 * @arg USART_IT_PE: Parity Error interrupt
\r
950 * @retval The new state of USART_IT (SET or RESET).
\r
952 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
\r
954 uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
\r
955 ITStatus bitstatus = RESET;
\r
956 /* Check the parameters */
\r
957 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
958 assert_param(IS_USART_GET_IT(USART_IT));
\r
959 /* The CTS interrupt is not available for UART4 and UART5 */
\r
960 if (USART_IT == USART_IT_CTS)
\r
962 assert_param(IS_USART_123_PERIPH(USARTx));
\r
965 /* Get the USART register index */
\r
966 usartreg = (((uint8_t)USART_IT) >> 0x05);
\r
967 /* Get the interrupt position */
\r
968 itmask = USART_IT & IT_Mask;
\r
969 itmask = (uint32_t)0x01 << itmask;
\r
971 if (usartreg == 0x01) /* The IT is in CR1 register */
\r
973 itmask &= USARTx->CR1;
\r
975 else if (usartreg == 0x02) /* The IT is in CR2 register */
\r
977 itmask &= USARTx->CR2;
\r
979 else /* The IT is in CR3 register */
\r
981 itmask &= USARTx->CR3;
\r
984 bitpos = USART_IT >> 0x08;
\r
985 bitpos = (uint32_t)0x01 << bitpos;
\r
986 bitpos &= USARTx->SR;
\r
987 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
\r
1000 * @brief Clears the USARTx
\92s interrupt pending bits.
\r
1001 * @param USARTx: Select the USART or the UART peripheral.
\r
1002 * This parameter can be one of the following values:
\r
1003 * USART1, USART2, USART3, UART4 or UART5.
\r
1004 * @param USART_IT: specifies the interrupt pending bit to clear.
\r
1005 * This parameter can be one of the following values:
\r
1006 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
\r
1007 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
1008 * @arg USART_IT_TC: Transmission complete interrupt.
\r
1009 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
\r
1012 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
\r
1013 * error) and IDLE (Idle line detected) pending bits are cleared by
\r
1014 * software sequence: a read operation to USART_SR register
\r
1015 * (USART_GetITStatus()) followed by a read operation to USART_DR register
\r
1016 * (USART_ReceiveData()).
\r
1017 * - RXNE pending bit can be also cleared by a read to the USART_DR register
\r
1018 * (USART_ReceiveData()).
\r
1019 * - TC pending bit can be also cleared by software sequence: a read
\r
1020 * operation to USART_SR register (USART_GetITStatus()) followed by a write
\r
1021 * operation to USART_DR register (USART_SendData()).
\r
1022 * - TXE pending bit is cleared only by a write to the USART_DR register
\r
1023 * (USART_SendData()).
\r
1026 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
\r
1028 uint16_t bitpos = 0x00, itmask = 0x00;
\r
1029 /* Check the parameters */
\r
1030 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
1031 assert_param(IS_USART_CLEAR_IT(USART_IT));
\r
1032 /* The CTS interrupt is not available for UART4 and UART5 */
\r
1033 if (USART_IT == USART_IT_CTS)
\r
1035 assert_param(IS_USART_123_PERIPH(USARTx));
\r
1038 bitpos = USART_IT >> 0x08;
\r
1039 itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
\r
1040 USARTx->SR = (uint16_t)~itmask;
\r
1054 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
\r