stm32f10x_usart.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. //--波特率
  45. uint32_t USART_BaudRate; /*!< This member configures the USART communication baud rate.
  46. The baud rate is computed using the following formula:
  47. - IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate)))
  48. - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
  49. //--字长
  50. uint16_t USART_WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  51. This parameter can be a value of @ref USART_Word_Length */
  52. ///--停止位
  53. uint16_t USART_StopBits; /*!< Specifies the number of stop bits transmitted.
  54. This parameter can be a value of @ref USART_Stop_Bits */
  55. //奇偶校验-
  56. uint16_t USART_Parity; /*!< Specifies the parity mode.
  57. This parameter can be a value of @ref USART_Parity
  58. @note When parity is enabled, the computed parity is inserted
  59. at the MSB position of the transmitted data (9th bit when
  60. the word length is set to 9 data bits; 8th bit when the
  61. word length is set to 8 data bits). */
  62. //--发送/接收使能
  63. uint16_t USART_Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
  64. This parameter can be a value of @ref USART_Mode */
  65. //--硬件流控制
  66. uint16_t USART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled
  67. or disabled.
  68. This parameter can be a value of @ref USART_Hardware_Flow_Control */
  69. } USART_InitTypeDef;
  70. /**
  71. * @brief USART Clock Init Structure definition
  72. */
  73. typedef struct
  74. {
  75. uint16_t USART_Clock; /*!< Specifies whether the USART clock is enabled or disabled.
  76. This parameter can be a value of @ref USART_Clock */
  77. uint16_t USART_CPOL; /*!< Specifies the steady state value of the serial clock.
  78. This parameter can be a value of @ref USART_Clock_Polarity */
  79. uint16_t USART_CPHA; /*!< Specifies the clock transition on which the bit capture is made.
  80. This parameter can be a value of @ref USART_Clock_Phase */
  81. uint16_t USART_LastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
  82. data bit (MSB) has to be output on the SCLK pin in synchronous mode.
  83. This parameter can be a value of @ref USART_Last_Bit */
  84. } USART_ClockInitTypeDef;
  85. /**
  86. * @}
  87. */
  88. /** @defgroup USART_Exported_Constants
  89. * @{
  90. */
  91. #define IS_USART_ALL_PERIPH(PERIPH) (((PERIPH) == USART1) || \
  92. ((PERIPH) == USART2) || \
  93. ((PERIPH) == USART3) || \
  94. ((PERIPH) == UART4) || \
  95. ((PERIPH) == UART5))
  96. #define IS_USART_123_PERIPH(PERIPH) (((PERIPH) == USART1) || \
  97. ((PERIPH) == USART2) || \
  98. ((PERIPH) == USART3))
  99. #define IS_USART_1234_PERIPH(PERIPH) (((PERIPH) == USART1) || \
  100. ((PERIPH) == USART2) || \
  101. ((PERIPH) == USART3) || \
  102. ((PERIPH) == UART4))
  103. /** @defgroup USART_Word_Length
  104. * @{
  105. */
  106. #define USART_WordLength_8b ((uint16_t)0x0000)
  107. #define USART_WordLength_9b ((uint16_t)0x1000)
  108. #define IS_USART_WORD_LENGTH(LENGTH) (((LENGTH) == USART_WordLength_8b) || \
  109. ((LENGTH) == USART_WordLength_9b))
  110. /**
  111. * @}
  112. */
  113. /** @defgroup USART_Stop_Bits
  114. * @{
  115. */
  116. #define USART_StopBits_1 ((uint16_t)0x0000)
  117. #define USART_StopBits_0_5 ((uint16_t)0x1000)
  118. #define USART_StopBits_2 ((uint16_t)0x2000)
  119. #define USART_StopBits_1_5 ((uint16_t)0x3000)
  120. #define IS_USART_STOPBITS(STOPBITS) (((STOPBITS) == USART_StopBits_1) || \
  121. ((STOPBITS) == USART_StopBits_0_5) || \
  122. ((STOPBITS) == USART_StopBits_2) || \
  123. ((STOPBITS) == USART_StopBits_1_5))
  124. /**
  125. * @}
  126. */
  127. /** @defgroup USART_Parity
  128. * @{
  129. */
  130. #define USART_Parity_No ((uint16_t)0x0000)
  131. #define USART_Parity_Even ((uint16_t)0x0400)
  132. #define USART_Parity_Odd ((uint16_t)0x0600)
  133. #define IS_USART_PARITY(PARITY) (((PARITY) == USART_Parity_No) || \
  134. ((PARITY) == USART_Parity_Even) || \
  135. ((PARITY) == USART_Parity_Odd))
  136. /**
  137. * @}
  138. */
  139. /** @defgroup USART_Mode
  140. * @{
  141. */
  142. #define USART_Mode_Rx ((uint16_t)0x0004)
  143. #define USART_Mode_Tx ((uint16_t)0x0008)
  144. #define IS_USART_MODE(MODE) ((((MODE) & (uint16_t)0xFFF3) == 0x00) && ((MODE) != (uint16_t)0x00))
  145. /**
  146. * @}
  147. */
  148. /** @defgroup USART_Hardware_Flow_Control
  149. * @{
  150. */
  151. #define USART_HardwareFlowControl_None ((uint16_t)0x0000)
  152. #define USART_HardwareFlowControl_RTS ((uint16_t)0x0100)
  153. #define USART_HardwareFlowControl_CTS ((uint16_t)0x0200)
  154. #define USART_HardwareFlowControl_RTS_CTS ((uint16_t)0x0300)
  155. #define IS_USART_HARDWARE_FLOW_CONTROL(CONTROL)\
  156. (((CONTROL) == USART_HardwareFlowControl_None) || \
  157. ((CONTROL) == USART_HardwareFlowControl_RTS) || \
  158. ((CONTROL) == USART_HardwareFlowControl_CTS) || \
  159. ((CONTROL) == USART_HardwareFlowControl_RTS_CTS))
  160. /**
  161. * @}
  162. */
  163. /** @defgroup USART_Clock
  164. * @{
  165. */
  166. #define USART_Clock_Disable ((uint16_t)0x0000)
  167. #define USART_Clock_Enable ((uint16_t)0x0800)
  168. #define IS_USART_CLOCK(CLOCK) (((CLOCK) == USART_Clock_Disable) || \
  169. ((CLOCK) == USART_Clock_Enable))
  170. /**
  171. * @}
  172. */
  173. /** @defgroup USART_Clock_Polarity
  174. * @{
  175. */
  176. #define USART_CPOL_Low ((uint16_t)0x0000)
  177. #define USART_CPOL_High ((uint16_t)0x0400)
  178. #define IS_USART_CPOL(CPOL) (((CPOL) == USART_CPOL_Low) || ((CPOL) == USART_CPOL_High))
  179. /**
  180. * @}
  181. */
  182. /** @defgroup USART_Clock_Phase
  183. * @{
  184. */
  185. #define USART_CPHA_1Edge ((uint16_t)0x0000)
  186. #define USART_CPHA_2Edge ((uint16_t)0x0200)
  187. #define IS_USART_CPHA(CPHA) (((CPHA) == USART_CPHA_1Edge) || ((CPHA) == USART_CPHA_2Edge))
  188. /**
  189. * @}
  190. */
  191. /** @defgroup USART_Last_Bit
  192. * @{
  193. */
  194. #define USART_LastBit_Disable ((uint16_t)0x0000)
  195. #define USART_LastBit_Enable ((uint16_t)0x0100)
  196. #define IS_USART_LASTBIT(LASTBIT) (((LASTBIT) == USART_LastBit_Disable) || \
  197. ((LASTBIT) == USART_LastBit_Enable))
  198. /**
  199. * @}
  200. */
  201. /** @defgroup USART_Interrupt_definition
  202. * @{
  203. */
  204. #define USART_IT_PE ((uint16_t)0x0028)
  205. #define USART_IT_TXE ((uint16_t)0x0727)
  206. #define USART_IT_TC ((uint16_t)0x0626)
  207. #define USART_IT_RXNE ((uint16_t)0x0525)
  208. #define USART_IT_IDLE ((uint16_t)0x0424)
  209. #define USART_IT_LBD ((uint16_t)0x0846)
  210. #define USART_IT_CTS ((uint16_t)0x096A)
  211. #define USART_IT_ERR ((uint16_t)0x0060)
  212. #define USART_IT_ORE ((uint16_t)0x0360)
  213. #define USART_IT_NE ((uint16_t)0x0260)
  214. #define USART_IT_FE ((uint16_t)0x0160)
  215. #define IS_USART_CONFIG_IT(IT) (((IT) == USART_IT_PE) || ((IT) == USART_IT_TXE) || \
  216. ((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \
  217. ((IT) == USART_IT_IDLE) || ((IT) == USART_IT_LBD) || \
  218. ((IT) == USART_IT_CTS) || ((IT) == USART_IT_ERR))
  219. #define IS_USART_GET_IT(IT) (((IT) == USART_IT_PE) || ((IT) == USART_IT_TXE) || \
  220. ((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \
  221. ((IT) == USART_IT_IDLE) || ((IT) == USART_IT_LBD) || \
  222. ((IT) == USART_IT_CTS) || ((IT) == USART_IT_ORE) || \
  223. ((IT) == USART_IT_NE) || ((IT) == USART_IT_FE))
  224. #define IS_USART_CLEAR_IT(IT) (((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \
  225. ((IT) == USART_IT_LBD) || ((IT) == USART_IT_CTS))
  226. /**
  227. * @}
  228. */
  229. /** @defgroup USART_DMA_Requests
  230. * @{
  231. */
  232. #define USART_DMAReq_Tx ((uint16_t)0x0080)
  233. #define USART_DMAReq_Rx ((uint16_t)0x0040)
  234. #define IS_USART_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFF3F) == 0x00) && ((DMAREQ) != (uint16_t)0x00))
  235. /**
  236. * @}
  237. */
  238. /** @defgroup USART_WakeUp_methods
  239. * @{
  240. */
  241. #define USART_WakeUp_IdleLine ((uint16_t)0x0000)
  242. #define USART_WakeUp_AddressMark ((uint16_t)0x0800)
  243. #define IS_USART_WAKEUP(WAKEUP) (((WAKEUP) == USART_WakeUp_IdleLine) || \
  244. ((WAKEUP) == USART_WakeUp_AddressMark))
  245. /**
  246. * @}
  247. */
  248. /** @defgroup USART_LIN_Break_Detection_Length
  249. * @{
  250. */
  251. #define USART_LINBreakDetectLength_10b ((uint16_t)0x0000)
  252. #define USART_LINBreakDetectLength_11b ((uint16_t)0x0020)
  253. #define IS_USART_LIN_BREAK_DETECT_LENGTH(LENGTH) \
  254. (((LENGTH) == USART_LINBreakDetectLength_10b) || \
  255. ((LENGTH) == USART_LINBreakDetectLength_11b))
  256. /**
  257. * @}
  258. */
  259. /** @defgroup USART_IrDA_Low_Power
  260. * @{
  261. */
  262. #define USART_IrDAMode_LowPower ((uint16_t)0x0004)
  263. #define USART_IrDAMode_Normal ((uint16_t)0x0000)
  264. #define IS_USART_IRDA_MODE(MODE) (((MODE) == USART_IrDAMode_LowPower) || \
  265. ((MODE) == USART_IrDAMode_Normal))
  266. /**
  267. * @}
  268. */
  269. /** @defgroup USART_Flags
  270. * @{
  271. */
  272. #define USART_FLAG_CTS ((uint16_t)0x0200)
  273. #define USART_FLAG_LBD ((uint16_t)0x0100)
  274. #define USART_FLAG_TXE ((uint16_t)0x0080)
  275. #define USART_FLAG_TC ((uint16_t)0x0040)
  276. #define USART_FLAG_RXNE ((uint16_t)0x0020)
  277. #define USART_FLAG_IDLE ((uint16_t)0x0010)
  278. #define USART_FLAG_ORE ((uint16_t)0x0008)
  279. #define USART_FLAG_NE ((uint16_t)0x0004)
  280. #define USART_FLAG_FE ((uint16_t)0x0002)
  281. #define USART_FLAG_PE ((uint16_t)0x0001)
  282. #define IS_USART_FLAG(FLAG) (((FLAG) == USART_FLAG_PE) || ((FLAG) == USART_FLAG_TXE) || \
  283. ((FLAG) == USART_FLAG_TC) || ((FLAG) == USART_FLAG_RXNE) || \
  284. ((FLAG) == USART_FLAG_IDLE) || ((FLAG) == USART_FLAG_LBD) || \
  285. ((FLAG) == USART_FLAG_CTS) || ((FLAG) == USART_FLAG_ORE) || \
  286. ((FLAG) == USART_FLAG_NE) || ((FLAG) == USART_FLAG_FE))
  287. #define IS_USART_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xFC9F) == 0x00) && ((FLAG) != (uint16_t)0x00))
  288. #define IS_USART_PERIPH_FLAG(PERIPH, USART_FLAG) ((((*(uint32_t*)&(PERIPH)) != UART4_BASE) &&\
  289. ((*(uint32_t*)&(PERIPH)) != UART5_BASE)) \
  290. || ((USART_FLAG) != USART_FLAG_CTS))
  291. #define IS_USART_BAUDRATE(BAUDRATE) (((BAUDRATE) > 0) && ((BAUDRATE) < 0x0044AA21))
  292. #define IS_USART_ADDRESS(ADDRESS) ((ADDRESS) <= 0xF)
  293. #define IS_USART_DATA(DATA) ((DATA) <= 0x1FF)
  294. /**
  295. * @}
  296. */
  297. /**
  298. * @}
  299. */
  300. /** @defgroup USART_Exported_Macros
  301. * @{
  302. */
  303. /**
  304. * @}
  305. */
  306. /** @defgroup USART_Exported_Functions
  307. * @{
  308. */
  309. //--将外设 USARTx 寄存器重设为缺省值
  310. void USART_DeInit(USART_TypeDef* USARTx);
  311. //--根据USART_InitStruct 中指定的参数初始化外设USARTx 寄存器
  312. void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);
  313. //--把USART_InitStruct 中的每一个参数按缺省值填入
  314. void USART_StructInit(USART_InitTypeDef* USART_InitStruct);
  315. //--
  316. void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct);
  317. void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct);
  318. //--使能或者失能 USART 外设
  319. void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
  320. //--使能或者失能指定的 USART 中断
  321. void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
  322. //--使能或者失能指定 USART 的 DMA 请求
  323. void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
  324. //--设置 USART 节点的地址
  325. void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address);
  326. //--选择 USART 的唤醒方式
  327. void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp);
  328. //--检查 USART 是否处于静默模式
  329. void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  330. //--设置 USART LIN 中断检测长度
  331. void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength);
  332. //--使能或者失能 USARTx 的 LIN 模式
  333. void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  334. //--通过外设 USARTx 发送单个数据
  335. void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
  336. //--返回 USARTx 最近接收到的数据
  337. uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
  338. //--发送中断字
  339. void USART_SendBreak(USART_TypeDef* USARTx);
  340. //--设置指定的 USART 保护时间
  341. void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime);
  342. //--设置 USART 时钟预分频
  343. void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler);
  344. //--使能或者失能指定 USART 的智能卡模式
  345. void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  346. //--使能或者失能 NACK 传输
  347. void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  348. //--使能或者失能 USART 半双工模式
  349. void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  350. void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
  351. void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  352. //--设置 USART IrDA 模式
  353. void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode);
  354. //--使能或者失能 USART IrDA 模式
  355. void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState);
  356. //--检查指定的 USART 标志位设置与否
  357. FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
  358. //--清除 USARTx 的待处理标志位
  359. void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);
  360. //--检查指定的 USART 中断发生与否
  361. ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
  362. //--清除 USARTx 的中断待处理位
  363. void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);
  364. #ifdef __cplusplus
  365. }
  366. #endif
  367. #endif /* __STM32F10x_USART_H */
  368. /**
  369. * @}
  370. */
  371. /**
  372. * @}
  373. */
  374. /**
  375. * @}
  376. */
  377. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/