stm32f4xx_hal_uart.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_uart.h
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-2015
  7. * @brief Header file of UART HAL module.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2015 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 __STM32F4xx_HAL_UART_H
  39. #define __STM32F4xx_HAL_UART_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32f4xx_hal_def.h"
  45. /** @addtogroup STM32F4xx_HAL_Driver
  46. * @{
  47. */
  48. /** @addtogroup UART
  49. * @{
  50. */
  51. /* Exported types ------------------------------------------------------------*/
  52. /** @defgroup UART_Exported_Types UART Exported Types
  53. * @{
  54. */
  55. /**
  56. * @brief UART Init Structure definition
  57. */
  58. typedef struct
  59. {
  60. uint32_t BaudRate; /*!< This member configures the UART communication baud rate.
  61. The baud rate is computed using the following formula:
  62. - IntegerDivider = ((PCLKx) / (8 * (OVR8+1) * (huart->Init.BaudRate)))
  63. - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 8 * (OVR8+1)) + 0.5
  64. Where OVR8 is the "oversampling by 8 mode" configuration bit in the CR1 register. */
  65. uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  66. This parameter can be a value of @ref UART_Word_Length */
  67. uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
  68. This parameter can be a value of @ref UART_Stop_Bits */
  69. uint32_t Parity; /*!< Specifies the parity mode.
  70. This parameter can be a value of @ref UART_Parity
  71. @note When parity is enabled, the computed parity is inserted
  72. at the MSB position of the transmitted data (9th bit when
  73. the word length is set to 9 data bits; 8th bit when the
  74. word length is set to 8 data bits). */
  75. uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
  76. This parameter can be a value of @ref UART_Mode */
  77. uint32_t HwFlowCtl; /*!< Specifies whether the hardware flow control mode is enabled
  78. or disabled.
  79. This parameter can be a value of @ref UART_Hardware_Flow_Control */
  80. uint32_t OverSampling; /*!< Specifies whether the Over sampling 8 is enabled or disabled, to achieve higher speed (up to fPCLK/8).
  81. This parameter can be a value of @ref UART_Over_Sampling */
  82. }UART_InitTypeDef;
  83. /**
  84. * @brief HAL UART State structures definition
  85. */
  86. typedef enum
  87. {
  88. HAL_UART_STATE_RESET = 0x00, /*!< Peripheral is not yet Initialized */
  89. HAL_UART_STATE_READY = 0x01, /*!< Peripheral Initialized and ready for use */
  90. HAL_UART_STATE_BUSY = 0x02, /*!< an internal process is ongoing */
  91. HAL_UART_STATE_BUSY_TX = 0x12, /*!< Data Transmission process is ongoing */
  92. HAL_UART_STATE_BUSY_RX = 0x22, /*!< Data Reception process is ongoing */
  93. HAL_UART_STATE_BUSY_TX_RX = 0x32, /*!< Data Transmission and Reception process is ongoing */
  94. HAL_UART_STATE_TIMEOUT = 0x03, /*!< Timeout state */
  95. HAL_UART_STATE_ERROR = 0x04 /*!< Error */
  96. }HAL_UART_StateTypeDef;
  97. /**
  98. * @brief UART handle Structure definition
  99. */
  100. typedef struct
  101. {
  102. USART_TypeDef *Instance; /*!< UART registers base address */
  103. UART_InitTypeDef Init; /*!< UART communication parameters */
  104. uint8_t *pTxBuffPtr; /*!< Pointer to UART Tx transfer Buffer */
  105. uint16_t TxXferSize; /*!< UART Tx Transfer size */
  106. uint16_t TxXferCount; /*!< UART Tx Transfer Counter */
  107. uint8_t *pRxBuffPtr; /*!< Pointer to UART Rx transfer Buffer */
  108. uint16_t RxXferSize; /*!< UART Rx Transfer size */
  109. uint16_t RxXferCount; /*!< UART Rx Transfer Counter */
  110. DMA_HandleTypeDef *hdmatx; /*!< UART Tx DMA Handle parameters */
  111. DMA_HandleTypeDef *hdmarx; /*!< UART Rx DMA Handle parameters */
  112. HAL_LockTypeDef Lock; /*!< Locking object */
  113. __IO HAL_UART_StateTypeDef State; /*!< UART communication state */
  114. __IO uint32_t ErrorCode; /*!< UART Error code */
  115. }UART_HandleTypeDef;
  116. /**
  117. * @}
  118. */
  119. /* Exported constants --------------------------------------------------------*/
  120. /** @defgroup UART_Exported_Constants UART Exported constants
  121. * @{
  122. */
  123. /** @defgroup UART_Error_Code UART Error Code
  124. * @brief UART Error Code
  125. * @{
  126. */
  127. #define HAL_UART_ERROR_NONE ((uint32_t)0x00000000) /*!< No error */
  128. #define HAL_UART_ERROR_PE ((uint32_t)0x00000001) /*!< Parity error */
  129. #define HAL_UART_ERROR_NE ((uint32_t)0x00000002) /*!< Noise error */
  130. #define HAL_UART_ERROR_FE ((uint32_t)0x00000004) /*!< Frame error */
  131. #define HAL_UART_ERROR_ORE ((uint32_t)0x00000008) /*!< Overrun error */
  132. #define HAL_UART_ERROR_DMA ((uint32_t)0x00000010) /*!< DMA transfer error */
  133. /**
  134. * @}
  135. */
  136. /** @defgroup UART_Word_Length UART Word Length
  137. * @{
  138. */
  139. #define UART_WORDLENGTH_8B ((uint32_t)0x00000000)
  140. #define UART_WORDLENGTH_9B ((uint32_t)USART_CR1_M)
  141. /**
  142. * @}
  143. */
  144. /** @defgroup UART_Stop_Bits UART Number of Stop Bits
  145. * @{
  146. */
  147. #define UART_STOPBITS_1 ((uint32_t)0x00000000)
  148. #define UART_STOPBITS_2 ((uint32_t)USART_CR2_STOP_1)
  149. /**
  150. * @}
  151. */
  152. /** @defgroup UART_Parity UART Parity
  153. * @{
  154. */
  155. #define UART_PARITY_NONE ((uint32_t)0x00000000)
  156. #define UART_PARITY_EVEN ((uint32_t)USART_CR1_PCE)
  157. #define UART_PARITY_ODD ((uint32_t)(USART_CR1_PCE | USART_CR1_PS))
  158. /**
  159. * @}
  160. */
  161. /** @defgroup UART_Hardware_Flow_Control UART Hardware Flow Control
  162. * @{
  163. */
  164. #define UART_HWCONTROL_NONE ((uint32_t)0x00000000)
  165. #define UART_HWCONTROL_RTS ((uint32_t)USART_CR3_RTSE)
  166. #define UART_HWCONTROL_CTS ((uint32_t)USART_CR3_CTSE)
  167. #define UART_HWCONTROL_RTS_CTS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE))
  168. /**
  169. * @}
  170. */
  171. /** @defgroup UART_Mode UART Transfer Mode
  172. * @{
  173. */
  174. #define UART_MODE_RX ((uint32_t)USART_CR1_RE)
  175. #define UART_MODE_TX ((uint32_t)USART_CR1_TE)
  176. #define UART_MODE_TX_RX ((uint32_t)(USART_CR1_TE |USART_CR1_RE))
  177. /**
  178. * @}
  179. */
  180. /** @defgroup UART_State UART State
  181. * @{
  182. */
  183. #define UART_STATE_DISABLE ((uint32_t)0x00000000)
  184. #define UART_STATE_ENABLE ((uint32_t)USART_CR1_UE)
  185. /**
  186. * @}
  187. */
  188. /** @defgroup UART_Over_Sampling UART Over Sampling
  189. * @{
  190. */
  191. #define UART_OVERSAMPLING_16 ((uint32_t)0x00000000)
  192. #define UART_OVERSAMPLING_8 ((uint32_t)USART_CR1_OVER8)
  193. /**
  194. * @}
  195. */
  196. /** @defgroup UART_LIN_Break_Detection_Length UART LIN Break Detection Length
  197. * @{
  198. */
  199. #define UART_LINBREAKDETECTLENGTH_10B ((uint32_t)0x00000000)
  200. #define UART_LINBREAKDETECTLENGTH_11B ((uint32_t)0x00000020)
  201. /**
  202. * @}
  203. */
  204. /** @defgroup UART_WakeUp_functions UART Wakeup Functions
  205. * @{
  206. */
  207. #define UART_WAKEUPMETHOD_IDLELINE ((uint32_t)0x00000000)
  208. #define UART_WAKEUPMETHOD_ADDRESSMARK ((uint32_t)0x00000800)
  209. /**
  210. * @}
  211. */
  212. /** @defgroup UART_Flags UART FLags
  213. * Elements values convention: 0xXXXX
  214. * - 0xXXXX : Flag mask in the SR register
  215. * @{
  216. */
  217. #define UART_FLAG_CTS ((uint32_t)USART_SR_CTS)
  218. #define UART_FLAG_LBD ((uint32_t)USART_SR_LBD)
  219. #define UART_FLAG_TXE ((uint32_t)USART_SR_TXE)
  220. #define UART_FLAG_TC ((uint32_t)USART_SR_TC)
  221. #define UART_FLAG_RXNE ((uint32_t)USART_SR_RXNE)
  222. #define UART_FLAG_IDLE ((uint32_t)USART_SR_IDLE)
  223. #define UART_FLAG_ORE ((uint32_t)USART_SR_ORE)
  224. #define UART_FLAG_NE ((uint32_t)USART_SR_NE)
  225. #define UART_FLAG_FE ((uint32_t)USART_SR_FE)
  226. #define UART_FLAG_PE ((uint32_t)USART_SR_PE)
  227. /**
  228. * @}
  229. */
  230. /** @defgroup UART_Interrupt_definition UART Interrupt Definitions
  231. * Elements values convention: 0xY000XXXX
  232. * - XXXX : Interrupt mask (16 bits) in the Y register
  233. * - Y : Interrupt source register (2bits)
  234. * - 0001: CR1 register
  235. * - 0010: CR2 register
  236. * - 0011: CR3 register
  237. *
  238. * @{
  239. */
  240. #define UART_IT_PE ((uint32_t)(UART_CR1_REG_INDEX << 28 | USART_CR1_PEIE))
  241. #define UART_IT_TXE ((uint32_t)(UART_CR1_REG_INDEX << 28 | USART_CR1_TXEIE))
  242. #define UART_IT_TC ((uint32_t)(UART_CR1_REG_INDEX << 28 | USART_CR1_TCIE))
  243. #define UART_IT_RXNE ((uint32_t)(UART_CR1_REG_INDEX << 28 | USART_CR1_RXNEIE))
  244. #define UART_IT_IDLE ((uint32_t)(UART_CR1_REG_INDEX << 28 | USART_CR1_IDLEIE))
  245. #define UART_IT_LBD ((uint32_t)(UART_CR2_REG_INDEX << 28 | USART_CR2_LBDIE))
  246. #define UART_IT_CTS ((uint32_t)(UART_CR3_REG_INDEX << 28 | USART_CR3_CTSIE))
  247. #define UART_IT_ERR ((uint32_t)(UART_CR3_REG_INDEX << 28 | USART_CR3_EIE))
  248. /**
  249. * @}
  250. */
  251. /**
  252. * @}
  253. */
  254. /* Exported macro ------------------------------------------------------------*/
  255. /** @defgroup UART_Exported_Macros UART Exported Macros
  256. * @{
  257. */
  258. /** @brief Reset UART handle state
  259. * @param __HANDLE__: specifies the UART Handle.
  260. * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  261. * UART peripheral.
  262. * @retval None
  263. */
  264. #define __HAL_UART_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_UART_STATE_RESET)
  265. /** @brief Flushes the UART DR register
  266. * @param __HANDLE__: specifies the UART Handle.
  267. */
  268. #define __HAL_UART_FLUSH_DRREGISTER(__HANDLE__) ((__HANDLE__)->Instance->DR)
  269. /** @brief Checks whether the specified UART flag is set or not.
  270. * @param __HANDLE__: specifies the UART Handle.
  271. * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  272. * UART peripheral.
  273. * @param __FLAG__: specifies the flag to check.
  274. * This parameter can be one of the following values:
  275. * @arg UART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
  276. * @arg UART_FLAG_LBD: LIN Break detection flag
  277. * @arg UART_FLAG_TXE: Transmit data register empty flag
  278. * @arg UART_FLAG_TC: Transmission Complete flag
  279. * @arg UART_FLAG_RXNE: Receive data register not empty flag
  280. * @arg UART_FLAG_IDLE: Idle Line detection flag
  281. * @arg UART_FLAG_ORE: Overrun Error flag
  282. * @arg UART_FLAG_NE: Noise Error flag
  283. * @arg UART_FLAG_FE: Framing Error flag
  284. * @arg UART_FLAG_PE: Parity Error flag
  285. * @retval The new state of __FLAG__ (TRUE or FALSE).
  286. */
  287. #define __HAL_UART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
  288. /** @brief Clears the specified UART pending flag.
  289. * @param __HANDLE__: specifies the UART Handle.
  290. * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  291. * UART peripheral.
  292. * @param __FLAG__: specifies the flag to check.
  293. * This parameter can be any combination of the following values:
  294. * @arg UART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
  295. * @arg UART_FLAG_LBD: LIN Break detection flag.
  296. * @arg UART_FLAG_TC: Transmission Complete flag.
  297. * @arg UART_FLAG_RXNE: Receive data register not empty flag.
  298. *
  299. * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (Overrun
  300. * error) and IDLE (Idle line detected) flags are cleared by software
  301. * sequence: a read operation to USART_SR register followed by a read
  302. * operation to USART_DR register.
  303. * @note RXNE flag can be also cleared by a read to the USART_DR register.
  304. * @note TC flag can be also cleared by software sequence: a read operation to
  305. * USART_SR register followed by a write operation to USART_DR register.
  306. * @note TXE flag is cleared only by a write to the USART_DR register.
  307. *
  308. * @retval None
  309. */
  310. #define __HAL_UART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__))
  311. /** @brief Clear the UART PE pending flag.
  312. * @param __HANDLE__: specifies the UART Handle.
  313. * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  314. * UART peripheral.
  315. * @retval None
  316. */
  317. #define __HAL_UART_CLEAR_PEFLAG(__HANDLE__) \
  318. do{ \
  319. __IO uint32_t tmpreg = 0x00; \
  320. tmpreg = (__HANDLE__)->Instance->SR; \
  321. tmpreg = (__HANDLE__)->Instance->DR; \
  322. UNUSED(tmpreg); \
  323. } while(0)
  324. /** @brief Clear the UART FE pending flag.
  325. * @param __HANDLE__: specifies the UART Handle.
  326. * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  327. * UART peripheral.
  328. * @retval None
  329. */
  330. #define __HAL_UART_CLEAR_FEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  331. /** @brief Clear the UART NE pending flag.
  332. * @param __HANDLE__: specifies the UART Handle.
  333. * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  334. * UART peripheral.
  335. * @retval None
  336. */
  337. #define __HAL_UART_CLEAR_NEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  338. /** @brief Clear the UART ORE pending flag.
  339. * @param __HANDLE__: specifies the UART Handle.
  340. * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  341. * UART peripheral.
  342. * @retval None
  343. */
  344. #define __HAL_UART_CLEAR_OREFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  345. /** @brief Clear the UART IDLE pending flag.
  346. * @param __HANDLE__: specifies the UART Handle.
  347. * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  348. * UART peripheral.
  349. * @retval None
  350. */
  351. #define __HAL_UART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  352. /** @brief Enable the specified UART interrupt.
  353. * @param __HANDLE__: specifies the UART Handle.
  354. * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  355. * UART peripheral.
  356. * @param __INTERRUPT__: specifies the UART interrupt source to enable.
  357. * This parameter can be one of the following values:
  358. * @arg UART_IT_CTS: CTS change interrupt
  359. * @arg UART_IT_LBD: LIN Break detection interrupt
  360. * @arg UART_IT_TXE: Transmit Data Register empty interrupt
  361. * @arg UART_IT_TC: Transmission complete interrupt
  362. * @arg UART_IT_RXNE: Receive Data register not empty interrupt
  363. * @arg UART_IT_IDLE: Idle line detection interrupt
  364. * @arg UART_IT_PE: Parity Error interrupt
  365. * @arg UART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  366. * @retval None
  367. */
  368. #define UART_IT_MASK ((uint32_t)0x0000FFFF)
  369. #define __HAL_UART_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28) == 1)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & UART_IT_MASK)): \
  370. (((__INTERRUPT__) >> 28) == 2)? ((__HANDLE__)->Instance->CR2 |= ((__INTERRUPT__) & UART_IT_MASK)): \
  371. ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & UART_IT_MASK)))
  372. /** @brief Disable the specified UART interrupt.
  373. * @param __HANDLE__: specifies the UART Handle.
  374. * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  375. * UART peripheral.
  376. * @param __INTERRUPT__: specifies the UART interrupt source to disable.
  377. * This parameter can be one of the following values:
  378. * @arg UART_IT_CTS: CTS change interrupt
  379. * @arg UART_IT_LBD: LIN Break detection interrupt
  380. * @arg UART_IT_TXE: Transmit Data Register empty interrupt
  381. * @arg UART_IT_TC: Transmission complete interrupt
  382. * @arg UART_IT_RXNE: Receive Data register not empty interrupt
  383. * @arg UART_IT_IDLE: Idle line detection interrupt
  384. * @arg UART_IT_PE: Parity Error interrupt
  385. * @arg UART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  386. * @retval None
  387. */
  388. #define __HAL_UART_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28) == 1)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
  389. (((__INTERRUPT__) >> 28) == 2)? ((__HANDLE__)->Instance->CR2 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
  390. ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & UART_IT_MASK)))
  391. /** @brief Checks whether the specified UART interrupt has occurred or not.
  392. * @param __HANDLE__: specifies the UART Handle.
  393. * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  394. * UART peripheral.
  395. * @param __IT__: specifies the UART interrupt source to check.
  396. * This parameter can be one of the following values:
  397. * @arg UART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
  398. * @arg UART_IT_LBD: LIN Break detection interrupt
  399. * @arg UART_IT_TXE: Transmit Data Register empty interrupt
  400. * @arg UART_IT_TC: Transmission complete interrupt
  401. * @arg UART_IT_RXNE: Receive Data register not empty interrupt
  402. * @arg UART_IT_IDLE: Idle line detection interrupt
  403. * @arg USART_IT_ERR: Error interrupt
  404. * @retval The new state of __IT__ (TRUE or FALSE).
  405. */
  406. #define __HAL_UART_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28) == 1)? (__HANDLE__)->Instance->CR1:(((((uint32_t)(__IT__)) >> 28) == 2)? \
  407. (__HANDLE__)->Instance->CR2 : (__HANDLE__)->Instance->CR3)) & (((uint32_t)(__IT__)) & UART_IT_MASK))
  408. /** @brief Enable CTS flow control
  409. * This macro allows to enable CTS hardware flow control for a given UART instance,
  410. * without need to call HAL_UART_Init() function.
  411. * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
  412. * @note As macro is expected to be used for modifying CTS Hw flow control feature activation, without need
  413. * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
  414. * - UART instance should have already been initialised (through call of HAL_UART_Init() )
  415. * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
  416. * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
  417. * @param __HANDLE__: specifies the UART Handle.
  418. * The Handle Instance can be USART1, USART2 or LPUART.
  419. * @retval None
  420. */
  421. #define __HAL_UART_HWCONTROL_CTS_ENABLE(__HANDLE__) \
  422. do{ \
  423. SET_BIT((__HANDLE__)->Instance->CR3, USART_CR3_CTSE); \
  424. (__HANDLE__)->Init.HwFlowCtl |= USART_CR3_CTSE; \
  425. } while(0)
  426. /** @brief Disable CTS flow control
  427. * This macro allows to disable CTS hardware flow control for a given UART instance,
  428. * without need to call HAL_UART_Init() function.
  429. * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
  430. * @note As macro is expected to be used for modifying CTS Hw flow control feature activation, without need
  431. * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
  432. * - UART instance should have already been initialised (through call of HAL_UART_Init() )
  433. * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
  434. * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
  435. * @param __HANDLE__: specifies the UART Handle.
  436. * The Handle Instance can be USART1, USART2 or LPUART.
  437. * @retval None
  438. */
  439. #define __HAL_UART_HWCONTROL_CTS_DISABLE(__HANDLE__) \
  440. do{ \
  441. CLEAR_BIT((__HANDLE__)->Instance->CR3, USART_CR3_CTSE); \
  442. (__HANDLE__)->Init.HwFlowCtl &= ~(USART_CR3_CTSE); \
  443. } while(0)
  444. /** @brief Enable RTS flow control
  445. * This macro allows to enable RTS hardware flow control for a given UART instance,
  446. * without need to call HAL_UART_Init() function.
  447. * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
  448. * @note As macro is expected to be used for modifying RTS Hw flow control feature activation, without need
  449. * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
  450. * - UART instance should have already been initialised (through call of HAL_UART_Init() )
  451. * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
  452. * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
  453. * @param __HANDLE__: specifies the UART Handle.
  454. * The Handle Instance can be USART1, USART2 or LPUART.
  455. * @retval None
  456. */
  457. #define __HAL_UART_HWCONTROL_RTS_ENABLE(__HANDLE__) \
  458. do{ \
  459. SET_BIT((__HANDLE__)->Instance->CR3, USART_CR3_RTSE); \
  460. (__HANDLE__)->Init.HwFlowCtl |= USART_CR3_RTSE; \
  461. } while(0)
  462. /** @brief Disable RTS flow control
  463. * This macro allows to disable RTS hardware flow control for a given UART instance,
  464. * without need to call HAL_UART_Init() function.
  465. * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
  466. * @note As macro is expected to be used for modifying RTS Hw flow control feature activation, without need
  467. * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
  468. * - UART instance should have already been initialised (through call of HAL_UART_Init() )
  469. * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
  470. * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
  471. * @param __HANDLE__: specifies the UART Handle.
  472. * The Handle Instance can be USART1, USART2 or LPUART.
  473. * @retval None
  474. */
  475. #define __HAL_UART_HWCONTROL_RTS_DISABLE(__HANDLE__) \
  476. do{ \
  477. CLEAR_BIT((__HANDLE__)->Instance->CR3, USART_CR3_RTSE);\
  478. (__HANDLE__)->Init.HwFlowCtl &= ~(USART_CR3_RTSE); \
  479. } while(0)
  480. /** @brief macros to enables the UART's one bit sample method
  481. * @param __HANDLE__: specifies the UART Handle.
  482. * @retval None
  483. */
  484. #define __HAL_UART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT)
  485. /** @brief macros to disables the UART's one bit sample method
  486. * @param __HANDLE__: specifies the UART Handle.
  487. * @retval None
  488. */
  489. #define __HAL_UART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT))
  490. /** @brief Enable UART
  491. * @param __HANDLE__: specifies the UART Handle.
  492. * @retval None
  493. */
  494. #define __HAL_UART_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
  495. /** @brief Disable UART
  496. * @param __HANDLE__: specifies the UART Handle.
  497. * @retval None
  498. */
  499. #define __HAL_UART_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
  500. /**
  501. * @}
  502. */
  503. /* Exported functions --------------------------------------------------------*/
  504. /** @addtogroup UART_Exported_Functions
  505. * @{
  506. */
  507. /** @addtogroup UART_Exported_Functions_Group1
  508. * @{
  509. */
  510. /* Initialization/de-initialization functions **********************************/
  511. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart);
  512. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart);
  513. HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength);
  514. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod);
  515. HAL_StatusTypeDef HAL_UART_DeInit (UART_HandleTypeDef *huart);
  516. void HAL_UART_MspInit(UART_HandleTypeDef *huart);
  517. void HAL_UART_MspDeInit(UART_HandleTypeDef *huart);
  518. /**
  519. * @}
  520. */
  521. /** @addtogroup UART_Exported_Functions_Group2
  522. * @{
  523. */
  524. /* IO operation functions *******************************************************/
  525. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  526. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  527. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  528. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  529. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  530. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  531. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart);
  532. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart);
  533. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart);
  534. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart);
  535. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
  536. void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart);
  537. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
  538. void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart);
  539. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart);
  540. /**
  541. * @}
  542. */
  543. /** @addtogroup UART_Exported_Functions_Group3
  544. * @{
  545. */
  546. /* Peripheral Control functions ************************************************/
  547. HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart);
  548. HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart);
  549. HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart);
  550. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart);
  551. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart);
  552. /**
  553. * @}
  554. */
  555. /** @addtogroup UART_Exported_Functions_Group4
  556. * @{
  557. */
  558. /* Peripheral State functions **************************************************/
  559. HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart);
  560. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart);
  561. /**
  562. * @}
  563. */
  564. /**
  565. * @}
  566. */
  567. /* Private types -------------------------------------------------------------*/
  568. /* Private variables ---------------------------------------------------------*/
  569. /* Private constants ---------------------------------------------------------*/
  570. /** @defgroup UART_Private_Constants UART Private Constants
  571. * @{
  572. */
  573. /** @brief UART interruptions flag mask
  574. *
  575. */
  576. #define UART_CR1_REG_INDEX 1
  577. #define UART_CR2_REG_INDEX 2
  578. #define UART_CR3_REG_INDEX 3
  579. /**
  580. * @}
  581. */
  582. /* Private macros ------------------------------------------------------------*/
  583. /** @defgroup UART_Private_Macros UART Private Macros
  584. * @{
  585. */
  586. #define IS_UART_WORD_LENGTH(LENGTH) (((LENGTH) == UART_WORDLENGTH_8B) || \
  587. ((LENGTH) == UART_WORDLENGTH_9B))
  588. #define IS_UART_LIN_WORD_LENGTH(LENGTH) (((LENGTH) == UART_WORDLENGTH_8B))
  589. #define IS_UART_STOPBITS(STOPBITS) (((STOPBITS) == UART_STOPBITS_1) || \
  590. ((STOPBITS) == UART_STOPBITS_2))
  591. #define IS_UART_PARITY(PARITY) (((PARITY) == UART_PARITY_NONE) || \
  592. ((PARITY) == UART_PARITY_EVEN) || \
  593. ((PARITY) == UART_PARITY_ODD))
  594. #define IS_UART_HARDWARE_FLOW_CONTROL(CONTROL)\
  595. (((CONTROL) == UART_HWCONTROL_NONE) || \
  596. ((CONTROL) == UART_HWCONTROL_RTS) || \
  597. ((CONTROL) == UART_HWCONTROL_CTS) || \
  598. ((CONTROL) == UART_HWCONTROL_RTS_CTS))
  599. #define IS_UART_MODE(MODE) ((((MODE) & (uint32_t)0x0000FFF3) == 0x00) && ((MODE) != (uint32_t)0x000000))
  600. #define IS_UART_STATE(STATE) (((STATE) == UART_STATE_DISABLE) || \
  601. ((STATE) == UART_STATE_ENABLE))
  602. #define IS_UART_OVERSAMPLING(SAMPLING) (((SAMPLING) == UART_OVERSAMPLING_16) || \
  603. ((SAMPLING) == UART_OVERSAMPLING_8))
  604. #define IS_UART_LIN_OVERSAMPLING(SAMPLING) (((SAMPLING) == UART_OVERSAMPLING_16))
  605. #define IS_UART_LIN_BREAK_DETECT_LENGTH(LENGTH) (((LENGTH) == UART_LINBREAKDETECTLENGTH_10B) || \
  606. ((LENGTH) == UART_LINBREAKDETECTLENGTH_11B))
  607. #define IS_UART_WAKEUPMETHOD(WAKEUP) (((WAKEUP) == UART_WAKEUPMETHOD_IDLELINE) || \
  608. ((WAKEUP) == UART_WAKEUPMETHOD_ADDRESSMARK))
  609. #define IS_UART_BAUDRATE(BAUDRATE) ((BAUDRATE) < 10500001)
  610. #define IS_UART_ADDRESS(ADDRESS) ((ADDRESS) <= 0xF)
  611. #define UART_DIV_SAMPLING16(_PCLK_, _BAUD_) (((_PCLK_)*25)/(4*(_BAUD_)))
  612. #define UART_DIVMANT_SAMPLING16(_PCLK_, _BAUD_) (UART_DIV_SAMPLING16((_PCLK_), (_BAUD_))/100)
  613. #define UART_DIVFRAQ_SAMPLING16(_PCLK_, _BAUD_) (((UART_DIV_SAMPLING16((_PCLK_), (_BAUD_)) - (UART_DIVMANT_SAMPLING16((_PCLK_), (_BAUD_)) * 100)) * 16 + 50) / 100)
  614. #define UART_BRR_SAMPLING16(_PCLK_, _BAUD_) ((UART_DIVMANT_SAMPLING16((_PCLK_), (_BAUD_)) << 4)|(UART_DIVFRAQ_SAMPLING16((_PCLK_), (_BAUD_)) & 0x0F))
  615. #define UART_DIV_SAMPLING8(_PCLK_, _BAUD_) (((_PCLK_)*25)/(2*(_BAUD_)))
  616. #define UART_DIVMANT_SAMPLING8(_PCLK_, _BAUD_) (UART_DIV_SAMPLING8((_PCLK_), (_BAUD_))/100)
  617. #define UART_DIVFRAQ_SAMPLING8(_PCLK_, _BAUD_) (((UART_DIV_SAMPLING8((_PCLK_), (_BAUD_)) - (UART_DIVMANT_SAMPLING8((_PCLK_), (_BAUD_)) * 100)) * 16 + 50) / 100)
  618. #define UART_BRR_SAMPLING8(_PCLK_, _BAUD_) ((UART_DIVMANT_SAMPLING8((_PCLK_), (_BAUD_)) << 4)|(UART_DIVFRAQ_SAMPLING8((_PCLK_), (_BAUD_)) & 0x0F))
  619. /**
  620. * @}
  621. */
  622. /* Private functions ---------------------------------------------------------*/
  623. /** @defgroup UART_Private_Functions UART Private Functions
  624. * @{
  625. */
  626. /**
  627. * @}
  628. */
  629. /**
  630. * @}
  631. */
  632. /**
  633. * @}
  634. */
  635. #ifdef __cplusplus
  636. }
  637. #endif
  638. #endif /* __STM32F4xx_HAL_UART_H */
  639. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/