1 /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
\r
2 * File Name : stm32f10x_usart.c
\r
3 * Author : MCD Application Team
\r
4 * Date First Issued : 09/29/2006
\r
5 * Description : This file provides all the USART firmware functions.
\r
6 ********************************************************************************
\r
11 ********************************************************************************
\r
12 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
\r
14 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
\r
15 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
\r
16 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
\r
17 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
18 *******************************************************************************/
\r
20 /* Includes ------------------------------------------------------------------*/
\r
21 #include "stm32f10x_usart.h"
\r
22 #include "stm32f10x_rcc.h"
\r
24 /* Private typedef -----------------------------------------------------------*/
\r
25 /* Private define ------------------------------------------------------------*/
\r
26 /* USART RUN Mask */
\r
27 #define CR1_RUN_Set ((u16)0x2000) /* USART Enable Mask */
\r
28 #define CR1_RUN_Reset ((u16)0xDFFF) /* USART Disable Mask */
\r
30 #define CR2_Address_Mask ((u16)0xFFF0) /* USART address Mask */
\r
32 /* USART RWU Mask */
\r
33 #define CR1_RWU_Set ((u16)0x0002) /* USART mute mode Enable Mask */
\r
34 #define CR1_RWU_Reset ((u16)0xFFFD) /* USART mute mode Enable Mask */
\r
36 #define USART_IT_Mask ((u16)0x001F) /* USART Interrupt Mask */
\r
38 /* USART LIN Mask */
\r
39 #define CR2_LINE_Set ((u16)0x4000) /* USART LIN Enable Mask */
\r
40 #define CR2_LINE_Reset ((u16)0xBFFF) /* USART LIN Disable Mask */
\r
42 #define CR1_SBK_Set ((u16)0x0001) /* USART Break Character send Mask */
\r
45 #define CR3_SCEN_Set ((u16)0x0020) /* USART SC Enable Mask */
\r
46 #define CR3_SCEN_Reset ((u16)0xFFDF) /* USART SC Disable Mask */
\r
48 /* USART SC NACK Mask */
\r
49 #define CR3_NACK_Set ((u16)0x0010) /* USART SC NACK Enable Mask */
\r
50 #define CR3_NACK_Reset ((u16)0xFFEF) /* USART SC NACK Disable Mask */
\r
52 /* USART Half-Duplex Mask */
\r
53 #define CR3_HDSEL_Set ((u16)0x0008) /* USART Half-Duplex Enable Mask */
\r
54 #define CR3_HDSEL_Reset ((u16)0xFFF7) /* USART Half-Duplex Disable Mask */
\r
56 /* USART IrDA Mask */
\r
57 #define CR3_IRLP_Mask ((u16)0xFFFB) /* USART IrDA LowPower mode Mask */
\r
59 /* USART LIN Break detection */
\r
60 #define CR3_LBDL_Mask ((u16)0xFFDF) /* USART LIN Break detection Mask */
\r
62 /* USART WakeUp Method */
\r
63 #define CR3_WAKE_Mask ((u16)0xF7FF) /* USART WakeUp Method Mask */
\r
65 /* USART IrDA Mask */
\r
66 #define CR3_IREN_Set ((u16)0x0002) /* USART IrDA Enable Mask */
\r
67 #define CR3_IREN_Reset ((u16)0xFFFD) /* USART IrDA Disable Mask */
\r
69 #define GTPR_LSB_Mask ((u16)0x00FF) /* Guard Time Register LSB Mask */
\r
70 #define GTPR_MSB_Mask ((u16)0xFF00) /* Guard Time Register MSB Mask */
\r
72 #define CR1_CLEAR_Mask ((u16)0xE9F3) /* USART CR1 Mask */
\r
73 #define CR2_CLEAR_Mask ((u16)0xC0FF) /* USART CR2 Mask */
\r
74 #define CR3_CLEAR_Mask ((u16)0xFCFF) /* USART CR3 Mask */
\r
77 /* Private macro -------------------------------------------------------------*/
\r
78 /* Private variables ---------------------------------------------------------*/
\r
79 /* Private function prototypes -----------------------------------------------*/
\r
80 /* Private functions ---------------------------------------------------------*/
\r
82 /*******************************************************************************
\r
83 * Function Name : USART_DeInit
\r
84 * Description : Deinitializes the USARTx peripheral registers to their
\r
85 * default reset values.
\r
86 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
90 *******************************************************************************/
\r
91 void USART_DeInit(USART_TypeDef* USARTx)
\r
93 switch (*(u32*)&USARTx)
\r
96 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
\r
97 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
\r
101 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
\r
102 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
\r
106 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
\r
107 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
\r
115 /*******************************************************************************
\r
116 * Function Name : USART_Init
\r
117 * Description : Initializes the USARTx peripheral according to the specified
\r
118 * parameters in the USART_InitStruct .
\r
119 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
\r
120 * - USART_InitStruct: pointer to a USART_InitTypeDef structure
\r
121 * that contains the configuration information for the
\r
122 * specified USART peripheral.
\r
125 *******************************************************************************/
\r
126 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
\r
128 u32 tmpreg = 0x00, apbclock = 0x00;
\r
129 u32 integerdivider = 0x00;
\r
130 u32 fractionaldivider = 0x00;
\r
131 RCC_ClocksTypeDef RCC_ClocksStatus;
\r
133 /* Check the parameters */
\r
134 assert(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
\r
135 assert(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
\r
136 assert(IS_USART_PARITY(USART_InitStruct->USART_Parity));
\r
137 assert(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
\r
138 assert(IS_USART_MODE(USART_InitStruct->USART_Mode));
\r
139 assert(IS_USART_CLOCK(USART_InitStruct->USART_Clock));
\r
140 assert(IS_USART_CPOL(USART_InitStruct->USART_CPOL));
\r
141 assert(IS_USART_CPHA(USART_InitStruct->USART_CPHA));
\r
142 assert(IS_USART_LASTBIT(USART_InitStruct->USART_LastBit));
\r
144 /*---------------------------- USART CR2 Configuration -----------------------*/
\r
145 tmpreg = USARTx->CR2;
\r
146 /* Clear STOP[13:12], CLKEN, CPOL, CPHA and LBCL bits */
\r
147 tmpreg &= CR2_CLEAR_Mask;
\r
149 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
\r
150 /* Set STOP[13:12] bits according to USART_Mode value */
\r
151 /* Set CPOL bit according to USART_CPOL value */
\r
152 /* Set CPHA bit according to USART_CPHA value */
\r
153 /* Set LBCL bit according to USART_LastBit value */
\r
154 tmpreg |= (u32)USART_InitStruct->USART_StopBits | USART_InitStruct->USART_Clock |
\r
155 USART_InitStruct->USART_CPOL | USART_InitStruct->USART_CPHA |
\r
156 USART_InitStruct->USART_LastBit;
\r
158 /* Write to USART CR2 */
\r
159 USARTx->CR2 = (u16)tmpreg;
\r
161 /*---------------------------- USART CR1 Configuration -----------------------*/
\r
163 tmpreg = USARTx->CR1;
\r
164 /* Clear M, PCE, PS, TE and RE bits */
\r
165 tmpreg &= 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 |= (u32)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
\r
172 USART_InitStruct->USART_Mode;
\r
174 /* Write to USART CR1 */
\r
175 USARTx->CR1 = (u16)tmpreg;
\r
177 /*---------------------------- USART CR3 Configuration -----------------------*/
\r
179 tmpreg = USARTx->CR3;
\r
180 /* Clear CTSE and RTSE bits */
\r
181 tmpreg &= CR3_CLEAR_Mask;
\r
183 /* Configure the USART HFC -------------------------------------------------*/
\r
184 /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
\r
185 tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
\r
187 /* Write to USART CR3 */
\r
188 USARTx->CR3 = (u16)tmpreg;
\r
190 /*---------------------------- USART BRR Configuration -----------------------*/
\r
193 /* Configure the USART Baud Rate -------------------------------------------*/
\r
194 RCC_GetClocksFreq(&RCC_ClocksStatus);
\r
195 if ((*(u32*)&USARTx) == USART1_BASE)
\r
197 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
\r
201 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
\r
204 /* Determine the integer part */
\r
205 integerdivider = ((0x19 * apbclock) / (0x04 * (USART_InitStruct->USART_BaudRate)));
\r
206 tmpreg = (integerdivider / 0x64) << 0x04;
\r
208 /* Determine the fractional part */
\r
209 fractionaldivider = integerdivider - (0x64 * (tmpreg >> 0x04));
\r
210 tmpreg |= ((((fractionaldivider * 0x10) + 0x32) / 0x64)) & ((u8)0x0F);
\r
212 /* Write to USART BRR */
\r
213 USARTx->BRR = (u16)tmpreg;
\r
216 /*******************************************************************************
\r
217 * Function Name : USART_StructInit
\r
218 * Description : Fills each USART_InitStruct member with its default value.
\r
219 * Input : - USART_InitStruct: pointer to a USART_InitTypeDef structure
\r
220 * which will be initialized.
\r
223 *******************************************************************************/
\r
224 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
\r
226 /* USART_InitStruct members default value */
\r
227 USART_InitStruct->USART_BaudRate = 0x2580; /* 9600 Baud */
\r
228 USART_InitStruct->USART_WordLength = USART_WordLength_8b;
\r
229 USART_InitStruct->USART_StopBits = USART_StopBits_1;
\r
230 USART_InitStruct->USART_Parity = USART_Parity_No ;
\r
231 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
\r
232 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
\r
233 USART_InitStruct->USART_Clock = USART_Clock_Disable;
\r
234 USART_InitStruct->USART_CPOL = USART_CPOL_Low;
\r
235 USART_InitStruct->USART_CPHA = USART_CPHA_1Edge;
\r
236 USART_InitStruct->USART_LastBit = USART_LastBit_Disable;
\r
239 /*******************************************************************************
\r
240 * Function Name : USART_Cmd
\r
241 * Description : Enables or disables the specified USART peripheral.
\r
242 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
244 * : - NewState: new state of the USARTx peripheral.
\r
245 * This parameter can be: ENABLE or DISABLE.
\r
248 *******************************************************************************/
\r
249 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
251 /* Check the parameters */
\r
252 assert(IS_FUNCTIONAL_STATE(NewState));
\r
254 if (NewState != DISABLE)
\r
256 /* Enable the selected USART by setting the RUN bit in the CR1 register */
\r
257 USARTx->CR1 |= CR1_RUN_Set;
\r
261 /* Disable the selected USART by clearing the RUN bit in the CR1 register */
\r
262 USARTx->CR1 &= CR1_RUN_Reset;
\r
266 /*******************************************************************************
\r
267 * Function Name : USART_ITConfig
\r
268 * Description : Enables or disables the specified USART interrupts.
\r
269 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
271 * - USART_IT: specifies the USART interrupt sources to be
\r
272 * enabled or disabled.
\r
273 * This parameter can be one of the following values:
\r
282 * - NewState: new state of the specified USARTx interrupts.
\r
283 * This parameter can be: ENABLE or DISABLE.
\r
286 *******************************************************************************/
\r
287 void USART_ITConfig(USART_TypeDef* USARTx, u16 USART_IT, FunctionalState NewState)
\r
289 u32 usartreg = 0x00, itpos = 0x00, itmask = 0x00;
\r
290 u32 address = 0x00;
\r
292 /* Check the parameters */
\r
293 assert(IS_USART_CONFIG_IT(USART_IT));
\r
294 assert(IS_FUNCTIONAL_STATE(NewState));
\r
296 /* Get the USART register index */
\r
297 usartreg = (((u8)USART_IT) >> 0x05);
\r
299 /* Get the interrupt position */
\r
300 itpos = USART_IT & USART_IT_Mask;
\r
302 itmask = (((u32)0x01) << itpos);
\r
303 address = *(u32*)&(USARTx);
\r
305 if (usartreg == 0x01) /* The IT is in CR1 register */
\r
309 else if (usartreg == 0x02) /* The IT is in CR2 register */
\r
313 else /* The IT is in CR3 register */
\r
317 if (NewState != DISABLE)
\r
319 *(u32*)address |= itmask;
\r
323 *(u32*)address &= ~itmask;
\r
327 /*******************************************************************************
\r
328 * Function Name : USART_DMACmd
\r
329 * Description : Enables or disables the USART
\92s DMA interface.
\r
330 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
332 * - USART_DMAReq: specifies the DMA request.
\r
333 * This parameter can be any combination of the following values:
\r
334 * - USART_DMAReq_Tx
\r
335 * - USART_DMAReq_Rx
\r
336 * - NewState: new state of the DMA Request sources.
\r
337 * This parameter can be: ENABLE or DISABLE.
\r
340 *******************************************************************************/
\r
341 void USART_DMACmd(USART_TypeDef* USARTx, u16 USART_DMAReq, FunctionalState NewState)
\r
343 /* Check the parameters */
\r
344 assert(IS_USART_DMAREQ(USART_DMAReq));
\r
345 assert(IS_FUNCTIONAL_STATE(NewState));
\r
347 if (NewState != DISABLE)
\r
349 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
\r
350 DMAR bits in the USART CR3 register */
\r
351 USARTx->CR3 |= USART_DMAReq;
\r
355 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
\r
356 DMAR bits in the USART CR3 register */
\r
357 USARTx->CR3 &= (u16)~USART_DMAReq;
\r
361 /*******************************************************************************
\r
362 * Function Name : USART_SetAddress
\r
363 * Description : Sets the address of the USART node.
\r
364 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
366 * - USART_Address: Indicates the address of the USART node.
\r
369 *******************************************************************************/
\r
370 void USART_SetAddress(USART_TypeDef* USARTx, u8 USART_Address)
\r
372 /* Check the parameters */
\r
373 assert(IS_USART_ADDRESS(USART_Address));
\r
375 /* Clear the USART address */
\r
376 USARTx->CR2 &= CR2_Address_Mask;
\r
377 /* Set the USART address node */
\r
378 USARTx->CR2 |= USART_Address;
\r
381 /*******************************************************************************
\r
382 * Function Name : USART_WakeUpConfig
\r
383 * Description : Selects the USART WakeUp method.
\r
384 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
386 * - USART_WakeUp: specifies the USART wakeup method.
\r
387 * This parameter can be one of the following values:
\r
388 * - USART_WakeUp_IdleLine
\r
389 * - USART_WakeUp_AddressMark
\r
392 *******************************************************************************/
\r
393 void USART_WakeUpConfig(USART_TypeDef* USARTx, u16 USART_WakeUp)
\r
395 /* Check the parameters */
\r
396 assert(IS_USART_WAKEUP(USART_WakeUp));
\r
398 USARTx->CR1 &= CR3_WAKE_Mask;
\r
399 USARTx->CR1 |= USART_WakeUp;
\r
402 /*******************************************************************************
\r
403 * Function Name : USART_ReceiverWakeUpCmd
\r
404 * Description : Determines if the USART is in mute mode or not.
\r
405 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
407 * - NewState: new state of the USART mode.
\r
408 * This parameter can be: ENABLE or DISABLE.
\r
411 *******************************************************************************/
\r
412 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
414 /* Check the parameters */
\r
415 assert(IS_FUNCTIONAL_STATE(NewState));
\r
417 if (NewState != DISABLE)
\r
419 /* Enable the mute mode USART by setting the RWU bit in the CR1 register */
\r
420 USARTx->CR1 |= CR1_RWU_Set;
\r
424 /* Disable the mute mode USART by clearing the RWU bit in the CR1 register */
\r
425 USARTx->CR1 &= CR1_RWU_Reset;
\r
429 /*******************************************************************************
\r
430 * Function Name : USART_LINBreakDetectLengthConfig
\r
431 * Description : Sets the USART LIN Break detection length.
\r
432 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
434 * - USART_LINBreakDetectLength: specifies the LIN break
\r
435 * detection length.
\r
436 * This parameter can be one of the following values:
\r
437 * - USART_LINBreakDetectLength_10b
\r
438 * - USART_LINBreakDetectLength_11b
\r
441 *******************************************************************************/
\r
442 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, u16 USART_LINBreakDetectLength)
\r
444 /* Check the parameters */
\r
445 assert(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
\r
447 USARTx->CR2 &= CR3_LBDL_Mask;
\r
448 USARTx->CR2 |= USART_LINBreakDetectLength;
\r
451 /*******************************************************************************
\r
452 * Function Name : USART_LINCmd
\r
453 * Description : Enables or disables the USART
\92s LIN mode.
\r
454 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
456 * - NewState: new state of the USART LIN mode.
\r
457 * This parameter can be: ENABLE or DISABLE.
\r
460 *******************************************************************************/
\r
461 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
463 /* Check the parameters */
\r
464 assert(IS_FUNCTIONAL_STATE(NewState));
\r
466 if (NewState != DISABLE)
\r
468 /* Enable the LIN mode by setting the LINE bit in the CR2 register */
\r
469 USARTx->CR2 |= CR2_LINE_Set;
\r
473 /* Disable the LIN mode by clearing the LINE bit in the CR2 register */
\r
474 USARTx->CR2 &= CR2_LINE_Reset;
\r
478 /*******************************************************************************
\r
479 * Function Name : USART_SendData
\r
480 * Description : Transmits signle data through the USARTx peripheral.
\r
481 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
483 * - Data: the data to transmit.
\r
486 *******************************************************************************/
\r
487 void USART_SendData(USART_TypeDef* USARTx, u16 Data)
\r
489 /* Check the parameters */
\r
490 assert(IS_USART_DATA(Data));
\r
492 /* Transmit Data */
\r
493 USARTx->DR = (Data & (u16)0x01FF);
\r
496 /*******************************************************************************
\r
497 * Function Name : USART_ReceiveData
\r
498 * Description : Returns the most recent received data by the USARTx peripheral.
\r
499 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
502 * Return : The received data.
\r
503 *******************************************************************************/
\r
504 u16 USART_ReceiveData(USART_TypeDef* USARTx)
\r
507 return (u16)(USARTx->DR & (u16)0x01FF);
\r
510 /*******************************************************************************
\r
511 * Function Name : USART_SendBreak
\r
512 * Description : Transmits break characters.
\r
513 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
517 *******************************************************************************/
\r
518 void USART_SendBreak(USART_TypeDef* USARTx)
\r
520 /* Send break characters */
\r
521 USARTx->CR1 |= CR1_SBK_Set;
\r
524 /*******************************************************************************
\r
525 * Function Name : USART_SetGuardTime
\r
526 * Description : Sets the specified USART guard time.
\r
527 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
529 * - USART_GuardTime: specifies the guard time.
\r
532 *******************************************************************************/
\r
533 void USART_SetGuardTime(USART_TypeDef* USARTx, u8 USART_GuardTime)
\r
535 /* Clear the USART Guard time */
\r
536 USARTx->GTPR &= GTPR_LSB_Mask;
\r
537 /* Set the USART guard time */
\r
538 USARTx->GTPR |= (u16)((u16)USART_GuardTime << 0x08);
\r
541 /*******************************************************************************
\r
542 * Function Name : USART_SetPrescaler
\r
543 * Description : Sets the system clock prescaler.
\r
544 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
546 * - USART_Prescaler: specifies the prescaler clock.
\r
549 *******************************************************************************/
\r
550 void USART_SetPrescaler(USART_TypeDef* USARTx, u8 USART_Prescaler)
\r
552 /* Clear the USART prescaler */
\r
553 USARTx->GTPR &= GTPR_MSB_Mask;
\r
554 /* Set the USART prescaler */
\r
555 USARTx->GTPR |= USART_Prescaler;
\r
558 /*******************************************************************************
\r
559 * Function Name : USART_SmartCardCmd
\r
560 * Description : Enables or disables the USART
\92s Smart Card mode.
\r
561 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
563 * - NewState: new state of the Smart Card mode.
\r
564 * This parameter can be: ENABLE or DISABLE.
\r
567 *******************************************************************************/
\r
568 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
570 /* Check the parameters */
\r
571 assert(IS_FUNCTIONAL_STATE(NewState));
\r
573 if (NewState != DISABLE)
\r
575 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
\r
576 USARTx->CR3 |= CR3_SCEN_Set;
\r
580 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
\r
581 USARTx->CR3 &= CR3_SCEN_Reset;
\r
585 /*******************************************************************************
\r
586 * Function Name : USART_SmartCardNACKCmd
\r
587 * Description : Enables or disables NACK transmission.
\r
588 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
590 * - NewState: new state of the NACK transmission.
\r
591 * This parameter can be: ENABLE or DISABLE.
\r
594 *******************************************************************************/
\r
595 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
597 /* Check the parameters */
\r
598 assert(IS_FUNCTIONAL_STATE(NewState));
\r
600 if (NewState != DISABLE)
\r
602 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
\r
603 USARTx->CR3 |= CR3_NACK_Set;
\r
607 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
\r
608 USARTx->CR3 &= CR3_NACK_Reset;
\r
613 /*******************************************************************************
\r
614 * Function Name : USART_HalfDuplexCmd
\r
615 * Description : Enables or disables the USART
\92s Half Duplex communication.
\r
616 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
618 * - NewState: new state of the USART Communication.
\r
619 * This parameter can be: ENABLE or DISABLE.
\r
622 *******************************************************************************/
\r
623 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
625 /* Check the parameters */
\r
626 assert(IS_FUNCTIONAL_STATE(NewState));
\r
628 if (NewState != DISABLE)
\r
630 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
\r
631 USARTx->CR3 |= CR3_HDSEL_Set;
\r
635 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
\r
636 USARTx->CR3 &= CR3_HDSEL_Reset;
\r
640 /*******************************************************************************
\r
641 * Function Name : USART_IrDAConfig
\r
642 * Description : Configures the USART
\92s IrDA interface.
\r
643 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
645 * - USART_IrDAMode: specifies the IrDA mode.
\r
646 * This parameter can be one of the following values:
\r
647 * - USART_IrDAMode_LowPower
\r
648 * - USART_IrDAMode_Normal
\r
651 *******************************************************************************/
\r
652 void USART_IrDAConfig(USART_TypeDef* USARTx, u16 USART_IrDAMode)
\r
654 /* Check the parameters */
\r
655 assert(IS_USART_IRDA_MODE(USART_IrDAMode));
\r
657 USARTx->CR3 &= CR3_IRLP_Mask;
\r
658 USARTx->CR3 |= USART_IrDAMode;
\r
661 /*******************************************************************************
\r
662 * Function Name : USART_IrDACmd
\r
663 * Description : Enables or disables the USART
\92s IrDA interface.
\r
664 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
666 * - NewState: new state of the IrDA mode.
\r
667 * This parameter can be: ENABLE or DISABLE.
\r
670 *******************************************************************************/
\r
671 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
\r
673 /* Check the parameters */
\r
674 assert(IS_FUNCTIONAL_STATE(NewState));
\r
676 if (NewState != DISABLE)
\r
678 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
\r
679 USARTx->CR3 |= CR3_IREN_Set;
\r
683 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
\r
684 USARTx->CR3 &= CR3_IREN_Reset;
\r
688 /*******************************************************************************
\r
689 * Function Name : USART_GetFlagStatus
\r
690 * Description : Checks whether the specified USART flag is set or not.
\r
691 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
693 * - USART_FLAG: specifies the flag to check.
\r
694 * This parameter can be one of the following values:
\r
699 * - USART_FLAG_RXNE
\r
700 * - USART_FLAG_IDLE
\r
706 * Return : The new state of USART_FLAG (SET or RESET).
\r
707 *******************************************************************************/
\r
708 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, u16 USART_FLAG)
\r
710 FlagStatus bitstatus = RESET;
\r
712 /* Check the parameters */
\r
713 assert(IS_USART_FLAG(USART_FLAG));
\r
715 if ((USARTx->SR & USART_FLAG) != (u16)RESET)
\r
726 /*******************************************************************************
\r
727 * Function Name : USART_ClearFlag
\r
728 * Description : Clears the USARTx's pending flags.
\r
729 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
731 * - USART_FLAG: specifies the flag to clear.
\r
732 * This parameter can be any combination of the following values:
\r
737 * - USART_FLAG_RXNE
\r
738 * - USART_FLAG_IDLE
\r
745 *******************************************************************************/
\r
746 void USART_ClearFlag(USART_TypeDef* USARTx, u16 USART_FLAG)
\r
748 /* Check the parameters */
\r
749 assert(IS_USART_CLEAR_FLAG(USART_FLAG));
\r
751 USARTx->SR &= (u16)~USART_FLAG;
\r
754 /*******************************************************************************
\r
755 * Function Name : USART_GetITStatus
\r
756 * Description : Checks whether the specified USART interrupt has occurred or not.
\r
757 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
759 * - USART_IT: specifies the USART interrupt source to check.
\r
760 * This parameter can be one of the following values:
\r
772 * Return : The new state of USART_IT (SET or RESET).
\r
773 *******************************************************************************/
\r
774 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, u16 USART_IT)
\r
776 u32 bitpos = 0x00, itmask = 0x00, usartreg = 0;
\r
777 ITStatus bitstatus = RESET;
\r
779 /* Check the parameters */
\r
780 assert(IS_USART_IT(USART_IT));
\r
782 /* Get the USART register index */
\r
783 usartreg = (((u8)USART_IT) >> 0x05);
\r
785 /* Get the interrupt position */
\r
786 itmask = USART_IT & USART_IT_Mask;
\r
788 itmask = (u32)0x01 << itmask;
\r
790 if (usartreg == 0x01) /* The IT is in CR1 register */
\r
792 itmask &= USARTx->CR1;
\r
794 else if (usartreg == 0x02) /* The IT is in CR2 register */
\r
796 itmask &= USARTx->CR2;
\r
798 else /* The IT is in CR3 register */
\r
800 itmask &= USARTx->CR3;
\r
803 bitpos = USART_IT >> 0x08;
\r
805 bitpos = (u32)0x01 << bitpos;
\r
806 bitpos &= USARTx->SR;
\r
808 if ((itmask != (u16)RESET)&&(bitpos != (u16)RESET))
\r
819 /*******************************************************************************
\r
820 * Function Name : USART_ClearITPendingBit
\r
821 * Description : Clears the USARTx
\92s interrupt pending bits.
\r
822 * Input : - USARTx: where x can be 1, 2 or 3 to select the USART
\r
824 * - USART_IT: specifies the interrupt pending bit to clear.
\r
825 * This parameter can be one of the following values:
\r
838 *******************************************************************************/
\r
839 void USART_ClearITPendingBit(USART_TypeDef* USARTx, u16 USART_IT)
\r
841 u32 bitpos = 0x00, itmask = 0x00;
\r
843 /* Check the parameters */
\r
844 assert(IS_USART_IT(USART_IT));
\r
846 bitpos = USART_IT >> 0x08;
\r
848 itmask = (u32)0x01 << bitpos;
\r
849 USARTx->SR &= ~itmask;
\r
852 /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
\r