You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_usart.h 28KB

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