]> git.piffa.net Git - arduino/blob - sheets/gyro/GY-52 Three-axis gyroscope sending data /Three-axis gyroscope sending data/GY-52 Test program/STM32-CODE/serial port output MPU-3050/serial/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c
first commit
[arduino] / sheets / gyro / GY-52 Three-axis gyroscope sending data / Three-axis gyroscope sending data / GY-52 Test program / STM32-CODE / serial port output MPU-3050 / serial / Libraries / STM32F10x_StdPeriph_Driver / src / stm32f10x_usart.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f10x_usart.c\r
4   * @author  MCD Application Team\r
5   * @version V3.1.2\r
6   * @date    09/28/2009\r
7   * @brief   This file provides all the USART firmware functions.\r
8   ******************************************************************************\r
9   * @copy\r
10   *\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
17   *\r
18   * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>\r
19   */ \r
20 \r
21 /* Includes ------------------------------------------------------------------*/\r
22 #include "stm32f10x_usart.h"\r
23 #include "stm32f10x_rcc.h"\r
24 \r
25 /** @addtogroup STM32F10x_StdPeriph_Driver\r
26   * @{\r
27   */\r
28 \r
29 /** @defgroup USART \r
30   * @brief USART driver modules\r
31   * @{\r
32   */\r
33 \r
34 /** @defgroup USART_Private_TypesDefinitions\r
35   * @{\r
36   */\r
37 \r
38 /**\r
39   * @}\r
40   */\r
41 \r
42 /** @defgroup USART_Private_Defines\r
43   * @{\r
44   */\r
45 \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
48 \r
49 #define CR1_WAKE_Mask             ((uint16_t)0xF7FF)  /*!< USART WakeUp Method Mask */\r
50 \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 \r
56 #define CR2_Address_Mask          ((uint16_t)0xFFF0)  /*!< USART address Mask */\r
57 \r
58 #define CR2_LINEN_Set              ((uint16_t)0x4000)  /*!< USART LIN Enable Mask */\r
59 #define CR2_LINEN_Reset            ((uint16_t)0xBFFF)  /*!< USART LIN Disable Mask */\r
60 \r
61 #define CR2_LBDL_Mask             ((uint16_t)0xFFDF)  /*!< USART LIN Break detection Mask */\r
62 #define CR2_STOP_CLEAR_Mask       ((uint16_t)0xCFFF)  /*!< USART CR2 STOP Bits Mask */\r
63 #define CR2_CLOCK_CLEAR_Mask      ((uint16_t)0xF0FF)  /*!< USART CR2 Clock Mask */\r
64 \r
65 #define CR3_SCEN_Set              ((uint16_t)0x0020)  /*!< USART SC Enable Mask */\r
66 #define CR3_SCEN_Reset            ((uint16_t)0xFFDF)  /*!< USART SC Disable Mask */\r
67 \r
68 #define CR3_NACK_Set              ((uint16_t)0x0010)  /*!< USART SC NACK Enable Mask */\r
69 #define CR3_NACK_Reset            ((uint16_t)0xFFEF)  /*!< USART SC NACK Disable Mask */\r
70 \r
71 #define CR3_HDSEL_Set             ((uint16_t)0x0008)  /*!< USART Half-Duplex Enable Mask */\r
72 #define CR3_HDSEL_Reset           ((uint16_t)0xFFF7)  /*!< USART Half-Duplex Disable Mask */\r
73 \r
74 #define CR3_IRLP_Mask             ((uint16_t)0xFFFB)  /*!< USART IrDA LowPower mode Mask */\r
75 #define CR3_CLEAR_Mask            ((uint16_t)0xFCFF)  /*!< USART CR3 Mask */\r
76 \r
77 #define CR3_IREN_Set              ((uint16_t)0x0002)  /*!< USART IrDA Enable Mask */\r
78 #define CR3_IREN_Reset            ((uint16_t)0xFFFD)  /*!< USART IrDA Disable Mask */\r
79 #define GTPR_LSB_Mask             ((uint16_t)0x00FF)  /*!< Guard Time Register LSB Mask */\r
80 #define GTPR_MSB_Mask             ((uint16_t)0xFF00)  /*!< Guard Time Register MSB Mask */\r
81 #define IT_Mask                   ((uint16_t)0x001F)  /*!< USART Interrupt Mask */\r
82 \r
83 /**\r
84   * @}\r
85   */\r
86 \r
87 /** @defgroup USART_Private_Macros\r
88   * @{\r
89   */\r
90 \r
91 /**\r
92   * @}\r
93   */\r
94 \r
95 /** @defgroup USART_Private_Variables\r
96   * @{\r
97   */\r
98 \r
99 /**\r
100   * @}\r
101   */\r
102 \r
103 /** @defgroup USART_Private_FunctionPrototypes\r
104   * @{\r
105   */\r
106 \r
107 /**\r
108   * @}\r
109   */\r
110 \r
111 /** @defgroup USART_Private_Functions\r
112   * @{\r
113   */\r
114 \r
115 /**\r
116   * @brief  Deinitializes the USARTx peripheral registers to their default reset values.\r
117   * @param  USARTx: Select the USART or the UART peripheral. \r
118   *   This parameter can be one of the following values: USART1, USART2, USART3, UART4 or UART5.\r
119   * @retval None\r
120   */\r
121 void USART_DeInit(USART_TypeDef* USARTx)\r
122 {\r
123   /* Check the parameters */\r
124   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
125 \r
126   if (USARTx == USART1)\r
127   {\r
128     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);\r
129     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);\r
130   }\r
131   else if (USARTx == USART2)\r
132   {\r
133     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);\r
134     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);\r
135   }\r
136   else if (USARTx == USART3)\r
137   {\r
138     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);\r
139     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);\r
140   }    \r
141   else if (USARTx == UART4)\r
142   {\r
143     RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);\r
144     RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);\r
145   }    \r
146   else\r
147   {\r
148     if (USARTx == UART5)\r
149     { \r
150       RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);\r
151       RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);\r
152     }\r
153   }\r
154 }\r
155 \r
156 /**\r
157   * @brief  Initializes the USARTx peripheral according to the specified\r
158   *   parameters in the USART_InitStruct .\r
159   * @param  USARTx: Select the USART or the UART peripheral. \r
160   *   This parameter can be one of the following values:\r
161   *   USART1, USART2, USART3, UART4 or UART5.\r
162   * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure\r
163   *   that contains the configuration information for the specified USART peripheral.\r
164   * @retval None\r
165   */\r
166 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)\r
167 {\r
168   uint32_t tmpreg = 0x00, apbclock = 0x00;\r
169   uint32_t integerdivider = 0x00;\r
170   uint32_t fractionaldivider = 0x00;\r
171   uint32_t usartxbase = 0;\r
172   RCC_ClocksTypeDef RCC_ClocksStatus;\r
173   /* Check the parameters */\r
174   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
175   assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));  \r
176   assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));\r
177   assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));\r
178   assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));\r
179   assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));\r
180   assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));\r
181   /* The hardware flow control is available only for USART1, USART2 and USART3 */\r
182   if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)\r
183   {\r
184     assert_param(IS_USART_123_PERIPH(USARTx));\r
185   }\r
186 \r
187   usartxbase = (uint32_t)USARTx;\r
188 \r
189 /*---------------------------- USART CR2 Configuration -----------------------*/\r
190   tmpreg = USARTx->CR2;\r
191   /* Clear STOP[13:12] bits */\r
192   tmpreg &= CR2_STOP_CLEAR_Mask;\r
193   /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/\r
194   /* Set STOP[13:12] bits according to USART_StopBits value */\r
195   tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;\r
196   \r
197   /* Write to USART CR2 */\r
198   USARTx->CR2 = (uint16_t)tmpreg;\r
199 \r
200 /*---------------------------- USART CR1 Configuration -----------------------*/\r
201   tmpreg = USARTx->CR1;\r
202   /* Clear M, PCE, PS, TE and RE bits */\r
203   tmpreg &= CR1_CLEAR_Mask;\r
204   /* Configure the USART Word Length, Parity and mode ----------------------- */\r
205   /* Set the M bits according to USART_WordLength value */\r
206   /* Set PCE and PS bits according to USART_Parity value */\r
207   /* Set TE and RE bits according to USART_Mode value */\r
208   tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |\r
209             USART_InitStruct->USART_Mode;\r
210   /* Write to USART CR1 */\r
211   USARTx->CR1 = (uint16_t)tmpreg;\r
212 \r
213 /*---------------------------- USART CR3 Configuration -----------------------*/  \r
214   tmpreg = USARTx->CR3;\r
215   /* Clear CTSE and RTSE bits */\r
216   tmpreg &= CR3_CLEAR_Mask;\r
217   /* Configure the USART HFC -------------------------------------------------*/\r
218   /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */\r
219   tmpreg |= USART_InitStruct->USART_HardwareFlowControl;\r
220   /* Write to USART CR3 */\r
221   USARTx->CR3 = (uint16_t)tmpreg;\r
222 \r
223 /*---------------------------- USART BRR Configuration -----------------------*/\r
224   /* Configure the USART Baud Rate -------------------------------------------*/\r
225   RCC_GetClocksFreq(&RCC_ClocksStatus);\r
226   if (usartxbase == USART1_BASE)\r
227   {\r
228     apbclock = RCC_ClocksStatus.PCLK2_Frequency;\r
229   }\r
230   else\r
231   {\r
232     apbclock = RCC_ClocksStatus.PCLK1_Frequency;\r
233   }\r
234   /* Determine the integer part */\r
235   integerdivider = ((0x19 * apbclock) / (0x04 * (USART_InitStruct->USART_BaudRate)));\r
236   tmpreg = (integerdivider / 0x64) << 0x04;\r
237   /* Determine the fractional part */\r
238   fractionaldivider = integerdivider - (0x64 * (tmpreg >> 0x04));\r
239   tmpreg |= ((((fractionaldivider * 0x10) + 0x32) / 0x64)) & ((uint8_t)0x0F);\r
240   /* Write to USART BRR */\r
241   USARTx->BRR = (uint16_t)tmpreg;\r
242 }\r
243 \r
244 /**\r
245   * @brief  Fills each USART_InitStruct member with its default value.\r
246   * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure\r
247   *   which will be initialized.\r
248   * @retval None\r
249   */\r
250 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)\r
251 {\r
252   /* USART_InitStruct members default value */\r
253   USART_InitStruct->USART_BaudRate = 9600;\r
254   USART_InitStruct->USART_WordLength = USART_WordLength_8b;\r
255   USART_InitStruct->USART_StopBits = USART_StopBits_1;\r
256   USART_InitStruct->USART_Parity = USART_Parity_No ;\r
257   USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;\r
258   USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;  \r
259 }\r
260 \r
261 /**\r
262   * @brief  Initializes the USARTx peripheral Clock according to the \r
263   *   specified parameters in the USART_ClockInitStruct .\r
264   * @param  USARTx: where x can be 1, 2, 3 to select the USART peripheral.\r
265   * @param  USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef\r
266   *   structure that contains the configuration information for the specified \r
267   *   USART peripheral.  \r
268   * @note The Smart Card mode is not available for UART4 and UART5.\r
269   * @retval None\r
270   */\r
271 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)\r
272 {\r
273   uint32_t tmpreg = 0x00;\r
274   /* Check the parameters */\r
275   assert_param(IS_USART_123_PERIPH(USARTx));\r
276   assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));\r
277   assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));\r
278   assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));\r
279   assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));\r
280   \r
281 /*---------------------------- USART CR2 Configuration -----------------------*/\r
282   tmpreg = USARTx->CR2;\r
283   /* Clear CLKEN, CPOL, CPHA and LBCL bits */\r
284   tmpreg &= CR2_CLOCK_CLEAR_Mask;\r
285   /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/\r
286   /* Set CLKEN bit according to USART_Clock value */\r
287   /* Set CPOL bit according to USART_CPOL value */\r
288   /* Set CPHA bit according to USART_CPHA value */\r
289   /* Set LBCL bit according to USART_LastBit value */\r
290   tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL | \r
291                  USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;\r
292   /* Write to USART CR2 */\r
293   USARTx->CR2 = (uint16_t)tmpreg;\r
294 }\r
295 \r
296 /**\r
297   * @brief  Fills each USART_ClockInitStruct member with its default value.\r
298   * @param  USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef\r
299   *   structure which will be initialized.\r
300   * @retval None\r
301   */\r
302 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)\r
303 {\r
304   /* USART_ClockInitStruct members default value */\r
305   USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;\r
306   USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;\r
307   USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;\r
308   USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;\r
309 }\r
310 \r
311 /**\r
312   * @brief  Enables or disables the specified USART peripheral.\r
313   * @param  USARTx: Select the USART or the UART peripheral. \r
314   *   This parameter can be one of the following values:\r
315   *   USART1, USART2, USART3, UART4 or UART5.\r
316   * @param  NewState: new state of the USARTx peripheral.\r
317   *   This parameter can be: ENABLE or DISABLE.\r
318   * @retval None\r
319   */\r
320 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
321 {\r
322   /* Check the parameters */\r
323   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
324   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
325   \r
326   if (NewState != DISABLE)\r
327   {\r
328     /* Enable the selected USART by setting the UE bit in the CR1 register */\r
329     USARTx->CR1 |= CR1_UE_Set;\r
330   }\r
331   else\r
332   {\r
333     /* Disable the selected USART by clearing the UE bit in the CR1 register */\r
334     USARTx->CR1 &= CR1_UE_Reset;\r
335   }\r
336 }\r
337 \r
338 /**\r
339   * @brief  Enables or disables the specified USART interrupts.\r
340   * @param  USARTx: Select the USART or the UART peripheral. \r
341   *   This parameter can be one of the following values:\r
342   *   USART1, USART2, USART3, UART4 or UART5.\r
343   * @param  USART_IT: specifies the USART interrupt sources to be enabled or disabled.\r
344   *   This parameter can be one of the following values:\r
345   *     @arg USART_IT_CTS:  CTS change interrupt (not available for UART4 and UART5)\r
346   *     @arg USART_IT_LBD:  LIN Break detection interrupt\r
347   *     @arg USART_IT_TXE:  Tansmit Data Register empty interrupt\r
348   *     @arg USART_IT_TC:   Transmission complete interrupt\r
349   *     @arg USART_IT_RXNE: Receive Data register not empty interrupt\r
350   *     @arg USART_IT_IDLE: Idle line detection interrupt\r
351   *     @arg USART_IT_PE:   Parity Error interrupt\r
352   *     @arg USART_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)\r
353   * @param  NewState: new state of the specified USARTx interrupts.\r
354   *   This parameter can be: ENABLE or DISABLE.\r
355   * @retval None\r
356   */\r
357 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)\r
358 {\r
359   uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;\r
360   uint32_t usartxbase = 0x00;\r
361   /* Check the parameters */\r
362   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
363   assert_param(IS_USART_CONFIG_IT(USART_IT));\r
364   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
365   /* The CTS interrupt is not available for UART4 and UART5 */\r
366   if (USART_IT == USART_IT_CTS)\r
367   {\r
368     assert_param(IS_USART_123_PERIPH(USARTx));\r
369   }   \r
370   \r
371   usartxbase = (uint32_t)USARTx;\r
372 \r
373   /* Get the USART register index */\r
374   usartreg = (((uint8_t)USART_IT) >> 0x05);\r
375 \r
376   /* Get the interrupt position */\r
377   itpos = USART_IT & IT_Mask;\r
378   itmask = (((uint32_t)0x01) << itpos);\r
379     \r
380   if (usartreg == 0x01) /* The IT is in CR1 register */\r
381   {\r
382     usartxbase += 0x0C;\r
383   }\r
384   else if (usartreg == 0x02) /* The IT is in CR2 register */\r
385   {\r
386     usartxbase += 0x10;\r
387   }\r
388   else /* The IT is in CR3 register */\r
389   {\r
390     usartxbase += 0x14; \r
391   }\r
392   if (NewState != DISABLE)\r
393   {\r
394     *(__IO uint32_t*)usartxbase  |= itmask;\r
395   }\r
396   else\r
397   {\r
398     *(__IO uint32_t*)usartxbase &= ~itmask;\r
399   }\r
400 }\r
401 \r
402 /**\r
403   * @brief  Enables or disables the USART\92s DMA interface.\r
404   * @param  USARTx: Select the USART or the UART peripheral. \r
405   *   This parameter can be one of the following values:\r
406   *   USART1, USART2, USART3 or UART4.  \r
407   * @param  USART_DMAReq: specifies the DMA request.\r
408   *   This parameter can be any combination of the following values:\r
409   *     @arg USART_DMAReq_Tx: USART DMA transmit request\r
410   *     @arg USART_DMAReq_Rx: USART DMA receive request\r
411   * @param  NewState: new state of the DMA Request sources.\r
412   *   This parameter can be: ENABLE or DISABLE.\r
413   * @note The DMA mode is not available for UART5.  \r
414   * @retval None\r
415   */\r
416 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)\r
417 {\r
418   /* Check the parameters */\r
419   assert_param(IS_USART_1234_PERIPH(USARTx));\r
420   assert_param(IS_USART_DMAREQ(USART_DMAReq));  \r
421   assert_param(IS_FUNCTIONAL_STATE(NewState)); \r
422   if (NewState != DISABLE)\r
423   {\r
424     /* Enable the DMA transfer for selected requests by setting the DMAT and/or\r
425        DMAR bits in the USART CR3 register */\r
426     USARTx->CR3 |= USART_DMAReq;\r
427   }\r
428   else\r
429   {\r
430     /* Disable the DMA transfer for selected requests by clearing the DMAT and/or\r
431        DMAR bits in the USART CR3 register */\r
432     USARTx->CR3 &= (uint16_t)~USART_DMAReq;\r
433   }\r
434 }\r
435 \r
436 /**\r
437   * @brief  Sets the address of the USART node.\r
438   * @param  USARTx: Select the USART or the UART peripheral. \r
439   *   This parameter can be one of the following values:\r
440   *   USART1, USART2, USART3, UART4 or UART5.\r
441   * @param  USART_Address: Indicates the address of the USART node.\r
442   * @retval None\r
443   */\r
444 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)\r
445 {\r
446   /* Check the parameters */\r
447   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
448   assert_param(IS_USART_ADDRESS(USART_Address)); \r
449     \r
450   /* Clear the USART address */\r
451   USARTx->CR2 &= CR2_Address_Mask;\r
452   /* Set the USART address node */\r
453   USARTx->CR2 |= USART_Address;\r
454 }\r
455 \r
456 /**\r
457   * @brief  Selects the USART WakeUp method.\r
458   * @param  USARTx: Select the USART or the UART peripheral. \r
459   *   This parameter can be one of the following values:\r
460   *   USART1, USART2, USART3, UART4 or UART5.\r
461   * @param  USART_WakeUp: specifies the USART wakeup method.\r
462   *   This parameter can be one of the following values:\r
463   *     @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection\r
464   *     @arg USART_WakeUp_AddressMark: WakeUp by an address mark\r
465   * @retval None\r
466   */\r
467 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)\r
468 {\r
469   /* Check the parameters */\r
470   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
471   assert_param(IS_USART_WAKEUP(USART_WakeUp));\r
472   \r
473   USARTx->CR1 &= CR1_WAKE_Mask;\r
474   USARTx->CR1 |= USART_WakeUp;\r
475 }\r
476 \r
477 /**\r
478   * @brief  Determines if the USART is in mute mode or not.\r
479   * @param  USARTx: Select the USART or the UART peripheral. \r
480   *   This parameter can be one of the following values:\r
481   *   USART1, USART2, USART3, UART4 or UART5.\r
482   * @param  NewState: new state of the USART mute mode.\r
483   *   This parameter can be: ENABLE or DISABLE.\r
484   * @retval None\r
485   */\r
486 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
487 {\r
488   /* Check the parameters */\r
489   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
490   assert_param(IS_FUNCTIONAL_STATE(NewState)); \r
491   \r
492   if (NewState != DISABLE)\r
493   {\r
494     /* Enable the USART mute mode  by setting the RWU bit in the CR1 register */\r
495     USARTx->CR1 |= CR1_RWU_Set;\r
496   }\r
497   else\r
498   {\r
499     /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */\r
500     USARTx->CR1 &= CR1_RWU_Reset;\r
501   }\r
502 }\r
503 \r
504 /**\r
505   * @brief  Sets the USART LIN Break detection length.\r
506   * @param  USARTx: Select the USART or the UART peripheral. \r
507   *   This parameter can be one of the following values:\r
508   *   USART1, USART2, USART3, UART4 or UART5.\r
509   * @param  USART_LINBreakDetectLength: specifies the LIN break detection length.\r
510   *   This parameter can be one of the following values:\r
511   *     @arg USART_LINBreakDetectLength_10b: 10-bit break detection\r
512   *     @arg USART_LINBreakDetectLength_11b: 11-bit break detection\r
513   * @retval None\r
514   */\r
515 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)\r
516 {\r
517   /* Check the parameters */\r
518   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
519   assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));\r
520   \r
521   USARTx->CR2 &= CR2_LBDL_Mask;\r
522   USARTx->CR2 |= USART_LINBreakDetectLength;  \r
523 }\r
524 \r
525 /**\r
526   * @brief  Enables or disables the USART\92s LIN mode.\r
527   * @param  USARTx: Select the USART or the UART peripheral. \r
528   *   This parameter can be one of the following values:\r
529   *   USART1, USART2, USART3, UART4 or UART5.\r
530   * @param  NewState: new state of the USART LIN mode.\r
531   *   This parameter can be: ENABLE or DISABLE.\r
532   * @retval None\r
533   */\r
534 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
535 {\r
536   /* Check the parameters */\r
537   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
538   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
539   \r
540   if (NewState != DISABLE)\r
541   {\r
542     /* Enable the LIN mode by setting the LINEN bit in the CR2 register */\r
543     USARTx->CR2 |= CR2_LINEN_Set;\r
544   }\r
545   else\r
546   {\r
547     /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */\r
548     USARTx->CR2 &= CR2_LINEN_Reset;\r
549   }\r
550 }\r
551 \r
552 /**\r
553   * @brief  Transmits single data through the USARTx peripheral.\r
554   * @param  USARTx: Select the USART or the UART peripheral. \r
555   *   This parameter can be one of the following values:\r
556   *   USART1, USART2, USART3, UART4 or UART5.\r
557   * @param  Data: the data to transmit.\r
558   * @retval None\r
559   */\r
560 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)\r
561 {\r
562   /* Check the parameters */\r
563   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
564   assert_param(IS_USART_DATA(Data)); \r
565     \r
566   /* Transmit Data */\r
567   USARTx->DR = (Data & (uint16_t)0x01FF);\r
568 }\r
569 \r
570 /**\r
571   * @brief  Returns the most recent received data by the USARTx peripheral.\r
572   * @param  USARTx: Select the USART or the UART peripheral. \r
573   *   This parameter can be one of the following values:\r
574   *   USART1, USART2, USART3, UART4 or UART5.\r
575   * @retval The received data.\r
576   */\r
577 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)\r
578 {\r
579   /* Check the parameters */\r
580   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
581   \r
582   /* Receive Data */\r
583   return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);\r
584 }\r
585 \r
586 /**\r
587   * @brief  Transmits break characters.\r
588   * @param  USARTx: Select the USART or the UART peripheral. \r
589   *   This parameter can be one of the following values:\r
590   *   USART1, USART2, USART3, UART4 or UART5.\r
591   * @retval None\r
592   */\r
593 void USART_SendBreak(USART_TypeDef* USARTx)\r
594 {\r
595   /* Check the parameters */\r
596   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
597   \r
598   /* Send break characters */\r
599   USARTx->CR1 |= CR1_SBK_Set;\r
600 }\r
601 \r
602 /**\r
603   * @brief  Sets the specified USART guard time.\r
604   * @param  USARTx: where x can be 1, 2 or 3 to select the USART peripheral.\r
605   * @param  USART_GuardTime: specifies the guard time.\r
606   * @note The guard time bits are not available for UART4 and UART5.   \r
607   * @retval None\r
608   */\r
609 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)\r
610 {    \r
611   /* Check the parameters */\r
612   assert_param(IS_USART_123_PERIPH(USARTx));\r
613   \r
614   /* Clear the USART Guard time */\r
615   USARTx->GTPR &= GTPR_LSB_Mask;\r
616   /* Set the USART guard time */\r
617   USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);\r
618 }\r
619 \r
620 /**\r
621   * @brief  Sets the system clock prescaler.\r
622   * @param  USARTx: Select the USART or the UART peripheral. \r
623   *   This parameter can be one of the following values:\r
624   *   USART1, USART2, USART3, UART4 or UART5.\r
625   * @param  USART_Prescaler: specifies the prescaler clock.  \r
626   * @note   The function is used for IrDA mode with UART4 and UART5.\r
627   * @retval None\r
628   */\r
629 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)\r
630\r
631   /* Check the parameters */\r
632   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
633   \r
634   /* Clear the USART prescaler */\r
635   USARTx->GTPR &= GTPR_MSB_Mask;\r
636   /* Set the USART prescaler */\r
637   USARTx->GTPR |= USART_Prescaler;\r
638 }\r
639 \r
640 /**\r
641   * @brief  Enables or disables the USART\92s Smart Card mode.\r
642   * @param  USARTx: where x can be 1, 2 or 3 to select the USART peripheral.\r
643   * @param  NewState: new state of the Smart Card mode.\r
644   *   This parameter can be: ENABLE or DISABLE.     \r
645   * @note The Smart Card mode is not available for UART4 and UART5. \r
646   * @retval None\r
647   */\r
648 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
649 {\r
650   /* Check the parameters */\r
651   assert_param(IS_USART_123_PERIPH(USARTx));\r
652   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
653   if (NewState != DISABLE)\r
654   {\r
655     /* Enable the SC mode by setting the SCEN bit in the CR3 register */\r
656     USARTx->CR3 |= CR3_SCEN_Set;\r
657   }\r
658   else\r
659   {\r
660     /* Disable the SC mode by clearing the SCEN bit in the CR3 register */\r
661     USARTx->CR3 &= CR3_SCEN_Reset;\r
662   }\r
663 }\r
664 \r
665 /**\r
666   * @brief  Enables or disables NACK transmission.\r
667   * @param  USARTx: where x can be 1, 2 or 3 to select the USART peripheral. \r
668   * @param  NewState: new state of the NACK transmission.\r
669   *   This parameter can be: ENABLE or DISABLE.  \r
670   * @note The Smart Card mode is not available for UART4 and UART5.\r
671   * @retval None\r
672   */\r
673 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
674 {\r
675   /* Check the parameters */\r
676   assert_param(IS_USART_123_PERIPH(USARTx));  \r
677   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
678   if (NewState != DISABLE)\r
679   {\r
680     /* Enable the NACK transmission by setting the NACK bit in the CR3 register */\r
681     USARTx->CR3 |= CR3_NACK_Set;\r
682   }\r
683   else\r
684   {\r
685     /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */\r
686     USARTx->CR3 &= CR3_NACK_Reset;\r
687   }\r
688 }\r
689 \r
690 /**\r
691   * @brief  Enables or disables the USART\92s Half Duplex communication.\r
692   * @param  USARTx: Select the USART or the UART peripheral. \r
693   *   This parameter can be one of the following values:\r
694   *   USART1, USART2, USART3, UART4 or UART5.\r
695   * @param  NewState: new state of the USART Communication.\r
696   *   This parameter can be: ENABLE or DISABLE.\r
697   * @retval None\r
698   */\r
699 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
700 {\r
701   /* Check the parameters */\r
702   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
703   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
704   \r
705   if (NewState != DISABLE)\r
706   {\r
707     /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */\r
708     USARTx->CR3 |= CR3_HDSEL_Set;\r
709   }\r
710   else\r
711   {\r
712     /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */\r
713     USARTx->CR3 &= CR3_HDSEL_Reset;\r
714   }\r
715 }\r
716 \r
717 /**\r
718   * @brief  Configures the USART\92s IrDA interface.\r
719   * @param  USARTx: Select the USART or the UART peripheral. \r
720   *   This parameter can be one of the following values:\r
721   *   USART1, USART2, USART3, UART4 or UART5.\r
722   * @param  USART_IrDAMode: specifies the IrDA mode.\r
723   *   This parameter can be one of the following values:\r
724   *     @arg USART_IrDAMode_LowPower\r
725   *     @arg USART_IrDAMode_Normal\r
726   * @retval None\r
727   */\r
728 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)\r
729 {\r
730   /* Check the parameters */\r
731   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
732   assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));\r
733     \r
734   USARTx->CR3 &= CR3_IRLP_Mask;\r
735   USARTx->CR3 |= USART_IrDAMode;\r
736 }\r
737 \r
738 /**\r
739   * @brief  Enables or disables the USART\92s IrDA interface.\r
740   * @param  USARTx: Select the USART or the UART peripheral. \r
741   *   This parameter can be one of the following values:\r
742   *   USART1, USART2, USART3, UART4 or UART5.\r
743   * @param  NewState: new state of the IrDA mode.\r
744   *   This parameter can be: ENABLE or DISABLE.\r
745   * @retval None\r
746   */\r
747 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
748 {\r
749   /* Check the parameters */\r
750   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
751   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
752     \r
753   if (NewState != DISABLE)\r
754   {\r
755     /* Enable the IrDA mode by setting the IREN bit in the CR3 register */\r
756     USARTx->CR3 |= CR3_IREN_Set;\r
757   }\r
758   else\r
759   {\r
760     /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */\r
761     USARTx->CR3 &= CR3_IREN_Reset;\r
762   }\r
763 }\r
764 \r
765 /**\r
766   * @brief  Checks whether the specified USART flag is set or not.\r
767   * @param  USARTx: Select the USART or the UART peripheral. \r
768   *   This parameter can be one of the following values:\r
769   *   USART1, USART2, USART3, UART4 or UART5.\r
770   * @param  USART_FLAG: specifies the flag to check.\r
771   *   This parameter can be one of the following values:\r
772   *     @arg USART_FLAG_CTS:  CTS Change flag (not available for UART4 and UART5)\r
773   *     @arg USART_FLAG_LBD:  LIN Break detection flag\r
774   *     @arg USART_FLAG_TXE:  Transmit data register empty flag\r
775   *     @arg USART_FLAG_TC:   Transmission Complete flag\r
776   *     @arg USART_FLAG_RXNE: Receive data register not empty flag\r
777   *     @arg USART_FLAG_IDLE: Idle Line detection flag\r
778   *     @arg USART_FLAG_ORE:  OverRun Error flag\r
779   *     @arg USART_FLAG_NE:   Noise Error flag\r
780   *     @arg USART_FLAG_FE:   Framing Error flag\r
781   *     @arg USART_FLAG_PE:   Parity Error flag\r
782   * @retval The new state of USART_FLAG (SET or RESET).\r
783   */\r
784 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)\r
785 {\r
786   FlagStatus bitstatus = RESET;\r
787   /* Check the parameters */\r
788   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
789   assert_param(IS_USART_FLAG(USART_FLAG));\r
790   /* The CTS flag is not available for UART4 and UART5 */\r
791   if (USART_FLAG == USART_FLAG_CTS)\r
792   {\r
793     assert_param(IS_USART_123_PERIPH(USARTx));\r
794   }  \r
795   \r
796   if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)\r
797   {\r
798     bitstatus = SET;\r
799   }\r
800   else\r
801   {\r
802     bitstatus = RESET;\r
803   }\r
804   return bitstatus;\r
805 }\r
806 \r
807 /**\r
808   * @brief  Clears the USARTx's pending flags.\r
809   * @param  USARTx: Select the USART or the UART peripheral. \r
810   *   This parameter can be one of the following values:\r
811   *   USART1, USART2, USART3, UART4 or UART5.\r
812   * @param  USART_FLAG: specifies the flag to clear.\r
813   *   This parameter can be any combination of the following values:\r
814   *     @arg USART_FLAG_CTS:  CTS Change flag (not available for UART4 and UART5).\r
815   *     @arg USART_FLAG_LBD:  LIN Break detection flag.\r
816   *     @arg USART_FLAG_TC:   Transmission Complete flag.\r
817   *     @arg USART_FLAG_RXNE: Receive data register not empty flag.\r
818   *   \r
819   * @note\r
820   *   - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun \r
821   *     error) and IDLE (Idle line detected) flags are cleared by software \r
822   *     sequence: a read operation to USART_SR register (USART_GetFlagStatus()) \r
823   *     followed by a read operation to USART_DR register (USART_ReceiveData()).\r
824   *   - RXNE flag can be also cleared by a read to the USART_DR register \r
825   *     (USART_ReceiveData()).\r
826   *   - TC flag can be also cleared by software sequence: a read operation to \r
827   *     USART_SR register (USART_GetFlagStatus()) followed by a write operation\r
828   *     to USART_DR register (USART_SendData()).\r
829   *   - TXE flag is cleared only by a write to the USART_DR register \r
830   *     (USART_SendData()).\r
831   * @retval None\r
832   */\r
833 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)\r
834 {\r
835   /* Check the parameters */\r
836   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
837   assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));\r
838   /* The CTS flag is not available for UART4 and UART5 */\r
839   if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)\r
840   {\r
841     assert_param(IS_USART_123_PERIPH(USARTx));\r
842   } \r
843    \r
844   USARTx->SR = (uint16_t)~USART_FLAG;\r
845 }\r
846 \r
847 /**\r
848   * @brief  Checks whether the specified USART interrupt has occurred or not.\r
849   * @param  USARTx: Select the USART or the UART peripheral. \r
850   *   This parameter can be one of the following values:\r
851   *   USART1, USART2, USART3, UART4 or UART5.\r
852   * @param  USART_IT: specifies the USART interrupt source to check.\r
853   *   This parameter can be one of the following values:\r
854   *     @arg USART_IT_CTS:  CTS change interrupt (not available for UART4 and UART5)\r
855   *     @arg USART_IT_LBD:  LIN Break detection interrupt\r
856   *     @arg USART_IT_TXE:  Tansmit Data Register empty interrupt\r
857   *     @arg USART_IT_TC:   Transmission complete interrupt\r
858   *     @arg USART_IT_RXNE: Receive Data register not empty interrupt\r
859   *     @arg USART_IT_IDLE: Idle line detection interrupt\r
860   *     @arg USART_IT_ORE:  OverRun Error interrupt\r
861   *     @arg USART_IT_NE:   Noise Error interrupt\r
862   *     @arg USART_IT_FE:   Framing Error interrupt\r
863   *     @arg USART_IT_PE:   Parity Error interrupt\r
864   * @retval The new state of USART_IT (SET or RESET).\r
865   */\r
866 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)\r
867 {\r
868   uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;\r
869   ITStatus bitstatus = RESET;\r
870   /* Check the parameters */\r
871   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
872   assert_param(IS_USART_GET_IT(USART_IT));\r
873   /* The CTS interrupt is not available for UART4 and UART5 */ \r
874   if (USART_IT == USART_IT_CTS)\r
875   {\r
876     assert_param(IS_USART_123_PERIPH(USARTx));\r
877   }   \r
878   \r
879   /* Get the USART register index */\r
880   usartreg = (((uint8_t)USART_IT) >> 0x05);\r
881   /* Get the interrupt position */\r
882   itmask = USART_IT & IT_Mask;\r
883   itmask = (uint32_t)0x01 << itmask;\r
884   \r
885   if (usartreg == 0x01) /* The IT  is in CR1 register */\r
886   {\r
887     itmask &= USARTx->CR1;\r
888   }\r
889   else if (usartreg == 0x02) /* The IT  is in CR2 register */\r
890   {\r
891     itmask &= USARTx->CR2;\r
892   }\r
893   else /* The IT  is in CR3 register */\r
894   {\r
895     itmask &= USARTx->CR3;\r
896   }\r
897   \r
898   bitpos = USART_IT >> 0x08;\r
899   bitpos = (uint32_t)0x01 << bitpos;\r
900   bitpos &= USARTx->SR;\r
901   if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))\r
902   {\r
903     bitstatus = SET;\r
904   }\r
905   else\r
906   {\r
907     bitstatus = RESET;\r
908   }\r
909   \r
910   return bitstatus;  \r
911 }\r
912 \r
913 /**\r
914   * @brief  Clears the USARTx\92s interrupt pending bits.\r
915   * @param  USARTx: Select the USART or the UART peripheral. \r
916   *   This parameter can be one of the following values:\r
917   *   USART1, USART2, USART3, UART4 or UART5.\r
918   * @param  USART_IT: specifies the interrupt pending bit to clear.\r
919   *   This parameter can be one of the following values:\r
920   *     @arg USART_IT_CTS:  CTS change interrupt (not available for UART4 and UART5)\r
921   *     @arg USART_IT_LBD:  LIN Break detection interrupt\r
922   *     @arg USART_IT_TC:   Transmission complete interrupt. \r
923   *     @arg USART_IT_RXNE: Receive Data register not empty interrupt.\r
924   *   \r
925   * @note\r
926   *   - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun \r
927   *     error) and IDLE (Idle line detected) pending bits are cleared by \r
928   *     software sequence: a read operation to USART_SR register \r
929   *     (USART_GetITStatus()) followed by a read operation to USART_DR register \r
930   *     (USART_ReceiveData()).\r
931   *   - RXNE pending bit can be also cleared by a read to the USART_DR register \r
932   *     (USART_ReceiveData()).\r
933   *   - TC pending bit can be also cleared by software sequence: a read \r
934   *     operation to USART_SR register (USART_GetITStatus()) followed by a write \r
935   *     operation to USART_DR register (USART_SendData()).\r
936   *   - TXE pending bit is cleared only by a write to the USART_DR register \r
937   *     (USART_SendData()).\r
938   * @retval None\r
939   */\r
940 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)\r
941 {\r
942   uint16_t bitpos = 0x00, itmask = 0x00;\r
943   /* Check the parameters */\r
944   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
945   assert_param(IS_USART_CLEAR_IT(USART_IT));\r
946   /* The CTS interrupt is not available for UART4 and UART5 */\r
947   if (USART_IT == USART_IT_CTS)\r
948   {\r
949     assert_param(IS_USART_123_PERIPH(USARTx));\r
950   }   \r
951   \r
952   bitpos = USART_IT >> 0x08;\r
953   itmask = ((uint16_t)0x01 << (uint16_t)bitpos);\r
954   USARTx->SR = (uint16_t)~itmask;\r
955 }\r
956 /**\r
957   * @}\r
958   */\r
959 \r
960 /**\r
961   * @}\r
962   */\r
963 \r
964 /**\r
965   * @}\r
966   */\r
967 \r
968 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/\r