stm32f10x_usart.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_usart.h
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 11-March-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. /* Define to prevent recursive inclusion -------------------------------------*/
  23. #ifndef __STM32F10x_USART_H
  24. #define __STM32F10x_USART_H
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /* Includes ------------------------------------------------------------------*/
  29. #include "stm32f10x.h"
  30. /** @addtogroup STM32F10x_StdPeriph_Driver
  31. * @{
  32. */
  33. /** @addtogroup USART
  34. * @{
  35. */
  36. /** @defgroup USART_Exported_Types
  37. * @{
  38. */
  39. /**
  40. * @brief USART Init Structure definition
  41. */
  42. typedef struct
  43. {
  44. uint32_t USART_BaudRate; /*!< This member configures the USART communication baud rate.
  45. The baud rate is computed using the following formula:
  46. - IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate)))
  47. - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
  48. uint16_t USART_WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  49. This parameter can be a value of @ref USART_Word_Length */
  50. uint16_t USART_StopBits; /*!< Specifies the number of stop bits transmitted.
  51. This parameter can be a value of @ref USART_Stop_Bits */
  52. uint16_t USART_Parity; /*!< Specifies the parity mode.
  53. This parameter can be a value of @ref USART_Parity
  54. @note When parity is enabled, the computed parity is inserted
  55. at the MSB position of the transmitted data (9th bit when
  56. the word length is set to 9 data bits; 8th bit when the
  57. word length is set to 8 data bits). */
  58. uint16_t USART_Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
  59. This parameter can be a value of @ref USART_Mode */
  60. uint16_t USART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled
  61. or disabled.
  62. This parameter can be a value of @ref USART_Hardware_Flow_Control */
  63. } USART_InitTypeDef;
  64. /**
  65. * @brief USART Clock Init Structure definition
  66. */
  67. typedef struct
  68. {
  69. uint16_t USART_Clock; /*!< Specifies whether the USART clock is enabled or disabled.
  70. This parameter can be a value of @ref USART_Clock */
  71. uint16_t USART_CPOL; /*!< Specifies the steady state value of the serial clock.
  72. This parameter can be a value of @ref USART_Clock_Polarity */
  73. uint16_t USART_CPHA; /*!< Specifies the clock transition on which the bit capture is made.
  74. This parameter can be a value of @ref USART_Clock_Phase */
  75. uint16_t USART_LastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
  76. data bit (MSB) has to be output on the SCLK pin in synchronous mode.
  77. This parameter can be a value of @ref USART_Last_Bit */
  78. } USART_ClockInitTypeDef;
  79. /**
  80. * @}
  81. */
  82. /** @defgroup USART_Exported_Constants
  83. * @{
  84. */
  85. #define IS_USART_ALL_PERIPH(PERIPH) (((PERIPH) == USART1) || \
  86. ((PERIPH) == USART2) || \
  87. ((PERIPH) == USART3) || \
  88. ((PERIPH) == UART4) || \
  89. ((PERIPH) == UART5))
  90. #define IS_USART_123_PERIPH(PERIPH) (((PERIPH) == USART1) || \
  91. ((PERIPH) == USART2) || \
  92. ((PERIPH) == USART3))
  93. #define IS_USART_1234_PERIPH(PERIPH) (((PERIPH) == USART1) || \
  94. ((PERIPH) == USART2) || \
  95. ((PERIPH) == USART3) || \
  96. ((PERIPH) == UART4))
  97. /** @defgroup USART_Word_Length
  98. * @{
  99. */
  100. #define USART_WordLength_8b ((uint16_t)0x0000)
  101. #define USART_WordLength_9b ((uint16_t)0x1000)
  102. #define IS_USART_WORD_LENGTH(LENGTH) (((LENGTH) == USART_WordLength_8b) || \
  103. ((LENGTH) == USART_WordLength_9b))
  104. /**
  105. * @}
  106. */
  107. /** @defgroup USART_Stop_Bits
  108. * @{
  109. */
  110. #define USART_StopBits_1 ((uint16_t)0x0000)
  111. #define USART_StopBits_0_5 ((uint16_t)0x1000)
  112. #define USART_StopBits_2 ((uint16_t)0x2000)
  113. #define USART_StopBits_1_5 ((uint16_t)0x3000)
  114. #define IS_USART_STOPBITS(STOPBITS) (((STOPBITS) == USART_StopBits_1) || \
  115. ((STOPBITS) == USART_StopBits_0_5) || \
  116. ((STOPBITS) == USART_StopBits_2) || \
  117. ((STOPBITS) == USART_StopBits_1_5))
  118. /**
  119. * @}
  120. */
  121. /** @defgroup USART_Parity
  122. * @{
  123. */
  124. #define USART_Parity_No ((uint16_t)0x0000)
  125. #define USART_Parity_Even ((uint16_t)0x0400)
  126. #define USART_Parity_Odd ((uint16_t)0x0600)
  127. #define IS_USART_PARITY(PARITY) (((PARITY) == USART_Parity_No) || \
  128. ((PARITY) == USART_Parity_Even) || \
  129. ((PARITY) == USART_Parity_Odd))
  130. /**
  131. * @}
  132. */
  133. /** @defgroup USART_Mode
  134. * @{
  135. */
  136. #define USART_Mode_Rx ((uint16_t)0x0004)
  137. #define USART_Mode_Tx ((uint16_t)0x0008)
  138. #define IS_USART_MODE(MODE) ((((MODE) & (uint16_t)0xFFF3) == 0x00) && ((MODE) != (uint16_t)0x00))
  139. /**
  140. * @}
  141. */
  142. /** @defgroup USART_Hardware_Flow_Control
  143. * @{
  144. */
  145. #define USART_HardwareFlowControl_None ((uint16_t)0x0000)
  146. #define USART_HardwareFlowControl_RTS ((uint16_t)0x0100)
  147. #define USART_HardwareFlowControl_CTS ((uint16_t)0x0200)
  148. #define USART_HardwareFlowControl_RTS_CTS ((uint16_t)0x0300)
  149. #define IS_USART_HARDWARE_FLOW_CONTROL(CONTROL)\
  150. (((CONTROL) == USART_HardwareFlowControl_None) || \
  151. ((CONTROL) == USART_HardwareFlowControl_RTS) || \
  152. ((CONTROL) == USART_HardwareFlowControl_CTS) || \
  153. ((CONTROL) == USART_HardwareFlowControl_RTS_CTS))
  154. /**
  155. * @}
  156. */
  157. /** @defgroup USART_Clock
  158. * @{
  159. */
  160. #define USART_Clock_Disable ((uint16_t)0x0000)
  161. #define USART_Clock_Enable ((uint16_t)0x0800)
  162. #define IS_USART_CLOCK(CLOCK) (((CLOCK) == USART_Clock_Disable) || \
  163. ((CLOCK) == USART_Clock_Enable))
  164. /**
  165. * @}
  166. */
  167. /** @defgroup USART_Clock_Polarity
  168. * @{
  169. */
  170. #define USART_CPOL_Low ((uint16_t)0x0000)
  171. #define USART_CPOL_High ((uint16_t)0x0400)
  172. #define IS_USART_CPOL(CPOL) (((CPOL) == USART_CPOL_Low) || ((CPOL) == USART_CPOL_High))
  173. /**
  174. * @}
  175. */
  176. /** @defgroup USART_Clock_Phase
  177. * @{
  178. */
  179. #define USART_CPHA_1Edge ((uint16_t)0x0000)
  180. #define USART_CPHA_2Edge ((uint16_t)0x0200)
  181. #define IS_USART_CPHA(CPHA) (((CPHA) == USART_CPHA_1Edge) || ((CPHA) == USART_CPHA_2Edge))
  182. /**
  183. * @}
  184. */
  185. /** @defgroup USART_Last_Bit
  186. * @{
  187. */
  188. #define USART_LastBit_Disable ((uint16_t)0x0000)
  189. #define USART_LastBit_Enable ((uint16_t)0x0100)
  190. #define IS_USART_LASTBIT(LASTBIT) (((LASTBIT) == USART_LastBit_Disable) || \
  191. ((LASTBIT) == USART_LastBit_Enable))
  192. /**
  193. * @}
  194. */
  195. /** @defgroup USART_Interrupt_definition
  196. * @{
  197. */
  198. #define USART_IT_PE ((uint16_t)0x0028)
  199. #define USART_IT_TXE ((uint16_t)0x0727)
  200. #define USART_IT_TC ((uint16_t)0x0626)
  201. #define USART_IT_RXNE ((uint16_t)0x0525)
  202. #define USART_IT_IDLE ((uint16_t)0x0424)
  203. #define USART_IT_LBD ((uint16_t)0x0846)
  204. #define USART_IT_CTS ((uint16_t)0x096A)
  205. #define USART_IT_ERR ((uint16_t)0x0060)
  206. #define USART_IT_ORE ((uint16_t)0x0360)
  207. #define USART_IT_NE ((uint16_t)0x0260)
  208. #define USART_IT_FE ((uint16_t)0x0160)
  209. #define IS_USART_CONFIG_IT(IT) (((IT) == USART_IT_PE) || ((IT) == USART_IT_TXE) || \
  210. ((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \
  211. ((IT) == USART_IT_IDLE) || ((IT) == USART_IT_LBD) || \
  212. ((IT) == USART_IT_CTS) || ((IT) == USART_IT_ERR))
  213. #define IS_USART_GET_IT(IT) (((IT) == USART_IT_PE) || ((IT) == USART_IT_TXE) || \
  214. ((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \
  215. ((IT) == USART_IT_IDLE) || ((IT) == USART_IT_LBD) || \
  216. ((IT) == USART_IT_CTS) || ((IT) == USART_IT_ORE) || \
  217. ((IT) == USART_IT_NE) || ((IT) == USART_IT_FE))
  218. #define IS_USART_CLEAR_IT(IT) (((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \
  219. ((IT) == USART_IT_LBD) || ((IT) == USART_IT_CTS))
  220. /**
  221. * @}
  222. */
  223. /** @defgroup USART_DMA_Requests
  224. * @{
  225. */
  226. #define USART_DMAReq_Tx ((uint16_t)0x0080)
  227. #define USART_DMAReq_Rx ((uint16_t)0x0040)
  228. #define IS_USART_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFF3F) == 0x00) && ((DMAREQ) != (uint16_t)0x00))
  229. /**
  230. * @}
  231. */
  232. /** @defgroup USART_WakeUp_methods
  233. * @{
  234. */
  235. #define USART_WakeUp_IdleLine ((uint16_t)0x0000)
  236. #define USART_WakeUp_AddressMark ((uint16_t)0x0800)
  237. #define IS_USART_WAKEUP(WAKEUP) (((WAKEUP) == USART_WakeUp_IdleLine) || \
  238. ((WAKEUP) == USART_WakeUp_AddressMark))
  239. /**
  240. * @}
  241. */
  242. /** @defgroup USART_LIN_Break_Detection_Length
  243. * @{
  244. */
  245. #define USART_LINBreakDetectLength_10b ((uint16_t)0x0000)
  246. #define USART_LINBreakDetectLength_11b ((uint16_t)0x0020)
  247. #define IS_USART_LIN_BREAK_DETECT_LENGTH(LENGTH) \
  248. (((LENGTH) == USART_LINBreakDetectLength_10b) || \
  249. ((LENGTH) == USART_LINBreakDetectLength_11b))
  250. /**
  251. * @}
  252. */
  253. /** @defgroup USART_IrDA_Low_Power
  254. * @{
  255. */
  256. #define USART_IrDAMode_LowPower ((uint16_t)0x0004)
  257. #define USART_IrDAMode_Normal ((uint16_t)0x0000)
  258. #define IS_USART_IRDA_MODE(MODE) (((MODE) == USART_IrDAMode_LowPower) || \
  259. ((MODE) == USART_IrDAMode_Normal))
  260. /**
  261. * @}
  262. */
  263. /** @defgroup USART_Flags
  264. * @{
  265. */
  266. #define USART_FLAG_CTS ((uint16_t)0x0200)
  267. #define USART_FLAG_LBD ((uint16_t)0x0100)
  268. #define USART_FLAG_TXE ((uint16_t)0x0080)
  269. #define USART_FLAG_TC ((uint16_t)0x0040)
  270. #define USART_FLAG_RXNE ((uint16_t)0x0020)
  271. #define USART_FLAG_IDLE ((uint16_t)0x0010)
  272. #define USART_FLAG_ORE ((uint16_t)0x0008)
  273. #define USART_FLAG_NE ((uint16_t)0x0004)
  274. #define USART_FLAG_FE ((uint16_t)0x0002)
  275. #define USART_FLAG_PE ((uint16_t)0x0001)
  276. #define IS_USART_FLAG(FLAG) (((FLAG) == USART_FLAG_PE) || ((FLAG) == USART_FLAG_TXE) || \
  277. ((FLAG) == USART_FLAG_TC) || ((FLAG) == USART_FLAG_RXNE) || \
  278. ((FLAG) == USART_FLAG_IDLE) || ((FLAG) == USART_FLAG_LBD) || \
  279. ((FLAG) == USART_FLAG_CTS) || ((FLAG) == USART_FLAG_ORE) || \
  280. ((FLAG) == USART_FLAG_NE) || ((FLAG) == USART_FLAG_FE))
  281. #define IS_USART_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xFC9F) == 0x00) && ((FLAG) != (uint16_t)0x00))
  282. #define IS_USART_PERIPH_FLAG(PERIPH, USART_FLAG) ((((*(uint32_t*)&(PERIPH)) != UART4_BASE) &&\
  283. ((*(uint32_t*)&(PERIPH)) != UART5_BASE)) \
  284. || ((USART_FLAG) != USART_FLAG_CTS))
  285. #define IS_USART_BAUDRATE(BAUDRATE) (((BAUDRATE) > 0) && ((BAUDRATE) < 0x0044AA21))
  286. #define IS_USART_ADDRESS(ADDRESS) ((ADDRESS) <= 0xF)
  287. #define IS_USART_DATA(DATA) ((DATA) <= 0x1FF)
  288. /**
  289. * @}
  290. */
  291. /**
  292. * @}
  293. */
  294. /** @defgroup USART_Exported_Macros
  295. * @{
  296. */
  297. /**
  298. * @}
  299. */
  300. /** @defgroup USART_Exported_Functions
  301. * @{
  302. */
  303. void USART_DeInit(USART_TypeDef* USARTx);
  304. void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);
  305. void USART_StructInit(USART_InitTypeDef* USART_InitStruct);
  306. void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct);
  307. void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct);
  308. void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
  309. void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
  310. void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
  311. void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address);
  312. void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp);
  313. void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  314. void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength);
  315. void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  316. void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
  317. uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
  318. void USART_SendBreak(USART_TypeDef* USARTx);
  319. void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime);
  320. void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler);
  321. void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  322. void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  323. void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  324. void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
  325. void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  326. void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode);
  327. void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState);
  328. FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
  329. void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);
  330. ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
  331. void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);
  332. #ifdef __cplusplus
  333. }
  334. #endif
  335. #endif /* __STM32F10x_USART_H */
  336. /**
  337. * @}
  338. */
  339. /**
  340. * @}
  341. */
  342. /**
  343. * @}
  344. */
  345. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/