1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_usart.h
4   * @author  MCD Application Team
5   * @version V1.1.2
6   * @date    05-March-2012
7   * @brief   This file contains all the functions prototypes for the USART
8   *          firmware library.
9   ******************************************************************************
10   * @attention
11   *
12   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
13   *
14   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
15   * You may not use this file except in compliance with the License.
16   * You may obtain a copy of the License at:
17   *
18   *        http://www.st.com/software_license_agreement_liberty_v2
19   *
20   * Unless required by applicable law or agreed to in writing, software
21   * distributed under the License is distributed on an "AS IS" BASIS,
22   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23   * See the License for the specific language governing permissions and
24   * limitations under the License.
25   *
26   ******************************************************************************
27   */
28 
29 /* Define to prevent recursive inclusion -------------------------------------*/
30 #ifndef __STM32F2xx_USART_H
31 #define __STM32F2xx_USART_H
32 
33 #ifdef __cplusplus
34  extern "C" {
35 #endif
36 
37 /* Includes ------------------------------------------------------------------*/
38 #include "stm32f2xx.h"
39 
40 /** @addtogroup STM32F2xx_StdPeriph_Driver
41   * @{
42   */
43 
44 /** @addtogroup USART
45   * @{
46   */
47 
48 /* Exported types ------------------------------------------------------------*/
49 
50 /**
51   * @brief  USART Init Structure definition
52   */
53 
54 typedef struct
55 {
56   uint32_t USART_BaudRate;            /*!< This member configures the USART communication baud rate.
57                                            The baud rate is computed using the following formula:
58                                             - IntegerDivider = ((PCLKx) / (8 * (OVR8+1) * (USART_InitStruct->USART_BaudRate)))
59                                             - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 8 * (OVR8+1)) + 0.5
60                                            Where OVR8 is the "oversampling by 8 mode" configuration bit in the CR1 register. */
61 
62   uint16_t USART_WordLength;          /*!< Specifies the number of data bits transmitted or received in a frame.
63                                            This parameter can be a value of @ref USART_Word_Length */
64 
65   uint16_t USART_StopBits;            /*!< Specifies the number of stop bits transmitted.
66                                            This parameter can be a value of @ref USART_Stop_Bits */
67 
68   uint16_t USART_Parity;              /*!< Specifies the parity mode.
69                                            This parameter can be a value of @ref USART_Parity
70                                            @note When parity is enabled, the computed parity is inserted
71                                                  at the MSB position of the transmitted data (9th bit when
72                                                  the word length is set to 9 data bits; 8th bit when the
73                                                  word length is set to 8 data bits). */
74 
75   uint16_t USART_Mode;                /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
76                                            This parameter can be a value of @ref USART_Mode */
77 
78   uint16_t USART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled
79                                            or disabled.
80                                            This parameter can be a value of @ref USART_Hardware_Flow_Control */
81 } USART_InitTypeDef;
82 
83 /**
84   * @brief  USART Clock Init Structure definition
85   */
86 
87 typedef struct
88 {
89 
90   uint16_t USART_Clock;   /*!< Specifies whether the USART clock is enabled or disabled.
91                                This parameter can be a value of @ref USART_Clock */
92 
93   uint16_t USART_CPOL;    /*!< Specifies the steady state of the serial clock.
94                                This parameter can be a value of @ref USART_Clock_Polarity */
95 
96   uint16_t USART_CPHA;    /*!< Specifies the clock transition on which the bit capture is made.
97                                This parameter can be a value of @ref USART_Clock_Phase */
98 
99   uint16_t USART_LastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
100                                data bit (MSB) has to be output on the SCLK pin in synchronous mode.
101                                This parameter can be a value of @ref USART_Last_Bit */
102 } USART_ClockInitTypeDef;
103 
104 /* Exported constants --------------------------------------------------------*/
105 
106 /** @defgroup USART_Exported_Constants
107   * @{
108   */
109 
110 #define IS_USART_ALL_PERIPH(PERIPH) (((PERIPH) == USART1) || \
111                                      ((PERIPH) == USART2) || \
112                                      ((PERIPH) == USART3) || \
113                                      ((PERIPH) == UART4)  || \
114                                      ((PERIPH) == UART5)  || \
115                                      ((PERIPH) == USART6))
116 
117 #define IS_USART_1236_PERIPH(PERIPH) (((PERIPH) == USART1) || \
118                                       ((PERIPH) == USART2) || \
119                                       ((PERIPH) == USART3) || \
120                                       ((PERIPH) == USART6))
121 
122 /** @defgroup USART_Word_Length
123   * @{
124   */
125 
126 #define USART_WordLength_8b                  ((uint16_t)0x0000)
127 #define USART_WordLength_9b                  ((uint16_t)0x1000)
128 
129 #define IS_USART_WORD_LENGTH(LENGTH) (((LENGTH) == USART_WordLength_8b) || \
130                                       ((LENGTH) == USART_WordLength_9b))
131 /**
132   * @}
133   */
134 
135 /** @defgroup USART_Stop_Bits
136   * @{
137   */
138 
139 #define USART_StopBits_1                     ((uint16_t)0x0000)
140 #define USART_StopBits_0_5                   ((uint16_t)0x1000)
141 #define USART_StopBits_2                     ((uint16_t)0x2000)
142 #define USART_StopBits_1_5                   ((uint16_t)0x3000)
143 #define IS_USART_STOPBITS(STOPBITS) (((STOPBITS) == USART_StopBits_1) || \
144                                      ((STOPBITS) == USART_StopBits_0_5) || \
145                                      ((STOPBITS) == USART_StopBits_2) || \
146                                      ((STOPBITS) == USART_StopBits_1_5))
147 /**
148   * @}
149   */
150 
151 /** @defgroup USART_Parity
152   * @{
153   */
154 
155 #define USART_Parity_No                      ((uint16_t)0x0000)
156 #define USART_Parity_Even                    ((uint16_t)0x0400)
157 #define USART_Parity_Odd                     ((uint16_t)0x0600)
158 #define IS_USART_PARITY(PARITY) (((PARITY) == USART_Parity_No) || \
159                                  ((PARITY) == USART_Parity_Even) || \
160                                  ((PARITY) == USART_Parity_Odd))
161 /**
162   * @}
163   */
164 
165 /** @defgroup USART_Mode
166   * @{
167   */
168 
169 #define USART_Mode_Rx                        ((uint16_t)0x0004)
170 #define USART_Mode_Tx                        ((uint16_t)0x0008)
171 #define IS_USART_MODE(MODE) ((((MODE) & (uint16_t)0xFFF3) == 0x00) && ((MODE) != (uint16_t)0x00))
172 /**
173   * @}
174   */
175 
176 /** @defgroup USART_Hardware_Flow_Control
177   * @{
178   */
179 #define USART_HardwareFlowControl_None       ((uint16_t)0x0000)
180 #define USART_HardwareFlowControl_RTS        ((uint16_t)0x0100)
181 #define USART_HardwareFlowControl_CTS        ((uint16_t)0x0200)
182 #define USART_HardwareFlowControl_RTS_CTS    ((uint16_t)0x0300)
183 #define IS_USART_HARDWARE_FLOW_CONTROL(CONTROL)\
184                               (((CONTROL) == USART_HardwareFlowControl_None) || \
185                                ((CONTROL) == USART_HardwareFlowControl_RTS) || \
186                                ((CONTROL) == USART_HardwareFlowControl_CTS) || \
187                                ((CONTROL) == USART_HardwareFlowControl_RTS_CTS))
188 /**
189   * @}
190   */
191 
192 /** @defgroup USART_Clock
193   * @{
194   */
195 #define USART_Clock_Disable                  ((uint16_t)0x0000)
196 #define USART_Clock_Enable                   ((uint16_t)0x0800)
197 #define IS_USART_CLOCK(CLOCK) (((CLOCK) == USART_Clock_Disable) || \
198                                ((CLOCK) == USART_Clock_Enable))
199 /**
200   * @}
201   */
202 
203 /** @defgroup USART_Clock_Polarity
204   * @{
205   */
206 
207 #define USART_CPOL_Low                       ((uint16_t)0x0000)
208 #define USART_CPOL_High                      ((uint16_t)0x0400)
209 #define IS_USART_CPOL(CPOL) (((CPOL) == USART_CPOL_Low) || ((CPOL) == USART_CPOL_High))
210 
211 /**
212   * @}
213   */
214 
215 /** @defgroup USART_Clock_Phase
216   * @{
217   */
218 
219 #define USART_CPHA_1Edge                     ((uint16_t)0x0000)
220 #define USART_CPHA_2Edge                     ((uint16_t)0x0200)
221 #define IS_USART_CPHA(CPHA) (((CPHA) == USART_CPHA_1Edge) || ((CPHA) == USART_CPHA_2Edge))
222 
223 /**
224   * @}
225   */
226 
227 /** @defgroup USART_Last_Bit
228   * @{
229   */
230 
231 #define USART_LastBit_Disable                ((uint16_t)0x0000)
232 #define USART_LastBit_Enable                 ((uint16_t)0x0100)
233 #define IS_USART_LASTBIT(LASTBIT) (((LASTBIT) == USART_LastBit_Disable) || \
234                                    ((LASTBIT) == USART_LastBit_Enable))
235 /**
236   * @}
237   */
238 
239 /** @defgroup USART_Interrupt_definition
240   * @{
241   */
242 
243 #define USART_IT_PE                          ((uint16_t)0x0028)
244 #define USART_IT_TXE                         ((uint16_t)0x0727)
245 #define USART_IT_TC                          ((uint16_t)0x0626)
246 #define USART_IT_RXNE                        ((uint16_t)0x0525)
247 #define USART_IT_ORE_RX                      ((uint16_t)0x0325) /* In case interrupt is generated if the RXNEIE bit is set */
248 #define USART_IT_IDLE                        ((uint16_t)0x0424)
249 #define USART_IT_LBD                         ((uint16_t)0x0846)
250 #define USART_IT_CTS                         ((uint16_t)0x096A)
251 #define USART_IT_ERR                         ((uint16_t)0x0060)
252 #define USART_IT_ORE_ER                      ((uint16_t)0x0360) /* In case interrupt is generated if the EIE bit is set */
253 #define USART_IT_NE                          ((uint16_t)0x0260)
254 #define USART_IT_FE                          ((uint16_t)0x0160)
255 
256 /** @defgroup USART_Legacy
257   * @{
258   */
259 #define USART_IT_ORE                          USART_IT_ORE_ER
260 /**
261   * @}
262   */
263 
264 #define IS_USART_CONFIG_IT(IT) (((IT) == USART_IT_PE) || ((IT) == USART_IT_TXE) || \
265                                 ((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \
266                                 ((IT) == USART_IT_IDLE) || ((IT) == USART_IT_LBD) || \
267                                 ((IT) == USART_IT_CTS) || ((IT) == USART_IT_ERR))
268 #define IS_USART_GET_IT(IT) (((IT) == USART_IT_PE) || ((IT) == USART_IT_TXE) || \
269                              ((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \
270                              ((IT) == USART_IT_IDLE) || ((IT) == USART_IT_LBD) || \
271                              ((IT) == USART_IT_CTS) || ((IT) == USART_IT_ORE) || \
272                              ((IT) == USART_IT_ORE_RX) || ((IT) == USART_IT_ORE_ER) || \
273                              ((IT) == USART_IT_NE) || ((IT) == USART_IT_FE))
274 #define IS_USART_CLEAR_IT(IT) (((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \
275                                ((IT) == USART_IT_LBD) || ((IT) == USART_IT_CTS))
276 /**
277   * @}
278   */
279 
280 /** @defgroup USART_DMA_Requests
281   * @{
282   */
283 
284 #define USART_DMAReq_Tx                      ((uint16_t)0x0080)
285 #define USART_DMAReq_Rx                      ((uint16_t)0x0040)
286 #define IS_USART_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFF3F) == 0x00) && ((DMAREQ) != (uint16_t)0x00))
287 
288 /**
289   * @}
290   */
291 
292 /** @defgroup USART_WakeUp_methods
293   * @{
294   */
295 
296 #define USART_WakeUp_IdleLine                ((uint16_t)0x0000)
297 #define USART_WakeUp_AddressMark             ((uint16_t)0x0800)
298 #define IS_USART_WAKEUP(WAKEUP) (((WAKEUP) == USART_WakeUp_IdleLine) || \
299                                  ((WAKEUP) == USART_WakeUp_AddressMark))
300 /**
301   * @}
302   */
303 
304 /** @defgroup USART_LIN_Break_Detection_Length
305   * @{
306   */
307 
308 #define USART_LINBreakDetectLength_10b      ((uint16_t)0x0000)
309 #define USART_LINBreakDetectLength_11b      ((uint16_t)0x0020)
310 #define IS_USART_LIN_BREAK_DETECT_LENGTH(LENGTH) \
311                                (((LENGTH) == USART_LINBreakDetectLength_10b) || \
312                                 ((LENGTH) == USART_LINBreakDetectLength_11b))
313 /**
314   * @}
315   */
316 
317 /** @defgroup USART_IrDA_Low_Power
318   * @{
319   */
320 
321 #define USART_IrDAMode_LowPower              ((uint16_t)0x0004)
322 #define USART_IrDAMode_Normal                ((uint16_t)0x0000)
323 #define IS_USART_IRDA_MODE(MODE) (((MODE) == USART_IrDAMode_LowPower) || \
324                                   ((MODE) == USART_IrDAMode_Normal))
325 /**
326   * @}
327   */
328 
329 /** @defgroup USART_Flags
330   * @{
331   */
332 
333 #define USART_FLAG_CTS                       ((uint16_t)0x0200)
334 #define USART_FLAG_LBD                       ((uint16_t)0x0100)
335 #define USART_FLAG_TXE                       ((uint16_t)0x0080)
336 #define USART_FLAG_TC                        ((uint16_t)0x0040)
337 #define USART_FLAG_RXNE                      ((uint16_t)0x0020)
338 #define USART_FLAG_IDLE                      ((uint16_t)0x0010)
339 #define USART_FLAG_ORE                       ((uint16_t)0x0008)
340 #define USART_FLAG_NE                        ((uint16_t)0x0004)
341 #define USART_FLAG_FE                        ((uint16_t)0x0002)
342 #define USART_FLAG_PE                        ((uint16_t)0x0001)
343 #define IS_USART_FLAG(FLAG) (((FLAG) == USART_FLAG_PE) || ((FLAG) == USART_FLAG_TXE) || \
344                              ((FLAG) == USART_FLAG_TC) || ((FLAG) == USART_FLAG_RXNE) || \
345                              ((FLAG) == USART_FLAG_IDLE) || ((FLAG) == USART_FLAG_LBD) || \
346                              ((FLAG) == USART_FLAG_CTS) || ((FLAG) == USART_FLAG_ORE) || \
347                              ((FLAG) == USART_FLAG_NE) || ((FLAG) == USART_FLAG_FE))
348 
349 #define IS_USART_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xFC9F) == 0x00) && ((FLAG) != (uint16_t)0x00))
350 
351 #define IS_USART_BAUDRATE(BAUDRATE) (((BAUDRATE) > 0) && ((BAUDRATE) < 7500001))
352 #define IS_USART_ADDRESS(ADDRESS) ((ADDRESS) <= 0xF)
353 #define IS_USART_DATA(DATA) ((DATA) <= 0x1FF)
354 
355 /**
356   * @}
357   */
358 
359 /**
360   * @}
361   */
362 
363 /* Exported macro ------------------------------------------------------------*/
364 /* Exported functions --------------------------------------------------------*/
365 
366 /*  Function used to set the USART configuration to the default reset state ***/
367 void USART_DeInit(USART_TypeDef* USARTx);
368 
369 /* Initialization and Configuration functions *********************************/
370 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);
371 void USART_StructInit(USART_InitTypeDef* USART_InitStruct);
372 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct);
373 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct);
374 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
375 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler);
376 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
377 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState);
378 
379 /* Data transfers functions ***************************************************/
380 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
381 uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
382 
383 /* Multi-Processor Communication functions ************************************/
384 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address);
385 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp);
386 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState);
387 
388 /* LIN mode functions *********************************************************/
389 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength);
390 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState);
391 void USART_SendBreak(USART_TypeDef* USARTx);
392 
393 /* Half-duplex mode function **************************************************/
394 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState);
395 
396 /* Smartcard mode functions ***************************************************/
397 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState);
398 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState);
399 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime);
400 
401 /* IrDA mode functions ********************************************************/
402 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode);
403 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState);
404 
405 /* DMA transfers management functions *****************************************/
406 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
407 
408 /* Interrupts and flags management functions **********************************/
409 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
410 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
411 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);
412 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
413 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);
414 
415 #ifdef __cplusplus
416 }
417 #endif
418 
419 #endif /* __STM32F2xx_USART_H */
420 
421 /**
422   * @}
423   */
424 
425 /**
426   * @}
427   */
428 
429 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
430