]> git.sur5r.net Git - freertos/blob - Demo/Cortex_STM32L152_IAR/system_and_ST_code/STM32L1xx_StdPeriph_Driver/src/stm32l1xx_usart.c
90c149253502d36201e26ad4e03187a07836ed4d
[freertos] / Demo / Cortex_STM32L152_IAR / system_and_ST_code / STM32L1xx_StdPeriph_Driver / src / stm32l1xx_usart.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_usart.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    07/02/2010\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 2010 STMicroelectronics</center></h2>\r
19   */ \r
20 \r
21 /* Includes ------------------------------------------------------------------*/\r
22 #include "stm32l1xx_usart.h"\r
23 #include "stm32l1xx_rcc.h"\r
24 \r
25 /** @addtogroup STM32L1xx_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 /*!< 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
49                                               USART_CR1_RE))\r
50 \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
54 \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
57 \r
58 /*!< USART Interrupts mask */\r
59 #define IT_MASK                   ((uint16_t)0x001F)\r
60 \r
61 /**\r
62   * @}\r
63   */\r
64 \r
65 /** @defgroup USART_Private_Macros\r
66   * @{\r
67   */\r
68 \r
69 /**\r
70   * @}\r
71   */\r
72 \r
73 /** @defgroup USART_Private_Variables\r
74   * @{\r
75   */\r
76 \r
77 /**\r
78   * @}\r
79   */\r
80 \r
81 /** @defgroup USART_Private_FunctionPrototypes\r
82   * @{\r
83   */\r
84 \r
85 /**\r
86   * @}\r
87   */\r
88 \r
89 /** @defgroup USART_Private_Functions\r
90   * @{\r
91   */\r
92 \r
93 /**\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
97   * @retval None\r
98   */\r
99 void USART_DeInit(USART_TypeDef* USARTx)\r
100 {\r
101   /* Check the parameters */\r
102   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
103 \r
104   if (USARTx == USART1)\r
105   {\r
106     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);\r
107     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);\r
108   }\r
109   else if (USARTx == USART2)\r
110   {\r
111     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);\r
112     RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);\r
113   }\r
114   else \r
115   {\r
116     if (USARTx == USART3)\r
117     {\r
118       RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);\r
119       RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);    \r
120     }\r
121   }    \r
122 }\r
123 \r
124 /**\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
132   * @retval None\r
133   */\r
134 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)\r
135 {\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
140 \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
149   \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
154 \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
158   \r
159   /* Write to USART CR2 */\r
160   USARTx->CR2 = (uint16_t)tmpreg;\r
161 \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
166 \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
173 \r
174   /* Write to USART CR1 */\r
175   USARTx->CR1 = (uint16_t)tmpreg;\r
176 \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
181 \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
185 \r
186   /* Write to USART CR3 */\r
187   USARTx->CR3 = (uint16_t)tmpreg;\r
188 \r
189 /*---------------------------- USART BRR Configuration -----------------------*/\r
190   /* Configure the USART Baud Rate -------------------------------------------*/\r
191   RCC_GetClocksFreq(&RCC_ClocksStatus);\r
192   if (USARTx == USART1) \r
193   {\r
194     apbclock = RCC_ClocksStatus.PCLK2_Frequency;\r
195   }\r
196   else\r
197   {\r
198     apbclock = RCC_ClocksStatus.PCLK1_Frequency;\r
199   }\r
200 \r
201   /* Determine the integer part */\r
202   if ((USARTx->CR1 & USART_CR1_OVER8) != 0)\r
203   {\r
204     /* Integer part computing in case Oversampling mode is 8 Samples */\r
205     integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));    \r
206   }\r
207   else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */\r
208   {\r
209     /* Integer part computing in case Oversampling mode is 16 Samples */\r
210     integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));    \r
211   }\r
212   tmpreg = (integerdivider / 100) << 4;\r
213 \r
214   /* Determine the fractional part */\r
215   fractionaldivider = integerdivider - (100 * (tmpreg >> 4));\r
216 \r
217   /* Implement the fractional part in the register */\r
218   if ((USARTx->CR1 & USART_CR1_OVER8) != 0)\r
219   {\r
220     tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);\r
221   }\r
222   else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */\r
223   {\r
224     tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);\r
225   }\r
226  \r
227   /* Write to USART BRR */\r
228   USARTx->BRR = (uint16_t)tmpreg;\r
229 }\r
230 \r
231 /**\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
235   * @retval None\r
236   */\r
237 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)\r
238 {\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
246 }\r
247 \r
248 /**\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
255   * @retval None\r
256   */\r
257 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)\r
258 {\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
266   \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
280 }\r
281 \r
282 /**\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
286   * @retval None\r
287   */\r
288 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)\r
289 {\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
295 }\r
296 \r
297 /**\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
304   * @retval None\r
305   */\r
306 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
307 {\r
308   /* Check the parameters */\r
309   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
310   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
311   \r
312   if (NewState != DISABLE)\r
313   {\r
314     /* Enable the selected USART by setting the UE bit in the CR1 register */\r
315     USARTx->CR1 |= USART_CR1_UE;\r
316   }\r
317   else\r
318   {\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
321   }\r
322 }\r
323 \r
324 /**\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
341   * @retval None\r
342   */\r
343 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)\r
344 {\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
351   \r
352   usartxbase = (uint32_t)USARTx;\r
353 \r
354   /* Get the USART register index */\r
355   usartreg = (((uint8_t)USART_IT) >> 0x05);\r
356 \r
357   /* Get the interrupt position */\r
358   itpos = USART_IT & IT_MASK;\r
359   itmask = (((uint32_t)0x01) << itpos);\r
360     \r
361   if (usartreg == 0x01) /* The IT is in CR1 register */\r
362   {\r
363     usartxbase += 0x0C;\r
364   }\r
365   else if (usartreg == 0x02) /* The IT is in CR2 register */\r
366   {\r
367     usartxbase += 0x10;\r
368   }\r
369   else /* The IT is in CR3 register */\r
370   {\r
371     usartxbase += 0x14; \r
372   }\r
373   if (NewState != DISABLE)\r
374   {\r
375     *(__IO uint32_t*)usartxbase  |= itmask;\r
376   }\r
377   else\r
378   {\r
379     *(__IO uint32_t*)usartxbase &= ~itmask;\r
380   }\r
381 }\r
382 \r
383 /**\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
395   * @retval None\r
396   */\r
397 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)\r
398 {\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
403 \r
404   if (NewState != DISABLE)\r
405   {\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
409   }\r
410   else\r
411   {\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
415   }\r
416 }\r
417 \r
418 /**\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
424   * @retval None\r
425   */\r
426 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)\r
427 {\r
428   /* Check the parameters */\r
429   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
430   assert_param(IS_USART_ADDRESS(USART_Address)); \r
431     \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
436 }\r
437 \r
438 /**\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
447   * @retval None\r
448   */\r
449 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)\r
450 {\r
451   /* Check the parameters */\r
452   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
453   assert_param(IS_USART_WAKEUP(USART_WakeUp));\r
454   \r
455   USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_WAKE);\r
456   USARTx->CR1 |= USART_WakeUp;\r
457 }\r
458 \r
459 /**\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
466   * @retval None\r
467   */\r
468 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
469 {\r
470   /* Check the parameters */\r
471   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
472   assert_param(IS_FUNCTIONAL_STATE(NewState)); \r
473   \r
474   if (NewState != DISABLE)\r
475   {\r
476     /* Enable the USART mute mode  by setting the RWU bit in the CR1 register */\r
477     USARTx->CR1 |= USART_CR1_RWU;\r
478   }\r
479   else\r
480   {\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
483   }\r
484 }\r
485 \r
486 /**\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
495   * @retval None\r
496   */\r
497 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)\r
498 {\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
502   \r
503   USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LBDL);\r
504   USARTx->CR2 |= USART_LINBreakDetectLength;  \r
505 }\r
506 \r
507 /**\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
514   * @retval None\r
515   */\r
516 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
517 {\r
518   /* Check the parameters */\r
519   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
520   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
521   \r
522   if (NewState != DISABLE)\r
523   {\r
524     /* Enable the LIN mode by setting the LINEN bit in the CR2 register */\r
525     USARTx->CR2 |= USART_CR2_LINEN;\r
526   }\r
527   else\r
528   {\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
531   }\r
532 }\r
533 \r
534 /**\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
540   * @retval None\r
541   */\r
542 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)\r
543 {\r
544   /* Check the parameters */\r
545   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
546   assert_param(IS_USART_DATA(Data)); \r
547     \r
548   /* Transmit Data */\r
549   USARTx->DR = (Data & (uint16_t)0x01FF);\r
550 }\r
551 \r
552 /**\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
558   */\r
559 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)\r
560 {\r
561   /* Check the parameters */\r
562   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
563   \r
564   /* Receive Data */\r
565   return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);\r
566 }\r
567 \r
568 /**\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
573   * @retval None\r
574   */\r
575 void USART_SendBreak(USART_TypeDef* USARTx)\r
576 {\r
577   /* Check the parameters */\r
578   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
579   \r
580   /* Send break characters */\r
581   USARTx->CR1 |= USART_CR1_SBK;\r
582 }\r
583 \r
584 /**\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
591   * @retval None\r
592   */\r
593 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)\r
594 {    \r
595   /* Check the parameters */\r
596   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
597   \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
602 }\r
603 \r
604 /**\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
611   * @retval None\r
612   */\r
613 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)\r
614\r
615   /* Check the parameters */\r
616   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
617   \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
622 }\r
623 \r
624 /**\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
632   * @retval None\r
633   */\r
634 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
635 {\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
640   {\r
641     /* Enable the SC mode by setting the SCEN bit in the CR3 register */\r
642     USARTx->CR3 |= USART_CR3_SCEN;\r
643   }\r
644   else\r
645   {\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
648   }\r
649 }\r
650 \r
651 /**\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
659   * @retval None\r
660   */\r
661 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
662 {\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
667   {\r
668     /* Enable the NACK transmission by setting the NACK bit in the CR3 register */\r
669     USARTx->CR3 |= USART_CR3_NACK;\r
670   }\r
671   else\r
672   {\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
675   }\r
676 }\r
677 \r
678 /**\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
685   * @retval None\r
686   */\r
687 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
688 {\r
689   /* Check the parameters */\r
690   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
691   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
692   \r
693   if (NewState != DISABLE)\r
694   {\r
695     /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */\r
696     USARTx->CR3 |= USART_CR3_HDSEL;\r
697   }\r
698   else\r
699   {\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
702   }\r
703 }\r
704 \r
705 /**\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
712   *\r
713   * @note\r
714   *   This function has to be called before calling USART_Init()\r
715   *   function in order to have correct baudrate Divider value.\r
716   * @retval : None\r
717   */\r
718 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
719 {\r
720   /* Check the parameters */\r
721   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
722   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
723   \r
724   if (NewState != DISABLE)\r
725   {\r
726     /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */\r
727     USARTx->CR1 |= USART_CR1_OVER8;\r
728   }\r
729   else\r
730   {\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
733   }\r
734 }  \r
735 \r
736 /**\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
743   * @retval : None\r
744   */\r
745 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
746 {\r
747   /* Check the parameters */\r
748   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
749   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
750   \r
751   if (NewState != DISABLE)\r
752   {\r
753     /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */\r
754     USARTx->CR3 |= USART_CR3_ONEBIT;\r
755   }\r
756   else\r
757   {\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
760   }\r
761 }\r
762 \r
763 /**\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
772   * @retval None\r
773   */\r
774 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)\r
775 {\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
779     \r
780   USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IRLP);\r
781   USARTx->CR3 |= USART_IrDAMode;\r
782 }\r
783 \r
784 /**\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
791   * @retval None\r
792   */\r
793 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)\r
794 {\r
795   /* Check the parameters */\r
796   assert_param(IS_USART_ALL_PERIPH(USARTx));\r
797   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
798     \r
799   if (NewState != DISABLE)\r
800   {\r
801     /* Enable the IrDA mode by setting the IREN bit in the CR3 register */\r
802     USARTx->CR3 |= USART_CR3_IREN;\r
803   }\r
804   else\r
805   {\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
808   }\r
809 }\r
810 \r
811 /**\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
829   */\r
830 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)\r
831 {\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
836   \r
837   if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)\r
838   {\r
839     bitstatus = SET;\r
840   }\r
841   else\r
842   {\r
843     bitstatus = RESET;\r
844   }\r
845   return bitstatus;\r
846 }\r
847 \r
848 /**\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
859   *   \r
860   * @note\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
872   * @retval None\r
873   */\r
874 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)\r
875 {\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
879      \r
880   USARTx->SR = (uint16_t)~USART_FLAG;\r
881 }\r
882 \r
883 /**\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
901   */\r
902 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)\r
903 {\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
909   \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
915   \r
916   if (usartreg == 0x01) /* The IT  is in CR1 register */\r
917   {\r
918     itmask &= USARTx->CR1;\r
919   }\r
920   else if (usartreg == 0x02) /* The IT  is in CR2 register */\r
921   {\r
922     itmask &= USARTx->CR2;\r
923   }\r
924   else /* The IT  is in CR3 register */\r
925   {\r
926     itmask &= USARTx->CR3;\r
927   }\r
928   \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
933   {\r
934     bitstatus = SET;\r
935   }\r
936   else\r
937   {\r
938     bitstatus = RESET;\r
939   }\r
940   \r
941   return bitstatus;  \r
942 }\r
943 \r
944 /**\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
955   *   \r
956   * @note\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
969   * @retval None\r
970   */\r
971 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)\r
972 {\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
977   \r
978   bitpos = USART_IT >> 0x08;\r
979   itmask = ((uint16_t)0x01 << (uint16_t)bitpos);\r
980   USARTx->SR = (uint16_t)~itmask;\r
981 }\r
982 \r
983 /**\r
984   * @}\r
985   */\r
986 \r
987 /**\r
988   * @}\r
989   */\r
990 \r
991 /**\r
992   * @}\r
993   */\r
994 \r
995 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r