stm32f3xx_hal_usart.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_hal_usart.h
  4. * @author MCD Application Team
  5. * @version V1.4.0
  6. * @date 16-December-2016
  7. * @brief Header file of USART HAL module.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __STM32F3xx_HAL_USART_H
  39. #define __STM32F3xx_HAL_USART_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32f3xx_hal_def.h"
  45. /** @addtogroup STM32F3xx_HAL_Driver
  46. * @{
  47. */
  48. /** @addtogroup USART
  49. * @{
  50. */
  51. /* Exported types ------------------------------------------------------------*/
  52. /** @defgroup USART_Exported_Types USART Exported Types
  53. * @{
  54. */
  55. /**
  56. * @brief USART Init Structure definition
  57. */
  58. typedef struct
  59. {
  60. uint32_t BaudRate; /*!< This member configures the Usart communication baud rate.
  61. The baud rate is computed using the following formula:
  62. Baud Rate Register = ((PCLKx) / ((huart->Init.BaudRate))). */
  63. uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  64. This parameter can be a value of @ref USARTEx_Word_Length. */
  65. uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
  66. This parameter can be a value of @ref USART_Stop_Bits. */
  67. uint32_t Parity; /*!< Specifies the parity mode.
  68. This parameter can be a value of @ref USART_Parity
  69. @note When parity is enabled, the computed parity is inserted
  70. at the MSB position of the transmitted data (9th bit when
  71. the word length is set to 9 data bits; 8th bit when the
  72. word length is set to 8 data bits). */
  73. uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
  74. This parameter can be a value of @ref USART_Mode. */
  75. uint32_t CLKPolarity; /*!< Specifies the steady state of the serial clock.
  76. This parameter can be a value of @ref USART_Clock_Polarity. */
  77. uint32_t CLKPhase; /*!< Specifies the clock transition on which the bit capture is made.
  78. This parameter can be a value of @ref USART_Clock_Phase. */
  79. uint32_t CLKLastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
  80. data bit (MSB) has to be output on the SCLK pin in synchronous mode.
  81. This parameter can be a value of @ref USART_Last_Bit. */
  82. }USART_InitTypeDef;
  83. /**
  84. * @brief HAL USART State structures definition
  85. */
  86. typedef enum
  87. {
  88. HAL_USART_STATE_RESET = 0x00U, /*!< Peripheral is not initialized */
  89. HAL_USART_STATE_READY = 0x01U, /*!< Peripheral Initialized and ready for use */
  90. HAL_USART_STATE_BUSY = 0x02U, /*!< an internal process is ongoing */
  91. HAL_USART_STATE_BUSY_TX = 0x12U, /*!< Data Transmission process is ongoing */
  92. HAL_USART_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */
  93. HAL_USART_STATE_BUSY_TX_RX = 0x32U, /*!< Data Transmission Reception process is ongoing */
  94. HAL_USART_STATE_TIMEOUT = 0x03U, /*!< Timeout state */
  95. HAL_USART_STATE_ERROR = 0x04U /*!< Error */
  96. }HAL_USART_StateTypeDef;
  97. /**
  98. * @brief USART clock sources definitions
  99. */
  100. typedef enum
  101. {
  102. USART_CLOCKSOURCE_PCLK1 = 0x00U, /*!< PCLK1 clock source */
  103. USART_CLOCKSOURCE_PCLK2 = 0x01U, /*!< PCLK2 clock source */
  104. USART_CLOCKSOURCE_HSI = 0x02U, /*!< HSI clock source */
  105. USART_CLOCKSOURCE_SYSCLK = 0x04U, /*!< SYSCLK clock source */
  106. USART_CLOCKSOURCE_LSE = 0x08U, /*!< LSE clock source */
  107. USART_CLOCKSOURCE_UNDEFINED = 0x10U /*!< Undefined clock source */
  108. }USART_ClockSourceTypeDef;
  109. /**
  110. * @brief USART handle Structure definition
  111. */
  112. typedef struct
  113. {
  114. USART_TypeDef *Instance; /*!< USART registers base address */
  115. USART_InitTypeDef Init; /*!< USART communication parameters */
  116. uint8_t *pTxBuffPtr; /*!< Pointer to USART Tx transfer Buffer */
  117. uint16_t TxXferSize; /*!< USART Tx Transfer size */
  118. __IO uint16_t TxXferCount; /*!< USART Tx Transfer Counter */
  119. uint8_t *pRxBuffPtr; /*!< Pointer to USART Rx transfer Buffer */
  120. uint16_t RxXferSize; /*!< USART Rx Transfer size */
  121. __IO uint16_t RxXferCount; /*!< USART Rx Transfer Counter */
  122. uint16_t Mask; /*!< USART Rx RDR register mask */
  123. DMA_HandleTypeDef *hdmatx; /*!< USART Tx DMA Handle parameters */
  124. DMA_HandleTypeDef *hdmarx; /*!< USART Rx DMA Handle parameters */
  125. HAL_LockTypeDef Lock; /*!< Locking object */
  126. __IO HAL_USART_StateTypeDef State; /*!< USART communication state */
  127. __IO uint32_t ErrorCode; /*!< USART Error code */
  128. }USART_HandleTypeDef;
  129. /**
  130. * @}
  131. */
  132. /* Exported constants --------------------------------------------------------*/
  133. /** @defgroup USART_Exported_Constants USART Exported Constants
  134. * @{
  135. */
  136. /** @defgroup USART_Error USART Error
  137. * @{
  138. */
  139. #define HAL_USART_ERROR_NONE (0x00000000U) /*!< No error */
  140. #define HAL_USART_ERROR_PE (0x00000001U) /*!< Parity error */
  141. #define HAL_USART_ERROR_NE (0x00000002U) /*!< Noise error */
  142. #define HAL_USART_ERROR_FE (0x00000004U) /*!< frame error */
  143. #define HAL_USART_ERROR_ORE (0x00000008U) /*!< Overrun error */
  144. #define HAL_USART_ERROR_DMA (0x00000010U) /*!< DMA transfer error */
  145. /**
  146. * @}
  147. */
  148. /** @defgroup USART_Stop_Bits USART Number of Stop Bits
  149. * @{
  150. */
  151. #define USART_STOPBITS_0_5 ((uint32_t)USART_CR2_STOP_0) /*!< USART frame with 0.5 stop bit */
  152. #define USART_STOPBITS_1 (0x00000000U) /*!< USART frame with 1 stop bit */
  153. #define USART_STOPBITS_1_5 ((uint32_t)(USART_CR2_STOP_0 | USART_CR2_STOP_1)) /*!< USART frame with 1.5 stop bits */
  154. #define USART_STOPBITS_2 ((uint32_t)USART_CR2_STOP_1) /*!< USART frame with 2 stop bits */
  155. /**
  156. * @}
  157. */
  158. /** @defgroup USART_Parity USART Parity
  159. * @{
  160. */
  161. #define USART_PARITY_NONE (0x00000000U) /*!< No parity */
  162. #define USART_PARITY_EVEN ((uint32_t)USART_CR1_PCE) /*!< Even parity */
  163. #define USART_PARITY_ODD ((uint32_t)(USART_CR1_PCE | USART_CR1_PS)) /*!< Odd parity */
  164. /**
  165. * @}
  166. */
  167. /** @defgroup USART_Mode USART Mode
  168. * @{
  169. */
  170. #define USART_MODE_RX ((uint32_t)USART_CR1_RE) /*!< RX mode */
  171. #define USART_MODE_TX ((uint32_t)USART_CR1_TE) /*!< TX mode */
  172. #define USART_MODE_TX_RX ((uint32_t)(USART_CR1_TE |USART_CR1_RE)) /*!< RX and TX mode */
  173. /**
  174. * @}
  175. */
  176. /** @defgroup USART_Clock USART Clock
  177. * @{
  178. */
  179. #define USART_CLOCK_DISABLE (0x00000000U) /*!< USART clock disable */
  180. #define USART_CLOCK_ENABLE ((uint32_t)USART_CR2_CLKEN) /*!< USART clock enable */
  181. /**
  182. * @}
  183. */
  184. /** @defgroup USART_Clock_Polarity USART Clock Polarity
  185. * @{
  186. */
  187. #define USART_POLARITY_LOW (0x00000000U) /*!< USART Clock signal is steady Low */
  188. #define USART_POLARITY_HIGH ((uint32_t)USART_CR2_CPOL) /*!< USART Clock signal is steady High */
  189. /**
  190. * @}
  191. */
  192. /** @defgroup USART_Clock_Phase USART Clock Phase
  193. * @{
  194. */
  195. #define USART_PHASE_1EDGE (0x00000000U) /*!< USART frame phase on first clock transition */
  196. #define USART_PHASE_2EDGE ((uint32_t)USART_CR2_CPHA) /*!< USART frame phase on second clock transition */
  197. /**
  198. * @}
  199. */
  200. /** @defgroup USART_Last_Bit USART Last Bit
  201. * @{
  202. */
  203. #define USART_LASTBIT_DISABLE (0x00000000U) /*!< USART frame last data bit clock pulse not output to SCLK pin */
  204. #define USART_LASTBIT_ENABLE ((uint32_t)USART_CR2_LBCL) /*!< USART frame last data bit clock pulse output to SCLK pin */
  205. /**
  206. * @}
  207. */
  208. /** @defgroup USART_Request_Parameters USART Request Parameters
  209. * @{
  210. */
  211. #define USART_RXDATA_FLUSH_REQUEST ((uint32_t)USART_RQR_RXFRQ) /*!< Receive Data flush Request */
  212. #define USART_TXDATA_FLUSH_REQUEST ((uint32_t)USART_RQR_TXFRQ) /*!< Transmit data flush Request */
  213. /**
  214. * @}
  215. */
  216. /** @defgroup USART_Flags USART Flags
  217. * Elements values convention: 0xXXXX
  218. * - 0xXXXX : Flag mask in the ISR register
  219. * @{
  220. */
  221. #define USART_FLAG_REACK (0x00400000U) /*!< USART receive enable acknowledge flag */
  222. #define USART_FLAG_TEACK (0x00200000U) /*!< USART transmit enable acknowledge flag */
  223. #define USART_FLAG_BUSY (0x00010000U) /*!< USART busy flag */
  224. #define USART_FLAG_CTS (0x00000400U) /*!< USART clear to send flag */
  225. #define USART_FLAG_CTSIF (0x00000200U) /*!< USART clear to send interrupt flag */
  226. #define USART_FLAG_LBDF (0x00000100U) /*!< USART LIN break detection flag */
  227. #define USART_FLAG_TXE (0x00000080U) /*!< USART transmit data register empty */
  228. #define USART_FLAG_TC (0x00000040U) /*!< USART transmission complete */
  229. #define USART_FLAG_RXNE (0x00000020U) /*!< USART read data register not empty */
  230. #define USART_FLAG_IDLE (0x00000010U) /*!< USART idle flag */
  231. #define USART_FLAG_ORE (0x00000008U) /*!< USART overrun error */
  232. #define USART_FLAG_NE (0x00000004U) /*!< USART noise error */
  233. #define USART_FLAG_FE (0x00000002U) /*!< USART frame error */
  234. #define USART_FLAG_PE (0x00000001U) /*!< USART parity error */
  235. /**
  236. * @}
  237. */
  238. /** @defgroup USART_Interrupt_definition USART Interrupts Definition
  239. * Elements values convention: 0000ZZZZ0XXYYYYYb
  240. * - YYYYY : Interrupt source position in the XX register (5bits)
  241. * - XX : Interrupt source register (2bits)
  242. * - 01: CR1 register
  243. * - 10: CR2 register
  244. * - 11: CR3 register
  245. * - ZZZZ : Flag position in the ISR register(4bits)
  246. * @{
  247. */
  248. #define USART_IT_PE ((uint16_t)0x0028U) /*!< USART parity error interruption */
  249. #define USART_IT_TXE ((uint16_t)0x0727U) /*!< USART transmit data register empty interruption */
  250. #define USART_IT_TC ((uint16_t)0x0626U) /*!< USART transmission complete interruption */
  251. #define USART_IT_RXNE ((uint16_t)0x0525U) /*!< USART read data register not empty interruption */
  252. #define USART_IT_IDLE ((uint16_t)0x0424U) /*!< USART idle interruption */
  253. #define USART_IT_ERR ((uint16_t)0x0060U) /*!< USART error interruption */
  254. #define USART_IT_ORE ((uint16_t)0x0300U) /*!< USART overrun error interruption */
  255. #define USART_IT_NE ((uint16_t)0x0200U) /*!< USART noise error interruption */
  256. #define USART_IT_FE ((uint16_t)0x0100U) /*!< USART frame error interruption */
  257. /**
  258. * @}
  259. */
  260. /** @defgroup USART_IT_CLEAR_Flags USART Interruption Clear Flags
  261. * @{
  262. */
  263. #define USART_CLEAR_PEF USART_ICR_PECF /*!< Parity Error Clear Flag */
  264. #define USART_CLEAR_FEF USART_ICR_FECF /*!< Framing Error Clear Flag */
  265. #define USART_CLEAR_NEF USART_ICR_NCF /*!< Noise detected Clear Flag */
  266. #define USART_CLEAR_OREF USART_ICR_ORECF /*!< OverRun Error Clear Flag */
  267. #define USART_CLEAR_IDLEF USART_ICR_IDLECF /*!< IDLE line detected Clear Flag */
  268. #define USART_CLEAR_TCF USART_ICR_TCCF /*!< Transmission Complete Clear Flag */
  269. #define USART_CLEAR_CTSF USART_ICR_CTSCF /*!< CTS Interrupt Clear Flag */
  270. /**
  271. * @}
  272. */
  273. /** @defgroup USART_Interruption_Mask USART Interruption Flags Mask
  274. * @{
  275. */
  276. #define USART_IT_MASK ((uint16_t)0x001FU) /*!< USART interruptions flags mask */
  277. /**
  278. * @}
  279. */
  280. /**
  281. * @}
  282. */
  283. /* Exported macros -----------------------------------------------------------*/
  284. /** @defgroup USART_Exported_Macros USART Exported Macros
  285. * @{
  286. */
  287. /** @brief Reset USART handle state.
  288. * @param __HANDLE__: USART handle.
  289. * @retval None
  290. */
  291. #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_USART_STATE_RESET)
  292. /** @brief Flush the USART Data registers.
  293. * @param __HANDLE__: specifies the USART Handle.
  294. * @retval None
  295. */
  296. #define __HAL_USART_FLUSH_DRREGISTER(__HANDLE__) \
  297. do{ \
  298. SET_BIT((__HANDLE__)->Instance->RQR, USART_RXDATA_FLUSH_REQUEST); \
  299. SET_BIT((__HANDLE__)->Instance->RQR, USART_TXDATA_FLUSH_REQUEST); \
  300. } while(0U)
  301. /** @brief Check whether the specified USART flag is set or not.
  302. * @param __HANDLE__: specifies the USART Handle
  303. * @param __FLAG__: specifies the flag to check.
  304. * This parameter can be one of the following values:
  305. * @arg @ref USART_FLAG_REACK Receive enable acknowledge flag
  306. * @arg @ref USART_FLAG_TEACK Transmit enable acknowledge flag
  307. * @arg @ref USART_FLAG_BUSY Busy flag
  308. * @arg @ref USART_FLAG_CTS CTS Change flag
  309. * @arg @ref USART_FLAG_TXE Transmit data register empty flag
  310. * @arg @ref USART_FLAG_TC Transmission Complete flag
  311. * @arg @ref USART_FLAG_RXNE Receive data register not empty flag
  312. * @arg @ref USART_FLAG_IDLE Idle Line detection flag
  313. * @arg @ref USART_FLAG_ORE OverRun Error flag
  314. * @arg @ref USART_FLAG_NE Noise Error flag
  315. * @arg @ref USART_FLAG_FE Framing Error flag
  316. * @arg @ref USART_FLAG_PE Parity Error flag
  317. * @retval The new state of __FLAG__ (TRUE or FALSE).
  318. */
  319. #define __HAL_USART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->ISR & (__FLAG__)) == (__FLAG__))
  320. /** @brief Clear the specified USART pending flag.
  321. * @param __HANDLE__: specifies the USART Handle.
  322. * @param __FLAG__: specifies the flag to check.
  323. * This parameter can be any combination of the following values:
  324. * @arg @ref USART_CLEAR_PEF
  325. * @arg @ref USART_CLEAR_FEF
  326. * @arg @ref USART_CLEAR_NEF
  327. * @arg @ref USART_CLEAR_OREF
  328. * @arg @ref USART_CLEAR_IDLEF
  329. * @arg @ref USART_CLEAR_TCF
  330. * @arg @ref USART_CLEAR_CTSF
  331. * @retval None
  332. */
  333. #define __HAL_USART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->ICR = (__FLAG__))
  334. /** @brief Clear the USART PE pending flag.
  335. * @param __HANDLE__: specifies the USART Handle.
  336. * @retval None
  337. */
  338. #define __HAL_USART_CLEAR_PEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_PEF)
  339. /** @brief Clear the USART FE pending flag.
  340. * @param __HANDLE__: specifies the USART Handle.
  341. * @retval None
  342. */
  343. #define __HAL_USART_CLEAR_FEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_FEF)
  344. /** @brief Clear the USART NE pending flag.
  345. * @param __HANDLE__: specifies the USART Handle.
  346. * @retval None
  347. */
  348. #define __HAL_USART_CLEAR_NEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_NEF)
  349. /** @brief Clear the USART ORE pending flag.
  350. * @param __HANDLE__: specifies the USART Handle.
  351. * @retval None
  352. */
  353. #define __HAL_USART_CLEAR_OREFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_OREF)
  354. /** @brief Clear the USART IDLE pending flag.
  355. * @param __HANDLE__: specifies the USART Handle.
  356. * @retval None
  357. */
  358. #define __HAL_USART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_IDLEF)
  359. /** @brief Enable the specified USART interrupt.
  360. * @param __HANDLE__: specifies the USART Handle.
  361. * @param __INTERRUPT__: specifies the USART interrupt source to enable.
  362. * This parameter can be one of the following values:
  363. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  364. * @arg @ref USART_IT_TC Transmission complete interrupt
  365. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  366. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  367. * @arg @ref USART_IT_PE Parity Error interrupt
  368. * @arg @ref USART_IT_ERR Error interrupt(Frame error, noise error, overrun error)
  369. * @retval None
  370. */
  371. #define __HAL_USART_ENABLE_IT(__HANDLE__, __INTERRUPT__) (((((__INTERRUPT__) & 0xFFU) >> 5U) == 1U)? ((__HANDLE__)->Instance->CR1 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  372. ((((__INTERRUPT__) & 0xFFU) >> 5U) == 2U)? ((__HANDLE__)->Instance->CR2 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  373. ((__HANDLE__)->Instance->CR3 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))))
  374. /** @brief Disable the specified USART interrupt.
  375. * @param __HANDLE__: specifies the USART Handle.
  376. * @param __INTERRUPT__: specifies the USART interrupt source to disable.
  377. * This parameter can be one of the following values:
  378. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  379. * @arg @ref USART_IT_TC Transmission complete interrupt
  380. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  381. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  382. * @arg @ref USART_IT_PE Parity Error interrupt
  383. * @arg @ref USART_IT_ERR Error interrupt(Frame error, noise error, overrun error)
  384. * @retval None
  385. */
  386. #define __HAL_USART_DISABLE_IT(__HANDLE__, __INTERRUPT__) (((((__INTERRUPT__) & 0xFFU) >> 5U) == 1U)? ((__HANDLE__)->Instance->CR1 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  387. ((((__INTERRUPT__) & 0xFFU) >> 5U) == 2U)? ((__HANDLE__)->Instance->CR2 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  388. ((__HANDLE__)->Instance->CR3 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))))
  389. /** @brief Check whether the specified USART interrupt has occurred or not.
  390. * @param __HANDLE__: specifies the USART Handle.
  391. * @param __IT__: specifies the USART interrupt source to check.
  392. * This parameter can be one of the following values:
  393. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  394. * @arg @ref USART_IT_TC Transmission complete interrupt
  395. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  396. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  397. * @arg @ref USART_IT_ORE OverRun Error interrupt
  398. * @arg @ref USART_IT_NE Noise Error interrupt
  399. * @arg @ref USART_IT_FE Framing Error interrupt
  400. * @arg @ref USART_IT_PE Parity Error interrupt
  401. * @retval The new state of __IT__ (TRUE or FALSE).
  402. */
  403. #define __HAL_USART_GET_IT(__HANDLE__, __IT__) ((__HANDLE__)->Instance->ISR & (1U << ((__IT__)>> 0x08U)))
  404. /** @brief Check whether the specified USART interrupt source is enabled or not.
  405. * @param __HANDLE__: specifies the USART Handle.
  406. * @param __IT__: specifies the USART interrupt source to check.
  407. * This parameter can be one of the following values:
  408. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  409. * @arg @ref USART_IT_TC Transmission complete interrupt
  410. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  411. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  412. * @arg @ref USART_IT_ORE OverRun Error interrupt
  413. * @arg @ref USART_IT_NE Noise Error interrupt
  414. * @arg @ref USART_IT_FE Framing Error interrupt
  415. * @arg @ref USART_IT_PE Parity Error interrupt
  416. * @retval The new state of __IT__ (TRUE or FALSE).
  417. */
  418. #define __HAL_USART_GET_IT_SOURCE(__HANDLE__, __IT__) ((((((uint8_t)(__IT__)) >> 5U) == 1U)? (__HANDLE__)->Instance->CR1:(((((uint8_t)(__IT__)) >> 5U) == 2U)? \
  419. (__HANDLE__)->Instance->CR2 : (__HANDLE__)->Instance->CR3)) & (1U << \
  420. (((uint16_t)(__IT__)) & USART_IT_MASK)))
  421. /** @brief Clear the specified USART ISR flag, in setting the proper ICR register flag.
  422. * @param __HANDLE__: specifies the USART Handle.
  423. * @param __IT_CLEAR__: specifies the interrupt clear register flag that needs to be set
  424. * to clear the corresponding interrupt.
  425. * This parameter can be one of the following values:
  426. * @arg @ref USART_CLEAR_PEF Parity Error Clear Flag
  427. * @arg @ref USART_CLEAR_FEF Framing Error Clear Flag
  428. * @arg @ref USART_CLEAR_NEF Noise detected Clear Flag
  429. * @arg @ref USART_CLEAR_OREF OverRun Error Clear Flag
  430. * @arg @ref USART_CLEAR_IDLEF IDLE line detected Clear Flag
  431. * @arg @ref USART_CLEAR_TCF Transmission Complete Clear Flag
  432. * @arg @ref USART_CLEAR_CTSF CTS Interrupt Clear Flag
  433. * @retval None
  434. */
  435. #define __HAL_USART_CLEAR_IT(__HANDLE__, __IT_CLEAR__) ((__HANDLE__)->Instance->ICR = (uint32_t)(__IT_CLEAR__))
  436. /** @brief Set a specific USART request flag.
  437. * @param __HANDLE__: specifies the USART Handle.
  438. * @param __REQ__: specifies the request flag to set.
  439. * This parameter can be one of the following values:
  440. * @arg @ref USART_RXDATA_FLUSH_REQUEST Receive Data flush Request
  441. * @arg @ref USART_TXDATA_FLUSH_REQUEST Transmit data flush Request
  442. *
  443. * @retval None
  444. */
  445. #define __HAL_USART_SEND_REQ(__HANDLE__, __REQ__) ((__HANDLE__)->Instance->RQR |= (__REQ__))
  446. /** @brief Enable the USART one bit sample method.
  447. * @param __HANDLE__: specifies the USART Handle.
  448. * @retval None
  449. */
  450. #define __HAL_USART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT)
  451. /** @brief Disable the USART one bit sample method.
  452. * @param __HANDLE__: specifies the USART Handle.
  453. * @retval None
  454. */
  455. #define __HAL_USART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_ONEBIT))
  456. /** @brief Enable USART.
  457. * @param __HANDLE__: specifies the USART Handle.
  458. * @retval None
  459. */
  460. #define __HAL_USART_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
  461. /** @brief Disable USART.
  462. * @param __HANDLE__: specifies the USART Handle.
  463. * @retval None
  464. */
  465. #define __HAL_USART_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
  466. /**
  467. * @}
  468. */
  469. /* Private macros --------------------------------------------------------*/
  470. /** @defgroup USART_Private_Macros USART Private Macros
  471. * @{
  472. */
  473. /** @brief Check USART Baud rate.
  474. * @param __BAUDRATE__: Baudrate specified by the user.
  475. * The maximum Baud Rate is derived from the maximum clock on F3 (i.e. 72 MHz)
  476. * divided by the smallest oversampling used on the USART (i.e. 8).
  477. * @retval Test result (TRUE or FALSE).
  478. */
  479. #define IS_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) < 9000001U)
  480. /**
  481. * @brief Ensure that USART frame number of stop bits is valid.
  482. * @param __STOPBITS__: USART frame number of stop bits.
  483. * @retval SET (__STOPBITS__ is valid) or RESET (__STOPBITS__ is invalid)
  484. */
  485. #define IS_USART_STOPBITS(__STOPBITS__) (((__STOPBITS__) == USART_STOPBITS_0_5) || \
  486. ((__STOPBITS__) == USART_STOPBITS_1) || \
  487. ((__STOPBITS__) == USART_STOPBITS_1_5) || \
  488. ((__STOPBITS__) == USART_STOPBITS_2))
  489. /**
  490. * @brief Ensure that USART frame parity is valid.
  491. * @param __PARITY__: USART frame parity.
  492. * @retval SET (__PARITY__ is valid) or RESET (__PARITY__ is invalid)
  493. */
  494. #define IS_USART_PARITY(__PARITY__) (((__PARITY__) == USART_PARITY_NONE) || \
  495. ((__PARITY__) == USART_PARITY_EVEN) || \
  496. ((__PARITY__) == USART_PARITY_ODD))
  497. /**
  498. * @brief Ensure that USART communication mode is valid.
  499. * @param __MODE__: USART communication mode.
  500. * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
  501. */
  502. #define IS_USART_MODE(__MODE__) ((((__MODE__) & 0xFFFFFFF3U) == 0x00U) && ((__MODE__) != 0x00U))
  503. /**
  504. * @brief Ensure that USART clock state is valid.
  505. * @param __CLOCK__: USART clock state.
  506. * @retval SET (__CLOCK__ is valid) or RESET (__CLOCK__ is invalid)
  507. */
  508. #define IS_USART_CLOCK(__CLOCK__) (((__CLOCK__) == USART_CLOCK_DISABLE) || \
  509. ((__CLOCK__) == USART_CLOCK_ENABLE))
  510. /**
  511. * @brief Ensure that USART frame polarity is valid.
  512. * @param __CPOL__: USART frame polarity.
  513. * @retval SET (__CPOL__ is valid) or RESET (__CPOL__ is invalid)
  514. */
  515. #define IS_USART_POLARITY(__CPOL__) (((__CPOL__) == USART_POLARITY_LOW) || ((__CPOL__) == USART_POLARITY_HIGH))
  516. /**
  517. * @brief Ensure that USART frame phase is valid.
  518. * @param __CPHA__: USART frame phase.
  519. * @retval SET (__CPHA__ is valid) or RESET (__CPHA__ is invalid)
  520. */
  521. #define IS_USART_PHASE(__CPHA__) (((__CPHA__) == USART_PHASE_1EDGE) || ((__CPHA__) == USART_PHASE_2EDGE))
  522. /**
  523. * @brief Ensure that USART frame last bit clock pulse setting is valid.
  524. * @param __LASTBIT__: USART frame last bit clock pulse setting.
  525. * @retval SET (__LASTBIT__ is valid) or RESET (__LASTBIT__ is invalid)
  526. */
  527. #define IS_USART_LASTBIT(__LASTBIT__) (((__LASTBIT__) == USART_LASTBIT_DISABLE) || \
  528. ((__LASTBIT__) == USART_LASTBIT_ENABLE))
  529. /**
  530. * @brief Ensure that USART request parameter is valid.
  531. * @param __PARAM__: USART request parameter.
  532. * @retval SET (__PARAM__ is valid) or RESET (__PARAM__ is invalid)
  533. */
  534. #define IS_USART_REQUEST_PARAMETER(__PARAM__) (((__PARAM__) == USART_RXDATA_FLUSH_REQUEST) || \
  535. ((__PARAM__) == USART_TXDATA_FLUSH_REQUEST))
  536. /**
  537. * @}
  538. */
  539. /* Include USART HAL Extended module */
  540. #include "stm32f3xx_hal_usart_ex.h"
  541. /* Exported functions --------------------------------------------------------*/
  542. /** @addtogroup USART_Exported_Functions USART Exported Functions
  543. * @{
  544. */
  545. /** @addtogroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
  546. * @{
  547. */
  548. /* Initialization and de-initialization functions ****************************/
  549. HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart);
  550. HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart);
  551. void HAL_USART_MspInit(USART_HandleTypeDef *husart);
  552. void HAL_USART_MspDeInit(USART_HandleTypeDef *husart);
  553. /**
  554. * @}
  555. */
  556. /** @addtogroup USART_Exported_Functions_Group2 IO operation functions
  557. * @{
  558. */
  559. /* IO operation functions *****************************************************/
  560. HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout);
  561. HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
  562. HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
  563. HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size);
  564. HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
  565. HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size);
  566. HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size);
  567. HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
  568. HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size);
  569. HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart);
  570. HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart);
  571. HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart);
  572. /* Transfer Abort functions */
  573. HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart);
  574. HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart);
  575. void HAL_USART_IRQHandler(USART_HandleTypeDef *husart);
  576. void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart);
  577. void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart);
  578. void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart);
  579. void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart);
  580. void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart);
  581. void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart);
  582. void HAL_USART_AbortCpltCallback (USART_HandleTypeDef *husart);
  583. /**
  584. * @}
  585. */
  586. /* Peripheral Control functions ***********************************************/
  587. /** @addtogroup USART_Exported_Functions_Group3 Peripheral State and Error functions
  588. * @{
  589. */
  590. /* Peripheral State and Error functions ***************************************/
  591. HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart);
  592. uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart);
  593. /**
  594. * @}
  595. */
  596. /**
  597. * @}
  598. */
  599. /**
  600. * @}
  601. */
  602. /**
  603. * @}
  604. */
  605. #ifdef __cplusplus
  606. }
  607. #endif
  608. #endif /* __STM32F3xx_HAL_USART_H */
  609. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/