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