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