stm32f1xx_hal_uart.h 38 KB

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