stm32l4xx_hal_usart.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_usart.h
  4. * @author MCD Application Team
  5. * @brief Header file of USART HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2017 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 STM32L4xx_HAL_USART_H
  20. #define STM32L4xx_HAL_USART_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32l4xx_hal_def.h"
  26. /** @addtogroup STM32L4xx_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. Baud Rate Register[15:4] = ((2 * fclk_pres) /
  44. ((huart->Init.BaudRate)))[15:4]
  45. Baud Rate Register[3] = 0
  46. Baud Rate Register[2:0] = (((2 * fclk_pres) /
  47. ((huart->Init.BaudRate)))[3:0]) >> 1
  48. where fclk_pres is the USART input clock frequency (fclk)
  49. (divided by a prescaler if applicable)
  50. @note Oversampling by 8 is systematically applied to
  51. achieve high baud rates. */
  52. uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  53. This parameter can be a value of @ref USARTEx_Word_Length. */
  54. uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
  55. This parameter can be a value of @ref USART_Stop_Bits. */
  56. uint32_t Parity; /*!< Specifies the parity mode.
  57. This parameter can be a value of @ref USART_Parity
  58. @note When parity is enabled, the computed parity is inserted
  59. at the MSB position of the transmitted data (9th bit when
  60. the word length is set to 9 data bits; 8th bit when the
  61. word length is set to 8 data bits). */
  62. uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
  63. This parameter can be a value of @ref USART_Mode. */
  64. uint32_t CLKPolarity; /*!< Specifies the steady state of the serial clock.
  65. This parameter can be a value of @ref USART_Clock_Polarity. */
  66. uint32_t CLKPhase; /*!< Specifies the clock transition on which the bit capture is made.
  67. This parameter can be a value of @ref USART_Clock_Phase. */
  68. uint32_t CLKLastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
  69. data bit (MSB) has to be output on the SCLK pin in synchronous mode.
  70. This parameter can be a value of @ref USART_Last_Bit. */
  71. #if defined(USART_PRESC_PRESCALER)
  72. uint32_t ClockPrescaler; /*!< Specifies the prescaler value used to divide the USART clock source.
  73. This parameter can be a value of @ref USART_ClockPrescaler. */
  74. #endif /* USART_PRESC_PRESCALER */
  75. } USART_InitTypeDef;
  76. /**
  77. * @brief HAL USART State structures definition
  78. */
  79. typedef enum
  80. {
  81. HAL_USART_STATE_RESET = 0x00U, /*!< Peripheral is not initialized */
  82. HAL_USART_STATE_READY = 0x01U, /*!< Peripheral Initialized and ready for use */
  83. HAL_USART_STATE_BUSY = 0x02U, /*!< an internal process is ongoing */
  84. HAL_USART_STATE_BUSY_TX = 0x12U, /*!< Data Transmission process is ongoing */
  85. HAL_USART_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */
  86. HAL_USART_STATE_BUSY_TX_RX = 0x32U, /*!< Data Transmission Reception process is ongoing */
  87. HAL_USART_STATE_TIMEOUT = 0x03U, /*!< Timeout state */
  88. HAL_USART_STATE_ERROR = 0x04U /*!< Error */
  89. } HAL_USART_StateTypeDef;
  90. /**
  91. * @brief USART clock sources definitions
  92. */
  93. typedef enum
  94. {
  95. USART_CLOCKSOURCE_PCLK1 = 0x00U, /*!< PCLK1 clock source */
  96. USART_CLOCKSOURCE_PCLK2 = 0x01U, /*!< PCLK2 clock source */
  97. USART_CLOCKSOURCE_HSI = 0x02U, /*!< HSI clock source */
  98. USART_CLOCKSOURCE_SYSCLK = 0x04U, /*!< SYSCLK clock source */
  99. USART_CLOCKSOURCE_LSE = 0x08U, /*!< LSE clock source */
  100. USART_CLOCKSOURCE_UNDEFINED = 0x10U /*!< Undefined clock source */
  101. } USART_ClockSourceTypeDef;
  102. /**
  103. * @brief USART handle Structure definition
  104. */
  105. typedef struct __USART_HandleTypeDef
  106. {
  107. USART_TypeDef *Instance; /*!< USART registers base address */
  108. USART_InitTypeDef Init; /*!< USART communication parameters */
  109. const uint8_t *pTxBuffPtr; /*!< Pointer to USART Tx transfer Buffer */
  110. uint16_t TxXferSize; /*!< USART Tx Transfer size */
  111. __IO uint16_t TxXferCount; /*!< USART Tx Transfer Counter */
  112. uint8_t *pRxBuffPtr; /*!< Pointer to USART Rx transfer Buffer */
  113. uint16_t RxXferSize; /*!< USART Rx Transfer size */
  114. __IO uint16_t RxXferCount; /*!< USART Rx Transfer Counter */
  115. uint16_t Mask; /*!< USART Rx RDR register mask */
  116. #if defined(USART_CR1_FIFOEN)
  117. uint16_t NbRxDataToProcess; /*!< Number of data to process during RX ISR execution */
  118. uint16_t NbTxDataToProcess; /*!< Number of data to process during TX ISR execution */
  119. #endif /* USART_CR1_FIFOEN */
  120. #if defined(USART_CR2_SLVEN)
  121. uint32_t SlaveMode; /*!< Enable/Disable USART SPI Slave Mode. This parameter can be a value
  122. of @ref USARTEx_Slave_Mode */
  123. #endif /* USART_CR2_SLVEN */
  124. #if defined(USART_CR1_FIFOEN)
  125. uint32_t FifoMode; /*!< Specifies if the FIFO mode will be used. This parameter can be a value
  126. of @ref USARTEx_FIFO_mode. */
  127. #endif /* USART_CR1_FIFOEN */
  128. void (*RxISR)(struct __USART_HandleTypeDef *husart); /*!< Function pointer on Rx IRQ handler */
  129. void (*TxISR)(struct __USART_HandleTypeDef *husart); /*!< Function pointer on Tx IRQ handler */
  130. DMA_HandleTypeDef *hdmatx; /*!< USART Tx DMA Handle parameters */
  131. DMA_HandleTypeDef *hdmarx; /*!< USART Rx DMA Handle parameters */
  132. HAL_LockTypeDef Lock; /*!< Locking object */
  133. __IO HAL_USART_StateTypeDef State; /*!< USART communication state */
  134. __IO uint32_t ErrorCode; /*!< USART Error code */
  135. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  136. void (* TxHalfCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Half Complete Callback */
  137. void (* TxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Complete Callback */
  138. void (* RxHalfCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Rx Half Complete Callback */
  139. void (* RxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Rx Complete Callback */
  140. void (* TxRxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Rx Complete Callback */
  141. void (* ErrorCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Error Callback */
  142. void (* AbortCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Abort Complete Callback */
  143. #if defined(USART_CR1_FIFOEN)
  144. void (* RxFifoFullCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Rx Fifo Full Callback */
  145. void (* TxFifoEmptyCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Fifo Empty Callback */
  146. #endif /* USART_CR1_FIFOEN */
  147. void (* MspInitCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Msp Init callback */
  148. void (* MspDeInitCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Msp DeInit callback */
  149. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  150. } USART_HandleTypeDef;
  151. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  152. /**
  153. * @brief HAL USART Callback ID enumeration definition
  154. */
  155. typedef enum
  156. {
  157. HAL_USART_TX_HALFCOMPLETE_CB_ID = 0x00U, /*!< USART Tx Half Complete Callback ID */
  158. HAL_USART_TX_COMPLETE_CB_ID = 0x01U, /*!< USART Tx Complete Callback ID */
  159. HAL_USART_RX_HALFCOMPLETE_CB_ID = 0x02U, /*!< USART Rx Half Complete Callback ID */
  160. HAL_USART_RX_COMPLETE_CB_ID = 0x03U, /*!< USART Rx Complete Callback ID */
  161. HAL_USART_TX_RX_COMPLETE_CB_ID = 0x04U, /*!< USART Tx Rx Complete Callback ID */
  162. HAL_USART_ERROR_CB_ID = 0x05U, /*!< USART Error Callback ID */
  163. HAL_USART_ABORT_COMPLETE_CB_ID = 0x06U, /*!< USART Abort Complete Callback ID */
  164. #if defined(USART_CR1_FIFOEN)
  165. HAL_USART_RX_FIFO_FULL_CB_ID = 0x07U, /*!< USART Rx Fifo Full Callback ID */
  166. HAL_USART_TX_FIFO_EMPTY_CB_ID = 0x08U, /*!< USART Tx Fifo Empty Callback ID */
  167. #endif /* USART_CR1_FIFOEN */
  168. HAL_USART_MSPINIT_CB_ID = 0x09U, /*!< USART MspInit callback ID */
  169. HAL_USART_MSPDEINIT_CB_ID = 0x0AU /*!< USART MspDeInit callback ID */
  170. } HAL_USART_CallbackIDTypeDef;
  171. /**
  172. * @brief HAL USART Callback pointer definition
  173. */
  174. typedef void (*pUSART_CallbackTypeDef)(USART_HandleTypeDef *husart); /*!< pointer to an USART callback function */
  175. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  176. /**
  177. * @}
  178. */
  179. /* Exported constants --------------------------------------------------------*/
  180. /** @defgroup USART_Exported_Constants USART Exported Constants
  181. * @{
  182. */
  183. /** @defgroup USART_Error_Definition USART Error Definition
  184. * @{
  185. */
  186. #define HAL_USART_ERROR_NONE (0x00000000U) /*!< No error */
  187. #define HAL_USART_ERROR_PE (0x00000001U) /*!< Parity error */
  188. #define HAL_USART_ERROR_NE (0x00000002U) /*!< Noise error */
  189. #define HAL_USART_ERROR_FE (0x00000004U) /*!< Frame error */
  190. #define HAL_USART_ERROR_ORE (0x00000008U) /*!< Overrun error */
  191. #define HAL_USART_ERROR_DMA (0x00000010U) /*!< DMA transfer error */
  192. #if defined(USART_CR2_SLVEN)
  193. #define HAL_USART_ERROR_UDR (0x00000020U) /*!< SPI slave underrun error */
  194. #endif /* USART_CR2_SLVEN */
  195. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  196. #define HAL_USART_ERROR_INVALID_CALLBACK (0x00000040U) /*!< Invalid Callback error */
  197. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  198. #define HAL_USART_ERROR_RTO (0x00000080U) /*!< Receiver Timeout error */
  199. /**
  200. * @}
  201. */
  202. /** @defgroup USART_Stop_Bits USART Number of Stop Bits
  203. * @{
  204. */
  205. #define USART_STOPBITS_0_5 USART_CR2_STOP_0 /*!< USART frame with 0.5 stop bit */
  206. #define USART_STOPBITS_1 0x00000000U /*!< USART frame with 1 stop bit */
  207. #define USART_STOPBITS_1_5 (USART_CR2_STOP_0 | USART_CR2_STOP_1) /*!< USART frame with 1.5 stop bits */
  208. #define USART_STOPBITS_2 USART_CR2_STOP_1 /*!< USART frame with 2 stop bits */
  209. /**
  210. * @}
  211. */
  212. /** @defgroup USART_Parity USART Parity
  213. * @{
  214. */
  215. #define USART_PARITY_NONE 0x00000000U /*!< No parity */
  216. #define USART_PARITY_EVEN USART_CR1_PCE /*!< Even parity */
  217. #define USART_PARITY_ODD (USART_CR1_PCE | USART_CR1_PS) /*!< Odd parity */
  218. /**
  219. * @}
  220. */
  221. /** @defgroup USART_Mode USART Mode
  222. * @{
  223. */
  224. #define USART_MODE_RX USART_CR1_RE /*!< RX mode */
  225. #define USART_MODE_TX USART_CR1_TE /*!< TX mode */
  226. #define USART_MODE_TX_RX (USART_CR1_TE |USART_CR1_RE) /*!< RX and TX mode */
  227. /**
  228. * @}
  229. */
  230. /** @defgroup USART_Clock USART Clock
  231. * @{
  232. */
  233. #define USART_CLOCK_DISABLE 0x00000000U /*!< USART clock disable */
  234. #define USART_CLOCK_ENABLE USART_CR2_CLKEN /*!< USART clock enable */
  235. /**
  236. * @}
  237. */
  238. /** @defgroup USART_Clock_Polarity USART Clock Polarity
  239. * @{
  240. */
  241. #define USART_POLARITY_LOW 0x00000000U /*!< Driver enable signal is active high */
  242. #define USART_POLARITY_HIGH USART_CR2_CPOL /*!< Driver enable signal is active low */
  243. /**
  244. * @}
  245. */
  246. /** @defgroup USART_Clock_Phase USART Clock Phase
  247. * @{
  248. */
  249. #define USART_PHASE_1EDGE 0x00000000U /*!< USART frame phase on first clock transition */
  250. #define USART_PHASE_2EDGE USART_CR2_CPHA /*!< USART frame phase on second clock transition */
  251. /**
  252. * @}
  253. */
  254. /** @defgroup USART_Last_Bit USART Last Bit
  255. * @{
  256. */
  257. #define USART_LASTBIT_DISABLE 0x00000000U /*!< USART frame last data bit clock pulse not output to SCLK pin */
  258. #define USART_LASTBIT_ENABLE USART_CR2_LBCL /*!< USART frame last data bit clock pulse output to SCLK pin */
  259. /**
  260. * @}
  261. */
  262. #if defined(USART_PRESC_PRESCALER)
  263. /** @defgroup USART_ClockPrescaler USART Clock Prescaler
  264. * @{
  265. */
  266. #define USART_PRESCALER_DIV1 0x00000000U /*!< fclk_pres = fclk */
  267. #define USART_PRESCALER_DIV2 0x00000001U /*!< fclk_pres = fclk/2 */
  268. #define USART_PRESCALER_DIV4 0x00000002U /*!< fclk_pres = fclk/4 */
  269. #define USART_PRESCALER_DIV6 0x00000003U /*!< fclk_pres = fclk/6 */
  270. #define USART_PRESCALER_DIV8 0x00000004U /*!< fclk_pres = fclk/8 */
  271. #define USART_PRESCALER_DIV10 0x00000005U /*!< fclk_pres = fclk/10 */
  272. #define USART_PRESCALER_DIV12 0x00000006U /*!< fclk_pres = fclk/12 */
  273. #define USART_PRESCALER_DIV16 0x00000007U /*!< fclk_pres = fclk/16 */
  274. #define USART_PRESCALER_DIV32 0x00000008U /*!< fclk_pres = fclk/32 */
  275. #define USART_PRESCALER_DIV64 0x00000009U /*!< fclk_pres = fclk/64 */
  276. #define USART_PRESCALER_DIV128 0x0000000AU /*!< fclk_pres = fclk/128 */
  277. #define USART_PRESCALER_DIV256 0x0000000BU /*!< fclk_pres = fclk/256 */
  278. /**
  279. * @}
  280. */
  281. #endif /* USART_PRESC_PRESCALER */
  282. /** @defgroup USART_Request_Parameters USART Request Parameters
  283. * @{
  284. */
  285. #define USART_RXDATA_FLUSH_REQUEST USART_RQR_RXFRQ /*!< Receive Data flush Request */
  286. #define USART_TXDATA_FLUSH_REQUEST USART_RQR_TXFRQ /*!< Transmit data flush Request */
  287. /**
  288. * @}
  289. */
  290. /** @defgroup USART_Flags USART Flags
  291. * Elements values convention: 0xXXXX
  292. * - 0xXXXX : Flag mask in the ISR register
  293. * @{
  294. */
  295. #if defined(USART_CR1_FIFOEN)
  296. #define USART_FLAG_TXFT USART_ISR_TXFT /*!< USART TXFIFO threshold flag */
  297. #define USART_FLAG_RXFT USART_ISR_RXFT /*!< USART RXFIFO threshold flag */
  298. #define USART_FLAG_RXFF USART_ISR_RXFF /*!< USART RXFIFO Full flag */
  299. #define USART_FLAG_TXFE USART_ISR_TXFE /*!< USART TXFIFO Empty flag */
  300. #endif /* USART_CR1_FIFOEN */
  301. #define USART_FLAG_REACK USART_ISR_REACK /*!< USART receive enable acknowledge flag */
  302. #define USART_FLAG_TEACK USART_ISR_TEACK /*!< USART transmit enable acknowledge flag */
  303. #define USART_FLAG_BUSY USART_ISR_BUSY /*!< USART busy flag */
  304. #if defined(USART_CR2_SLVEN)
  305. #define USART_FLAG_UDR USART_ISR_UDR /*!< SPI slave underrun error flag */
  306. #endif /* USART_CR2_SLVEN */
  307. #if defined(USART_CR1_FIFOEN)
  308. #define USART_FLAG_TXE USART_ISR_TXE_TXFNF /*!< USART transmit data register empty */
  309. #define USART_FLAG_TXFNF USART_ISR_TXE_TXFNF /*!< USART TXFIFO not full */
  310. #else
  311. #define USART_FLAG_TXE USART_ISR_TXE /*!< USART transmit data register empty */
  312. #endif /* USART_CR1_FIFOEN */
  313. #define USART_FLAG_RTOF USART_ISR_RTOF /*!< USART receiver timeout flag */
  314. #define USART_FLAG_TC USART_ISR_TC /*!< USART transmission complete */
  315. #if defined(USART_CR1_FIFOEN)
  316. #define USART_FLAG_RXNE USART_ISR_RXNE_RXFNE /*!< USART read data register not empty */
  317. #define USART_FLAG_RXFNE USART_ISR_RXNE_RXFNE /*!< USART RXFIFO not empty */
  318. #else
  319. #define USART_FLAG_RXNE USART_ISR_RXNE /*!< USART read data register not empty */
  320. #endif /* USART_CR1_FIFOEN */
  321. #define USART_FLAG_IDLE USART_ISR_IDLE /*!< USART idle flag */
  322. #define USART_FLAG_ORE USART_ISR_ORE /*!< USART overrun error */
  323. #define USART_FLAG_NE USART_ISR_NE /*!< USART noise error */
  324. #define USART_FLAG_FE USART_ISR_FE /*!< USART frame error */
  325. #define USART_FLAG_PE USART_ISR_PE /*!< USART parity error */
  326. /**
  327. * @}
  328. */
  329. /** @defgroup USART_Interrupt_definition USART Interrupts Definition
  330. * Elements values convention: 0000ZZZZ0XXYYYYYb
  331. * - YYYYY : Interrupt source position in the XX register (5bits)
  332. * - XX : Interrupt source register (2bits)
  333. * - 01: CR1 register
  334. * - 10: CR2 register
  335. * - 11: CR3 register
  336. * - ZZZZ : Flag position in the ISR register(4bits)
  337. * @{
  338. */
  339. #define USART_IT_PE 0x0028U /*!< USART parity error interruption */
  340. #define USART_IT_TXE 0x0727U /*!< USART transmit data register empty interruption */
  341. #if defined(USART_CR1_FIFOEN)
  342. #define USART_IT_TXFNF 0x0727U /*!< USART TX FIFO not full interruption */
  343. #endif /* USART_CR1_FIFOEN */
  344. #define USART_IT_TC 0x0626U /*!< USART transmission complete interruption */
  345. #define USART_IT_RXNE 0x0525U /*!< USART read data register not empty interruption */
  346. #if defined(USART_CR1_FIFOEN)
  347. #define USART_IT_RXFNE 0x0525U /*!< USART RXFIFO not empty interruption */
  348. #endif /* USART_CR1_FIFOEN */
  349. #define USART_IT_IDLE 0x0424U /*!< USART idle interruption */
  350. #define USART_IT_ERR 0x0060U /*!< USART error interruption */
  351. #define USART_IT_ORE 0x0300U /*!< USART overrun error interruption */
  352. #define USART_IT_NE 0x0200U /*!< USART noise error interruption */
  353. #define USART_IT_FE 0x0100U /*!< USART frame error interruption */
  354. #if defined(USART_CR1_FIFOEN)
  355. #define USART_IT_RXFF 0x183FU /*!< USART RXFIFO full interruption */
  356. #define USART_IT_TXFE 0x173EU /*!< USART TXFIFO empty interruption */
  357. #define USART_IT_RXFT 0x1A7CU /*!< USART RXFIFO threshold reached interruption */
  358. #define USART_IT_TXFT 0x1B77U /*!< USART TXFIFO threshold reached interruption */
  359. #endif /* USART_CR1_FIFOEN */
  360. /**
  361. * @}
  362. */
  363. /** @defgroup USART_IT_CLEAR_Flags USART Interruption Clear Flags
  364. * @{
  365. */
  366. #define USART_CLEAR_PEF USART_ICR_PECF /*!< Parity Error Clear Flag */
  367. #define USART_CLEAR_FEF USART_ICR_FECF /*!< Framing Error Clear Flag */
  368. #define USART_CLEAR_NEF USART_ICR_NECF /*!< Noise Error detected Clear Flag */
  369. #define USART_CLEAR_OREF USART_ICR_ORECF /*!< OverRun Error Clear Flag */
  370. #define USART_CLEAR_IDLEF USART_ICR_IDLECF /*!< IDLE line detected Clear Flag */
  371. #define USART_CLEAR_TCF USART_ICR_TCCF /*!< Transmission Complete Clear Flag */
  372. #if defined(USART_CR2_SLVEN)
  373. #define USART_CLEAR_UDRF USART_ICR_UDRCF /*!< SPI slave underrun error Clear Flag */
  374. #endif /* USART_CR2_SLVEN */
  375. #if defined(USART_CR1_FIFOEN)
  376. #define USART_CLEAR_TXFECF USART_ICR_TXFECF /*!< TXFIFO Empty Clear Flag */
  377. #endif /* USART_CR1_FIFOEN */
  378. #define USART_CLEAR_RTOF USART_ICR_RTOCF /*!< USART receiver timeout clear flag */
  379. /**
  380. * @}
  381. */
  382. /** @defgroup USART_Interruption_Mask USART Interruption Flags Mask
  383. * @{
  384. */
  385. #define USART_IT_MASK 0x001FU /*!< USART interruptions flags mask */
  386. #define USART_CR_MASK 0x00E0U /*!< USART control register mask */
  387. #define USART_CR_POS 5U /*!< USART control register position */
  388. #define USART_ISR_MASK 0x1F00U /*!< USART ISR register mask */
  389. #define USART_ISR_POS 8U /*!< USART ISR register position */
  390. /**
  391. * @}
  392. */
  393. /**
  394. * @}
  395. */
  396. /* Exported macros -----------------------------------------------------------*/
  397. /** @defgroup USART_Exported_Macros USART Exported Macros
  398. * @{
  399. */
  400. /** @brief Reset USART handle state.
  401. * @param __HANDLE__ USART handle.
  402. * @retval None
  403. */
  404. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  405. #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) do{ \
  406. (__HANDLE__)->State = HAL_USART_STATE_RESET; \
  407. (__HANDLE__)->MspInitCallback = NULL; \
  408. (__HANDLE__)->MspDeInitCallback = NULL; \
  409. } while(0U)
  410. #else
  411. #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_USART_STATE_RESET)
  412. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  413. /** @brief Check whether the specified USART flag is set or not.
  414. * @param __HANDLE__ specifies the USART Handle
  415. * @param __FLAG__ specifies the flag to check.
  416. * This parameter can be one of the following values:
  417. * @arg @ref USART_FLAG_TXFT TXFIFO threshold flag
  418. * @arg @ref USART_FLAG_RXFT RXFIFO threshold flag
  419. * @arg @ref USART_FLAG_RXFF RXFIFO Full flag
  420. * @arg @ref USART_FLAG_TXFE TXFIFO Empty flag
  421. * @arg @ref USART_FLAG_REACK Receive enable acknowledge flag
  422. * @arg @ref USART_FLAG_TEACK Transmit enable acknowledge flag
  423. * @arg @ref USART_FLAG_BUSY Busy flag
  424. * @arg @ref USART_FLAG_UDR SPI slave underrun error flag
  425. * @arg @ref USART_FLAG_TXE Transmit data register empty flag
  426. * @arg @ref USART_FLAG_TXFNF TXFIFO not full flag
  427. * @arg @ref USART_FLAG_TC Transmission Complete flag
  428. * @arg @ref USART_FLAG_RXNE Receive data register not empty flag
  429. * @arg @ref USART_FLAG_RXFNE RXFIFO not empty flag
  430. * @arg @ref USART_FLAG_RTOF Receiver Timeout flag
  431. * @arg @ref USART_FLAG_IDLE Idle Line detection flag
  432. * @arg @ref USART_FLAG_ORE OverRun Error flag
  433. * @arg @ref USART_FLAG_NE Noise Error flag
  434. * @arg @ref USART_FLAG_FE Framing Error flag
  435. * @arg @ref USART_FLAG_PE Parity Error flag
  436. * @retval The new state of __FLAG__ (TRUE or FALSE).
  437. */
  438. #define __HAL_USART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->ISR & (__FLAG__)) == (__FLAG__))
  439. /** @brief Clear the specified USART pending flag.
  440. * @param __HANDLE__ specifies the USART Handle.
  441. * @param __FLAG__ specifies the flag to check.
  442. * This parameter can be any combination of the following values:
  443. * @arg @ref USART_CLEAR_PEF Parity Error Clear Flag
  444. * @arg @ref USART_CLEAR_FEF Framing Error Clear Flag
  445. * @arg @ref USART_CLEAR_NEF Noise detected Clear Flag
  446. * @arg @ref USART_CLEAR_OREF Overrun Error Clear Flag
  447. * @arg @ref USART_CLEAR_IDLEF IDLE line detected Clear Flag
  448. * @arg @ref USART_CLEAR_TXFECF TXFIFO empty clear Flag
  449. * @arg @ref USART_CLEAR_TCF Transmission Complete Clear Flag
  450. * @arg @ref USART_CLEAR_RTOF Receiver Timeout clear flag
  451. * @arg @ref USART_CLEAR_UDRF SPI slave underrun error Clear Flag
  452. * @retval None
  453. */
  454. #define __HAL_USART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->ICR = (__FLAG__))
  455. /** @brief Clear the USART PE pending flag.
  456. * @param __HANDLE__ specifies the USART Handle.
  457. * @retval None
  458. */
  459. #define __HAL_USART_CLEAR_PEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_PEF)
  460. /** @brief Clear the USART FE pending flag.
  461. * @param __HANDLE__ specifies the USART Handle.
  462. * @retval None
  463. */
  464. #define __HAL_USART_CLEAR_FEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_FEF)
  465. /** @brief Clear the USART NE pending flag.
  466. * @param __HANDLE__ specifies the USART Handle.
  467. * @retval None
  468. */
  469. #define __HAL_USART_CLEAR_NEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_NEF)
  470. /** @brief Clear the USART ORE pending flag.
  471. * @param __HANDLE__ specifies the USART Handle.
  472. * @retval None
  473. */
  474. #define __HAL_USART_CLEAR_OREFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_OREF)
  475. /** @brief Clear the USART IDLE pending flag.
  476. * @param __HANDLE__ specifies the USART Handle.
  477. * @retval None
  478. */
  479. #define __HAL_USART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_IDLEF)
  480. #if defined(USART_CR1_FIFOEN)
  481. /** @brief Clear the USART TX FIFO empty clear flag.
  482. * @param __HANDLE__ specifies the USART Handle.
  483. * @retval None
  484. */
  485. #define __HAL_USART_CLEAR_TXFECF(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_TXFECF)
  486. #endif /* USART_CR1_FIFOEN */
  487. #if defined(USART_CR2_SLVEN)
  488. /** @brief Clear SPI slave underrun error flag.
  489. * @param __HANDLE__ specifies the USART Handle.
  490. * @retval None
  491. */
  492. #define __HAL_USART_CLEAR_UDRFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_UDRF)
  493. #endif /* USART_CR2_SLVEN */
  494. /** @brief Enable the specified USART interrupt.
  495. * @param __HANDLE__ specifies the USART Handle.
  496. * @param __INTERRUPT__ specifies the USART interrupt source to enable.
  497. * This parameter can be one of the following values:
  498. * @arg @ref USART_IT_RXFF RXFIFO Full interrupt
  499. * @arg @ref USART_IT_TXFE TXFIFO Empty interrupt
  500. * @arg @ref USART_IT_RXFT RXFIFO threshold interrupt
  501. * @arg @ref USART_IT_TXFT TXFIFO threshold interrupt
  502. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  503. * @arg @ref USART_IT_TXFNF TX FIFO not full interrupt
  504. * @arg @ref USART_IT_TC Transmission complete interrupt
  505. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  506. * @arg @ref USART_IT_RXFNE RXFIFO not empty interrupt
  507. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  508. * @arg @ref USART_IT_PE Parity Error interrupt
  509. * @arg @ref USART_IT_ERR Error interrupt(Frame error, noise error, overrun error)
  510. * @retval None
  511. */
  512. #define __HAL_USART_ENABLE_IT(__HANDLE__, __INTERRUPT__)\
  513. (((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 1U)?\
  514. ((__HANDLE__)->Instance->CR1 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  515. ((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 2U)?\
  516. ((__HANDLE__)->Instance->CR2 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  517. ((__HANDLE__)->Instance->CR3 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))))
  518. /** @brief Disable the specified USART interrupt.
  519. * @param __HANDLE__ specifies the USART Handle.
  520. * @param __INTERRUPT__ specifies the USART interrupt source to disable.
  521. * This parameter can be one of the following values:
  522. * @arg @ref USART_IT_RXFF RXFIFO Full interrupt
  523. * @arg @ref USART_IT_TXFE TXFIFO Empty interrupt
  524. * @arg @ref USART_IT_RXFT RXFIFO threshold interrupt
  525. * @arg @ref USART_IT_TXFT TXFIFO threshold interrupt
  526. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  527. * @arg @ref USART_IT_TXFNF TX FIFO not full interrupt
  528. * @arg @ref USART_IT_TC Transmission complete interrupt
  529. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  530. * @arg @ref USART_IT_RXFNE RXFIFO not empty interrupt
  531. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  532. * @arg @ref USART_IT_PE Parity Error interrupt
  533. * @arg @ref USART_IT_ERR Error interrupt(Frame error, noise error, overrun error)
  534. * @retval None
  535. */
  536. #define __HAL_USART_DISABLE_IT(__HANDLE__, __INTERRUPT__)\
  537. (((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 1U)?\
  538. ((__HANDLE__)->Instance->CR1 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  539. ((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 2U)?\
  540. ((__HANDLE__)->Instance->CR2 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  541. ((__HANDLE__)->Instance->CR3 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))))
  542. /** @brief Check whether the specified USART interrupt has occurred or not.
  543. * @param __HANDLE__ specifies the USART Handle.
  544. * @param __INTERRUPT__ specifies the USART interrupt source to check.
  545. * This parameter can be one of the following values:
  546. * @arg @ref USART_IT_RXFF RXFIFO Full interrupt
  547. * @arg @ref USART_IT_TXFE TXFIFO Empty interrupt
  548. * @arg @ref USART_IT_RXFT RXFIFO threshold interrupt
  549. * @arg @ref USART_IT_TXFT TXFIFO threshold interrupt
  550. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  551. * @arg @ref USART_IT_TXFNF TX FIFO not full interrupt
  552. * @arg @ref USART_IT_TC Transmission complete interrupt
  553. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  554. * @arg @ref USART_IT_RXFNE RXFIFO not empty interrupt
  555. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  556. * @arg @ref USART_IT_ORE OverRun Error interrupt
  557. * @arg @ref USART_IT_NE Noise Error interrupt
  558. * @arg @ref USART_IT_FE Framing Error interrupt
  559. * @arg @ref USART_IT_PE Parity Error interrupt
  560. * @retval The new state of __INTERRUPT__ (SET or RESET).
  561. */
  562. #define __HAL_USART_GET_IT(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->ISR\
  563. & (0x01U << (((__INTERRUPT__) & USART_ISR_MASK)>>\
  564. USART_ISR_POS))) != 0U) ? SET : RESET)
  565. /** @brief Check whether the specified USART interrupt source is enabled or not.
  566. * @param __HANDLE__ specifies the USART Handle.
  567. * @param __INTERRUPT__ specifies the USART interrupt source to check.
  568. * This parameter can be one of the following values:
  569. * @arg @ref USART_IT_RXFF RXFIFO Full interrupt
  570. * @arg @ref USART_IT_TXFE TXFIFO Empty interrupt
  571. * @arg @ref USART_IT_RXFT RXFIFO threshold interrupt
  572. * @arg @ref USART_IT_TXFT TXFIFO threshold interrupt
  573. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  574. * @arg @ref USART_IT_TXFNF TX FIFO not full interrupt
  575. * @arg @ref USART_IT_TC Transmission complete interrupt
  576. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  577. * @arg @ref USART_IT_RXFNE RXFIFO not empty interrupt
  578. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  579. * @arg @ref USART_IT_ORE OverRun Error interrupt
  580. * @arg @ref USART_IT_NE Noise Error interrupt
  581. * @arg @ref USART_IT_FE Framing Error interrupt
  582. * @arg @ref USART_IT_PE Parity Error interrupt
  583. * @retval The new state of __INTERRUPT__ (SET or RESET).
  584. */
  585. #define __HAL_USART_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((((((uint8_t)(__INTERRUPT__)) >> 0x05U) == 0x01U) ?\
  586. (__HANDLE__)->Instance->CR1 : \
  587. (((((uint8_t)(__INTERRUPT__)) >> 0x05U) == 0x02U) ?\
  588. (__HANDLE__)->Instance->CR2 : \
  589. (__HANDLE__)->Instance->CR3)) & (0x01U <<\
  590. (((uint16_t)(__INTERRUPT__)) &\
  591. USART_IT_MASK))) != 0U) ? SET : RESET)
  592. /** @brief Clear the specified USART ISR flag, in setting the proper ICR register flag.
  593. * @param __HANDLE__ specifies the USART Handle.
  594. * @param __IT_CLEAR__ specifies the interrupt clear register flag that needs to be set
  595. * to clear the corresponding interrupt.
  596. * This parameter can be one of the following values:
  597. * @arg @ref USART_CLEAR_PEF Parity Error Clear Flag
  598. * @arg @ref USART_CLEAR_FEF Framing Error Clear Flag
  599. * @arg @ref USART_CLEAR_NEF Noise detected Clear Flag
  600. * @arg @ref USART_CLEAR_OREF Overrun Error Clear Flag
  601. * @arg @ref USART_CLEAR_IDLEF IDLE line detected Clear Flag
  602. * @arg @ref USART_CLEAR_RTOF Receiver timeout clear flag
  603. * @arg @ref USART_CLEAR_TXFECF TXFIFO empty clear Flag
  604. * @arg @ref USART_CLEAR_TCF Transmission Complete Clear Flag
  605. * @retval None
  606. */
  607. #define __HAL_USART_CLEAR_IT(__HANDLE__, __IT_CLEAR__) ((__HANDLE__)->Instance->ICR = (uint32_t)(__IT_CLEAR__))
  608. /** @brief Set a specific USART request flag.
  609. * @param __HANDLE__ specifies the USART Handle.
  610. * @param __REQ__ specifies the request flag to set.
  611. * This parameter can be one of the following values:
  612. * @arg @ref USART_RXDATA_FLUSH_REQUEST Receive Data flush Request
  613. * @arg @ref USART_TXDATA_FLUSH_REQUEST Transmit data flush Request
  614. *
  615. * @retval None
  616. */
  617. #define __HAL_USART_SEND_REQ(__HANDLE__, __REQ__) ((__HANDLE__)->Instance->RQR |= (uint16_t)(__REQ__))
  618. /** @brief Enable the USART one bit sample method.
  619. * @param __HANDLE__ specifies the USART Handle.
  620. * @retval None
  621. */
  622. #define __HAL_USART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT)
  623. /** @brief Disable the USART one bit sample method.
  624. * @param __HANDLE__ specifies the USART Handle.
  625. * @retval None
  626. */
  627. #define __HAL_USART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= ~USART_CR3_ONEBIT)
  628. /** @brief Enable USART.
  629. * @param __HANDLE__ specifies the USART Handle.
  630. * @retval None
  631. */
  632. #define __HAL_USART_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
  633. /** @brief Disable USART.
  634. * @param __HANDLE__ specifies the USART Handle.
  635. * @retval None
  636. */
  637. #define __HAL_USART_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
  638. /**
  639. * @}
  640. */
  641. /* Private macros --------------------------------------------------------*/
  642. /** @defgroup USART_Private_Macros USART Private Macros
  643. * @{
  644. */
  645. #if defined(USART_PRESC_PRESCALER)
  646. /** @brief Get USART clock division factor from clock prescaler value.
  647. * @param __CLOCKPRESCALER__ USART prescaler value.
  648. * @retval USART clock division factor
  649. */
  650. #define USART_GET_DIV_FACTOR(__CLOCKPRESCALER__) \
  651. (((__CLOCKPRESCALER__) == USART_PRESCALER_DIV1) ? 1U : \
  652. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV2) ? 2U : \
  653. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV4) ? 4U : \
  654. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV6) ? 6U : \
  655. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV8) ? 8U : \
  656. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV10) ? 10U : \
  657. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV12) ? 12U : \
  658. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV16) ? 16U : \
  659. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV32) ? 32U : \
  660. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV64) ? 64U : \
  661. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV128) ? 128U : 256U)
  662. /** @brief BRR division operation to set BRR register in 8-bit oversampling mode.
  663. * @param __PCLK__ USART clock.
  664. * @param __BAUD__ Baud rate set by the user.
  665. * @param __CLOCKPRESCALER__ USART prescaler value.
  666. * @retval Division result
  667. */
  668. #define USART_DIV_SAMPLING8(__PCLK__, __BAUD__, __CLOCKPRESCALER__)\
  669. (((((__PCLK__)/USART_GET_DIV_FACTOR(__CLOCKPRESCALER__))*2U)\
  670. + ((__BAUD__)/2U)) / (__BAUD__))
  671. #else
  672. /** @brief BRR division operation to set BRR register in 8-bit oversampling mode.
  673. * @param __PCLK__ USART clock.
  674. * @param __BAUD__ Baud rate set by the user.
  675. * @retval Division result
  676. */
  677. #define USART_DIV_SAMPLING8(__PCLK__, __BAUD__) ((((__PCLK__)*2U) + ((__BAUD__)/2U)) / (__BAUD__))
  678. #endif /* USART_PRESC_PRESCALER */
  679. /** @brief Check USART Baud rate.
  680. * @param __BAUDRATE__ Baudrate specified by the user.
  681. * The maximum Baud Rate is derived from the maximum clock on L4
  682. * divided by the smallest oversampling used on the USART (i.e. 8)
  683. * (i.e. 120 MHz on STM32L4Rx/L4Sx, 80 Mhz otherwise)
  684. * @retval SET (__BAUDRATE__ is valid) or RESET (__BAUDRATE__ is invalid)
  685. */
  686. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  687. #define IS_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 15000000U)
  688. #else
  689. #define IS_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 10000000U)
  690. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  691. /**
  692. * @brief Ensure that USART frame number of stop bits is valid.
  693. * @param __STOPBITS__ USART frame number of stop bits.
  694. * @retval SET (__STOPBITS__ is valid) or RESET (__STOPBITS__ is invalid)
  695. */
  696. #define IS_USART_STOPBITS(__STOPBITS__) (((__STOPBITS__) == USART_STOPBITS_0_5) || \
  697. ((__STOPBITS__) == USART_STOPBITS_1) || \
  698. ((__STOPBITS__) == USART_STOPBITS_1_5) || \
  699. ((__STOPBITS__) == USART_STOPBITS_2))
  700. /**
  701. * @brief Ensure that USART frame parity is valid.
  702. * @param __PARITY__ USART frame parity.
  703. * @retval SET (__PARITY__ is valid) or RESET (__PARITY__ is invalid)
  704. */
  705. #define IS_USART_PARITY(__PARITY__) (((__PARITY__) == USART_PARITY_NONE) || \
  706. ((__PARITY__) == USART_PARITY_EVEN) || \
  707. ((__PARITY__) == USART_PARITY_ODD))
  708. /**
  709. * @brief Ensure that USART communication mode is valid.
  710. * @param __MODE__ USART communication mode.
  711. * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
  712. */
  713. #define IS_USART_MODE(__MODE__) ((((__MODE__) & 0xFFFFFFF3U) == 0x00U) && ((__MODE__) != 0x00U))
  714. /**
  715. * @brief Ensure that USART clock state is valid.
  716. * @param __CLOCK__ USART clock state.
  717. * @retval SET (__CLOCK__ is valid) or RESET (__CLOCK__ is invalid)
  718. */
  719. #define IS_USART_CLOCK(__CLOCK__) (((__CLOCK__) == USART_CLOCK_DISABLE) || \
  720. ((__CLOCK__) == USART_CLOCK_ENABLE))
  721. /**
  722. * @brief Ensure that USART frame polarity is valid.
  723. * @param __CPOL__ USART frame polarity.
  724. * @retval SET (__CPOL__ is valid) or RESET (__CPOL__ is invalid)
  725. */
  726. #define IS_USART_POLARITY(__CPOL__) (((__CPOL__) == USART_POLARITY_LOW) || ((__CPOL__) == USART_POLARITY_HIGH))
  727. /**
  728. * @brief Ensure that USART frame phase is valid.
  729. * @param __CPHA__ USART frame phase.
  730. * @retval SET (__CPHA__ is valid) or RESET (__CPHA__ is invalid)
  731. */
  732. #define IS_USART_PHASE(__CPHA__) (((__CPHA__) == USART_PHASE_1EDGE) || ((__CPHA__) == USART_PHASE_2EDGE))
  733. /**
  734. * @brief Ensure that USART frame last bit clock pulse setting is valid.
  735. * @param __LASTBIT__ USART frame last bit clock pulse setting.
  736. * @retval SET (__LASTBIT__ is valid) or RESET (__LASTBIT__ is invalid)
  737. */
  738. #define IS_USART_LASTBIT(__LASTBIT__) (((__LASTBIT__) == USART_LASTBIT_DISABLE) || \
  739. ((__LASTBIT__) == USART_LASTBIT_ENABLE))
  740. /**
  741. * @brief Ensure that USART request parameter is valid.
  742. * @param __PARAM__ USART request parameter.
  743. * @retval SET (__PARAM__ is valid) or RESET (__PARAM__ is invalid)
  744. */
  745. #define IS_USART_REQUEST_PARAMETER(__PARAM__) (((__PARAM__) == USART_RXDATA_FLUSH_REQUEST) || \
  746. ((__PARAM__) == USART_TXDATA_FLUSH_REQUEST))
  747. #if defined(USART_PRESC_PRESCALER)
  748. /**
  749. * @brief Ensure that USART Prescaler is valid.
  750. * @param __CLOCKPRESCALER__ USART Prescaler value.
  751. * @retval SET (__CLOCKPRESCALER__ is valid) or RESET (__CLOCKPRESCALER__ is invalid)
  752. */
  753. #define IS_USART_PRESCALER(__CLOCKPRESCALER__) (((__CLOCKPRESCALER__) == USART_PRESCALER_DIV1) || \
  754. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV2) || \
  755. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV4) || \
  756. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV6) || \
  757. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV8) || \
  758. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV10) || \
  759. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV12) || \
  760. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV16) || \
  761. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV32) || \
  762. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV64) || \
  763. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV128) || \
  764. ((__CLOCKPRESCALER__) == USART_PRESCALER_DIV256))
  765. #endif /* USART_PRESC_PRESCALER */
  766. /**
  767. * @}
  768. */
  769. /* Include USART HAL Extended module */
  770. #include "stm32l4xx_hal_usart_ex.h"
  771. /* Exported functions --------------------------------------------------------*/
  772. /** @addtogroup USART_Exported_Functions USART Exported Functions
  773. * @{
  774. */
  775. /** @addtogroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
  776. * @{
  777. */
  778. /* Initialization and de-initialization functions ****************************/
  779. HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart);
  780. HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart);
  781. void HAL_USART_MspInit(USART_HandleTypeDef *husart);
  782. void HAL_USART_MspDeInit(USART_HandleTypeDef *husart);
  783. /* Callbacks Register/UnRegister functions ***********************************/
  784. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  785. HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID,
  786. pUSART_CallbackTypeDef pCallback);
  787. HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID);
  788. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  789. /**
  790. * @}
  791. */
  792. /** @addtogroup USART_Exported_Functions_Group2 IO operation functions
  793. * @{
  794. */
  795. /* IO operation functions *****************************************************/
  796. HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size,
  797. uint32_t Timeout);
  798. HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
  799. HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  800. uint16_t Size, uint32_t Timeout);
  801. HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size);
  802. HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
  803. HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  804. uint16_t Size);
  805. HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size);
  806. HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
  807. HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  808. uint16_t Size);
  809. HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart);
  810. HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart);
  811. HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart);
  812. /* Transfer Abort functions */
  813. HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart);
  814. HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart);
  815. void HAL_USART_IRQHandler(USART_HandleTypeDef *husart);
  816. void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart);
  817. void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart);
  818. void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart);
  819. void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart);
  820. void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart);
  821. void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart);
  822. void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart);
  823. /**
  824. * @}
  825. */
  826. /** @addtogroup USART_Exported_Functions_Group4 Peripheral State and Error functions
  827. * @{
  828. */
  829. /* Peripheral State and Error functions ***************************************/
  830. HAL_USART_StateTypeDef HAL_USART_GetState(const USART_HandleTypeDef *husart);
  831. uint32_t HAL_USART_GetError(const USART_HandleTypeDef *husart);
  832. /**
  833. * @}
  834. */
  835. /**
  836. * @}
  837. */
  838. /**
  839. * @}
  840. */
  841. /**
  842. * @}
  843. */
  844. #ifdef __cplusplus
  845. }
  846. #endif
  847. #endif /* STM32L4xx_HAL_USART_H */