2 ******************************************************************************
\r
3 * @file stm32l1xx_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 "stm32l1xx_usart.h"
\r
23 #include "stm32l1xx_rcc.h"
\r
25 /** @addtogroup STM32L1xx_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 /*!< USART CR1 register clear Mask ((~(uint16_t)0xE9F3)) */
\r
47 #define CR1_CLEAR_MASK ((uint16_t)(USART_CR1_M | USART_CR1_PCE | \
\r
48 USART_CR1_PS | USART_CR1_TE | \
\r
51 /*!< USART CR2 register clock bits clear Mask ((~(uint16_t)0xF0FF)) */
\r
52 #define CR2_CLOCK_CLEAR_MASK ((uint16_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
\r
53 USART_CR2_CPHA | USART_CR2_LBCL))
\r
55 /*!< USART CR3 register clear Mask ((~(uint16_t)0xFCFF)) */
\r
56 #define CR3_CLEAR_MASK ((uint16_t)(USART_CR3_RTSE | USART_CR3_CTSE))
\r
58 /*!< USART Interrupts mask */
\r
59 #define IT_MASK ((uint16_t)0x001F)
\r
65 /** @defgroup USART_Private_Macros
\r
73 /** @defgroup USART_Private_Variables
\r
81 /** @defgroup USART_Private_FunctionPrototypes
\r
89 /** @defgroup USART_Private_Functions
\r
94 * @brief Deinitializes the USARTx peripheral registers to their default reset values.
\r
95 * @param USARTx: Select the USART peripheral.
\r
96 * This parameter can be one of the following values: USART1, USART2 or USART3.
\r
99 void USART_DeInit(USART_TypeDef* USARTx)
\r
101 /* Check the parameters */
\r
102 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
104 if (USARTx == USART1)
\r
106 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
\r
107 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
\r
109 else if (USARTx == USART2)
\r
111 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
\r
112 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
\r
116 if (USARTx == USART3)
\r
118 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
\r
119 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
\r
125 * @brief Initializes the USARTx peripheral according to the specified
\r
126 * parameters in the USART_InitStruct .
\r
127 * @param USARTx: Select the USART peripheral.
\r
128 * This parameter can be one of the following values:
\r
129 * USART1, USART2 or USART3.
\r
130 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
\r
131 * that contains the configuration information for the specified USART peripheral.
\r
134 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
\r
136 uint32_t tmpreg = 0x00, apbclock = 0x00;
\r
137 uint32_t integerdivider = 0x00;
\r
138 uint32_t fractionaldivider = 0x00;
\r
139 RCC_ClocksTypeDef RCC_ClocksStatus;
\r
141 /* Check the parameters */
\r
142 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
143 assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
\r
144 assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
\r
145 assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
\r
146 assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
\r
147 assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
\r
148 assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
\r
150 /*---------------------------- USART CR2 Configuration -----------------------*/
\r
151 tmpreg = USARTx->CR2;
\r
152 /* Clear STOP[13:12] bits */
\r
153 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
\r
155 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
\r
156 /* Set STOP[13:12] bits according to USART_StopBits value */
\r
157 tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
\r
159 /* Write to USART CR2 */
\r
160 USARTx->CR2 = (uint16_t)tmpreg;
\r
162 /*---------------------------- USART CR1 Configuration -----------------------*/
\r
163 tmpreg = USARTx->CR1;
\r
164 /* Clear M, PCE, PS, TE and RE bits */
\r
165 tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);
\r
167 /* Configure the USART Word Length, Parity and mode ----------------------- */
\r
168 /* Set the M bits according to USART_WordLength value */
\r
169 /* Set PCE and PS bits according to USART_Parity value */
\r
170 /* Set TE and RE bits according to USART_Mode value */
\r
171 tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
\r
172 USART_InitStruct->USART_Mode;
\r
174 /* Write to USART CR1 */
\r
175 USARTx->CR1 = (uint16_t)tmpreg;
\r
177 /*---------------------------- USART CR3 Configuration -----------------------*/
\r
178 tmpreg = USARTx->CR3;
\r
179 /* Clear CTSE and RTSE bits */
\r
180 tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);
\r
182 /* Configure the USART HFC -------------------------------------------------*/
\r
183 /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
\r
184 tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
\r
186 /* Write to USART CR3 */
\r
187 USARTx->CR3 = (uint16_t)tmpreg;
\r
189 /*---------------------------- USART BRR Configuration -----------------------*/
\r
190 /* Configure the USART Baud Rate -------------------------------------------*/
\r
191 RCC_GetClocksFreq(&RCC_ClocksStatus);
\r
192 if (USARTx == USART1)
\r
194 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
\r
198 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
\r
201 /* Determine the integer part */
\r
202 if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
\r
204 /* Integer part computing in case Oversampling mode is 8 Samples */
\r
205 integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
\r
207 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
\r
209 /* Integer part computing in case Oversampling mode is 16 Samples */
\r
210 integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
\r
212 tmpreg = (integerdivider / 100) << 4;
\r
214 /* Determine the fractional part */
\r
215 fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
\r
217 /* Implement the fractional part in the register */
\r
218 if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
\r
220 tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
\r
222 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
\r
224 tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
\r
227 /* Write to USART BRR */
\r
228 USARTx->BRR = (uint16_t)tmpreg;
\r
232 * @brief Fills each USART_InitStruct member with its default value.
\r
233 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
\r
234 * which will be initialized.
\r
237 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
\r
239 /* USART_InitStruct members default value */
\r
240 USART_InitStruct->USART_BaudRate = 9600;
\r
241 USART_InitStruct->USART_WordLength = USART_WordLength_8b;
\r
242 USART_InitStruct->USART_StopBits = USART_StopBits_1;
\r
243 USART_InitStruct->USART_Parity = USART_Parity_No ;
\r
244 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
\r
245 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
\r
249 * @brief Initializes the USARTx peripheral Clock according to the
\r
250 * specified parameters in the USART_ClockInitStruct .
\r
251 * @param USARTx: where x can be 1, 2, 3 to select the USART peripheral.
\r
252 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
\r
253 * structure that contains the configuration information for the specified
\r
254 * USART peripheral.
\r
257 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
\r
259 uint32_t tmpreg = 0x00;
\r
260 /* Check the parameters */
\r
261 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
262 assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
\r
263 assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
\r
264 assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
\r
265 assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
\r
267 /*---------------------------- USART CR2 Configuration -----------------------*/
\r
268 tmpreg = USARTx->CR2;
\r
269 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
\r
270 tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);
\r
271 /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
\r
272 /* Set CLKEN bit according to USART_Clock value */
\r
273 /* Set CPOL bit according to USART_CPOL value */
\r
274 /* Set CPHA bit according to USART_CPHA value */
\r
275 /* Set LBCL bit according to USART_LastBit value */
\r
276 tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
\r
277 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
\r
278 /* Write to USART CR2 */
\r
279 USARTx->CR2 = (uint16_t)tmpreg;
\r
283 * @brief Fills each USART_ClockInitStruct member with its default value.
\r
284 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
\r
285 * structure which will be initialized.
\r
288 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
\r
290 /* USART_ClockInitStruct members default value */
\r
291 USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
\r
292 USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
\r
293 USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
\r
294 USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
\r
298 * @brief Enables or disables the specified USART peripheral.
\r
299 * @param USARTx: Select the USART peripheral.
\r
300 * This parameter can be one of the following values:
\r
301 * USART1, USART2 or USART3.
\r
302 * @param NewState: new state of the USARTx peripheral.
\r
303 * This parameter can be: ENABLE or DISABLE.
\r
306 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
308 /* Check the parameters */
\r
309 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
310 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
312 if (NewState != DISABLE)
\r
314 /* Enable the selected USART by setting the UE bit in the CR1 register */
\r
315 USARTx->CR1 |= USART_CR1_UE;
\r
319 /* Disable the selected USART by clearing the UE bit in the CR1 register */
\r
320 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_UE);
\r
325 * @brief Enables or disables the specified USART interrupts.
\r
326 * @param USARTx: Select the USART peripheral.
\r
327 * This parameter can be one of the following values:
\r
328 * USART1, USART2 or USART3.
\r
329 * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
\r
330 * This parameter can be one of the following values:
\r
331 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
\r
332 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
333 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
\r
334 * @arg USART_IT_TC: Transmission complete interrupt
\r
335 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
\r
336 * @arg USART_IT_IDLE: Idle line detection interrupt
\r
337 * @arg USART_IT_PE: Parity Error interrupt
\r
338 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
\r
339 * @param NewState: new state of the specified USARTx interrupts.
\r
340 * This parameter can be: ENABLE or DISABLE.
\r
343 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
\r
345 uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
\r
346 uint32_t usartxbase = 0x00;
\r
347 /* Check the parameters */
\r
348 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
349 assert_param(IS_USART_CONFIG_IT(USART_IT));
\r
350 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
352 usartxbase = (uint32_t)USARTx;
\r
354 /* Get the USART register index */
\r
355 usartreg = (((uint8_t)USART_IT) >> 0x05);
\r
357 /* Get the interrupt position */
\r
358 itpos = USART_IT & IT_MASK;
\r
359 itmask = (((uint32_t)0x01) << itpos);
\r
361 if (usartreg == 0x01) /* The IT is in CR1 register */
\r
363 usartxbase += 0x0C;
\r
365 else if (usartreg == 0x02) /* The IT is in CR2 register */
\r
367 usartxbase += 0x10;
\r
369 else /* The IT is in CR3 register */
\r
371 usartxbase += 0x14;
\r
373 if (NewState != DISABLE)
\r
375 *(__IO uint32_t*)usartxbase |= itmask;
\r
379 *(__IO uint32_t*)usartxbase &= ~itmask;
\r
384 * @brief Enables or disables the USART
\92s DMA interface.
\r
385 * @param USARTx: Select the USART peripheral.
\r
386 * This parameter can be one of the following values:
\r
387 * USART1, USART2 or USART3.
\r
388 * @param USART_DMAReq: specifies the DMA request.
\r
389 * This parameter can be any combination of the following values:
\r
390 * @arg USART_DMAReq_Tx: USART DMA transmit request
\r
391 * @arg USART_DMAReq_Rx: USART DMA receive request
\r
392 * @param NewState: new state of the DMA Request sources.
\r
393 * This parameter can be: ENABLE or DISABLE.
\r
394 * @note The DMA mode is not available for UART5.
\r
397 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
\r
399 /* Check the parameters */
\r
400 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
401 assert_param(IS_USART_DMAREQ(USART_DMAReq));
\r
402 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
404 if (NewState != DISABLE)
\r
406 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
\r
407 DMAR bits in the USART CR3 register */
\r
408 USARTx->CR3 |= USART_DMAReq;
\r
412 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
\r
413 DMAR bits in the USART CR3 register */
\r
414 USARTx->CR3 &= (uint16_t)~USART_DMAReq;
\r
419 * @brief Sets the address of the USART node.
\r
420 * @param USARTx: Select the USART peripheral.
\r
421 * This parameter can be one of the following values:
\r
422 * USART1, USART2 or USART3.
\r
423 * @param USART_Address: Indicates the address of the USART node.
\r
426 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
\r
428 /* Check the parameters */
\r
429 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
430 assert_param(IS_USART_ADDRESS(USART_Address));
\r
432 /* Clear the USART address */
\r
433 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_ADD);
\r
434 /* Set the USART address node */
\r
435 USARTx->CR2 |= USART_Address;
\r
439 * @brief Selects the USART WakeUp method.
\r
440 * @param USARTx: Select the USART peripheral.
\r
441 * This parameter can be one of the following values:
\r
442 * USART1, USART2 or USART3.
\r
443 * @param USART_WakeUp: specifies the USART wakeup method.
\r
444 * This parameter can be one of the following values:
\r
445 * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
\r
446 * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
\r
449 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
\r
451 /* Check the parameters */
\r
452 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
453 assert_param(IS_USART_WAKEUP(USART_WakeUp));
\r
455 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_WAKE);
\r
456 USARTx->CR1 |= USART_WakeUp;
\r
460 * @brief Determines if the USART is in mute mode or not.
\r
461 * @param USARTx: Select the USART peripheral.
\r
462 * This parameter can be one of the following values:
\r
463 * USART1, USART2 or USART3.
\r
464 * @param NewState: new state of the USART mute mode.
\r
465 * This parameter can be: ENABLE or DISABLE.
\r
468 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
470 /* Check the parameters */
\r
471 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
472 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
474 if (NewState != DISABLE)
\r
476 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
\r
477 USARTx->CR1 |= USART_CR1_RWU;
\r
481 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
\r
482 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_RWU);
\r
487 * @brief Sets the USART LIN Break detection length.
\r
488 * @param USARTx: Select the USART peripheral.
\r
489 * This parameter can be one of the following values:
\r
490 * USART1, USART2 or USART3.
\r
491 * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
\r
492 * This parameter can be one of the following values:
\r
493 * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
\r
494 * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
\r
497 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
\r
499 /* Check the parameters */
\r
500 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
501 assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
\r
503 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LBDL);
\r
504 USARTx->CR2 |= USART_LINBreakDetectLength;
\r
508 * @brief Enables or disables the USART
\92s LIN mode.
\r
509 * @param USARTx: Select the USART peripheral.
\r
510 * This parameter can be one of the following values:
\r
511 * USART1, USART2 or USART3.
\r
512 * @param NewState: new state of the USART LIN mode.
\r
513 * This parameter can be: ENABLE or DISABLE.
\r
516 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
518 /* Check the parameters */
\r
519 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
520 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
522 if (NewState != DISABLE)
\r
524 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
\r
525 USARTx->CR2 |= USART_CR2_LINEN;
\r
529 /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
\r
530 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LINEN);
\r
535 * @brief Transmits single data through the USARTx peripheral.
\r
536 * @param USARTx: Select the USART peripheral.
\r
537 * This parameter can be one of the following values:
\r
538 * USART1, USART2 or USART3.
\r
539 * @param Data: the data to transmit.
\r
542 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
\r
544 /* Check the parameters */
\r
545 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
546 assert_param(IS_USART_DATA(Data));
\r
548 /* Transmit Data */
\r
549 USARTx->DR = (Data & (uint16_t)0x01FF);
\r
553 * @brief Returns the most recent received data by the USARTx peripheral.
\r
554 * @param USARTx: Select the USART peripheral.
\r
555 * This parameter can be one of the following values:
\r
556 * USART1, USART2 or USART3.
\r
557 * @retval The received data.
\r
559 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
\r
561 /* Check the parameters */
\r
562 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
565 return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
\r
569 * @brief Transmits break characters.
\r
570 * @param USARTx: Select the USART peripheral.
\r
571 * This parameter can be one of the following values:
\r
572 * USART1, USART2 or USART3.
\r
575 void USART_SendBreak(USART_TypeDef* USARTx)
\r
577 /* Check the parameters */
\r
578 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
580 /* Send break characters */
\r
581 USARTx->CR1 |= USART_CR1_SBK;
\r
585 * @brief Sets the specified USART guard time.
\r
586 * @param USARTx: Select the USART peripheral.
\r
587 * This parameter can be one of the following values:
\r
588 * USART1, USART2 or USART3.
\r
589 * @param USART_GuardTime: specifies the guard time.
\r
590 * @note The guard time bits are not available for UART4 and UART5.
\r
593 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
\r
595 /* Check the parameters */
\r
596 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
598 /* Clear the USART Guard time */
\r
599 USARTx->GTPR &= USART_GTPR_PSC;
\r
600 /* Set the USART guard time */
\r
601 USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
\r
605 * @brief Sets the system clock prescaler.
\r
606 * @param USARTx: Select the USART peripheral.
\r
607 * This parameter can be one of the following values:
\r
608 * USART1, USART2 or USART3.
\r
609 * @param USART_Prescaler: specifies the prescaler clock.
\r
610 * @note The function is used for IrDA mode with UART4 and UART5.
\r
613 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
\r
615 /* Check the parameters */
\r
616 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
618 /* Clear the USART prescaler */
\r
619 USARTx->GTPR &= USART_GTPR_GT;
\r
620 /* Set the USART prescaler */
\r
621 USARTx->GTPR |= USART_Prescaler;
\r
625 * @brief Enables or disables the USART
\92s Smart Card mode.
\r
626 * @param USARTx: Select the USART peripheral.
\r
627 * This parameter can be one of the following values:
\r
628 * USART1, USART2 or USART3.
\r
629 * @param NewState: new state of the Smart Card mode.
\r
630 * This parameter can be: ENABLE or DISABLE.
\r
631 * @note The Smart Card mode is not available for UART4 and UART5.
\r
634 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
636 /* Check the parameters */
\r
637 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
638 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
639 if (NewState != DISABLE)
\r
641 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
\r
642 USARTx->CR3 |= USART_CR3_SCEN;
\r
646 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
\r
647 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_SCEN);
\r
652 * @brief Enables or disables NACK transmission.
\r
653 * @param USARTx: Select the USART peripheral.
\r
654 * This parameter can be one of the following values:
\r
655 * USART1, USART2 or USART3.
\r
656 * @param NewState: new state of the NACK transmission.
\r
657 * This parameter can be: ENABLE or DISABLE.
\r
658 * @note The Smart Card mode is not available for UART4 and UART5.
\r
661 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
663 /* Check the parameters */
\r
664 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
665 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
666 if (NewState != DISABLE)
\r
668 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
\r
669 USARTx->CR3 |= USART_CR3_NACK;
\r
673 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
\r
674 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_NACK);
\r
679 * @brief Enables or disables the USART
\92s Half Duplex communication.
\r
680 * @param USARTx: Select the USART peripheral.
\r
681 * This parameter can be one of the following values:
\r
682 * USART1, USART2 or USART3.
\r
683 * @param NewState: new state of the USART Communication.
\r
684 * This parameter can be: ENABLE or DISABLE.
\r
687 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
689 /* Check the parameters */
\r
690 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
691 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
693 if (NewState != DISABLE)
\r
695 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
\r
696 USARTx->CR3 |= USART_CR3_HDSEL;
\r
700 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
\r
701 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_HDSEL);
\r
706 * @brief Enables or disables the USART's 8x oversampling mode.
\r
707 * @param USARTx: Select the USART or the UART peripheral.
\r
708 * This parameter can be one of the following values:
\r
709 * USART1, USART2, USART3.
\r
710 * @param NewState: new state of the USART 8x oversampling mode.
\r
711 * This parameter can be: ENABLE or DISABLE.
\r
714 * This function has to be called before calling USART_Init()
\r
715 * function in order to have correct baudrate Divider value.
\r
718 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
720 /* Check the parameters */
\r
721 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
722 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
724 if (NewState != DISABLE)
\r
726 /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
\r
727 USARTx->CR1 |= USART_CR1_OVER8;
\r
731 /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
\r
732 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_OVER8);
\r
737 * @brief Enables or disables the USART's one bit sampling methode.
\r
738 * @param USARTx: Select the USART or the UART peripheral.
\r
739 * This parameter can be one of the following values:
\r
740 * USART1, USART2, USART3.
\r
741 * @param NewState: new state of the USART one bit sampling methode.
\r
742 * This parameter can be: ENABLE or DISABLE.
\r
745 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
747 /* Check the parameters */
\r
748 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
749 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
751 if (NewState != DISABLE)
\r
753 /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
\r
754 USARTx->CR3 |= USART_CR3_ONEBIT;
\r
758 /* Disable tthe one bit method by clearing the ONEBITE bit in the CR3 register */
\r
759 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT);
\r
764 * @brief Configures the USART
\92s IrDA interface.
\r
765 * @param USARTx: Select the USART peripheral.
\r
766 * This parameter can be one of the following values:
\r
767 * USART1, USART2 or USART3.
\r
768 * @param USART_IrDAMode: specifies the IrDA mode.
\r
769 * This parameter can be one of the following values:
\r
770 * @arg USART_IrDAMode_LowPower
\r
771 * @arg USART_IrDAMode_Normal
\r
774 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
\r
776 /* Check the parameters */
\r
777 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
778 assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
\r
780 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IRLP);
\r
781 USARTx->CR3 |= USART_IrDAMode;
\r
785 * @brief Enables or disables the USART
\92s IrDA interface.
\r
786 * @param USARTx: Select the USART peripheral.
\r
787 * This parameter can be one of the following values:
\r
788 * USART1, USART2 or USART3.
\r
789 * @param NewState: new state of the IrDA mode.
\r
790 * This parameter can be: ENABLE or DISABLE.
\r
793 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
795 /* Check the parameters */
\r
796 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
797 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
799 if (NewState != DISABLE)
\r
801 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
\r
802 USARTx->CR3 |= USART_CR3_IREN;
\r
806 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
\r
807 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IREN);
\r
812 * @brief Checks whether the specified USART flag is set or not.
\r
813 * @param USARTx: Select the USART peripheral.
\r
814 * This parameter can be one of the following values:
\r
815 * USART1, USART2 or USART3.
\r
816 * @param USART_FLAG: specifies the flag to check.
\r
817 * This parameter can be one of the following values:
\r
818 * @arg USART_FLAG_CTS: CTS Change flag
\r
819 * @arg USART_FLAG_LBD: LIN Break detection flag
\r
820 * @arg USART_FLAG_TXE: Transmit data register empty flag
\r
821 * @arg USART_FLAG_TC: Transmission Complete flag
\r
822 * @arg USART_FLAG_RXNE: Receive data register not empty flag
\r
823 * @arg USART_FLAG_IDLE: Idle Line detection flag
\r
824 * @arg USART_FLAG_ORE: OverRun Error flag
\r
825 * @arg USART_FLAG_NE: Noise Error flag
\r
826 * @arg USART_FLAG_FE: Framing Error flag
\r
827 * @arg USART_FLAG_PE: Parity Error flag
\r
828 * @retval The new state of USART_FLAG (SET or RESET).
\r
830 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
\r
832 FlagStatus bitstatus = RESET;
\r
833 /* Check the parameters */
\r
834 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
835 assert_param(IS_USART_FLAG(USART_FLAG));
\r
837 if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
\r
849 * @brief Clears the USARTx's pending flags.
\r
850 * @param USARTx: Select the USART peripheral.
\r
851 * This parameter can be one of the following values:
\r
852 * USART1, USART2 or USART3.
\r
853 * @param USART_FLAG: specifies the flag to clear.
\r
854 * This parameter can be any combination of the following values:
\r
855 * @arg USART_FLAG_CTS: CTS Change flag.
\r
856 * @arg USART_FLAG_LBD: LIN Break detection flag.
\r
857 * @arg USART_FLAG_TC: Transmission Complete flag.
\r
858 * @arg USART_FLAG_RXNE: Receive data register not empty flag.
\r
861 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
\r
862 * error) and IDLE (Idle line detected) flags are cleared by software
\r
863 * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
\r
864 * followed by a read operation to USART_DR register (USART_ReceiveData()).
\r
865 * - RXNE flag can be also cleared by a read to the USART_DR register
\r
866 * (USART_ReceiveData()).
\r
867 * - TC flag can be also cleared by software sequence: a read operation to
\r
868 * USART_SR register (USART_GetFlagStatus()) followed by a write operation
\r
869 * to USART_DR register (USART_SendData()).
\r
870 * - TXE flag is cleared only by a write to the USART_DR register
\r
871 * (USART_SendData()).
\r
874 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
\r
876 /* Check the parameters */
\r
877 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
878 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
\r
880 USARTx->SR = (uint16_t)~USART_FLAG;
\r
884 * @brief Checks whether the specified USART interrupt has occurred or not.
\r
885 * @param USARTx: Select the USART peripheral.
\r
886 * This parameter can be one of the following values:
\r
887 * USART1, USART2 or USART3.
\r
888 * @param USART_IT: specifies the USART interrupt source to check.
\r
889 * This parameter can be one of the following values:
\r
890 * @arg USART_IT_CTS: CTS change interrupt
\r
891 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
892 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
\r
893 * @arg USART_IT_TC: Transmission complete interrupt
\r
894 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
\r
895 * @arg USART_IT_IDLE: Idle line detection interrupt
\r
896 * @arg USART_IT_ORE: OverRun Error interrupt
\r
897 * @arg USART_IT_NE: Noise Error interrupt
\r
898 * @arg USART_IT_FE: Framing Error interrupt
\r
899 * @arg USART_IT_PE: Parity Error interrupt
\r
900 * @retval The new state of USART_IT (SET or RESET).
\r
902 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
\r
904 uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
\r
905 ITStatus bitstatus = RESET;
\r
906 /* Check the parameters */
\r
907 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
908 assert_param(IS_USART_GET_IT(USART_IT));
\r
910 /* Get the USART register index */
\r
911 usartreg = (((uint8_t)USART_IT) >> 0x05);
\r
912 /* Get the interrupt position */
\r
913 itmask = USART_IT & IT_MASK;
\r
914 itmask = (uint32_t)0x01 << itmask;
\r
916 if (usartreg == 0x01) /* The IT is in CR1 register */
\r
918 itmask &= USARTx->CR1;
\r
920 else if (usartreg == 0x02) /* The IT is in CR2 register */
\r
922 itmask &= USARTx->CR2;
\r
924 else /* The IT is in CR3 register */
\r
926 itmask &= USARTx->CR3;
\r
929 bitpos = USART_IT >> 0x08;
\r
930 bitpos = (uint32_t)0x01 << bitpos;
\r
931 bitpos &= USARTx->SR;
\r
932 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
\r
945 * @brief Clears the USARTx
\92s interrupt pending bits.
\r
946 * @param USARTx: Select the USART peripheral.
\r
947 * This parameter can be one of the following values:
\r
948 * USART1, USART2 or USART3.
\r
949 * @param USART_IT: specifies the interrupt pending bit to clear.
\r
950 * This parameter can be one of the following values:
\r
951 * @arg USART_IT_CTS: CTS change interrupt
\r
952 * @arg USART_IT_LBD: LIN Break detection interrupt
\r
953 * @arg USART_IT_TC: Transmission complete interrupt.
\r
954 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
\r
957 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
\r
958 * error) and IDLE (Idle line detected) pending bits are cleared by
\r
959 * software sequence: a read operation to USART_SR register
\r
960 * (USART_GetITStatus()) followed by a read operation to USART_DR register
\r
961 * (USART_ReceiveData()).
\r
962 * - RXNE pending bit can be also cleared by a read to the USART_DR register
\r
963 * (USART_ReceiveData()).
\r
964 * - TC pending bit can be also cleared by software sequence: a read
\r
965 * operation to USART_SR register (USART_GetITStatus()) followed by a write
\r
966 * operation to USART_DR register (USART_SendData()).
\r
967 * - TXE pending bit is cleared only by a write to the USART_DR register
\r
968 * (USART_SendData()).
\r
971 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
\r
973 uint16_t bitpos = 0x00, itmask = 0x00;
\r
974 /* Check the parameters */
\r
975 assert_param(IS_USART_ALL_PERIPH(USARTx));
\r
976 assert_param(IS_USART_CLEAR_IT(USART_IT));
\r
978 bitpos = USART_IT >> 0x08;
\r
979 itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
\r
980 USARTx->SR = (uint16_t)~itmask;
\r
995 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
\r