stm32l1xx_hal_uart.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_uart.h
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  7. * @brief This file contains all the functions prototypes for the UART
  8. * firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ******************************************************************************
  37. */
  38. /* Define to prevent recursive inclusion -------------------------------------*/
  39. #ifndef __STM32L1xx_HAL_UART_H
  40. #define __STM32L1xx_HAL_UART_H
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /* Includes ------------------------------------------------------------------*/
  45. #include "stm32l1xx_hal_def.h"
  46. /** @addtogroup STM32L1xx_HAL_Driver
  47. * @{
  48. */
  49. /** @addtogroup UART
  50. * @{
  51. */
  52. /* Exported types ------------------------------------------------------------*/
  53. /** @defgroup UART_Exported_Types UART Exported Types
  54. * @{
  55. */
  56. /**
  57. * @brief UART Init Structure definition
  58. */
  59. typedef struct
  60. {
  61. uint32_t BaudRate; /*!< This member configures the UART communication baud rate.
  62. The baud rate is computed using the following formula:
  63. - IntegerDivider = ((PCLKx) / (8 * (OVR8+1) * (huart->Init.BaudRate)))
  64. - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 8 * (OVR8+1)) + 0.5
  65. Where OVR8 is the "oversampling by 8 mode" configuration bit in the CR1 register. */
  66. uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  67. This parameter can be a value of @ref UART_Word_Length */
  68. uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
  69. This parameter can be a value of @ref UART_Stop_Bits */
  70. uint32_t Parity; /*!< Specifies the parity mode.
  71. This parameter can be a value of @ref UART_Parity
  72. @note When parity is enabled, the computed parity is inserted
  73. at the MSB position of the transmitted data (9th bit when
  74. the word length is set to 9 data bits; 8th bit when the
  75. word length is set to 8 data bits). */
  76. uint32_t Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
  77. This parameter can be a value of @ref UART_Mode */
  78. uint32_t HwFlowCtl; /*!< Specifies wether the hardware flow control mode is enabled
  79. or disabled.
  80. This parameter can be a value of @ref UART_Hardware_Flow_Control */
  81. uint32_t OverSampling; /*!< Specifies wether the Over sampling 8 is enabled or disabled, to achieve higher speed (up to fPCLK/8).
  82. This parameter can be a value of @ref UART_Over_Sampling */
  83. }UART_InitTypeDef;
  84. /**
  85. * @brief HAL UART State structures definition
  86. */
  87. typedef enum
  88. {
  89. HAL_UART_STATE_RESET = 0x00, /*!< Peripheral is not initialized */
  90. HAL_UART_STATE_READY = 0x01, /*!< Peripheral Initialized and ready for use */
  91. HAL_UART_STATE_BUSY = 0x02, /*!< an internal process is ongoing */
  92. HAL_UART_STATE_BUSY_TX = 0x12, /*!< Data Transmission process is ongoing */
  93. HAL_UART_STATE_BUSY_RX = 0x22, /*!< Data Reception process is ongoing */
  94. HAL_UART_STATE_BUSY_TX_RX = 0x32, /*!< Data Transmission and Reception process is ongoing */
  95. HAL_UART_STATE_TIMEOUT = 0x03, /*!< Timeout state */
  96. HAL_UART_STATE_ERROR = 0x04 /*!< Error */
  97. }HAL_UART_StateTypeDef;
  98. /**
  99. * @brief HAL UART Error Code structure definition
  100. */
  101. typedef enum
  102. {
  103. HAL_UART_ERROR_NONE = 0x00, /*!< No error */
  104. HAL_UART_ERROR_PE = 0x01, /*!< Parity error */
  105. HAL_UART_ERROR_NE = 0x02, /*!< Noise error */
  106. HAL_UART_ERROR_FE = 0x04, /*!< frame error */
  107. HAL_UART_ERROR_ORE = 0x08, /*!< Overrun error */
  108. HAL_UART_ERROR_DMA = 0x10 /*!< DMA transfer error */
  109. }HAL_UART_ErrorTypeDef;
  110. /**
  111. * @brief UART handle Structure definition
  112. */
  113. typedef struct
  114. {
  115. USART_TypeDef *Instance; /* UART registers base address */
  116. UART_InitTypeDef Init; /* UART communication parameters */
  117. uint8_t *pTxBuffPtr; /* Pointer to UART Tx transfer Buffer */
  118. uint16_t TxXferSize; /* UART Tx Transfer size */
  119. uint16_t TxXferCount; /* UART Tx Transfer Counter */
  120. uint8_t *pRxBuffPtr; /* Pointer to UART Rx transfer Buffer */
  121. uint16_t RxXferSize; /* UART Rx Transfer size */
  122. uint16_t RxXferCount; /* UART Rx Transfer Counter */
  123. DMA_HandleTypeDef *hdmatx; /* UART Tx DMA Handle parameters */
  124. DMA_HandleTypeDef *hdmarx; /* UART Rx DMA Handle parameters */
  125. HAL_LockTypeDef Lock; /* Locking object */
  126. __IO HAL_UART_StateTypeDef State; /* UART communication state */
  127. __IO HAL_UART_ErrorTypeDef ErrorCode; /* UART Error code */
  128. }UART_HandleTypeDef;
  129. /**
  130. * @}
  131. */
  132. /* Exported constants --------------------------------------------------------*/
  133. /** @defgroup UART_Exported_Constants UART Exported constants
  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. #define IS_UART_WORD_LENGTH(LENGTH) (((LENGTH) == UART_WORDLENGTH_8B) || \
  142. ((LENGTH) == UART_WORDLENGTH_9B))
  143. /**
  144. * @}
  145. */
  146. /** @defgroup UART_Stop_Bits UART Number of Stop Bits
  147. * @{
  148. */
  149. #define UART_STOPBITS_1 ((uint32_t)0x00000000)
  150. #define UART_STOPBITS_2 ((uint32_t)USART_CR2_STOP_1)
  151. #define IS_UART_STOPBITS(STOPBITS) (((STOPBITS) == UART_STOPBITS_1) || \
  152. ((STOPBITS) == UART_STOPBITS_2))
  153. /**
  154. * @}
  155. */
  156. /** @defgroup UART_Parity UART Parity
  157. * @{
  158. */
  159. #define UART_PARITY_NONE ((uint32_t)0x00000000)
  160. #define UART_PARITY_EVEN ((uint32_t)USART_CR1_PCE)
  161. #define UART_PARITY_ODD ((uint32_t)(USART_CR1_PCE | USART_CR1_PS))
  162. #define IS_UART_PARITY(PARITY) (((PARITY) == UART_PARITY_NONE) || \
  163. ((PARITY) == UART_PARITY_EVEN) || \
  164. ((PARITY) == UART_PARITY_ODD))
  165. /**
  166. * @}
  167. */
  168. /** @defgroup UART_Hardware_Flow_Control UART Hardware Flow Control
  169. * @{
  170. */
  171. #define UART_HWCONTROL_NONE ((uint32_t)0x00000000)
  172. #define UART_HWCONTROL_RTS ((uint32_t)USART_CR3_RTSE)
  173. #define UART_HWCONTROL_CTS ((uint32_t)USART_CR3_CTSE)
  174. #define UART_HWCONTROL_RTS_CTS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE))
  175. #define IS_UART_HARDWARE_FLOW_CONTROL(CONTROL)\
  176. (((CONTROL) == UART_HWCONTROL_NONE) || \
  177. ((CONTROL) == UART_HWCONTROL_RTS) || \
  178. ((CONTROL) == UART_HWCONTROL_CTS) || \
  179. ((CONTROL) == UART_HWCONTROL_RTS_CTS))
  180. /**
  181. * @}
  182. */
  183. /** @defgroup UART_Mode UART Transfer Mode
  184. * @{
  185. */
  186. #define UART_MODE_RX ((uint32_t)USART_CR1_RE)
  187. #define UART_MODE_TX ((uint32_t)USART_CR1_TE)
  188. #define UART_MODE_TX_RX ((uint32_t)(USART_CR1_TE |USART_CR1_RE))
  189. #define IS_UART_MODE(MODE) ((((MODE) & (uint32_t)0x0000FFF3) == 0x00) && ((MODE) != (uint32_t)0x00000000))
  190. /**
  191. * @}
  192. */
  193. /** @defgroup UART_State UART State
  194. * @{
  195. */
  196. #define UART_STATE_DISABLE ((uint32_t)0x00000000)
  197. #define UART_STATE_ENABLE ((uint32_t)USART_CR1_UE)
  198. #define IS_UART_STATE(STATE) (((STATE) == UART_STATE_DISABLE) || \
  199. ((STATE) == UART_STATE_ENABLE))
  200. /**
  201. * @}
  202. */
  203. /** @defgroup UART_Over_Sampling UART Over Sampling
  204. * @{
  205. */
  206. #define UART_OVERSAMPLING_16 ((uint32_t)0x00000000)
  207. #define UART_OVERSAMPLING_8 ((uint32_t)USART_CR1_OVER8)
  208. #define IS_UART_OVERSAMPLING(SAMPLING) (((SAMPLING) == UART_OVERSAMPLING_16) || \
  209. ((SAMPLING) == UART_OVERSAMPLING_8))
  210. /**
  211. * @}
  212. */
  213. /** @defgroup UART_LIN_Break_Detection_Length UART LIN Break Detection Length
  214. * @{
  215. */
  216. #define UART_LINBREAKDETECTLENGTH_10B ((uint32_t)0x00000000)
  217. #define UART_LINBREAKDETECTLENGTH_11B ((uint32_t)USART_CR2_LBDL)
  218. #define IS_UART_LIN_BREAK_DETECT_LENGTH(LENGTH) (((LENGTH) == UART_LINBREAKDETECTLENGTH_10B) || \
  219. ((LENGTH) == UART_LINBREAKDETECTLENGTH_11B))
  220. /**
  221. * @}
  222. */
  223. /** @defgroup UART_WakeUp_functions UART Wakeup Functions
  224. * @{
  225. */
  226. #define UART_WAKEUPMETHOD_IDLELINE ((uint32_t)0x00000000)
  227. #define UART_WAKEUPMETHOD_ADDRESSMARK ((uint32_t)0x00000800)
  228. #define IS_UART_WAKEUPMETHOD(WAKEUP) (((WAKEUP) == UART_WAKEUPMETHOD_IDLELINE) || \
  229. ((WAKEUP) == UART_WAKEUPMETHOD_ADDRESSMARK))
  230. /**
  231. * @}
  232. */
  233. /** @defgroup UART_Flags UART FLags
  234. * Elements values convention: 0xXXXX
  235. * - 0xXXXX : Flag mask in the SR register
  236. * @{
  237. */
  238. #define UART_FLAG_CTS ((uint32_t)USART_SR_CTS)
  239. #define UART_FLAG_LBD ((uint32_t)USART_SR_LBD)
  240. #define UART_FLAG_TXE ((uint32_t)USART_SR_TXE)
  241. #define UART_FLAG_TC ((uint32_t)USART_SR_TC)
  242. #define UART_FLAG_RXNE ((uint32_t)USART_SR_RXNE)
  243. #define UART_FLAG_IDLE ((uint32_t)USART_SR_IDLE)
  244. #define UART_FLAG_ORE ((uint32_t)USART_SR_ORE)
  245. #define UART_FLAG_NE ((uint32_t)USART_SR_NE)
  246. #define UART_FLAG_FE ((uint32_t)USART_SR_FE)
  247. #define UART_FLAG_PE ((uint32_t)USART_SR_PE)
  248. /**
  249. * @}
  250. */
  251. /** @defgroup UART_Interrupt_definition UART Interrupt Definitions
  252. * Elements values convention: 0xY000XXXX
  253. * - XXXX : Interrupt mask in the XX register
  254. * - Y : Interrupt source register (2bits)
  255. * - 01: CR1 register
  256. * - 10: CR2 register
  257. * - 11: CR3 register
  258. *
  259. * @{
  260. */
  261. #define UART_IT_PE ((uint32_t)0x10000100)
  262. #define UART_IT_TXE ((uint32_t)0x10000080)
  263. #define UART_IT_TC ((uint32_t)0x10000040)
  264. #define UART_IT_RXNE ((uint32_t)0x10000020)
  265. #define UART_IT_IDLE ((uint32_t)0x10000010)
  266. #define UART_IT_LBD ((uint32_t)0x20000040)
  267. #define UART_IT_CTS ((uint32_t)0x30000400)
  268. #define UART_IT_ERR ((uint32_t)0x30000001)
  269. /**
  270. * @}
  271. */
  272. /** @defgroup UART_Interruption_Mask UART interruptions flag mask
  273. * @{
  274. */
  275. #define UART_IT_MASK ((uint32_t)0x0000FFFF)
  276. /**
  277. * @}
  278. */
  279. /**
  280. * @}
  281. */
  282. /* Exported macro ------------------------------------------------------------*/
  283. /** @defgroup UART_Exported_Macros UART Exported Macros
  284. * @{
  285. */
  286. /** @brief Reset UART handle state
  287. * @param __HANDLE__: specifies the UART Handle.
  288. * This parameter can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  289. * UART peripheral (availability depending on device for UARTy).
  290. * @retval None
  291. */
  292. #define __HAL_UART_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_UART_STATE_RESET)
  293. /** @brief Flushs the UART DR register
  294. * @param __HANDLE__: specifies the UART Handle.
  295. */
  296. #define __HAL_UART_FLUSH_DRREGISTER(__HANDLE__) ((__HANDLE__)->Instance->DR)
  297. /** @brief Checks whether the specified UART flag is set or not.
  298. * @param __HANDLE__: specifies the UART Handle.
  299. * This parameter can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  300. * UART peripheral (availability depending on device for UARTy).
  301. * @param __FLAG__: specifies the flag to check.
  302. * This parameter can be one of the following values:
  303. * @arg UART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
  304. * @arg UART_FLAG_LBD: LIN Break detection flag
  305. * @arg UART_FLAG_TXE: Transmit data register empty flag
  306. * @arg UART_FLAG_TC: Transmission Complete flag
  307. * @arg UART_FLAG_RXNE: Receive data register not empty flag
  308. * @arg UART_FLAG_IDLE: Idle Line detection flag
  309. * @arg UART_FLAG_ORE: OverRun Error flag
  310. * @arg UART_FLAG_NE: Noise Error flag
  311. * @arg UART_FLAG_FE: Framing Error flag
  312. * @arg UART_FLAG_PE: Parity Error flag
  313. * @retval The new state of __FLAG__ (TRUE or FALSE).
  314. */
  315. #define __HAL_UART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
  316. /** @brief Clears the specified UART pending flag.
  317. * @param __HANDLE__: specifies the UART Handle.
  318. * This parameter can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  319. * UART peripheral (availability depending on device for UARTy).
  320. * @param __FLAG__: specifies the flag to check.
  321. * This parameter can be any combination of the following values:
  322. * @arg UART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
  323. * @arg UART_FLAG_LBD: LIN Break detection flag.
  324. * @arg UART_FLAG_TC: Transmission Complete flag.
  325. * @arg UART_FLAG_RXNE: Receive data register not empty flag.
  326. *
  327. * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  328. * error) and IDLE (Idle line detected) flags are cleared by software
  329. * sequence: a read operation to USART_SR register followed by a read
  330. * operation to USART_DR register.
  331. * @note RXNE flag can be also cleared by a read to the USART_DR register.
  332. * @note TC flag can be also cleared by software sequence: a read operation to
  333. * USART_SR register followed by a write operation to USART_DR register.
  334. * @note TXE flag is cleared only by a write to the USART_DR register.
  335. *
  336. * @retval None
  337. */
  338. #define __HAL_UART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__))
  339. /** @brief Clear the UART PE pending flag.
  340. * @param __HANDLE__: specifies the UART Handle.
  341. * This parameter can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  342. * UART peripheral (availability depending on device for UARTy).
  343. * @retval None
  344. */
  345. #define __HAL_UART_CLEAR_PEFLAG(__HANDLE__) do{(__HANDLE__)->Instance->SR;\
  346. (__HANDLE__)->Instance->DR;}while(0)
  347. /** @brief Clear the UART FE pending flag.
  348. * @param __HANDLE__: specifies the UART Handle.
  349. * This parameter can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  350. * UART peripheral (availability depending on device for UARTy).
  351. * @retval None
  352. */
  353. #define __HAL_UART_CLEAR_FEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  354. /** @brief Clear the UART NE pending flag.
  355. * @param __HANDLE__: specifies the UART Handle.
  356. * This parameter can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  357. * UART peripheral (availability depending on device for UARTy).
  358. * @retval None
  359. */
  360. #define __HAL_UART_CLEAR_NEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  361. /** @brief Clear the UART ORE pending flag.
  362. * @param __HANDLE__: specifies the UART Handle.
  363. * This parameter can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  364. * UART peripheral (availability depending on device for UARTy).
  365. * @retval None
  366. */
  367. #define __HAL_UART_CLEAR_OREFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  368. /** @brief Clear the UART IDLE pending flag.
  369. * @param __HANDLE__: specifies the UART Handle.
  370. * This parameter can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  371. * UART peripheral (availability depending on device for UARTy).
  372. * @retval None
  373. */
  374. #define __HAL_UART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
  375. /** @brief Enables or disables the specified UART interrupt.
  376. * @param __HANDLE__: specifies the UART Handle.
  377. * This parameter can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  378. * UART peripheral (availability depending on device for UARTy).
  379. * @param __INTERRUPT__: specifies the UART interrupt source to check.
  380. * This parameter can be one of the following values:
  381. * @arg UART_IT_CTS: CTS change interrupt
  382. * @arg UART_IT_LBD: LIN Break detection interrupt
  383. * @arg UART_IT_TXE: Transmit Data Register empty interrupt
  384. * @arg UART_IT_TC: Transmission complete interrupt
  385. * @arg UART_IT_RXNE: Receive Data register not empty interrupt
  386. * @arg UART_IT_IDLE: Idle line detection interrupt
  387. * @arg UART_IT_PE: Parity Error interrupt
  388. * @arg UART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  389. * @retval None
  390. */
  391. #define __HAL_UART_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28) == 1)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & UART_IT_MASK)): \
  392. (((__INTERRUPT__) >> 28) == 2)? ((__HANDLE__)->Instance->CR2 |= ((__INTERRUPT__) & UART_IT_MASK)): \
  393. ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & UART_IT_MASK)))
  394. #define __HAL_UART_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28) == 1)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
  395. (((__INTERRUPT__) >> 28) == 2)? ((__HANDLE__)->Instance->CR2 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
  396. ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & UART_IT_MASK)))
  397. /** @brief Checks whether the specified UART interrupt has occurred or not.
  398. * @param __HANDLE__: specifies the UART Handle.
  399. * This parameter can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  400. * UART peripheral (availability depending on device for UARTy).
  401. * @param __IT__: specifies the UART interrupt source to check.
  402. * This parameter can be one of the following values:
  403. * @arg UART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
  404. * @arg UART_IT_LBD: LIN Break detection interrupt
  405. * @arg UART_IT_TXE: Transmit Data Register empty interrupt
  406. * @arg UART_IT_TC: Transmission complete interrupt
  407. * @arg UART_IT_RXNE: Receive Data register not empty interrupt
  408. * @arg UART_IT_IDLE: Idle line detection interrupt
  409. * @arg USART_IT_ERR: Error interrupt
  410. * @retval The new state of __IT__ (TRUE or FALSE).
  411. */
  412. #define __HAL_UART_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28) == 1)? (__HANDLE__)->Instance->CR1:(((((uint32_t)(__IT__)) >> 28) == 2)? \
  413. (__HANDLE__)->Instance->CR2 : (__HANDLE__)->Instance->CR3)) & (((uint32_t)(__IT__)) & UART_IT_MASK))
  414. /** @brief macros to enables or disables the UART's one bit sampling method
  415. * @param __HANDLE__: specifies the UART Handle.
  416. * This parameter can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  417. * UART peripheral (availability depending on device for UARTy).
  418. * @retval None
  419. */
  420. #define __HAL_UART_ONEBIT_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT)
  421. #define __HAL_UART_ONEBIT_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT))
  422. /** @brief Enable UART
  423. * @param __HANDLE__: specifies the UART Handle.
  424. * The Handle Instance can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  425. * UART peripheral (availability depending on device for UARTy).
  426. * @retval None
  427. */
  428. #define __HAL_UART_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
  429. /** @brief Disable UART
  430. * The Handle Instance can be USARTx with x: 1, 2 or 3, or UARTy with y:4 or 5 to select the USART or
  431. * UART peripheral (availability depending on device for UARTy).
  432. * @retval None
  433. */
  434. #define __HAL_UART_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
  435. /**
  436. * @}
  437. */
  438. /* Private macros --------------------------------------------------------*/
  439. /** @defgroup UART_Private_Macros UART Private Macros
  440. * @{
  441. */
  442. #define UART_DIV_SAMPLING16(_PCLK_, _BAUD_) (((_PCLK_)*25)/(4*(_BAUD_)))
  443. #define UART_DIVMANT_SAMPLING16(_PCLK_, _BAUD_) (UART_DIV_SAMPLING16((_PCLK_), (_BAUD_))/100)
  444. #define UART_DIVFRAQ_SAMPLING16(_PCLK_, _BAUD_) (((UART_DIV_SAMPLING16((_PCLK_), (_BAUD_)) - (UART_DIVMANT_SAMPLING16((_PCLK_), (_BAUD_)) * 100)) * 16 + 50) / 100)
  445. #define UART_BRR_SAMPLING16(_PCLK_, _BAUD_) ((UART_DIVMANT_SAMPLING16((_PCLK_), (_BAUD_)) << 4)|(UART_DIVFRAQ_SAMPLING16((_PCLK_), (_BAUD_)) & 0x0F))
  446. #define UART_DIV_SAMPLING8(_PCLK_, _BAUD_) (((_PCLK_)*25)/(2*(_BAUD_)))
  447. #define UART_DIVMANT_SAMPLING8(_PCLK_, _BAUD_) (UART_DIV_SAMPLING8((_PCLK_), (_BAUD_))/100)
  448. #define UART_DIVFRAQ_SAMPLING8(_PCLK_, _BAUD_) (((UART_DIV_SAMPLING8((_PCLK_), (_BAUD_)) - (UART_DIVMANT_SAMPLING8((_PCLK_), (_BAUD_)) * 100)) * 16 + 50) / 100)
  449. #define UART_BRR_SAMPLING8(_PCLK_, _BAUD_) ((UART_DIVMANT_SAMPLING8((_PCLK_), (_BAUD_)) << 4)|(UART_DIVFRAQ_SAMPLING8((_PCLK_), (_BAUD_)) & 0x0F))
  450. /** @brief Check UART Baud rate
  451. * @param __BAUDRATE__: Baudrate specified by the user
  452. * The maximum Baud Rate is derived from the maximum clock on APB (i.e. 32 MHz)
  453. * divided by the smallest oversampling used on the USART (i.e. 8)
  454. * @retval Test result (TRUE or FALSE).
  455. */
  456. #define IS_UART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) < 4000001)
  457. #define IS_UART_ADDRESS(ADDRESS) ((ADDRESS) <= 0xF)
  458. /**
  459. * @}
  460. */
  461. /* Exported functions --------------------------------------------------------*/
  462. /** @addtogroup UART_Exported_Functions UART Exported Functions
  463. * @{
  464. */
  465. /** @addtogroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
  466. * @{
  467. */
  468. /* Initialization and de-initialization functions ****************************/
  469. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart);
  470. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart);
  471. HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength);
  472. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod);
  473. HAL_StatusTypeDef HAL_UART_DeInit (UART_HandleTypeDef *huart);
  474. void HAL_UART_MspInit(UART_HandleTypeDef *huart);
  475. void HAL_UART_MspDeInit(UART_HandleTypeDef *huart);
  476. /**
  477. * @}
  478. */
  479. /** @addtogroup UART_Exported_Functions_Group2 IO operation functions
  480. * @{
  481. */
  482. /* IO operation functions *****************************************************/
  483. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  484. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  485. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  486. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  487. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  488. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
  489. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart);
  490. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart);
  491. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart);
  492. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart);
  493. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
  494. void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart);
  495. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
  496. void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart);
  497. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart);
  498. /**
  499. * @}
  500. */
  501. /** @addtogroup UART_Exported_Functions_Group3 Peripheral Control functions
  502. * @{
  503. */
  504. /* Peripheral Control functions ************************************************/
  505. HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart);
  506. HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart);
  507. HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart);
  508. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart);
  509. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart);
  510. /**
  511. * @}
  512. */
  513. /** @addtogroup UART_Exported_Functions_Group4 Peripheral State and Errors functions
  514. * @{
  515. */
  516. /* Peripheral State and Errors functions **************************************************/
  517. HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart);
  518. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart);
  519. /**
  520. * @}
  521. */
  522. /**
  523. * @}
  524. */
  525. /**
  526. * @}
  527. */
  528. /**
  529. * @}
  530. */
  531. #ifdef __cplusplus
  532. }
  533. #endif
  534. #endif /* __STM32L1xx_HAL_UART_H */
  535. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/