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.
 
 
 

779 lines
37 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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 STM32L0xx_HAL_USART_H
  21. #define STM32L0xx_HAL_USART_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32l0xx_hal_def.h"
  27. /** @addtogroup STM32L0xx_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. Baud Rate Register[15:4] = ((2 * fclk_pres) / ((huart->Init.BaudRate)))[15:4]
  45. Baud Rate Register[3] = 0
  46. Baud Rate Register[2:0] = (((2 * fclk_pres) / ((huart->Init.BaudRate)))[3:0]) >> 1
  47. where fclk_pres is the USART input clock frequency
  48. @note Oversampling by 8 is systematically applied to achieve high baud rates. */
  49. uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
  50. This parameter can be a value of @ref USARTEx_Word_Length. */
  51. uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
  52. This parameter can be a value of @ref USART_Stop_Bits. */
  53. uint32_t Parity; /*!< Specifies the parity mode.
  54. This parameter can be a value of @ref USART_Parity
  55. @note When parity is enabled, the computed parity is inserted
  56. at the MSB position of the transmitted data (9th bit when
  57. the word length is set to 9 data bits; 8th bit when the
  58. word length is set to 8 data bits). */
  59. uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
  60. This parameter can be a value of @ref USART_Mode. */
  61. uint32_t CLKPolarity; /*!< Specifies the steady state of the serial clock.
  62. This parameter can be a value of @ref USART_Clock_Polarity. */
  63. uint32_t CLKPhase; /*!< Specifies the clock transition on which the bit capture is made.
  64. This parameter can be a value of @ref USART_Clock_Phase. */
  65. uint32_t CLKLastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
  66. data bit (MSB) has to be output on the SCLK pin in synchronous mode.
  67. This parameter can be a value of @ref USART_Last_Bit. */
  68. } USART_InitTypeDef;
  69. /**
  70. * @brief HAL USART State structures definition
  71. */
  72. typedef enum
  73. {
  74. HAL_USART_STATE_RESET = 0x00U, /*!< Peripheral is not initialized */
  75. HAL_USART_STATE_READY = 0x01U, /*!< Peripheral Initialized and ready for use */
  76. HAL_USART_STATE_BUSY = 0x02U, /*!< an internal process is ongoing */
  77. HAL_USART_STATE_BUSY_TX = 0x12U, /*!< Data Transmission process is ongoing */
  78. HAL_USART_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */
  79. HAL_USART_STATE_BUSY_TX_RX = 0x32U, /*!< Data Transmission Reception process is ongoing */
  80. HAL_USART_STATE_TIMEOUT = 0x03U, /*!< Timeout state */
  81. HAL_USART_STATE_ERROR = 0x04U /*!< Error */
  82. } HAL_USART_StateTypeDef;
  83. /**
  84. * @brief USART clock sources definitions
  85. */
  86. typedef enum
  87. {
  88. USART_CLOCKSOURCE_PCLK1 = 0x00U, /*!< PCLK1 clock source */
  89. USART_CLOCKSOURCE_PCLK2 = 0x01U, /*!< PCLK2 clock source */
  90. USART_CLOCKSOURCE_HSI = 0x02U, /*!< HSI clock source */
  91. USART_CLOCKSOURCE_SYSCLK = 0x04U, /*!< SYSCLK clock source */
  92. USART_CLOCKSOURCE_LSE = 0x08U, /*!< LSE clock source */
  93. USART_CLOCKSOURCE_UNDEFINED = 0x10U /*!< Undefined clock source */
  94. } USART_ClockSourceTypeDef;
  95. /**
  96. * @brief USART handle Structure definition
  97. */
  98. typedef struct __USART_HandleTypeDef
  99. {
  100. USART_TypeDef *Instance; /*!< USART registers base address */
  101. USART_InitTypeDef Init; /*!< USART communication parameters */
  102. uint8_t *pTxBuffPtr; /*!< Pointer to USART Tx transfer Buffer */
  103. uint16_t TxXferSize; /*!< USART Tx Transfer size */
  104. __IO uint16_t TxXferCount; /*!< USART Tx Transfer Counter */
  105. uint8_t *pRxBuffPtr; /*!< Pointer to USART Rx transfer Buffer */
  106. uint16_t RxXferSize; /*!< USART Rx Transfer size */
  107. __IO uint16_t RxXferCount; /*!< USART Rx Transfer Counter */
  108. uint16_t Mask; /*!< USART Rx RDR register mask */
  109. void (*RxISR)(struct __USART_HandleTypeDef *husart); /*!< Function pointer on Rx IRQ handler */
  110. void (*TxISR)(struct __USART_HandleTypeDef *husart); /*!< Function pointer on Tx IRQ handler */
  111. DMA_HandleTypeDef *hdmatx; /*!< USART Tx DMA Handle parameters */
  112. DMA_HandleTypeDef *hdmarx; /*!< USART Rx DMA Handle parameters */
  113. HAL_LockTypeDef Lock; /*!< Locking object */
  114. __IO HAL_USART_StateTypeDef State; /*!< USART communication state */
  115. __IO uint32_t ErrorCode; /*!< USART Error code */
  116. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  117. void (* TxHalfCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Half Complete Callback */
  118. void (* TxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Complete Callback */
  119. void (* RxHalfCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Rx Half Complete Callback */
  120. void (* RxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Rx Complete Callback */
  121. void (* TxRxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Rx Complete Callback */
  122. void (* ErrorCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Error Callback */
  123. void (* AbortCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Abort Complete Callback */
  124. void (* MspInitCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Msp Init callback */
  125. void (* MspDeInitCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Msp DeInit callback */
  126. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  127. } USART_HandleTypeDef;
  128. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  129. /**
  130. * @brief HAL USART Callback ID enumeration definition
  131. */
  132. typedef enum
  133. {
  134. HAL_USART_TX_HALFCOMPLETE_CB_ID = 0x00U, /*!< USART Tx Half Complete Callback ID */
  135. HAL_USART_TX_COMPLETE_CB_ID = 0x01U, /*!< USART Tx Complete Callback ID */
  136. HAL_USART_RX_HALFCOMPLETE_CB_ID = 0x02U, /*!< USART Rx Half Complete Callback ID */
  137. HAL_USART_RX_COMPLETE_CB_ID = 0x03U, /*!< USART Rx Complete Callback ID */
  138. HAL_USART_TX_RX_COMPLETE_CB_ID = 0x04U, /*!< USART Tx Rx Complete Callback ID */
  139. HAL_USART_ERROR_CB_ID = 0x05U, /*!< USART Error Callback ID */
  140. HAL_USART_ABORT_COMPLETE_CB_ID = 0x06U, /*!< USART Abort Complete Callback ID */
  141. HAL_USART_MSPINIT_CB_ID = 0x09U, /*!< USART MspInit callback ID */
  142. HAL_USART_MSPDEINIT_CB_ID = 0x0AU /*!< USART MspDeInit callback ID */
  143. } HAL_USART_CallbackIDTypeDef;
  144. /**
  145. * @brief HAL USART Callback pointer definition
  146. */
  147. typedef void (*pUSART_CallbackTypeDef)(USART_HandleTypeDef *husart); /*!< pointer to an USART callback function */
  148. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  149. /**
  150. * @}
  151. */
  152. /* Exported constants --------------------------------------------------------*/
  153. /** @defgroup USART_Exported_Constants USART Exported Constants
  154. * @{
  155. */
  156. /** @defgroup USART_Error_Definition USART Error Definition
  157. * @{
  158. */
  159. #define HAL_USART_ERROR_NONE ((uint32_t)0x00000000U) /*!< No error */
  160. #define HAL_USART_ERROR_PE ((uint32_t)0x00000001U) /*!< Parity error */
  161. #define HAL_USART_ERROR_NE ((uint32_t)0x00000002U) /*!< Noise error */
  162. #define HAL_USART_ERROR_FE ((uint32_t)0x00000004U) /*!< Frame error */
  163. #define HAL_USART_ERROR_ORE ((uint32_t)0x00000008U) /*!< Overrun error */
  164. #define HAL_USART_ERROR_DMA ((uint32_t)0x00000010U) /*!< DMA transfer error */
  165. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  166. #define HAL_USART_ERROR_INVALID_CALLBACK ((uint32_t)0x00000040U) /*!< Invalid Callback error */
  167. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  168. /**
  169. * @}
  170. */
  171. /** @defgroup USART_Stop_Bits USART Number of Stop Bits
  172. * @{
  173. */
  174. #define USART_STOPBITS_0_5 USART_CR2_STOP_0 /*!< USART frame with 0.5 stop bit */
  175. #define USART_STOPBITS_1 0x00000000U /*!< USART frame with 1 stop bit */
  176. #define USART_STOPBITS_1_5 (USART_CR2_STOP_0 | USART_CR2_STOP_1) /*!< USART frame with 1.5 stop bits */
  177. #define USART_STOPBITS_2 USART_CR2_STOP_1 /*!< USART frame with 2 stop bits */
  178. /**
  179. * @}
  180. */
  181. /** @defgroup USART_Parity USART Parity
  182. * @{
  183. */
  184. #define USART_PARITY_NONE 0x00000000U /*!< No parity */
  185. #define USART_PARITY_EVEN USART_CR1_PCE /*!< Even parity */
  186. #define USART_PARITY_ODD (USART_CR1_PCE | USART_CR1_PS) /*!< Odd parity */
  187. /**
  188. * @}
  189. */
  190. /** @defgroup USART_Mode USART Mode
  191. * @{
  192. */
  193. #define USART_MODE_RX USART_CR1_RE /*!< RX mode */
  194. #define USART_MODE_TX USART_CR1_TE /*!< TX mode */
  195. #define USART_MODE_TX_RX (USART_CR1_TE |USART_CR1_RE) /*!< RX and TX mode */
  196. /**
  197. * @}
  198. */
  199. /** @defgroup USART_Over_Sampling USART Over Sampling
  200. * @{
  201. */
  202. #define USART_OVERSAMPLING_16 0x00000000U /*!< Oversampling by 16 */
  203. #define USART_OVERSAMPLING_8 USART_CR1_OVER8 /*!< Oversampling by 8 */
  204. /**
  205. * @}
  206. */
  207. /** @defgroup USART_Clock USART Clock
  208. * @{
  209. */
  210. #define USART_CLOCK_DISABLE 0x00000000U /*!< USART clock disable */
  211. #define USART_CLOCK_ENABLE USART_CR2_CLKEN /*!< USART clock enable */
  212. /**
  213. * @}
  214. */
  215. /** @defgroup USART_Clock_Polarity USART Clock Polarity
  216. * @{
  217. */
  218. #define USART_POLARITY_LOW 0x00000000U /*!< Driver enable signal is active high */
  219. #define USART_POLARITY_HIGH USART_CR2_CPOL /*!< Driver enable signal is active low */
  220. /**
  221. * @}
  222. */
  223. /** @defgroup USART_Clock_Phase USART Clock Phase
  224. * @{
  225. */
  226. #define USART_PHASE_1EDGE 0x00000000U /*!< USART frame phase on first clock transition */
  227. #define USART_PHASE_2EDGE USART_CR2_CPHA /*!< USART frame phase on second clock transition */
  228. /**
  229. * @}
  230. */
  231. /** @defgroup USART_Last_Bit USART Last Bit
  232. * @{
  233. */
  234. #define USART_LASTBIT_DISABLE 0x00000000U /*!< USART frame last data bit clock pulse not output to SCLK pin */
  235. #define USART_LASTBIT_ENABLE USART_CR2_LBCL /*!< USART frame last data bit clock pulse output to SCLK pin */
  236. /**
  237. * @}
  238. */
  239. /** @defgroup USART_Request_Parameters USART Request Parameters
  240. * @{
  241. */
  242. #define USART_RXDATA_FLUSH_REQUEST USART_RQR_RXFRQ /*!< Receive Data flush Request */
  243. #define USART_TXDATA_FLUSH_REQUEST USART_RQR_TXFRQ /*!< Transmit data flush Request */
  244. /**
  245. * @}
  246. */
  247. /** @defgroup USART_Flags USART Flags
  248. * Elements values convention: 0xXXXX
  249. * - 0xXXXX : Flag mask in the ISR register
  250. * @{
  251. */
  252. #define USART_FLAG_REACK USART_ISR_REACK /*!< USART receive enable acknowledge flag */
  253. #define USART_FLAG_TEACK USART_ISR_TEACK /*!< USART transmit enable acknowledge flag */
  254. #define USART_FLAG_BUSY USART_ISR_BUSY /*!< USART busy flag */
  255. #define USART_FLAG_TXE USART_ISR_TXE /*!< USART transmit data register empty */
  256. #define USART_FLAG_TC USART_ISR_TC /*!< USART transmission complete */
  257. #define USART_FLAG_RXNE USART_ISR_RXNE /*!< USART read data register not empty */
  258. #define USART_FLAG_IDLE USART_ISR_IDLE /*!< USART idle flag */
  259. #define USART_FLAG_ORE USART_ISR_ORE /*!< USART overrun error */
  260. #define USART_FLAG_NE USART_ISR_NE /*!< USART noise error */
  261. #define USART_FLAG_FE USART_ISR_FE /*!< USART frame error */
  262. #define USART_FLAG_PE USART_ISR_PE /*!< USART parity error */
  263. /**
  264. * @}
  265. */
  266. /** @defgroup USART_Interrupt_definition USART Interrupts Definition
  267. * Elements values convention: 0000ZZZZ0XXYYYYYb
  268. * - YYYYY : Interrupt source position in the XX register (5bits)
  269. * - XX : Interrupt source register (2bits)
  270. * - 01: CR1 register
  271. * - 10: CR2 register
  272. * - 11: CR3 register
  273. * - ZZZZ : Flag position in the ISR register(4bits)
  274. * @{
  275. */
  276. #define USART_IT_PE 0x0028U /*!< USART parity error interruption */
  277. #define USART_IT_TXE 0x0727U /*!< USART transmit data register empty interruption */
  278. #define USART_IT_TC 0x0626U /*!< USART transmission complete interruption */
  279. #define USART_IT_RXNE 0x0525U /*!< USART read data register not empty interruption */
  280. #define USART_IT_IDLE 0x0424U /*!< USART idle interruption */
  281. #define USART_IT_ERR 0x0060U /*!< USART error interruption */
  282. #define USART_IT_ORE 0x0300U /*!< USART overrun error interruption */
  283. #define USART_IT_NE 0x0200U /*!< USART noise error interruption */
  284. #define USART_IT_FE 0x0100U /*!< USART frame error interruption */
  285. /**
  286. * @}
  287. */
  288. /** @defgroup USART_IT_CLEAR_Flags USART Interruption Clear Flags
  289. * @{
  290. */
  291. #define USART_CLEAR_PEF USART_ICR_PECF /*!< Parity Error Clear Flag */
  292. #define USART_CLEAR_FEF USART_ICR_FECF /*!< Framing Error Clear Flag */
  293. #define USART_CLEAR_NEF USART_ICR_NCF /*!< Noise Error detected Clear Flag */
  294. #define USART_CLEAR_OREF USART_ICR_ORECF /*!< OverRun Error Clear Flag */
  295. #define USART_CLEAR_IDLEF USART_ICR_IDLECF /*!< IDLE line detected Clear Flag */
  296. #define USART_CLEAR_TCF USART_ICR_TCCF /*!< Transmission Complete Clear Flag */
  297. /**
  298. * @}
  299. */
  300. /** @defgroup USART_Interruption_Mask USART Interruption Flags Mask
  301. * @{
  302. */
  303. #define USART_IT_MASK 0x001FU /*!< USART interruptions flags mask */
  304. #define USART_CR_MASK 0x00E0U /*!< USART control register mask */
  305. #define USART_CR_POS 5U /*!< USART control register position */
  306. #define USART_ISR_MASK 0x1F00U /*!< USART ISR register mask */
  307. #define USART_ISR_POS 8U /*!< USART ISR register position */
  308. /**
  309. * @}
  310. */
  311. /**
  312. * @}
  313. */
  314. /* Exported macros -----------------------------------------------------------*/
  315. /** @defgroup USART_Exported_Macros USART Exported Macros
  316. * @{
  317. */
  318. /** @brief Reset USART handle state.
  319. * @param __HANDLE__ USART handle.
  320. * @retval None
  321. */
  322. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  323. #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) do{ \
  324. (__HANDLE__)->State = HAL_USART_STATE_RESET; \
  325. (__HANDLE__)->MspInitCallback = NULL; \
  326. (__HANDLE__)->MspDeInitCallback = NULL; \
  327. } while(0U)
  328. #else
  329. #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_USART_STATE_RESET)
  330. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  331. /** @brief Flush the USART Data registers
  332. * @param __HANDLE__ specifies the USART Handle.
  333. */
  334. #define __HAL_USART_FLUSH_DRREGISTER(__HANDLE__) \
  335. do{ \
  336. SET_BIT((__HANDLE__)->Instance->RQR, USART_RXDATA_FLUSH_REQUEST); \
  337. SET_BIT((__HANDLE__)->Instance->RQR, USART_TXDATA_FLUSH_REQUEST); \
  338. } while(0U)
  339. /** @brief Check whether the specified USART flag is set or not.
  340. * @param __HANDLE__ specifies the USART Handle
  341. * @param __FLAG__ specifies the flag to check.
  342. * This parameter can be one of the following values:
  343. * @arg @ref USART_FLAG_REACK Receive enable acknowledge flag
  344. * @arg @ref USART_FLAG_TEACK Transmit enable acknowledge flag
  345. * @arg @ref USART_FLAG_BUSY Busy flag
  346. * @arg @ref USART_FLAG_TXE Transmit data register empty flag
  347. * @arg @ref USART_FLAG_TC Transmission Complete flag
  348. * @arg @ref USART_FLAG_RXNE Receive data register not empty flag
  349. * @arg @ref USART_FLAG_IDLE Idle Line detection flag
  350. * @arg @ref USART_FLAG_ORE OverRun Error flag
  351. * @arg @ref USART_FLAG_NE Noise Error flag
  352. * @arg @ref USART_FLAG_FE Framing Error flag
  353. * @arg @ref USART_FLAG_PE Parity Error flag
  354. * @retval The new state of __FLAG__ (TRUE or FALSE).
  355. */
  356. #define __HAL_USART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->ISR & (__FLAG__)) == (__FLAG__))
  357. /** @brief Clear the specified USART pending flag.
  358. * @param __HANDLE__ specifies the USART Handle.
  359. * @param __FLAG__ specifies the flag to check.
  360. * This parameter can be any combination of the following values:
  361. * @arg @ref USART_CLEAR_PEF Parity Error Clear Flag
  362. * @arg @ref USART_CLEAR_FEF Framing Error Clear Flag
  363. * @arg @ref USART_CLEAR_NEF Noise detected Clear Flag
  364. * @arg @ref USART_CLEAR_OREF Overrun Error Clear Flag
  365. * @arg @ref USART_CLEAR_IDLEF IDLE line detected Clear Flag
  366. * @arg @ref USART_CLEAR_TCF Transmission Complete Clear Flag
  367. * @retval None
  368. */
  369. #define __HAL_USART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->ICR = (__FLAG__))
  370. /** @brief Clear the USART PE pending flag.
  371. * @param __HANDLE__ specifies the USART Handle.
  372. * @retval None
  373. */
  374. #define __HAL_USART_CLEAR_PEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_PEF)
  375. /** @brief Clear the USART FE pending flag.
  376. * @param __HANDLE__ specifies the USART Handle.
  377. * @retval None
  378. */
  379. #define __HAL_USART_CLEAR_FEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_FEF)
  380. /** @brief Clear the USART NE pending flag.
  381. * @param __HANDLE__ specifies the USART Handle.
  382. * @retval None
  383. */
  384. #define __HAL_USART_CLEAR_NEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_NEF)
  385. /** @brief Clear the USART ORE pending flag.
  386. * @param __HANDLE__ specifies the USART Handle.
  387. * @retval None
  388. */
  389. #define __HAL_USART_CLEAR_OREFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_OREF)
  390. /** @brief Clear the USART IDLE pending flag.
  391. * @param __HANDLE__ specifies the USART Handle.
  392. * @retval None
  393. */
  394. #define __HAL_USART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_IDLEF)
  395. /** @brief Enable the specified USART interrupt.
  396. * @param __HANDLE__ specifies the USART Handle.
  397. * @param __INTERRUPT__ specifies the USART interrupt source to enable.
  398. * This parameter can be one of the following values:
  399. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  400. * @arg @ref USART_IT_TC Transmission complete interrupt
  401. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  402. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  403. * @arg @ref USART_IT_PE Parity Error interrupt
  404. * @arg @ref USART_IT_ERR Error interrupt(Frame error, noise error, overrun error)
  405. * @retval None
  406. */
  407. #define __HAL_USART_ENABLE_IT(__HANDLE__, __INTERRUPT__) (((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 1U)? ((__HANDLE__)->Instance->CR1 |= ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  408. ((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 2U)? ((__HANDLE__)->Instance->CR2 |= ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  409. ((__HANDLE__)->Instance->CR3 |= ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))))
  410. /** @brief Disable the specified USART interrupt.
  411. * @param __HANDLE__ specifies the USART Handle.
  412. * @param __INTERRUPT__ specifies the USART interrupt source to disable.
  413. * This parameter can be one of the following values:
  414. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  415. * @arg @ref USART_IT_TC Transmission complete interrupt
  416. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  417. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  418. * @arg @ref USART_IT_PE Parity Error interrupt
  419. * @arg @ref USART_IT_ERR Error interrupt(Frame error, noise error, overrun error)
  420. * @retval None
  421. */
  422. #define __HAL_USART_DISABLE_IT(__HANDLE__, __INTERRUPT__) (((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 1U)? ((__HANDLE__)->Instance->CR1 &= ~ ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  423. ((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 2U)? ((__HANDLE__)->Instance->CR2 &= ~ ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))): \
  424. ((__HANDLE__)->Instance->CR3 &= ~ ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))))
  425. /** @brief Check whether the specified USART interrupt has occurred or not.
  426. * @param __HANDLE__ specifies the USART Handle.
  427. * @param __INTERRUPT__ specifies the USART interrupt source to check.
  428. * This parameter can be one of the following values:
  429. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  430. * @arg @ref USART_IT_TC Transmission complete interrupt
  431. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  432. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  433. * @arg @ref USART_IT_ORE OverRun Error interrupt
  434. * @arg @ref USART_IT_NE Noise Error interrupt
  435. * @arg @ref USART_IT_FE Framing Error interrupt
  436. * @arg @ref USART_IT_PE Parity Error interrupt
  437. * @retval The new state of __INTERRUPT__ (SET or RESET).
  438. */
  439. #define __HAL_USART_GET_IT(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->ISR & ((uint32_t)0x01U << (((__INTERRUPT__) & USART_ISR_MASK)>> USART_ISR_POS))) != 0U) ? SET : RESET)
  440. /** @brief Check whether the specified USART interrupt source is enabled or not.
  441. * @param __HANDLE__ specifies the USART Handle.
  442. * @param __INTERRUPT__ specifies the USART interrupt source to check.
  443. * This parameter can be one of the following values:
  444. * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
  445. * @arg @ref USART_IT_TC Transmission complete interrupt
  446. * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
  447. * @arg @ref USART_IT_IDLE Idle line detection interrupt
  448. * @arg @ref USART_IT_ORE OverRun Error interrupt
  449. * @arg @ref USART_IT_NE Noise Error interrupt
  450. * @arg @ref USART_IT_FE Framing Error interrupt
  451. * @arg @ref USART_IT_PE Parity Error interrupt
  452. * @retval The new state of __INTERRUPT__ (SET or RESET).
  453. */
  454. #define __HAL_USART_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((((((uint8_t)(__INTERRUPT__)) >> 0x05U) == 0x01U) ? (__HANDLE__)->Instance->CR1 : \
  455. (((((uint8_t)(__INTERRUPT__)) >> 0x05U) == 0x02U) ? (__HANDLE__)->Instance->CR2 : \
  456. (__HANDLE__)->Instance->CR3)) & (0x01U << (((uint16_t)(__INTERRUPT__)) & USART_IT_MASK))) != 0U) ? SET : RESET)
  457. /** @brief Clear the specified USART ISR flag, in setting the proper ICR register flag.
  458. * @param __HANDLE__ specifies the USART Handle.
  459. * @param __IT_CLEAR__ specifies the interrupt clear register flag that needs to be set
  460. * to clear the corresponding interrupt.
  461. * This parameter can be one of the following values:
  462. * @arg @ref USART_CLEAR_PEF Parity Error Clear Flag
  463. * @arg @ref USART_CLEAR_FEF Framing Error Clear Flag
  464. * @arg @ref USART_CLEAR_NEF Noise detected Clear Flag
  465. * @arg @ref USART_CLEAR_OREF Overrun Error Clear Flag
  466. * @arg @ref USART_CLEAR_IDLEF IDLE line detected Clear Flag
  467. * @arg @ref USART_CLEAR_TCF Transmission Complete Clear Flag
  468. * @retval None
  469. */
  470. #define __HAL_USART_CLEAR_IT(__HANDLE__, __IT_CLEAR__) ((__HANDLE__)->Instance->ICR = (uint32_t)(__IT_CLEAR__))
  471. /** @brief Set a specific USART request flag.
  472. * @param __HANDLE__ specifies the USART Handle.
  473. * @param __REQ__ specifies the request flag to set.
  474. * This parameter can be one of the following values:
  475. * @arg @ref USART_RXDATA_FLUSH_REQUEST Receive Data flush Request
  476. * @arg @ref USART_TXDATA_FLUSH_REQUEST Transmit data flush Request
  477. *
  478. * @retval None
  479. */
  480. #define __HAL_USART_SEND_REQ(__HANDLE__, __REQ__) ((__HANDLE__)->Instance->RQR |= (__REQ__))
  481. /** @brief Enable the USART one bit sample method.
  482. * @param __HANDLE__ specifies the USART Handle.
  483. * @retval None
  484. */
  485. #define __HAL_USART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT)
  486. /** @brief Disable the USART one bit sample method.
  487. * @param __HANDLE__ specifies the USART Handle.
  488. * @retval None
  489. */
  490. #define __HAL_USART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= ~USART_CR3_ONEBIT)
  491. /** @brief Enable USART.
  492. * @param __HANDLE__ specifies the USART Handle.
  493. * @retval None
  494. */
  495. #define __HAL_USART_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
  496. /** @brief Disable USART.
  497. * @param __HANDLE__ specifies the USART Handle.
  498. * @retval None
  499. */
  500. #define __HAL_USART_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
  501. /**
  502. * @}
  503. */
  504. /* Private macros --------------------------------------------------------*/
  505. /** @defgroup USART_Private_Macros USART Private Macros
  506. * @{
  507. */
  508. /** @brief BRR division operation to set BRR register in 8-bit oversampling mode.
  509. * @param __PCLK__ USART clock.
  510. * @param __BAUD__ Baud rate set by the user.
  511. * @retval Division result
  512. */
  513. #define USART_DIV_SAMPLING8(__PCLK__, __BAUD__) ((((__PCLK__)*2U) + ((__BAUD__)/2U)) / (__BAUD__))
  514. /** @brief Check USART Baud rate.
  515. * @param __BAUDRATE__ Baudrate specified by the user.
  516. * The maximum Baud Rate is derived from the maximum clock on L0 (i.e. 32 MHz)
  517. * divided by the smallest oversampling used on the USART (i.e. 8)
  518. * @retval SET (__BAUDRATE__ is valid) or RESET (__BAUDRATE__ is invalid) */
  519. #define IS_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 4000000U)
  520. /**
  521. * @brief Ensure that USART frame number of stop bits is valid.
  522. * @param __STOPBITS__ USART frame number of stop bits.
  523. * @retval SET (__STOPBITS__ is valid) or RESET (__STOPBITS__ is invalid)
  524. */
  525. #define IS_USART_STOPBITS(__STOPBITS__) (((__STOPBITS__) == USART_STOPBITS_0_5) || \
  526. ((__STOPBITS__) == USART_STOPBITS_1) || \
  527. ((__STOPBITS__) == USART_STOPBITS_1_5) || \
  528. ((__STOPBITS__) == USART_STOPBITS_2))
  529. /**
  530. * @brief Ensure that USART frame parity is valid.
  531. * @param __PARITY__ USART frame parity.
  532. * @retval SET (__PARITY__ is valid) or RESET (__PARITY__ is invalid)
  533. */
  534. #define IS_USART_PARITY(__PARITY__) (((__PARITY__) == USART_PARITY_NONE) || \
  535. ((__PARITY__) == USART_PARITY_EVEN) || \
  536. ((__PARITY__) == USART_PARITY_ODD))
  537. /**
  538. * @brief Ensure that USART communication mode is valid.
  539. * @param __MODE__ USART communication mode.
  540. * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
  541. */
  542. #define IS_USART_MODE(__MODE__) ((((__MODE__) & 0xFFFFFFF3U) == 0x00U) && ((__MODE__) != 0x00U))
  543. /**
  544. * @brief Ensure that USART oversampling is valid.
  545. * @param __SAMPLING__ USART oversampling.
  546. * @retval SET (__SAMPLING__ is valid) or RESET (__SAMPLING__ is invalid)
  547. */
  548. #define IS_USART_OVERSAMPLING(__SAMPLING__) (((__SAMPLING__) == USART_OVERSAMPLING_16) || \
  549. ((__SAMPLING__) == USART_OVERSAMPLING_8))
  550. /**
  551. * @brief Ensure that USART clock state is valid.
  552. * @param __CLOCK__ USART clock state.
  553. * @retval SET (__CLOCK__ is valid) or RESET (__CLOCK__ is invalid)
  554. */
  555. #define IS_USART_CLOCK(__CLOCK__) (((__CLOCK__) == USART_CLOCK_DISABLE) || \
  556. ((__CLOCK__) == USART_CLOCK_ENABLE))
  557. /**
  558. * @brief Ensure that USART frame polarity is valid.
  559. * @param __CPOL__ USART frame polarity.
  560. * @retval SET (__CPOL__ is valid) or RESET (__CPOL__ is invalid)
  561. */
  562. #define IS_USART_POLARITY(__CPOL__) (((__CPOL__) == USART_POLARITY_LOW) || ((__CPOL__) == USART_POLARITY_HIGH))
  563. /**
  564. * @brief Ensure that USART frame phase is valid.
  565. * @param __CPHA__ USART frame phase.
  566. * @retval SET (__CPHA__ is valid) or RESET (__CPHA__ is invalid)
  567. */
  568. #define IS_USART_PHASE(__CPHA__) (((__CPHA__) == USART_PHASE_1EDGE) || ((__CPHA__) == USART_PHASE_2EDGE))
  569. /**
  570. * @brief Ensure that USART frame last bit clock pulse setting is valid.
  571. * @param __LASTBIT__ USART frame last bit clock pulse setting.
  572. * @retval SET (__LASTBIT__ is valid) or RESET (__LASTBIT__ is invalid)
  573. */
  574. #define IS_USART_LASTBIT(__LASTBIT__) (((__LASTBIT__) == USART_LASTBIT_DISABLE) || \
  575. ((__LASTBIT__) == USART_LASTBIT_ENABLE))
  576. /**
  577. * @brief Ensure that USART request parameter is valid.
  578. * @param __PARAM__ USART request parameter.
  579. * @retval SET (__PARAM__ is valid) or RESET (__PARAM__ is invalid)
  580. */
  581. #define IS_USART_REQUEST_PARAMETER(__PARAM__) (((__PARAM__) == USART_RXDATA_FLUSH_REQUEST) || \
  582. ((__PARAM__) == USART_TXDATA_FLUSH_REQUEST))
  583. /**
  584. * @}
  585. */
  586. /* Include USART HAL Extended module */
  587. #include "stm32l0xx_hal_usart_ex.h"
  588. /* Exported functions --------------------------------------------------------*/
  589. /** @addtogroup USART_Exported_Functions USART Exported Functions
  590. * @{
  591. */
  592. /** @addtogroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
  593. * @{
  594. */
  595. /* Initialization and de-initialization functions ****************************/
  596. HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart);
  597. HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart);
  598. void HAL_USART_MspInit(USART_HandleTypeDef *husart);
  599. void HAL_USART_MspDeInit(USART_HandleTypeDef *husart);
  600. /* Callbacks Register/UnRegister functions ***********************************/
  601. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  602. HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID, pUSART_CallbackTypeDef pCallback);
  603. HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID);
  604. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  605. /**
  606. * @}
  607. */
  608. /** @addtogroup USART_Exported_Functions_Group2 IO operation functions
  609. * @{
  610. */
  611. /* IO operation functions *****************************************************/
  612. HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout);
  613. HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
  614. HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
  615. HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size);
  616. HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
  617. HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size);
  618. HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size);
  619. HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
  620. HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size);
  621. HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart);
  622. HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart);
  623. HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart);
  624. /* Transfer Abort functions */
  625. HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart);
  626. HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart);
  627. void HAL_USART_IRQHandler(USART_HandleTypeDef *husart);
  628. void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart);
  629. void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart);
  630. void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart);
  631. void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart);
  632. void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart);
  633. void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart);
  634. void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart);
  635. /**
  636. * @}
  637. */
  638. /** @addtogroup USART_Exported_Functions_Group4 Peripheral State and Error functions
  639. * @{
  640. */
  641. /* Peripheral State and Error functions ***************************************/
  642. HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart);
  643. uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart);
  644. /**
  645. * @}
  646. */
  647. /**
  648. * @}
  649. */
  650. /**
  651. * @}
  652. */
  653. /**
  654. * @}
  655. */
  656. #ifdef __cplusplus
  657. }
  658. #endif
  659. #endif /* STM32L0xx_HAL_USART_H */
  660. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/