stm32f1xx_hal_usart.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_usart.h
  4. * @author MCD Application Team
  5. * @brief Header file of USART HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* Define to prevent recursive inclusion -------------------------------------*/
  19. #ifndef __STM32F1xx_HAL_USART_H
  20. #define __STM32F1xx_HAL_USART_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32f1xx_hal_def.h"
  26. /** @addtogroup STM32F1xx_HAL_Driver
  27. * @{
  28. */
  29. /** @addtogroup USART
  30. * @{
  31. */
  32. /* Exported types ------------------------------------------------------------*/
  33. /** @defgroup USART_Exported_Types USART Exported Types
  34. * @{
  35. */
  36. /**
  37. * @brief USART Init Structure definition
  38. */
  39. typedef struct
  40. {
  41. uint32_t BaudRate; /*!< This member configures the Usart communication baud rate.
  42. The baud rate is computed using the following formula:
  43. - IntegerDivider = ((PCLKx) / (16 * (husart->Init.BaudRate)))
  44. - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 16) + 0.5 */
  45. uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  46. This parameter can be a value of @ref USART_Word_Length */
  47. uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
  48. This parameter can be a value of @ref USART_Stop_Bits */
  49. uint32_t Parity; /*!< Specifies the parity mode.
  50. This parameter can be a value of @ref USART_Parity
  51. @note When parity is enabled, the computed parity is inserted
  52. at the MSB position of the transmitted data (9th bit when
  53. the word length is set to 9 data bits; 8th bit when the
  54. word length is set to 8 data bits). */
  55. uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
  56. This parameter can be a value of @ref USART_Mode */
  57. uint32_t CLKPolarity; /*!< Specifies the steady state of the serial clock.
  58. This parameter can be a value of @ref USART_Clock_Polarity */
  59. uint32_t CLKPhase; /*!< Specifies the clock transition on which the bit capture is made.
  60. This parameter can be a value of @ref USART_Clock_Phase */
  61. uint32_t CLKLastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
  62. data bit (MSB) has to be output on the SCLK pin in synchronous mode.
  63. This parameter can be a value of @ref USART_Last_Bit */
  64. } USART_InitTypeDef;
  65. /**
  66. * @brief HAL State structures definition
  67. */
  68. typedef enum
  69. {
  70. HAL_USART_STATE_RESET = 0x00U, /*!< Peripheral is not yet Initialized */
  71. HAL_USART_STATE_READY = 0x01U, /*!< Peripheral Initialized and ready for use */
  72. HAL_USART_STATE_BUSY = 0x02U, /*!< an internal process is ongoing */
  73. HAL_USART_STATE_BUSY_TX = 0x12U, /*!< Data Transmission process is ongoing */
  74. HAL_USART_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */
  75. HAL_USART_STATE_BUSY_TX_RX = 0x32U, /*!< Data Transmission Reception process is ongoing */
  76. HAL_USART_STATE_TIMEOUT = 0x03U, /*!< Timeout state */
  77. HAL_USART_STATE_ERROR = 0x04U /*!< Error */
  78. } HAL_USART_StateTypeDef;
  79. /**
  80. * @brief USART handle Structure definition
  81. */
  82. typedef struct __USART_HandleTypeDef
  83. {
  84. USART_TypeDef *Instance; /*!< USART registers base address */
  85. USART_InitTypeDef Init; /*!< Usart communication parameters */
  86. const uint8_t *pTxBuffPtr; /*!< Pointer to Usart Tx transfer Buffer */
  87. uint16_t TxXferSize; /*!< Usart Tx Transfer size */
  88. __IO uint16_t TxXferCount; /*!< Usart Tx Transfer Counter */
  89. uint8_t *pRxBuffPtr; /*!< Pointer to Usart Rx transfer Buffer */
  90. uint16_t RxXferSize; /*!< Usart Rx Transfer size */
  91. __IO uint16_t RxXferCount; /*!< Usart Rx Transfer Counter */
  92. DMA_HandleTypeDef *hdmatx; /*!< Usart Tx DMA Handle parameters */
  93. DMA_HandleTypeDef *hdmarx; /*!< Usart Rx DMA Handle parameters */
  94. HAL_LockTypeDef Lock; /*!< Locking object */
  95. __IO HAL_USART_StateTypeDef State; /*!< Usart communication state */
  96. __IO uint32_t ErrorCode; /*!< USART Error code */
  97. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  98. void (* TxHalfCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Half Complete Callback */
  99. void (* TxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Complete Callback */
  100. void (* RxHalfCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Rx Half Complete Callback */
  101. void (* RxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Rx Complete Callback */
  102. void (* TxRxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Rx Complete Callback */
  103. void (* ErrorCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Error Callback */
  104. void (* AbortCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Abort Complete Callback */
  105. void (* MspInitCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Msp Init callback */
  106. void (* MspDeInitCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Msp DeInit callback */
  107. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  108. } USART_HandleTypeDef;
  109. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  110. /**
  111. * @brief HAL USART Callback ID enumeration definition
  112. */
  113. typedef enum
  114. {
  115. HAL_USART_TX_HALFCOMPLETE_CB_ID = 0x00U, /*!< USART Tx Half Complete Callback ID */
  116. HAL_USART_TX_COMPLETE_CB_ID = 0x01U, /*!< USART Tx Complete Callback ID */
  117. HAL_USART_RX_HALFCOMPLETE_CB_ID = 0x02U, /*!< USART Rx Half Complete Callback ID */
  118. HAL_USART_RX_COMPLETE_CB_ID = 0x03U, /*!< USART Rx Complete Callback ID */
  119. HAL_USART_TX_RX_COMPLETE_CB_ID = 0x04U, /*!< USART Tx Rx Complete Callback ID */
  120. HAL_USART_ERROR_CB_ID = 0x05U, /*!< USART Error Callback ID */
  121. HAL_USART_ABORT_COMPLETE_CB_ID = 0x06U, /*!< USART Abort Complete Callback ID */
  122. HAL_USART_MSPINIT_CB_ID = 0x07U, /*!< USART MspInit callback ID */
  123. HAL_USART_MSPDEINIT_CB_ID = 0x08U /*!< USART MspDeInit callback ID */
  124. } HAL_USART_CallbackIDTypeDef;
  125. /**
  126. * @brief HAL USART Callback pointer definition
  127. */
  128. typedef void (*pUSART_CallbackTypeDef)(USART_HandleTypeDef *husart); /*!< pointer to an USART callback function */
  129. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  130. /**
  131. * @}
  132. */
  133. /* Exported constants --------------------------------------------------------*/
  134. /** @defgroup USART_Exported_Constants USART Exported Constants
  135. * @{
  136. */
  137. /** @defgroup USART_Error_Code USART Error Code
  138. * @brief USART Error Code
  139. * @{
  140. */
  141. #define HAL_USART_ERROR_NONE 0x00000000U /*!< No error */
  142. #define HAL_USART_ERROR_PE 0x00000001U /*!< Parity error */
  143. #define HAL_USART_ERROR_NE 0x00000002U /*!< Noise error */
  144. #define HAL_USART_ERROR_FE 0x00000004U /*!< Frame error */
  145. #define HAL_USART_ERROR_ORE 0x00000008U /*!< Overrun error */
  146. #define HAL_USART_ERROR_DMA 0x00000010U /*!< DMA transfer error */
  147. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  148. #define HAL_USART_ERROR_INVALID_CALLBACK 0x00000020U /*!< Invalid Callback error */
  149. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  150. /**
  151. * @}
  152. */
  153. /** @defgroup USART_Word_Length USART Word Length
  154. * @{
  155. */
  156. #define USART_WORDLENGTH_8B 0x00000000U
  157. #define USART_WORDLENGTH_9B ((uint32_t)USART_CR1_M)
  158. /**
  159. * @}
  160. */
  161. /** @defgroup USART_Stop_Bits USART Number of Stop Bits
  162. * @{
  163. */
  164. #define USART_STOPBITS_1 0x00000000U
  165. #define USART_STOPBITS_0_5 ((uint32_t)USART_CR2_STOP_0)
  166. #define USART_STOPBITS_2 ((uint32_t)USART_CR2_STOP_1)
  167. #define USART_STOPBITS_1_5 ((uint32_t)(USART_CR2_STOP_0 | USART_CR2_STOP_1))
  168. /**
  169. * @}
  170. */
  171. /** @defgroup USART_Parity USART Parity
  172. * @{
  173. */
  174. #define USART_PARITY_NONE 0x00000000U
  175. #define USART_PARITY_EVEN ((uint32_t)USART_CR1_PCE)
  176. #define USART_PARITY_ODD ((uint32_t)(USART_CR1_PCE | USART_CR1_PS))
  177. /**
  178. * @}
  179. */
  180. /** @defgroup USART_Mode USART Mode
  181. * @{
  182. */
  183. #define USART_MODE_RX ((uint32_t)USART_CR1_RE)
  184. #define USART_MODE_TX ((uint32_t)USART_CR1_TE)
  185. #define USART_MODE_TX_RX ((uint32_t)(USART_CR1_TE | USART_CR1_RE))
  186. /**
  187. * @}
  188. */
  189. /** @defgroup USART_Clock USART Clock
  190. * @{
  191. */
  192. #define USART_CLOCK_DISABLE 0x00000000U
  193. #define USART_CLOCK_ENABLE ((uint32_t)USART_CR2_CLKEN)
  194. /**
  195. * @}
  196. */
  197. /** @defgroup USART_Clock_Polarity USART Clock Polarity
  198. * @{
  199. */
  200. #define USART_POLARITY_LOW 0x00000000U
  201. #define USART_POLARITY_HIGH ((uint32_t)USART_CR2_CPOL)
  202. /**
  203. * @}
  204. */
  205. /** @defgroup USART_Clock_Phase USART Clock Phase
  206. * @{
  207. */
  208. #define USART_PHASE_1EDGE 0x00000000U
  209. #define USART_PHASE_2EDGE ((uint32_t)USART_CR2_CPHA)
  210. /**
  211. * @}
  212. */
  213. /** @defgroup USART_Last_Bit USART Last Bit
  214. * @{
  215. */
  216. #define USART_LASTBIT_DISABLE 0x00000000U
  217. #define USART_LASTBIT_ENABLE ((uint32_t)USART_CR2_LBCL)
  218. /**
  219. * @}
  220. */
  221. /** @defgroup USART_NACK_State USART NACK State
  222. * @{
  223. */
  224. #define USART_NACK_ENABLE ((uint32_t)USART_CR3_NACK)
  225. #define USART_NACK_DISABLE 0x00000000U
  226. /**
  227. * @}
  228. */
  229. /** @defgroup USART_Flags USART Flags
  230. * Elements values convention: 0xXXXX
  231. * - 0xXXXX : Flag mask in the SR register
  232. * @{
  233. */
  234. #define USART_FLAG_TXE ((uint32_t)USART_SR_TXE)
  235. #define USART_FLAG_TC ((uint32_t)USART_SR_TC)
  236. #define USART_FLAG_RXNE ((uint32_t)USART_SR_RXNE)
  237. #define USART_FLAG_IDLE ((uint32_t)USART_SR_IDLE)
  238. #define USART_FLAG_ORE ((uint32_t)USART_SR_ORE)
  239. #define USART_FLAG_NE ((uint32_t)USART_SR_NE)
  240. #define USART_FLAG_FE ((uint32_t)USART_SR_FE)
  241. #define USART_FLAG_PE ((uint32_t)USART_SR_PE)
  242. /**
  243. * @}
  244. */
  245. /** @defgroup USART_Interrupt_definition USART Interrupts Definition
  246. * Elements values convention: 0xY000XXXX
  247. * - XXXX : Interrupt mask in the XX register
  248. * - Y : Interrupt source register (2bits)
  249. * - 01: CR1 register
  250. * - 10: CR2 register
  251. * - 11: CR3 register
  252. * @{
  253. */
  254. #define USART_IT_PE ((uint32_t)(USART_CR1_REG_INDEX << 28U | USART_CR1_PEIE))
  255. #define USART_IT_TXE ((uint32_t)(USART_CR1_REG_INDEX << 28U | USART_CR1_TXEIE))
  256. #define USART_IT_TC ((uint32_t)(USART_CR1_REG_INDEX << 28U | USART_CR1_TCIE))
  257. #define USART_IT_RXNE ((uint32_t)(USART_CR1_REG_INDEX << 28U | USART_CR1_RXNEIE))
  258. #define USART_IT_IDLE ((uint32_t)(USART_CR1_REG_INDEX << 28U | USART_CR1_IDLEIE))
  259. #define USART_IT_ERR ((uint32_t)(USART_CR3_REG_INDEX << 28U | USART_CR3_EIE))
  260. /**
  261. * @}
  262. */
  263. /**
  264. * @}
  265. */
  266. /* Exported macro ------------------------------------------------------------*/
  267. /** @defgroup USART_Exported_Macros USART Exported Macros
  268. * @{
  269. */
  270. /** @brief Reset USART handle state
  271. * @param __HANDLE__ specifies the USART Handle.
  272. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  273. * @retval None
  274. */
  275. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  276. #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) do{ \
  277. (__HANDLE__)->State = HAL_USART_STATE_RESET; \
  278. (__HANDLE__)->MspInitCallback = NULL; \
  279. (__HANDLE__)->MspDeInitCallback = NULL; \
  280. } while(0U)
  281. #else
  282. #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_USART_STATE_RESET)
  283. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  284. /** @brief Check whether the specified USART flag is set or not.
  285. * @param __HANDLE__ specifies the USART Handle.
  286. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  287. * @param __FLAG__ specifies the flag to check.
  288. * This parameter can be one of the following values:
  289. * @arg USART_FLAG_TXE: Transmit data register empty flag
  290. * @arg USART_FLAG_TC: Transmission Complete flag
  291. * @arg USART_FLAG_RXNE: Receive data register not empty flag
  292. * @arg USART_FLAG_IDLE: Idle Line detection flag
  293. * @arg USART_FLAG_ORE: Overrun Error flag
  294. * @arg USART_FLAG_NE: Noise Error flag
  295. * @arg USART_FLAG_FE: Framing Error flag
  296. * @arg USART_FLAG_PE: Parity Error flag
  297. * @retval The new state of __FLAG__ (TRUE or FALSE).
  298. */
  299. #define __HAL_USART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
  300. /** @brief Clear the specified USART pending flags.
  301. * @param __HANDLE__ specifies the USART Handle.
  302. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  303. * @param __FLAG__ specifies the flag to check.
  304. * This parameter can be any combination of the following values:
  305. * @arg USART_FLAG_TC: Transmission Complete flag.
  306. * @arg USART_FLAG_RXNE: Receive data register not empty flag.
  307. *
  308. * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (Overrun
  309. * error) and IDLE (Idle line detected) flags are cleared by software
  310. * sequence: a read operation to USART_SR register followed by a read
  311. * operation to USART_DR register.
  312. * @note RXNE flag can be also cleared by a read to the USART_DR register.
  313. * @note TC flag can be also cleared by software sequence: a read operation to
  314. * USART_SR register followed by a write operation to USART_DR register.
  315. * @note TXE flag is cleared only by a write to the USART_DR register.
  316. *
  317. * @retval None
  318. */
  319. #define __HAL_USART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__))
  320. /** @brief Clear the USART PE pending flag.
  321. * @param __HANDLE__ specifies the USART Handle.
  322. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  323. * @retval None
  324. */
  325. #define __HAL_USART_CLEAR_PEFLAG(__HANDLE__) \
  326. do{ \
  327. __IO uint32_t tmpreg = 0x00U; \
  328. tmpreg = (__HANDLE__)->Instance->SR; \
  329. tmpreg = (__HANDLE__)->Instance->DR; \
  330. UNUSED(tmpreg); \
  331. } while(0U)
  332. /** @brief Clear the USART FE pending flag.
  333. * @param __HANDLE__ specifies the USART Handle.
  334. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  335. * @retval None
  336. */
  337. #define __HAL_USART_CLEAR_FEFLAG(__HANDLE__) __HAL_USART_CLEAR_PEFLAG(__HANDLE__)
  338. /** @brief Clear the USART NE pending flag.
  339. * @param __HANDLE__ specifies the USART Handle.
  340. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  341. * @retval None
  342. */
  343. #define __HAL_USART_CLEAR_NEFLAG(__HANDLE__) __HAL_USART_CLEAR_PEFLAG(__HANDLE__)
  344. /** @brief Clear the USART ORE pending flag.
  345. * @param __HANDLE__ specifies the USART Handle.
  346. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  347. * @retval None
  348. */
  349. #define __HAL_USART_CLEAR_OREFLAG(__HANDLE__) __HAL_USART_CLEAR_PEFLAG(__HANDLE__)
  350. /** @brief Clear the USART IDLE pending flag.
  351. * @param __HANDLE__ specifies the USART Handle.
  352. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  353. * @retval None
  354. */
  355. #define __HAL_USART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_USART_CLEAR_PEFLAG(__HANDLE__)
  356. /** @brief Enables or disables the specified USART interrupts.
  357. * @param __HANDLE__ specifies the USART Handle.
  358. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  359. * @param __INTERRUPT__ specifies the USART interrupt source to check.
  360. * This parameter can be one of the following values:
  361. * @arg USART_IT_TXE: Transmit Data Register empty interrupt
  362. * @arg USART_IT_TC: Transmission complete interrupt
  363. * @arg USART_IT_RXNE: Receive Data register not empty interrupt
  364. * @arg USART_IT_IDLE: Idle line detection interrupt
  365. * @arg USART_IT_PE: Parity Error interrupt
  366. * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  367. * @retval None
  368. */
  369. #define __HAL_USART_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28U) == USART_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & USART_IT_MASK)): \
  370. (((__INTERRUPT__) >> 28U) == USART_CR2_REG_INDEX)? ((__HANDLE__)->Instance->CR2 |= ((__INTERRUPT__) & USART_IT_MASK)): \
  371. ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & USART_IT_MASK)))
  372. #define __HAL_USART_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28U) == USART_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & USART_IT_MASK)): \
  373. (((__INTERRUPT__) >> 28U) == USART_CR2_REG_INDEX)? ((__HANDLE__)->Instance->CR2 &= ~((__INTERRUPT__) & USART_IT_MASK)): \
  374. ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & USART_IT_MASK)))
  375. /** @brief Checks whether the specified USART interrupt has occurred or not.
  376. * @param __HANDLE__ specifies the USART Handle.
  377. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  378. * @param __IT__ specifies the USART interrupt source to check.
  379. * This parameter can be one of the following values:
  380. * @arg USART_IT_TXE: Transmit Data Register empty interrupt
  381. * @arg USART_IT_TC: Transmission complete interrupt
  382. * @arg USART_IT_RXNE: Receive Data register not empty interrupt
  383. * @arg USART_IT_IDLE: Idle line detection interrupt
  384. * @arg USART_IT_ERR: Error interrupt
  385. * @arg USART_IT_PE: Parity Error interrupt
  386. * @retval The new state of __IT__ (TRUE or FALSE).
  387. */
  388. #define __HAL_USART_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28U) == USART_CR1_REG_INDEX)? (__HANDLE__)->Instance->CR1:(((((uint32_t)(__IT__)) >> 28U) == USART_CR2_REG_INDEX)? \
  389. (__HANDLE__)->Instance->CR2 : (__HANDLE__)->Instance->CR3)) & (((uint32_t)(__IT__)) & USART_IT_MASK))
  390. /** @brief Macro to enable the USART's one bit sample method
  391. * @param __HANDLE__ specifies the USART Handle.
  392. * @retval None
  393. */
  394. #define __HAL_USART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 |= USART_CR3_ONEBIT)
  395. /** @brief Macro to disable the USART's one bit sample method
  396. * @param __HANDLE__ specifies the USART Handle.
  397. * @retval None
  398. */
  399. #define __HAL_USART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3\
  400. &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT))
  401. /** @brief Enable USART
  402. * @param __HANDLE__ specifies the USART Handle.
  403. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  404. * @retval None
  405. */
  406. #define __HAL_USART_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
  407. /** @brief Disable USART
  408. * @param __HANDLE__ specifies the USART Handle.
  409. * USART Handle selects the USARTx peripheral (USART availability and x value depending on device).
  410. * @retval None
  411. */
  412. #define __HAL_USART_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
  413. /**
  414. * @}
  415. */
  416. /* Exported functions --------------------------------------------------------*/
  417. /** @addtogroup USART_Exported_Functions
  418. * @{
  419. */
  420. /** @addtogroup USART_Exported_Functions_Group1
  421. * @{
  422. */
  423. /* Initialization/de-initialization functions **********************************/
  424. HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart);
  425. HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart);
  426. void HAL_USART_MspInit(USART_HandleTypeDef *husart);
  427. void HAL_USART_MspDeInit(USART_HandleTypeDef *husart);
  428. /* Callbacks Register/UnRegister functions ***********************************/
  429. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  430. HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID,
  431. pUSART_CallbackTypeDef pCallback);
  432. HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID);
  433. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  434. /**
  435. * @}
  436. */
  437. /** @addtogroup USART_Exported_Functions_Group2
  438. * @{
  439. */
  440. /* IO operation functions *******************************************************/
  441. HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size, uint32_t Timeout);
  442. HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
  443. HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  444. uint16_t Size, uint32_t Timeout);
  445. HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size);
  446. HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
  447. HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  448. uint16_t Size);
  449. HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size);
  450. HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
  451. HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  452. uint16_t Size);
  453. HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart);
  454. HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart);
  455. HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart);
  456. /* Transfer Abort functions */
  457. HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart);
  458. HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart);
  459. void HAL_USART_IRQHandler(USART_HandleTypeDef *husart);
  460. void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart);
  461. void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart);
  462. void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart);
  463. void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart);
  464. void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart);
  465. void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart);
  466. void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart);
  467. /**
  468. * @}
  469. */
  470. /** @addtogroup USART_Exported_Functions_Group3
  471. * @{
  472. */
  473. /* Peripheral State functions ************************************************/
  474. HAL_USART_StateTypeDef HAL_USART_GetState(const USART_HandleTypeDef *husart);
  475. uint32_t HAL_USART_GetError(const USART_HandleTypeDef *husart);
  476. /**
  477. * @}
  478. */
  479. /**
  480. * @}
  481. */
  482. /* Private types -------------------------------------------------------------*/
  483. /* Private variables ---------------------------------------------------------*/
  484. /* Private constants ---------------------------------------------------------*/
  485. /** @defgroup USART_Private_Constants USART Private Constants
  486. * @{
  487. */
  488. /** @brief USART interruptions flag mask
  489. *
  490. */
  491. #define USART_IT_MASK ((uint32_t) USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RXNEIE | \
  492. USART_CR1_IDLEIE | USART_CR2_LBDIE | USART_CR3_CTSIE | USART_CR3_EIE )
  493. #define USART_CR1_REG_INDEX 1U
  494. #define USART_CR2_REG_INDEX 2U
  495. #define USART_CR3_REG_INDEX 3U
  496. /**
  497. * @}
  498. */
  499. /* Private macros ------------------------------------------------------------*/
  500. /** @defgroup USART_Private_Macros USART Private Macros
  501. * @{
  502. */
  503. #define IS_USART_NACK_STATE(NACK) (((NACK) == USART_NACK_ENABLE) || \
  504. ((NACK) == USART_NACK_DISABLE))
  505. #define IS_USART_LASTBIT(LASTBIT) (((LASTBIT) == USART_LASTBIT_DISABLE) || \
  506. ((LASTBIT) == USART_LASTBIT_ENABLE))
  507. #define IS_USART_PHASE(CPHA) (((CPHA) == USART_PHASE_1EDGE) || \
  508. ((CPHA) == USART_PHASE_2EDGE))
  509. #define IS_USART_POLARITY(CPOL) (((CPOL) == USART_POLARITY_LOW) || \
  510. ((CPOL) == USART_POLARITY_HIGH))
  511. #define IS_USART_CLOCK(CLOCK) (((CLOCK) == USART_CLOCK_DISABLE) || \
  512. ((CLOCK) == USART_CLOCK_ENABLE))
  513. #define IS_USART_WORD_LENGTH(LENGTH) (((LENGTH) == USART_WORDLENGTH_8B) || \
  514. ((LENGTH) == USART_WORDLENGTH_9B))
  515. #define IS_USART_STOPBITS(STOPBITS) (((STOPBITS) == USART_STOPBITS_1) || \
  516. ((STOPBITS) == USART_STOPBITS_0_5) || \
  517. ((STOPBITS) == USART_STOPBITS_1_5) || \
  518. ((STOPBITS) == USART_STOPBITS_2))
  519. #define IS_USART_PARITY(PARITY) (((PARITY) == USART_PARITY_NONE) || \
  520. ((PARITY) == USART_PARITY_EVEN) || \
  521. ((PARITY) == USART_PARITY_ODD))
  522. #define IS_USART_MODE(MODE) ((((MODE) & (~((uint32_t)USART_MODE_TX_RX))) == 0x00U) && ((MODE) != 0x00U))
  523. #define IS_USART_BAUDRATE(BAUDRATE) ((BAUDRATE) <= 4500000U)
  524. #define USART_DIV(_PCLK_, _BAUD_) (((_PCLK_)*25U)/(4U*(_BAUD_)))
  525. #define USART_DIVMANT(_PCLK_, _BAUD_) (USART_DIV((_PCLK_), (_BAUD_))/100U)
  526. #define USART_DIVFRAQ(_PCLK_, _BAUD_) ((((USART_DIV((_PCLK_), (_BAUD_)) - (USART_DIVMANT((_PCLK_), (_BAUD_)) * 100U)) * 16U) + 50U) / 100U)
  527. /* UART BRR = mantissa + overflow + fraction
  528. = (UART DIVMANT << 4) + ((UART DIVFRAQ & 0xF0) << 1) + (UART DIVFRAQ & 0x0FU) */
  529. #define USART_BRR(_PCLK_, _BAUD_) (((USART_DIVMANT((_PCLK_), (_BAUD_)) << 4U) + \
  530. ((USART_DIVFRAQ((_PCLK_), (_BAUD_)) & 0xF0U) << 1U)) + \
  531. (USART_DIVFRAQ((_PCLK_), (_BAUD_)) & 0x0FU))
  532. /**
  533. * @}
  534. */
  535. /* Private functions ---------------------------------------------------------*/
  536. /** @defgroup USART_Private_Functions USART Private Functions
  537. * @{
  538. */
  539. /**
  540. * @}
  541. */
  542. /**
  543. * @}
  544. */
  545. /**
  546. * @}
  547. */
  548. #ifdef __cplusplus
  549. }
  550. #endif
  551. #endif /* __STM32F1xx_HAL_USART_H */