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   * 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 STM32L0xx_HAL_USART_H
21 #define STM32L0xx_HAL_USART_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32l0xx_hal_def.h"
29 
30 /** @addtogroup STM32L0xx_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  Flush the USART Data registers
392   * @param  __HANDLE__ specifies the USART Handle.
393   */
394 #define __HAL_USART_FLUSH_DRREGISTER(__HANDLE__)                        \
395   do{                                                                   \
396     SET_BIT((__HANDLE__)->Instance->RQR, USART_RXDATA_FLUSH_REQUEST); \
397     SET_BIT((__HANDLE__)->Instance->RQR, USART_TXDATA_FLUSH_REQUEST); \
398   } while(0U)
399 
400 /** @brief  Check whether the specified USART flag is set or not.
401   * @param  __HANDLE__ specifies the USART Handle
402   * @param  __FLAG__ specifies the flag to check.
403   *        This parameter can be one of the following values:
404   *            @arg @ref USART_FLAG_REACK Receive enable acknowledge flag
405   *            @arg @ref USART_FLAG_TEACK Transmit enable acknowledge flag
406   *            @arg @ref USART_FLAG_BUSY  Busy flag
407   *            @arg @ref USART_FLAG_TXE   Transmit data register empty flag
408   *            @arg @ref USART_FLAG_TC    Transmission Complete flag
409   *            @arg @ref USART_FLAG_RXNE  Receive data register not empty flag
410   *            @arg @ref USART_FLAG_RTOF  Receiver Timeout flag
411   *            @arg @ref USART_FLAG_IDLE  Idle Line detection flag
412   *            @arg @ref USART_FLAG_ORE   OverRun Error flag
413   *            @arg @ref USART_FLAG_NE    Noise Error flag
414   *            @arg @ref USART_FLAG_FE    Framing Error flag
415   *            @arg @ref USART_FLAG_PE    Parity Error flag
416   * @retval The new state of __FLAG__ (TRUE or FALSE).
417   */
418 #define __HAL_USART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->ISR & (__FLAG__)) == (__FLAG__))
419 
420 /** @brief  Clear the specified USART pending flag.
421   * @param  __HANDLE__ specifies the USART Handle.
422   * @param  __FLAG__ specifies the flag to check.
423   *          This parameter can be any combination of the following values:
424   *            @arg @ref USART_CLEAR_PEF      Parity Error Clear Flag
425   *            @arg @ref USART_CLEAR_FEF      Framing Error Clear Flag
426   *            @arg @ref USART_CLEAR_NEF      Noise detected Clear Flag
427   *            @arg @ref USART_CLEAR_OREF     Overrun Error Clear Flag
428   *            @arg @ref USART_CLEAR_IDLEF    IDLE line detected Clear Flag
429   *            @arg @ref USART_CLEAR_TCF      Transmission Complete Clear Flag
430   *            @arg @ref USART_CLEAR_RTOF     Receiver Timeout clear flag
431   * @retval None
432   */
433 #define __HAL_USART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->ICR = (__FLAG__))
434 
435 /** @brief  Clear the USART PE pending flag.
436   * @param  __HANDLE__ specifies the USART Handle.
437   * @retval None
438   */
439 #define __HAL_USART_CLEAR_PEFLAG(__HANDLE__)   __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_PEF)
440 
441 /** @brief  Clear the USART FE pending flag.
442   * @param  __HANDLE__ specifies the USART Handle.
443   * @retval None
444   */
445 #define __HAL_USART_CLEAR_FEFLAG(__HANDLE__)   __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_FEF)
446 
447 /** @brief  Clear the USART NE pending flag.
448   * @param  __HANDLE__ specifies the USART Handle.
449   * @retval None
450   */
451 #define __HAL_USART_CLEAR_NEFLAG(__HANDLE__)  __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_NEF)
452 
453 /** @brief  Clear the USART ORE pending flag.
454   * @param  __HANDLE__ specifies the USART Handle.
455   * @retval None
456   */
457 #define __HAL_USART_CLEAR_OREFLAG(__HANDLE__)   __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_OREF)
458 
459 /** @brief  Clear the USART IDLE pending flag.
460   * @param  __HANDLE__ specifies the USART Handle.
461   * @retval None
462   */
463 #define __HAL_USART_CLEAR_IDLEFLAG(__HANDLE__)   __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_IDLEF)
464 
465 
466 
467 /** @brief  Enable the specified USART interrupt.
468   * @param  __HANDLE__ specifies the USART Handle.
469   * @param  __INTERRUPT__ specifies the USART interrupt source to enable.
470   *          This parameter can be one of the following values:
471   *            @arg @ref USART_IT_TXE   Transmit Data Register empty interrupt
472   *            @arg @ref USART_IT_TC    Transmission complete interrupt
473   *            @arg @ref USART_IT_RXNE  Receive Data register not empty interrupt
474   *            @arg @ref USART_IT_IDLE  Idle line detection interrupt
475   *            @arg @ref USART_IT_PE    Parity Error interrupt
476   *            @arg @ref USART_IT_ERR   Error interrupt(Frame error, noise error, overrun error)
477   * @retval None
478   */
479 #define __HAL_USART_ENABLE_IT(__HANDLE__, __INTERRUPT__)\
480   (((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 1U)?\
481    ((__HANDLE__)->Instance->CR1 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
482    ((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 2U)?\
483    ((__HANDLE__)->Instance->CR2 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
484    ((__HANDLE__)->Instance->CR3 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))))
485 
486 /** @brief  Disable the specified USART interrupt.
487   * @param  __HANDLE__ specifies the USART Handle.
488   * @param  __INTERRUPT__ specifies the USART interrupt source to disable.
489   *          This parameter can be one of the following values:
490   *            @arg @ref USART_IT_TXE   Transmit Data Register empty interrupt
491   *            @arg @ref USART_IT_TC    Transmission complete interrupt
492   *            @arg @ref USART_IT_RXNE  Receive Data register not empty interrupt
493   *            @arg @ref USART_IT_IDLE  Idle line detection interrupt
494   *            @arg @ref USART_IT_PE    Parity Error interrupt
495   *            @arg @ref USART_IT_ERR   Error interrupt(Frame error, noise error, overrun error)
496   * @retval None
497   */
498 #define __HAL_USART_DISABLE_IT(__HANDLE__, __INTERRUPT__)\
499   (((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 1U)?\
500    ((__HANDLE__)->Instance->CR1 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
501    ((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 2U)?\
502    ((__HANDLE__)->Instance->CR2 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
503    ((__HANDLE__)->Instance->CR3 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))))
504 
505 /** @brief  Check whether the specified USART interrupt has occurred or not.
506   * @param  __HANDLE__ specifies the USART Handle.
507   * @param  __INTERRUPT__ specifies the USART interrupt source to check.
508   *          This parameter can be one of the following values:
509   *            @arg @ref USART_IT_TXE   Transmit Data Register empty interrupt
510   *            @arg @ref USART_IT_TC    Transmission complete interrupt
511   *            @arg @ref USART_IT_RXNE  Receive Data register not empty interrupt
512   *            @arg @ref USART_IT_IDLE  Idle line detection interrupt
513   *            @arg @ref USART_IT_ORE   OverRun Error interrupt
514   *            @arg @ref USART_IT_NE    Noise Error interrupt
515   *            @arg @ref USART_IT_FE    Framing Error interrupt
516   *            @arg @ref USART_IT_PE    Parity Error interrupt
517   * @retval The new state of __INTERRUPT__ (SET or RESET).
518   */
519 #define __HAL_USART_GET_IT(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->ISR\
520                                                          & (0x01U << (((__INTERRUPT__) & USART_ISR_MASK)>>\
521                                                                       USART_ISR_POS))) != 0U) ? SET : RESET)
522 
523 /** @brief  Check whether the specified USART interrupt source is enabled or not.
524   * @param  __HANDLE__ specifies the USART Handle.
525   * @param  __INTERRUPT__ specifies the USART interrupt source to check.
526   *          This parameter can be one of the following values:
527   *            @arg @ref USART_IT_TXE   Transmit Data Register empty interrupt
528   *            @arg @ref USART_IT_TC    Transmission complete interrupt
529   *            @arg @ref USART_IT_RXNE  Receive Data register not empty interrupt
530   *            @arg @ref USART_IT_IDLE  Idle line detection interrupt
531   *            @arg @ref USART_IT_ORE   OverRun Error interrupt
532   *            @arg @ref USART_IT_NE    Noise Error interrupt
533   *            @arg @ref USART_IT_FE    Framing Error interrupt
534   *            @arg @ref USART_IT_PE    Parity Error interrupt
535   * @retval The new state of __INTERRUPT__ (SET or RESET).
536   */
537 #define __HAL_USART_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((((((uint8_t)(__INTERRUPT__)) >> 0x05U) == 0x01U) ?\
538                                                                  (__HANDLE__)->Instance->CR1 : \
539                                                                  (((((uint8_t)(__INTERRUPT__)) >> 0x05U) == 0x02U) ?\
540                                                                   (__HANDLE__)->Instance->CR2 : \
541                                                                   (__HANDLE__)->Instance->CR3)) & (0x01U <<\
542                                                                       (((uint16_t)(__INTERRUPT__)) &\
543                                                                        USART_IT_MASK)))  != 0U) ? SET : RESET)
544 
545 /** @brief  Clear the specified USART ISR flag, in setting the proper ICR register flag.
546   * @param  __HANDLE__ specifies the USART Handle.
547   * @param  __IT_CLEAR__ specifies the interrupt clear register flag that needs to be set
548   *                       to clear the corresponding interrupt.
549   *          This parameter can be one of the following values:
550   *            @arg @ref USART_CLEAR_PEF      Parity Error Clear Flag
551   *            @arg @ref USART_CLEAR_FEF      Framing Error Clear Flag
552   *            @arg @ref USART_CLEAR_NEF      Noise detected Clear Flag
553   *            @arg @ref USART_CLEAR_OREF     Overrun Error Clear Flag
554   *            @arg @ref USART_CLEAR_IDLEF    IDLE line detected Clear Flag
555   *            @arg @ref USART_CLEAR_RTOF     Receiver timeout clear flag
556   *            @arg @ref USART_CLEAR_TCF      Transmission Complete Clear Flag
557   * @retval None
558   */
559 #define __HAL_USART_CLEAR_IT(__HANDLE__, __IT_CLEAR__) ((__HANDLE__)->Instance->ICR = (uint32_t)(__IT_CLEAR__))
560 
561 /** @brief  Set a specific USART request flag.
562   * @param  __HANDLE__ specifies the USART Handle.
563   * @param  __REQ__ specifies the request flag to set.
564   *          This parameter can be one of the following values:
565   *            @arg @ref USART_RXDATA_FLUSH_REQUEST Receive Data flush Request
566   *            @arg @ref USART_TXDATA_FLUSH_REQUEST Transmit data flush Request
567   *
568   * @retval None
569   */
570 #define __HAL_USART_SEND_REQ(__HANDLE__, __REQ__)      ((__HANDLE__)->Instance->RQR |= (uint16_t)(__REQ__))
571 
572 /** @brief  Enable the USART one bit sample method.
573   * @param  __HANDLE__ specifies the USART Handle.
574   * @retval None
575   */
576 #define __HAL_USART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT)
577 
578 /** @brief  Disable the USART one bit sample method.
579   * @param  __HANDLE__ specifies the USART Handle.
580   * @retval None
581   */
582 #define __HAL_USART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= ~USART_CR3_ONEBIT)
583 
584 /** @brief  Enable USART.
585   * @param  __HANDLE__ specifies the USART Handle.
586   * @retval None
587   */
588 #define __HAL_USART_ENABLE(__HANDLE__)                 ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
589 
590 /** @brief  Disable USART.
591   * @param  __HANDLE__ specifies the USART Handle.
592   * @retval None
593   */
594 #define __HAL_USART_DISABLE(__HANDLE__)                ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
595 
596 /**
597   * @}
598   */
599 
600 /* Private macros --------------------------------------------------------*/
601 /** @defgroup USART_Private_Macros   USART Private Macros
602   * @{
603   */
604 
605 /** @brief  BRR division operation to set BRR register in 8-bit oversampling mode.
606   * @param  __PCLK__ USART clock.
607   * @param  __BAUD__ Baud rate set by the user.
608   * @retval Division result
609   */
610 #define USART_DIV_SAMPLING8(__PCLK__, __BAUD__)   ((((__PCLK__)*2U) + ((__BAUD__)/2U)) / (__BAUD__))
611 
612 /** @brief  Check USART Baud rate.
613   * @param  __BAUDRATE__ Baudrate specified by the user.
614   *         The maximum Baud Rate is derived from the maximum clock on L0 (i.e. 32 MHz)
615   *         divided by the smallest oversampling used on the USART (i.e. 8)
616   * @retval SET (__BAUDRATE__ is valid) or RESET (__BAUDRATE__ is invalid)  */
617 #define IS_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 4000000U)
618 
619 /**
620   * @brief Ensure that USART frame number of stop bits is valid.
621   * @param __STOPBITS__ USART frame number of stop bits.
622   * @retval SET (__STOPBITS__ is valid) or RESET (__STOPBITS__ is invalid)
623   */
624 #define IS_USART_STOPBITS(__STOPBITS__) (((__STOPBITS__) == USART_STOPBITS_0_5) || \
625                                          ((__STOPBITS__) == USART_STOPBITS_1)   || \
626                                          ((__STOPBITS__) == USART_STOPBITS_1_5) || \
627                                          ((__STOPBITS__) == USART_STOPBITS_2))
628 
629 /**
630   * @brief Ensure that USART frame parity is valid.
631   * @param __PARITY__ USART frame parity.
632   * @retval SET (__PARITY__ is valid) or RESET (__PARITY__ is invalid)
633   */
634 #define IS_USART_PARITY(__PARITY__) (((__PARITY__) == USART_PARITY_NONE) || \
635                                      ((__PARITY__) == USART_PARITY_EVEN) || \
636                                      ((__PARITY__) == USART_PARITY_ODD))
637 
638 /**
639   * @brief Ensure that USART communication mode is valid.
640   * @param __MODE__ USART communication mode.
641   * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
642   */
643 #define IS_USART_MODE(__MODE__) ((((__MODE__) & 0xFFFFFFF3U) == 0x00U) && ((__MODE__) != 0x00U))
644 
645 /**
646   * @brief Ensure that USART clock state is valid.
647   * @param __CLOCK__ USART clock state.
648   * @retval SET (__CLOCK__ is valid) or RESET (__CLOCK__ is invalid)
649   */
650 #define IS_USART_CLOCK(__CLOCK__) (((__CLOCK__) == USART_CLOCK_DISABLE) || \
651                                    ((__CLOCK__) == USART_CLOCK_ENABLE))
652 
653 /**
654   * @brief Ensure that USART frame polarity is valid.
655   * @param __CPOL__ USART frame polarity.
656   * @retval SET (__CPOL__ is valid) or RESET (__CPOL__ is invalid)
657   */
658 #define IS_USART_POLARITY(__CPOL__) (((__CPOL__) == USART_POLARITY_LOW) || ((__CPOL__) == USART_POLARITY_HIGH))
659 
660 /**
661   * @brief Ensure that USART frame phase is valid.
662   * @param __CPHA__ USART frame phase.
663   * @retval SET (__CPHA__ is valid) or RESET (__CPHA__ is invalid)
664   */
665 #define IS_USART_PHASE(__CPHA__) (((__CPHA__) == USART_PHASE_1EDGE) || ((__CPHA__) == USART_PHASE_2EDGE))
666 
667 /**
668   * @brief Ensure that USART frame last bit clock pulse setting is valid.
669   * @param __LASTBIT__ USART frame last bit clock pulse setting.
670   * @retval SET (__LASTBIT__ is valid) or RESET (__LASTBIT__ is invalid)
671   */
672 #define IS_USART_LASTBIT(__LASTBIT__) (((__LASTBIT__) == USART_LASTBIT_DISABLE) || \
673                                        ((__LASTBIT__) == USART_LASTBIT_ENABLE))
674 
675 /**
676   * @brief Ensure that USART request parameter is valid.
677   * @param __PARAM__ USART request parameter.
678   * @retval SET (__PARAM__ is valid) or RESET (__PARAM__ is invalid)
679   */
680 #define IS_USART_REQUEST_PARAMETER(__PARAM__) (((__PARAM__) == USART_RXDATA_FLUSH_REQUEST) || \
681                                                ((__PARAM__) == USART_TXDATA_FLUSH_REQUEST))
682 
683 /**
684   * @}
685   */
686 
687 /* Include USART HAL Extended module */
688 #include "stm32l0xx_hal_usart_ex.h"
689 
690 /* Exported functions --------------------------------------------------------*/
691 /** @addtogroup USART_Exported_Functions USART Exported Functions
692   * @{
693   */
694 
695 /** @addtogroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
696   * @{
697   */
698 
699 /* Initialization and de-initialization functions  ****************************/
700 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart);
701 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart);
702 void HAL_USART_MspInit(USART_HandleTypeDef *husart);
703 void HAL_USART_MspDeInit(USART_HandleTypeDef *husart);
704 
705 /* Callbacks Register/UnRegister functions  ***********************************/
706 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
707 HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID,
708                                              pUSART_CallbackTypeDef pCallback);
709 HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID);
710 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
711 
712 /**
713   * @}
714   */
715 
716 /** @addtogroup USART_Exported_Functions_Group2 IO operation functions
717   * @{
718   */
719 
720 /* IO operation functions *****************************************************/
721 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size,
722                                      uint32_t Timeout);
723 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
724 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
725                                             uint16_t Size, uint32_t Timeout);
726 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size);
727 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
728 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
729                                                uint16_t Size);
730 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size);
731 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
732 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
733                                                 uint16_t Size);
734 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart);
735 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart);
736 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart);
737 /* Transfer Abort functions */
738 HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart);
739 HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart);
740 
741 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart);
742 void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart);
743 void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart);
744 void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart);
745 void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart);
746 void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart);
747 void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart);
748 void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart);
749 
750 /**
751   * @}
752   */
753 
754 /** @addtogroup USART_Exported_Functions_Group4 Peripheral State and Error functions
755   * @{
756   */
757 
758 /* Peripheral State and Error functions ***************************************/
759 HAL_USART_StateTypeDef HAL_USART_GetState(const USART_HandleTypeDef *husart);
760 uint32_t               HAL_USART_GetError(const USART_HandleTypeDef *husart);
761 
762 /**
763   * @}
764   */
765 
766 /**
767   * @}
768   */
769 
770 /**
771   * @}
772   */
773 
774 /**
775   * @}
776   */
777 
778 #ifdef __cplusplus
779 }
780 #endif
781 
782 #endif /* STM32L0xx_HAL_USART_H */
783 
784