1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_uart_ex.h
4   * @author  MCD Application Team
5   * @brief   Header file of UART HAL Extended 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_UART_EX_H
21 #define STM32L0xx_HAL_UART_EX_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 UARTEx
35   * @{
36   */
37 
38 /* Exported types ------------------------------------------------------------*/
39 /** @defgroup UARTEx_Exported_Types UARTEx Exported Types
40   * @{
41   */
42 
43 /**
44   * @brief  UART wake up from stop mode parameters
45   */
46 typedef struct
47 {
48   uint32_t WakeUpEvent;        /*!< Specifies which event will activate the Wakeup from Stop mode flag (WUF).
49                                     This parameter can be a value of @ref UART_WakeUp_from_Stop_Selection.
50                                     If set to UART_WAKEUP_ON_ADDRESS, the two other fields below must
51                                     be filled up. */
52 
53   uint16_t AddressLength;      /*!< Specifies whether the address is 4 or 7-bit long.
54                                     This parameter can be a value of @ref UARTEx_WakeUp_Address_Length.  */
55 
56   uint8_t Address;             /*!< UART/USART node address (7-bit long max). */
57 } UART_WakeUpTypeDef;
58 
59 /**
60   * @}
61   */
62 
63 /* Exported constants --------------------------------------------------------*/
64 /** @defgroup UARTEx_Exported_Constants UARTEx Exported Constants
65   * @{
66   */
67 
68 /** @defgroup UARTEx_Word_Length UARTEx Word Length
69   * @{
70   */
71 #define UART_WORDLENGTH_7B          USART_CR1_M1   /*!< 7-bit long UART frame */
72 #define UART_WORDLENGTH_8B          0x00000000U    /*!< 8-bit long UART frame */
73 #define UART_WORDLENGTH_9B          USART_CR1_M0   /*!< 9-bit long UART frame */
74 /**
75   * @}
76   */
77 
78 /** @defgroup UARTEx_WakeUp_Address_Length UARTEx WakeUp Address Length
79   * @{
80   */
81 #define UART_ADDRESS_DETECT_4B      0x00000000U      /*!< 4-bit long wake-up address */
82 #define UART_ADDRESS_DETECT_7B      USART_CR2_ADDM7  /*!< 7-bit long wake-up address */
83 /**
84   * @}
85   */
86 
87 /**
88   * @}
89   */
90 
91 /* Exported macros -----------------------------------------------------------*/
92 /* Exported functions --------------------------------------------------------*/
93 /** @addtogroup UARTEx_Exported_Functions
94   * @{
95   */
96 
97 /** @addtogroup UARTEx_Exported_Functions_Group1
98   * @{
99   */
100 
101 /* Initialization and de-initialization functions  ****************************/
102 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,
103                                    uint32_t DeassertionTime);
104 
105 /**
106   * @}
107   */
108 
109 /** @addtogroup UARTEx_Exported_Functions_Group2
110   * @{
111   */
112 
113 void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart);
114 
115 /**
116   * @}
117   */
118 
119 /** @addtogroup UARTEx_Exported_Functions_Group3
120   * @{
121   */
122 
123 /* Peripheral Control functions  **********************************************/
124 HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
125 HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart);
126 HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart);
127 
128 HAL_StatusTypeDef HAL_UARTEx_EnableClockStopMode(UART_HandleTypeDef *huart);
129 HAL_StatusTypeDef HAL_UARTEx_DisableClockStopMode(UART_HandleTypeDef *huart);
130 
131 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength);
132 
133 
134 HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen,
135                                            uint32_t Timeout);
136 HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
137 HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
138 
139 HAL_UART_RxEventTypeTypeDef HAL_UARTEx_GetRxEventType(UART_HandleTypeDef *huart);
140 
141 
142 /**
143   * @}
144   */
145 
146 /**
147   * @}
148   */
149 
150 /* Private macros ------------------------------------------------------------*/
151 /** @defgroup UARTEx_Private_Macros UARTEx Private Macros
152   * @{
153   */
154 
155 /** @brief  Report the UART clock source.
156   * @param  __HANDLE__ specifies the UART Handle.
157   * @param  __CLOCKSOURCE__ output variable.
158   * @retval UART clocking source, written in __CLOCKSOURCE__.
159   */
160 #if defined (STM32L031xx) || defined (STM32L041xx) || defined (STM32L011xx) || defined (STM32L021xx) \
161  || defined (STM32L010xB) || defined (STM32L010x8) || defined (STM32L010x6) || defined (STM32L010x4)
162 #define UART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
163   do {                                                        \
164     if((__HANDLE__)->Instance == USART2)                      \
165     {                                                         \
166       switch(__HAL_RCC_GET_USART2_SOURCE())                   \
167       {                                                       \
168         case RCC_USART2CLKSOURCE_PCLK1:                       \
169           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
170           break;                                              \
171         case RCC_USART2CLKSOURCE_HSI:                         \
172           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
173           break;                                              \
174         case RCC_USART2CLKSOURCE_SYSCLK:                      \
175           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
176           break;                                              \
177         case RCC_USART2CLKSOURCE_LSE:                         \
178           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
179           break;                                              \
180         default:                                              \
181           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
182           break;                                              \
183       }                                                       \
184     }                                                         \
185     else if((__HANDLE__)->Instance == LPUART1)                \
186     {                                                         \
187       switch(__HAL_RCC_GET_LPUART1_SOURCE())                  \
188       {                                                       \
189         case RCC_LPUART1CLKSOURCE_PCLK1:                      \
190           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
191           break;                                              \
192         case RCC_LPUART1CLKSOURCE_HSI:                        \
193           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
194           break;                                              \
195         case RCC_LPUART1CLKSOURCE_SYSCLK:                     \
196           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
197           break;                                              \
198         case RCC_LPUART1CLKSOURCE_LSE:                        \
199           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
200           break;                                              \
201         default:                                              \
202           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
203           break;                                              \
204       }                                                       \
205     }                                                         \
206     else                                                      \
207     {                                                         \
208       (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;         \
209     }                                                         \
210   } while(0)
211 
212 #elif defined (STM32L051xx) || defined (STM32L052xx) || defined (STM32L053xx) || defined (STM32L062xx) \
213    || defined (STM32L063xx)
214 
215 #define UART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
216   do {                                                        \
217     if((__HANDLE__)->Instance == USART1)                      \
218     {                                                         \
219       switch(__HAL_RCC_GET_USART1_SOURCE())                   \
220       {                                                       \
221         case RCC_USART1CLKSOURCE_PCLK2:                       \
222           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK2;         \
223           break;                                              \
224         case RCC_USART1CLKSOURCE_HSI:                         \
225           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
226           break;                                              \
227         case RCC_USART1CLKSOURCE_SYSCLK:                      \
228           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
229           break;                                              \
230         case RCC_USART1CLKSOURCE_LSE:                         \
231           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
232           break;                                              \
233         default:                                              \
234           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
235           break;                                              \
236       }                                                       \
237     }                                                         \
238     else if((__HANDLE__)->Instance == USART2)                 \
239     {                                                         \
240       switch(__HAL_RCC_GET_USART2_SOURCE())                   \
241       {                                                       \
242         case RCC_USART2CLKSOURCE_PCLK1:                       \
243           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
244           break;                                              \
245         case RCC_USART2CLKSOURCE_HSI:                         \
246           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
247           break;                                              \
248         case RCC_USART2CLKSOURCE_SYSCLK:                      \
249           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
250           break;                                              \
251         case RCC_USART2CLKSOURCE_LSE:                         \
252           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
253           break;                                              \
254         default:                                              \
255           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
256           break;                                              \
257       }                                                       \
258     }                                                         \
259     else if((__HANDLE__)->Instance == LPUART1)                \
260     {                                                         \
261       switch(__HAL_RCC_GET_LPUART1_SOURCE())                  \
262       {                                                       \
263         case RCC_LPUART1CLKSOURCE_PCLK1:                      \
264           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
265           break;                                              \
266         case RCC_LPUART1CLKSOURCE_HSI:                        \
267           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
268           break;                                              \
269         case RCC_LPUART1CLKSOURCE_SYSCLK:                     \
270           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
271           break;                                              \
272         case RCC_LPUART1CLKSOURCE_LSE:                        \
273           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
274           break;                                              \
275         default:                                              \
276           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
277           break;                                              \
278       }                                                       \
279     }                                                         \
280     else                                                      \
281     {                                                         \
282       (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;         \
283     }                                                         \
284   } while(0)
285 
286 #else
287 
288 #define UART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
289   do {                                                        \
290     if((__HANDLE__)->Instance == USART1)                      \
291     {                                                         \
292       switch(__HAL_RCC_GET_USART1_SOURCE())                   \
293       {                                                       \
294         case RCC_USART1CLKSOURCE_PCLK2:                       \
295           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK2;         \
296           break;                                              \
297         case RCC_USART1CLKSOURCE_HSI:                         \
298           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
299           break;                                              \
300         case RCC_USART1CLKSOURCE_SYSCLK:                      \
301           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
302           break;                                              \
303         case RCC_USART1CLKSOURCE_LSE:                         \
304           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
305           break;                                              \
306         default:                                              \
307           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
308           break;                                              \
309       }                                                       \
310     }                                                         \
311     else if((__HANDLE__)->Instance == USART2)                 \
312     {                                                         \
313       switch(__HAL_RCC_GET_USART2_SOURCE())                   \
314       {                                                       \
315         case RCC_USART2CLKSOURCE_PCLK1:                       \
316           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
317           break;                                              \
318         case RCC_USART2CLKSOURCE_HSI:                         \
319           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
320           break;                                              \
321         case RCC_USART2CLKSOURCE_SYSCLK:                      \
322           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
323           break;                                              \
324         case RCC_USART2CLKSOURCE_LSE:                         \
325           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
326           break;                                              \
327         default:                                              \
328           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
329           break;                                              \
330       }                                                       \
331     }                                                         \
332     else if((__HANDLE__)->Instance == USART4)                 \
333     {                                                         \
334       (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;             \
335     }                                                         \
336     else if((__HANDLE__)->Instance == USART5)                 \
337     {                                                         \
338       (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;             \
339     }                                                         \
340     else if((__HANDLE__)->Instance == LPUART1)                \
341     {                                                         \
342       switch(__HAL_RCC_GET_LPUART1_SOURCE())                  \
343       {                                                       \
344         case RCC_LPUART1CLKSOURCE_PCLK1:                      \
345           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
346           break;                                              \
347         case RCC_LPUART1CLKSOURCE_HSI:                        \
348           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
349           break;                                              \
350         case RCC_LPUART1CLKSOURCE_SYSCLK:                     \
351           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
352           break;                                              \
353         case RCC_LPUART1CLKSOURCE_LSE:                        \
354           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
355           break;                                              \
356         default:                                              \
357           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
358           break;                                              \
359       }                                                       \
360     }                                                         \
361     else                                                      \
362     {                                                         \
363       (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;         \
364     }                                                         \
365   } while(0)
366 #endif /* (STM32L031xx) || (STM32L041xx) || (STM32L011xx) || (STM32L021xx) || (STM32L010xB) ||
367           (STM32L010x8) || (STM32L010x6) || (STM32L010x4)*/
368 
369 
370 /** @brief  Report the UART mask to apply to retrieve the received data
371   *         according to the word length and to the parity bits activation.
372   * @note   If PCE = 1, the parity bit is not included in the data extracted
373   *         by the reception API().
374   *         This masking operation is not carried out in the case of
375   *         DMA transfers.
376   * @param  __HANDLE__ specifies the UART Handle.
377   * @retval None, the mask to apply to UART RDR register is stored in (__HANDLE__)->Mask field.
378   */
379 #define UART_MASK_COMPUTATION(__HANDLE__)                             \
380   do {                                                                \
381     if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_9B)          \
382     {                                                                 \
383       if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
384       {                                                               \
385         (__HANDLE__)->Mask = 0x01FFU ;                                \
386       }                                                               \
387       else                                                            \
388       {                                                               \
389         (__HANDLE__)->Mask = 0x00FFU ;                                \
390       }                                                               \
391     }                                                                 \
392     else if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_8B)     \
393     {                                                                 \
394       if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
395       {                                                               \
396         (__HANDLE__)->Mask = 0x00FFU ;                                \
397       }                                                               \
398       else                                                            \
399       {                                                               \
400         (__HANDLE__)->Mask = 0x007FU ;                                \
401       }                                                               \
402     }                                                                 \
403     else if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_7B)     \
404     {                                                                 \
405       if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
406       {                                                               \
407         (__HANDLE__)->Mask = 0x007FU ;                                \
408       }                                                               \
409       else                                                            \
410       {                                                               \
411         (__HANDLE__)->Mask = 0x003FU ;                                \
412       }                                                               \
413     }                                                                 \
414     else                                                              \
415     {                                                                 \
416       (__HANDLE__)->Mask = 0x0000U;                                   \
417     }                                                                 \
418   } while(0U)
419 
420 /**
421   * @brief Ensure that UART frame length is valid.
422   * @param __LENGTH__ UART frame length.
423   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
424   */
425 #define IS_UART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == UART_WORDLENGTH_7B) || \
426                                          ((__LENGTH__) == UART_WORDLENGTH_8B) || \
427                                          ((__LENGTH__) == UART_WORDLENGTH_9B))
428 
429 /**
430   * @brief Ensure that UART wake-up address length is valid.
431   * @param __ADDRESS__ UART wake-up address length.
432   * @retval SET (__ADDRESS__ is valid) or RESET (__ADDRESS__ is invalid)
433   */
434 #define IS_UART_ADDRESSLENGTH_DETECT(__ADDRESS__) (((__ADDRESS__) == UART_ADDRESS_DETECT_4B) || \
435                                                    ((__ADDRESS__) == UART_ADDRESS_DETECT_7B))
436 
437 /**
438   * @}
439   */
440 
441 /* Private functions ---------------------------------------------------------*/
442 
443 /**
444   * @}
445   */
446 
447 /**
448   * @}
449   */
450 
451 #ifdef __cplusplus
452 }
453 #endif
454 
455 #endif /* STM32L0xx_HAL_UART_EX_H */
456 
457