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