stm32f4xx_usart.h 17 KB

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