1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_usart.h
4   * @author  MCD Application Team
5   * @brief   Header file of USART HAL module.
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
10   *
11   * Redistribution and use in source and binary forms, with or without modification,
12   * are permitted provided that the following conditions are met:
13   *   1. Redistributions of source code must retain the above copyright notice,
14   *      this list of conditions and the following disclaimer.
15   *   2. Redistributions in binary form must reproduce the above copyright notice,
16   *      this list of conditions and the following disclaimer in the documentation
17   *      and/or other materials provided with the distribution.
18   *   3. Neither the name of STMicroelectronics nor the names of its contributors
19   *      may be used to endorse or promote products derived from this software
20   *      without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   *
33   ******************************************************************************
34   */
35 
36 /* Define to prevent recursive inclusion -------------------------------------*/
37 #ifndef __STM32L0xx_HAL_USART_H
38 #define __STM32L0xx_HAL_USART_H
39 
40 #ifdef __cplusplus
41  extern "C" {
42 #endif
43 
44 /* Includes ------------------------------------------------------------------*/
45 #include "stm32l0xx_hal_def.h"
46 
47 /** @addtogroup STM32L0xx_HAL_Driver
48   * @{
49   */
50 
51 /** @addtogroup USART
52   * @{
53   */
54 
55 /* Exported types ------------------------------------------------------------*/
56 /** @defgroup USART_Exported_Types USART Exported Types
57   * @{
58   */
59 
60 /**
61   * @brief USART Init Structure definition
62   */
63 typedef struct
64 {
65   uint32_t BaudRate;                  /*!< This member configures the Usart communication baud rate.
66                                            The baud rate is computed using the following formula:
67                                               Baud Rate Register = ((PCLKx) / ((huart->Init.BaudRate))). */
68 
69   uint32_t WordLength;                /*!< Specifies the number of data bits transmitted or received in a frame.
70                                            This parameter can be a value of @ref USARTEx_Word_Length. */
71 
72   uint32_t StopBits;                  /*!< Specifies the number of stop bits transmitted.
73                                            This parameter can be a value of @ref USART_Stop_Bits. */
74 
75   uint32_t Parity;                   /*!< Specifies the parity mode.
76                                            This parameter can be a value of @ref USART_Parity
77                                            @note When parity is enabled, the computed parity is inserted
78                                                  at the MSB position of the transmitted data (9th bit when
79                                                  the word length is set to 9 data bits; 8th bit when the
80                                                  word length is set to 8 data bits). */
81 
82   uint32_t Mode;                      /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
83                                            This parameter can be a value of @ref USART_Mode. */
84 
85   uint32_t CLKPolarity;               /*!< Specifies the steady state of the serial clock.
86                                            This parameter can be a value of @ref USART_Clock_Polarity. */
87 
88   uint32_t CLKPhase;                  /*!< Specifies the clock transition on which the bit capture is made.
89                                            This parameter can be a value of @ref USART_Clock_Phase. */
90 
91   uint32_t CLKLastBit;                /*!< Specifies whether the clock pulse corresponding to the last transmitted
92                                            data bit (MSB) has to be output on the SCLK pin in synchronous mode.
93                                            This parameter can be a value of @ref USART_Last_Bit. */
94 }USART_InitTypeDef;
95 
96 /**
97   * @brief HAL USART State structures definition
98   */
99 typedef enum
100 {
101   HAL_USART_STATE_RESET             = 0x00U,    /*!< Peripheral is not initialized                  */
102   HAL_USART_STATE_READY             = 0x01U,    /*!< Peripheral Initialized and ready for use       */
103   HAL_USART_STATE_BUSY              = 0x02U,    /*!< an internal process is ongoing                 */
104   HAL_USART_STATE_BUSY_TX           = 0x12U,    /*!< Data Transmission process is ongoing           */
105   HAL_USART_STATE_BUSY_RX           = 0x22U,    /*!< Data Reception process is ongoing              */
106   HAL_USART_STATE_BUSY_TX_RX        = 0x32U,    /*!< Data Transmission Reception process is ongoing */
107   HAL_USART_STATE_TIMEOUT           = 0x03U,    /*!< Timeout state                                  */
108   HAL_USART_STATE_ERROR             = 0x04U     /*!< Error                                          */
109 }HAL_USART_StateTypeDef;
110 
111 /**
112   * @brief  HAL USART Error Code structure definition
113   */
114 #define   HAL_USART_ERROR_NONE      ((uint32_t)0x00U)    /*!< No error            */
115 #define   HAL_USART_ERROR_PE        ((uint32_t)0x01U)    /*!< Parity error        */
116 #define   HAL_USART_ERROR_NE        ((uint32_t)0x02U)    /*!< Noise error         */
117 #define   HAL_USART_ERROR_FE        ((uint32_t)0x04U)    /*!< frame error         */
118 #define   HAL_USART_ERROR_ORE       ((uint32_t)0x08U)    /*!< Overrun error       */
119 #define   HAL_USART_ERROR_DMA       ((uint32_t)0x10U)    /*!< DMA transfer error  */
120 
121 /**
122   * @brief  USART clock sources definitions
123   */
124 typedef enum
125 {
126   USART_CLOCKSOURCE_PCLK1      = 0x00U,    /*!< PCLK1 clock source     */
127   USART_CLOCKSOURCE_PCLK2      = 0x01U,    /*!< PCLK2 clock source     */
128   USART_CLOCKSOURCE_HSI        = 0x02U,    /*!< HSI clock source       */
129   USART_CLOCKSOURCE_SYSCLK     = 0x04U,    /*!< SYSCLK clock source    */
130   USART_CLOCKSOURCE_LSE        = 0x08U,    /*!< LSE clock source       */
131   USART_CLOCKSOURCE_UNDEFINED  = 0x10U     /*!< Undefined clock source */
132 }USART_ClockSourceTypeDef;
133 
134 
135 /**
136   * @brief  USART handle Structure definition
137   */
138 typedef struct
139 {
140   USART_TypeDef                 *Instance;        /*!< USART registers base address        */
141 
142   USART_InitTypeDef             Init;             /*!< USART communication parameters      */
143 
144   uint8_t                       *pTxBuffPtr;      /*!< Pointer to USART Tx transfer Buffer */
145 
146   uint16_t                      TxXferSize;       /*!< USART Tx Transfer size              */
147 
148   __IO uint16_t                 TxXferCount;      /*!< USART Tx Transfer Counter           */
149 
150   uint8_t                       *pRxBuffPtr;      /*!< Pointer to USART Rx transfer Buffer */
151 
152   uint16_t                      RxXferSize;       /*!< USART Rx Transfer size              */
153 
154   __IO uint16_t                 RxXferCount;      /*!< USART Rx Transfer Counter           */
155 
156   uint16_t                      Mask;             /*!< USART Rx RDR register mask          */
157 
158   DMA_HandleTypeDef             *hdmatx;          /*!< USART Tx DMA Handle parameters      */
159 
160   DMA_HandleTypeDef             *hdmarx;          /*!< USART Rx DMA Handle parameters      */
161 
162   HAL_LockTypeDef               Lock;             /*!< Locking object                      */
163 
164   __IO HAL_USART_StateTypeDef   State;            /*!< USART communication state           */
165 
166   __IO uint32_t                 ErrorCode;        /*!< USART Error code                    */
167 
168 }USART_HandleTypeDef;
169 
170 /**
171   * @}
172   */
173 
174 /* Exported constants --------------------------------------------------------*/
175 /** @defgroup USART_Exported_Constants USART Exported Constants
176   * @{
177   */
178 
179 /** @defgroup USART_Stop_Bits  USART Number of Stop Bits
180   * @{
181   */
182 #define USART_STOPBITS_0_5                   USART_CR2_STOP_0                      /*!< USART frame with 0.5 stop bit  */
183 #define USART_STOPBITS_1                     ((uint32_t)0x00000000)                /*!< USART frame with 1 stop bit    */
184 #define USART_STOPBITS_1_5                   (USART_CR2_STOP_0 | USART_CR2_STOP_1) /*!< USART frame with 1.5 stop bits */
185 #define USART_STOPBITS_2                     USART_CR2_STOP_1                      /*!< USART frame with 2 stop bits   */
186 /**
187   * @}
188   */
189 
190 /** @defgroup USART_Parity    USART Parity
191   * @{
192   */
193 #define USART_PARITY_NONE                   ((uint32_t)0x00000000U)                     /*!< No parity   */
194 #define USART_PARITY_EVEN                   ((uint32_t)USART_CR1_PCE)                   /*!< Even parity */
195 #define USART_PARITY_ODD                    ((uint32_t)(USART_CR1_PCE | USART_CR1_PS))  /*!< Odd parity  */
196 /**
197   * @}
198   */
199 
200 /** @defgroup USART_Mode   USART Mode
201   * @{
202   */
203 #define USART_MODE_RX                       ((uint32_t)USART_CR1_RE)                   /*!< RX mode        */
204 #define USART_MODE_TX                       ((uint32_t)USART_CR1_TE)                   /*!< TX mode        */
205 #define USART_MODE_TX_RX                    ((uint32_t)(USART_CR1_TE |USART_CR1_RE))   /*!< RX and TX mode */
206 /**
207   * @}
208   */
209 
210 /** @defgroup USART_Clock  USART Clock
211   * @{
212   */
213 #define USART_CLOCK_DISABLE                 ((uint32_t)0x00000000U)       /*!< USART clock disable */
214 #define USART_CLOCK_ENABLE                  ((uint32_t)USART_CR2_CLKEN)   /*!< USART clock enable  */
215 /**
216   * @}
217   */
218 
219 /** @defgroup USART_Clock_Polarity  USART Clock Polarity
220   * @{
221   */
222 #define USART_POLARITY_LOW                  ((uint32_t)0x00000000U)      /*!< USART Clock signal is steady Low */
223 #define USART_POLARITY_HIGH                 ((uint32_t)USART_CR2_CPOL)   /*!< USART Clock signal is steady High  */
224 /**
225   * @}
226   */
227 
228 /** @defgroup USART_Clock_Phase   USART Clock Phase
229   * @{
230   */
231 #define USART_PHASE_1EDGE                   ((uint32_t)0x00000000U)      /*!< USART frame phase on first clock transition  */
232 #define USART_PHASE_2EDGE                   ((uint32_t)USART_CR2_CPHA)   /*!< USART frame phase on second clock transition */
233 /**
234   * @}
235   */
236 
237 /** @defgroup USART_Last_Bit  USART Last Bit
238   * @{
239   */
240 #define USART_LASTBIT_DISABLE               ((uint32_t)0x00000000U)      /*!< USART frame last data bit clock pulse not output to SCLK pin */
241 #define USART_LASTBIT_ENABLE                ((uint32_t)USART_CR2_LBCL)   /*!< USART frame last data bit clock pulse output to SCLK pin     */
242 /**
243   * @}
244   */
245 
246 /** @defgroup USART_Request_Parameters  USART Request Parameters
247   * @{
248   */
249 #define USART_RXDATA_FLUSH_REQUEST        ((uint32_t)USART_RQR_RXFRQ)        /*!< Receive Data flush Request  */
250 #define USART_TXDATA_FLUSH_REQUEST        ((uint32_t)USART_RQR_TXFRQ)        /*!< Transmit data flush Request */
251 /**
252   * @}
253   */
254 
255 /** @defgroup USART_Flags      USART Flags
256   *        Elements values convention: 0xXXXX
257   *           - 0xXXXX  : Flag mask in the ISR register
258   * @{
259   */
260 #define USART_FLAG_REACK                     USART_ISR_REACK /*!< USART receive enable acknowledge flag  */
261 #define USART_FLAG_TEACK                     USART_ISR_TEACK /*!< USART transmit enable acknowledge flag */
262 #define USART_FLAG_BUSY                      USART_ISR_BUSY  /*!< USART busy flag                        */
263 #define USART_FLAG_CTS                       USART_ISR_CTS   /*!< USART clear to send flag               */
264 #define USART_FLAG_CTSIF                     USART_ISR_CTSIF /*!< USART clear to send interrupt flag     */
265 #define USART_FLAG_LBDF                      USART_ISR_LBDF  /*!< USART LIN break detection flag         */
266 #define USART_FLAG_TXE                       USART_ISR_TXE   /*!< USART transmit data register empty     */
267 #define USART_FLAG_TC                        USART_ISR_TC    /*!< USART transmission complete            */
268 #define USART_FLAG_RXNE                      USART_ISR_RXNE  /*!< USART read data register not empty     */
269 #define USART_FLAG_IDLE                      USART_ISR_IDLE  /*!< USART idle flag                        */
270 #define USART_FLAG_ORE                       USART_ISR_ORE   /*!< USART overrun error                    */
271 #define USART_FLAG_NE                        USART_ISR_NE    /*!< USART noise error                      */
272 #define USART_FLAG_FE                        USART_ISR_FE    /*!< USART frame error                      */
273 #define USART_FLAG_PE                        USART_ISR_PE    /*!< USART parity error                     */
274 /**
275   * @}
276   */
277 
278 /** @defgroup USART_Interrupt_definition USART Interrupts Definition
279   *        Elements values convention: 0000ZZZZ0XXYYYYYb
280   *           - YYYYY  : Interrupt source position in the XX register (5bits)
281   *           - XX  : Interrupt source register (2bits)
282   *                 - 01: CR1 register
283   *                 - 10: CR2 register
284   *                 - 11: CR3 register
285   *           - ZZZZ  : Flag position in the ISR register(4bits)
286   * @{
287   */
288 
289 #define USART_IT_PE                          ((uint16_t)0x0028U)     /*!< USART parity error interruption                 */
290 #define USART_IT_TXE                         ((uint16_t)0x0727U)     /*!< USART transmit data register empty interruption */
291 #define USART_IT_TC                          ((uint16_t)0x0626U)     /*!< USART transmission complete interruption        */
292 #define USART_IT_RXNE                        ((uint16_t)0x0525U)     /*!< USART read data register not empty interruption */
293 #define USART_IT_IDLE                        ((uint16_t)0x0424U)     /*!< USART idle interruption                         */
294 #define USART_IT_ERR                         ((uint16_t)0x0060U)     /*!< USART error interruption                        */
295 #define USART_IT_ORE                         ((uint16_t)0x0300U)     /*!< USART overrun error interruption                */
296 #define USART_IT_NE                          ((uint16_t)0x0200U)     /*!< USART noise error interruption                  */
297 #define USART_IT_FE                          ((uint16_t)0x0100U)     /*!< USART frame error interruption                  */
298 /**
299   * @}
300   */
301 
302 /** @defgroup USART_IT_CLEAR_Flags    USART Interruption Clear Flags
303   * @{
304   */
305 #define USART_CLEAR_PEF                       USART_ICR_PECF            /*!< Parity Error Clear Flag          */
306 #define USART_CLEAR_FEF                       USART_ICR_FECF            /*!< Framing Error Clear Flag         */
307 #define USART_CLEAR_NEF                       USART_ICR_NCF             /*!< Noise detected Clear Flag        */
308 #define USART_CLEAR_OREF                      USART_ICR_ORECF           /*!< OverRun Error Clear Flag         */
309 #define USART_CLEAR_IDLEF                     USART_ICR_IDLECF          /*!< IDLE line detected Clear Flag    */
310 #define USART_CLEAR_TCF                       USART_ICR_TCCF            /*!< Transmission Complete Clear Flag */
311 #define USART_CLEAR_CTSF                      USART_ICR_CTSCF           /*!< CTS Interrupt Clear Flag */
312 /**
313   * @}
314   */
315 
316 /** @defgroup USART_Interruption_Mask    USART Interruption Flags Mask
317   * @{
318   */
319 #define USART_IT_MASK                             ((uint16_t)0x001FU)     /*!< USART interruptions flags mask */
320 /**
321   * @}
322   */
323 
324 /**
325   * @}
326   */
327 
328 /* Exported macros -----------------------------------------------------------*/
329 /** @defgroup USART_Exported_Macros USART Exported Macros
330   * @{
331   */
332 
333 /** @brief Reset USART handle state.
334   * @param  __HANDLE__: USART handle.
335   * @retval None
336   */
337 #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__)  ((__HANDLE__)->State = HAL_USART_STATE_RESET)
338 
339 /** @brief  Flush the USART Data registers
340   * @param  __HANDLE__: specifies the USART Handle.
341   */
342 #define __HAL_USART_FLUSH_DRREGISTER(__HANDLE__)  \
343   do{                \
344       SET_BIT((__HANDLE__)->Instance->RQR, USART_RXDATA_FLUSH_REQUEST); \
345       SET_BIT((__HANDLE__)->Instance->RQR, USART_TXDATA_FLUSH_REQUEST); \
346     } while(0)
347 
348 
349 /** @brief  Check whether the specified USART flag is set or not.
350   * @param  __HANDLE__: specifies the USART Handle
351   * @param  __FLAG__: specifies the flag to check.
352   *        This parameter can be one of the following values:
353   *            @arg @ref USART_FLAG_REACK Receive enable acknowledge flag
354   *            @arg @ref USART_FLAG_TEACK Transmit enable acknowledge flag
355   *            @arg @ref USART_FLAG_BUSY  Busy flag
356   *            @arg @ref USART_FLAG_CTS   CTS Change flag
357   *            @arg @ref USART_FLAG_TXE   Transmit data register empty flag
358   *            @arg @ref USART_FLAG_TC    Transmission Complete flag
359   *            @arg @ref USART_FLAG_RXNE  Receive data register not empty flag
360   *            @arg @ref USART_FLAG_IDLE  Idle Line detection flag
361   *            @arg @ref USART_FLAG_ORE   OverRun Error flag
362   *            @arg @ref USART_FLAG_NE    Noise Error flag
363   *            @arg @ref USART_FLAG_FE    Framing Error flag
364   *            @arg @ref USART_FLAG_PE    Parity Error flag
365   * @retval The new state of __FLAG__ (TRUE or FALSE).
366   */
367 #define __HAL_USART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->ISR & (__FLAG__)) == (__FLAG__))
368 
369 /** @brief  Clear the specified USART pending flag.
370   * @param  __HANDLE__: specifies the USART Handle.
371   * @param  __FLAG__: specifies the flag to check.
372   *          This parameter can be any combination of the following values:
373   *            @arg @ref USART_CLEAR_PEF
374   *            @arg @ref USART_CLEAR_FEF
375   *            @arg @ref USART_CLEAR_NEF
376   *            @arg @ref USART_CLEAR_OREF
377   *            @arg @ref USART_CLEAR_IDLEF
378   *            @arg @ref USART_CLEAR_TCF
379   *            @arg @ref USART_CLEAR_CTSF
380   * @retval None
381   */
382 #define __HAL_USART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->ICR = (__FLAG__))
383 
384 /** @brief  Clear the USART PE pending flag.
385   * @param  __HANDLE__: specifies the USART Handle.
386   * @retval None
387   */
388 #define __HAL_USART_CLEAR_PEFLAG(__HANDLE__)   __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_PEF)
389 
390 /** @brief  Clear the USART FE pending flag.
391   * @param  __HANDLE__: specifies the USART Handle.
392   * @retval None
393   */
394 #define __HAL_USART_CLEAR_FEFLAG(__HANDLE__)   __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_FEF)
395 
396 /** @brief  Clear the USART NE pending flag.
397   * @param  __HANDLE__: specifies the USART Handle.
398   * @retval None
399   */
400 #define __HAL_USART_CLEAR_NEFLAG(__HANDLE__)  __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_NEF)
401 
402 /** @brief  Clear the USART ORE pending flag.
403   * @param  __HANDLE__: specifies the USART Handle.
404   * @retval None
405   */
406 #define __HAL_USART_CLEAR_OREFLAG(__HANDLE__)   __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_OREF)
407 
408 /** @brief  Clear the USART IDLE pending flag.
409   * @param  __HANDLE__: specifies the USART Handle.
410   * @retval None
411   */
412 #define __HAL_USART_CLEAR_IDLEFLAG(__HANDLE__)   __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_IDLEF)
413 
414 /** @brief  Enable the specified USART interrupt.
415   * @param  __HANDLE__: specifies the USART Handle.
416   * @param  __INTERRUPT__: specifies the USART interrupt source to enable.
417   *          This parameter can be one of the following values:
418   *            @arg @ref USART_IT_TXE  Transmit Data Register empty interrupt
419   *            @arg @ref USART_IT_TC   Transmission complete interrupt
420   *            @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
421   *            @arg @ref USART_IT_IDLE Idle line detection interrupt
422   *            @arg @ref USART_IT_PE   Parity Error interrupt
423   *            @arg @ref USART_IT_ERR  Error interrupt(Frame error, noise error, overrun error)
424   * @retval None
425   */
426 #define __HAL_USART_ENABLE_IT(__HANDLE__, __INTERRUPT__)   (((((uint8_t)(__INTERRUPT__)) >> 5U) == 1U)? ((__HANDLE__)->Instance->CR1 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
427                                                             ((((uint8_t)(__INTERRUPT__)) >> 5U) == 2U)? ((__HANDLE__)->Instance->CR2 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
428                                                             ((__HANDLE__)->Instance->CR3 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))))
429 
430 /** @brief  Disable the specified USART interrupt.
431   * @param  __HANDLE__: specifies the USART Handle.
432   * @param  __INTERRUPT__: specifies the USART interrupt source to disable.
433   *          This parameter can be one of the following values:
434   *            @arg @ref USART_IT_TXE  Transmit Data Register empty interrupt
435   *            @arg @ref USART_IT_TC   Transmission complete interrupt
436   *            @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
437   *            @arg @ref USART_IT_IDLE Idle line detection interrupt
438   *            @arg @ref USART_IT_PE   Parity Error interrupt
439   *            @arg @ref USART_IT_ERR  Error interrupt(Frame error, noise error, overrun error)
440   * @retval None
441   */
442 #define __HAL_USART_DISABLE_IT(__HANDLE__, __INTERRUPT__)  (((((uint8_t)(__INTERRUPT__)) >> 5U) == 1U)? ((__HANDLE__)->Instance->CR1 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
443                                                             ((((uint8_t)(__INTERRUPT__)) >> 5U) == 2U)? ((__HANDLE__)->Instance->CR2 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))): \
444                                                             ((__HANDLE__)->Instance->CR3 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))))
445 
446 
447 /** @brief  Check whether the specified USART interrupt has occurred or not.
448   * @param  __HANDLE__: specifies the USART Handle.
449   * @param  __IT__: specifies the USART interrupt source to check.
450   *          This parameter can be one of the following values:
451   *            @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
452   *            @arg @ref USART_IT_TC  Transmission complete interrupt
453   *            @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
454   *            @arg @ref USART_IT_IDLE Idle line detection interrupt
455   *            @arg @ref USART_IT_ORE OverRun Error interrupt
456   *            @arg @ref USART_IT_NE Noise Error interrupt
457   *            @arg @ref USART_IT_FE Framing Error interrupt
458   *            @arg @ref USART_IT_PE Parity Error interrupt
459   * @retval The new state of __IT__ (TRUE or FALSE).
460   */
461 #define __HAL_USART_GET_IT(__HANDLE__, __IT__) ((__HANDLE__)->Instance->ISR & ((uint32_t)1U << ((__IT__)>> 0x08U)))
462 
463 /** @brief  Check whether the specified USART interrupt source is enabled or not.
464   * @param  __HANDLE__: specifies the USART Handle.
465   * @param  __IT__: specifies the USART interrupt source to check.
466   *          This parameter can be one of the following values:
467   *            @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
468   *            @arg @ref USART_IT_TC  Transmission complete interrupt
469   *            @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
470   *            @arg @ref USART_IT_IDLE Idle line detection interrupt
471   *            @arg @ref USART_IT_ORE OverRun Error interrupt
472   *            @arg @ref USART_IT_NE Noise Error interrupt
473   *            @arg @ref USART_IT_FE Framing Error interrupt
474   *            @arg @ref USART_IT_PE Parity Error interrupt
475   * @retval The new state of __IT__ (TRUE or FALSE).
476   */
477 #define __HAL_USART_GET_IT_SOURCE(__HANDLE__, __IT__) ((((((uint8_t)(__IT__)) >> 5U) == 1U)? (__HANDLE__)->Instance->CR1:(((((uint8_t)(__IT__)) >> 5U) == 2U)? \
478                                                    (__HANDLE__)->Instance->CR2 : (__HANDLE__)->Instance->CR3)) & ((uint32_t)1U << \
479                                                    (((uint16_t)(__IT__)) & USART_IT_MASK)))
480 
481 
482 /** @brief  Clear the specified USART ISR flag, in setting the proper ICR register flag.
483   * @param  __HANDLE__: specifies the USART Handle.
484   * @param  __IT_CLEAR__: specifies the interrupt clear register flag that needs to be set
485   *                       to clear the corresponding interrupt.
486   *          This parameter can be one of the following values:
487   *            @arg @ref USART_CLEAR_PEF Parity Error Clear Flag
488   *            @arg @ref USART_CLEAR_FEF Framing Error Clear Flag
489   *            @arg @ref USART_CLEAR_NEF Noise detected Clear Flag
490   *            @arg @ref USART_CLEAR_OREF OverRun Error Clear Flag
491   *            @arg @ref USART_CLEAR_IDLEF IDLE line detected Clear Flag
492   *            @arg @ref USART_CLEAR_TCF Transmission Complete Clear Flag
493   *            @arg @ref USART_CLEAR_CTSF CTS Interrupt Clear Flag
494   * @retval None
495   */
496 #define __HAL_USART_CLEAR_IT(__HANDLE__, __IT_CLEAR__) ((__HANDLE__)->Instance->ICR = (uint32_t)(__IT_CLEAR__))
497 
498 /** @brief  Set a specific USART request flag.
499   * @param  __HANDLE__: specifies the USART Handle.
500   * @param  __REQ__: specifies the request flag to set.
501   *          This parameter can be one of the following values:
502   *            @arg @ref USART_RXDATA_FLUSH_REQUEST Receive Data flush Request
503   *            @arg @ref USART_TXDATA_FLUSH_REQUEST Transmit data flush Request
504   *
505   * @retval None
506   */
507 #define __HAL_USART_SEND_REQ(__HANDLE__, __REQ__)      ((__HANDLE__)->Instance->RQR |= (uint32_t)(__REQ__))
508 
509 /** @brief  Enable the USART one bit sample method.
510   * @param  __HANDLE__: specifies the USART Handle.
511   * @retval None
512   */
513 #define __HAL_USART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT)
514 
515 /** @brief  Disable the USART one bit sample method.
516   * @param  __HANDLE__: specifies the USART Handle.
517   * @retval None
518   */
519 #define __HAL_USART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_ONEBIT))
520 
521 /** @brief  Enable USART.
522   * @param  __HANDLE__: specifies the USART Handle.
523   * @retval None
524   */
525 #define __HAL_USART_ENABLE(__HANDLE__)                 ((__HANDLE__)->Instance->CR1 |=  USART_CR1_UE)
526 
527 /** @brief  Disable USART.
528   * @param  __HANDLE__: specifies the USART Handle.
529   * @retval None
530   */
531 #define __HAL_USART_DISABLE(__HANDLE__)                ((__HANDLE__)->Instance->CR1 &=  ~USART_CR1_UE)
532 
533 /**
534   * @}
535   */
536 
537 /* Private macros --------------------------------------------------------*/
538 /** @defgroup USART_Private_Macros   USART Private Macros
539   * @{
540   */
541 
542 /** @brief  Check USART Baud rate.
543   * @param  __BAUDRATE__: Baudrate specified by the user.
544   *         The maximum Baud Rate is derived from the maximum clock on L0 (i.e. 32 MHz)
545   *         divided by the smallest oversampling used on the USART (i.e. 8).
546   * @retval Test result (TRUE or FALSE).
547   */
548 #define IS_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) < 4000001U)
549 
550 
551 /**
552   * @brief Ensure that USART frame number of stop bits is valid.
553   * @param __STOPBITS__: USART frame number of stop bits.
554   * @retval SET (__STOPBITS__ is valid) or RESET (__STOPBITS__ is invalid)
555   */
556 #define IS_USART_STOPBITS(__STOPBITS__) (((__STOPBITS__) == USART_STOPBITS_0_5) || \
557                                          ((__STOPBITS__) == USART_STOPBITS_1)   || \
558                                          ((__STOPBITS__) == USART_STOPBITS_1_5) || \
559                                          ((__STOPBITS__) == USART_STOPBITS_2))
560 
561 /**
562   * @brief Ensure that USART frame parity is valid.
563   * @param __PARITY__: USART frame parity.
564   * @retval SET (__PARITY__ is valid) or RESET (__PARITY__ is invalid)
565   */
566 #define IS_USART_PARITY(__PARITY__) (((__PARITY__) == USART_PARITY_NONE) || \
567                                      ((__PARITY__) == USART_PARITY_EVEN) || \
568                                      ((__PARITY__) == USART_PARITY_ODD))
569 
570 /**
571   * @brief Ensure that USART communication mode is valid.
572   * @param __MODE__: USART communication mode.
573   * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
574   */
575 #define IS_USART_MODE(MODE)     (((MODE) == USART_MODE_RX) || \
576                                  ((MODE) == USART_MODE_TX) || \
577                                  ((MODE) == USART_MODE_TX_RX))
578 
579 /**
580   * @brief Ensure that USART clock state is valid.
581   * @param __CLOCK__: USART clock state.
582   * @retval SET (__CLOCK__ is valid) or RESET (__CLOCK__ is invalid)
583   */
584 #define IS_USART_CLOCK(__CLOCK__) (((__CLOCK__) == USART_CLOCK_DISABLE) || \
585                                    ((__CLOCK__) == USART_CLOCK_ENABLE))
586 
587 /**
588   * @brief Ensure that USART frame polarity is valid.
589   * @param __CPOL__: USART frame polarity.
590   * @retval SET (__CPOL__ is valid) or RESET (__CPOL__ is invalid)
591   */
592 #define IS_USART_POLARITY(__CPOL__) (((__CPOL__) == USART_POLARITY_LOW) || ((__CPOL__) == USART_POLARITY_HIGH))
593 
594 /**
595   * @brief Ensure that USART frame phase is valid.
596   * @param __CPHA__: USART frame phase.
597   * @retval SET (__CPHA__ is valid) or RESET (__CPHA__ is invalid)
598   */
599 #define IS_USART_PHASE(__CPHA__) (((__CPHA__) == USART_PHASE_1EDGE) || ((__CPHA__) == USART_PHASE_2EDGE))
600 
601 /**
602   * @brief Ensure that USART frame last bit clock pulse setting is valid.
603   * @param __LASTBIT__: USART frame last bit clock pulse setting.
604   * @retval SET (__LASTBIT__ is valid) or RESET (__LASTBIT__ is invalid)
605   */
606 #define IS_USART_LASTBIT(__LASTBIT__) (((__LASTBIT__) == USART_LASTBIT_DISABLE) || \
607                                        ((__LASTBIT__) == USART_LASTBIT_ENABLE))
608 
609 /**
610   * @brief Ensure that USART request parameter is valid.
611   * @param __PARAM__: USART request parameter.
612   * @retval SET (__PARAM__ is valid) or RESET (__PARAM__ is invalid)
613   */
614 #define IS_USART_REQUEST_PARAMETER(__PARAM__) (((__PARAM__) == USART_RXDATA_FLUSH_REQUEST) || \
615                                                ((__PARAM__) == USART_TXDATA_FLUSH_REQUEST))
616 
617 /**
618   * @}
619   */
620 
621 /* Include USART HAL Extended module */
622 #include "stm32l0xx_hal_usart_ex.h"
623 
624 /* Exported functions --------------------------------------------------------*/
625 /** @addtogroup USART_Exported_Functions USART Exported Functions
626   * @{
627   */
628 
629 /** @addtogroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
630   * @{
631   */
632 
633 /* Initialization and de-initialization functions  ****************************/
634 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart);
635 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart);
636 void HAL_USART_MspInit(USART_HandleTypeDef *husart);
637 void HAL_USART_MspDeInit(USART_HandleTypeDef *husart);
638 
639 /**
640   * @}
641   */
642 
643 /** @addtogroup USART_Exported_Functions_Group2 IO operation functions
644   * @{
645   */
646 
647 /* IO operation functions *****************************************************/
648 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout);
649 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
650 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
651 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size);
652 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
653 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData,  uint16_t Size);
654 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size);
655 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
656 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size);
657 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart);
658 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart);
659 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart);
660 /* Transfer Abort functions */
661 HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart);
662 HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart);
663 
664 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart);
665 void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart);
666 void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart);
667 void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart);
668 void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart);
669 void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart);
670 void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart);
671 void HAL_USART_AbortCpltCallback (USART_HandleTypeDef *husart);
672 
673 /**
674   * @}
675   */
676 
677 /* Peripheral Control functions ***********************************************/
678 
679 /** @addtogroup USART_Exported_Functions_Group4 Peripheral State and Error functions
680   * @{
681   */
682 
683 /* Peripheral State and Error functions ***************************************/
684 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart);
685 uint32_t               HAL_USART_GetError(USART_HandleTypeDef *husart);
686 
687 /**
688   * @}
689   */
690 
691 /**
692   * @}
693   */
694 
695 /**
696   * @}
697   */
698 
699 /**
700   * @}
701   */
702 
703 #ifdef __cplusplus
704 }
705 #endif
706 
707 #endif /* __STM32L0xx_HAL_USART_H */
708 
709 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
710 
711