1 /**
2   ******************************************************************************
3   * @file    stm32l5xx_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) 2019 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 STM32L5xx_HAL_UART_EX_H
21 #define STM32L5xx_HAL_UART_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32l5xx_hal_def.h"
29 
30 /** @addtogroup STM32L5xx_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 /** @defgroup UARTEx_FIFO_mode UARTEx FIFO mode
88   * @brief    UART FIFO mode
89   * @{
90   */
91 #define UART_FIFOMODE_DISABLE       0x00000000U       /*!< FIFO mode disable */
92 #define UART_FIFOMODE_ENABLE        USART_CR1_FIFOEN  /*!< FIFO mode enable  */
93 /**
94   * @}
95   */
96 
97 /** @defgroup UARTEx_TXFIFO_threshold_level UARTEx TXFIFO threshold level
98   * @brief    UART TXFIFO threshold level
99   * @{
100   */
101 #define UART_TXFIFO_THRESHOLD_1_8   0x00000000U                               /*!< TX FIFO reaches 1/8 of its depth */
102 #define UART_TXFIFO_THRESHOLD_1_4   USART_CR3_TXFTCFG_0                       /*!< TX FIFO reaches 1/4 of its depth */
103 #define UART_TXFIFO_THRESHOLD_1_2   USART_CR3_TXFTCFG_1                       /*!< TX FIFO reaches 1/2 of its depth */
104 #define UART_TXFIFO_THRESHOLD_3_4   (USART_CR3_TXFTCFG_0|USART_CR3_TXFTCFG_1) /*!< TX FIFO reaches 3/4 of its depth */
105 #define UART_TXFIFO_THRESHOLD_7_8   USART_CR3_TXFTCFG_2                       /*!< TX FIFO reaches 7/8 of its depth */
106 #define UART_TXFIFO_THRESHOLD_8_8   (USART_CR3_TXFTCFG_2|USART_CR3_TXFTCFG_0) /*!< TX FIFO becomes empty            */
107 /**
108   * @}
109   */
110 
111 /** @defgroup UARTEx_RXFIFO_threshold_level UARTEx RXFIFO threshold level
112   * @brief    UART RXFIFO threshold level
113   * @{
114   */
115 #define UART_RXFIFO_THRESHOLD_1_8   0x00000000U                               /*!< RX FIFO reaches 1/8 of its depth */
116 #define UART_RXFIFO_THRESHOLD_1_4   USART_CR3_RXFTCFG_0                       /*!< RX FIFO reaches 1/4 of its depth */
117 #define UART_RXFIFO_THRESHOLD_1_2   USART_CR3_RXFTCFG_1                       /*!< RX FIFO reaches 1/2 of its depth */
118 #define UART_RXFIFO_THRESHOLD_3_4   (USART_CR3_RXFTCFG_0|USART_CR3_RXFTCFG_1) /*!< RX FIFO reaches 3/4 of its depth */
119 #define UART_RXFIFO_THRESHOLD_7_8   USART_CR3_RXFTCFG_2                       /*!< RX FIFO reaches 7/8 of its depth */
120 #define UART_RXFIFO_THRESHOLD_8_8   (USART_CR3_RXFTCFG_2|USART_CR3_RXFTCFG_0) /*!< RX FIFO becomes full             */
121 /**
122   * @}
123   */
124 
125 /**
126   * @}
127   */
128 
129 /* Exported macros -----------------------------------------------------------*/
130 /* Exported functions --------------------------------------------------------*/
131 /** @addtogroup UARTEx_Exported_Functions
132   * @{
133   */
134 
135 /** @addtogroup UARTEx_Exported_Functions_Group1
136   * @{
137   */
138 
139 /* Initialization and de-initialization functions  ****************************/
140 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,
141                                    uint32_t DeassertionTime);
142 
143 /**
144   * @}
145   */
146 
147 /** @addtogroup UARTEx_Exported_Functions_Group2
148   * @{
149   */
150 
151 void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart);
152 
153 void HAL_UARTEx_RxFifoFullCallback(UART_HandleTypeDef *huart);
154 void HAL_UARTEx_TxFifoEmptyCallback(UART_HandleTypeDef *huart);
155 
156 /**
157   * @}
158   */
159 
160 /** @addtogroup UARTEx_Exported_Functions_Group3
161   * @{
162   */
163 
164 /* Peripheral Control functions  **********************************************/
165 HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
166 HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart);
167 HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart);
168 
169 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength);
170 
171 HAL_StatusTypeDef HAL_UARTEx_EnableFifoMode(UART_HandleTypeDef *huart);
172 HAL_StatusTypeDef HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef *huart);
173 HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold);
174 HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold);
175 
176 HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen,
177                                            uint32_t Timeout);
178 HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
179 HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
180 
181 HAL_UART_RxEventTypeTypeDef HAL_UARTEx_GetRxEventType(UART_HandleTypeDef *huart);
182 
183 
184 /**
185   * @}
186   */
187 
188 /**
189   * @}
190   */
191 
192 /* Private macros ------------------------------------------------------------*/
193 /** @defgroup UARTEx_Private_Macros UARTEx Private Macros
194   * @{
195   */
196 
197 /** @brief  Report the UART clock source.
198   * @param  __HANDLE__ specifies the UART Handle.
199   * @param  __CLOCKSOURCE__ output variable.
200   * @retval UART clocking source, written in __CLOCKSOURCE__.
201   */
202 #define UART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
203   do {                                                        \
204     if((__HANDLE__)->Instance == USART1)                      \
205     {                                                         \
206       switch(__HAL_RCC_GET_USART1_SOURCE())                   \
207       {                                                       \
208         case RCC_USART1CLKSOURCE_PCLK2:                       \
209           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK2;         \
210           break;                                              \
211         case RCC_USART1CLKSOURCE_HSI:                         \
212           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
213           break;                                              \
214         case RCC_USART1CLKSOURCE_SYSCLK:                      \
215           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
216           break;                                              \
217         case RCC_USART1CLKSOURCE_LSE:                         \
218           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
219           break;                                              \
220         default:                                              \
221           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
222           break;                                              \
223       }                                                       \
224     }                                                         \
225     else if((__HANDLE__)->Instance == USART2)                 \
226     {                                                         \
227       switch(__HAL_RCC_GET_USART2_SOURCE())                   \
228       {                                                       \
229         case RCC_USART2CLKSOURCE_PCLK1:                       \
230           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
231           break;                                              \
232         case RCC_USART2CLKSOURCE_HSI:                         \
233           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
234           break;                                              \
235         case RCC_USART2CLKSOURCE_SYSCLK:                      \
236           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
237           break;                                              \
238         case RCC_USART2CLKSOURCE_LSE:                         \
239           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
240           break;                                              \
241         default:                                              \
242           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
243           break;                                              \
244       }                                                       \
245     }                                                         \
246     else if((__HANDLE__)->Instance == USART3)                 \
247     {                                                         \
248       switch(__HAL_RCC_GET_USART3_SOURCE())                   \
249       {                                                       \
250         case RCC_USART3CLKSOURCE_PCLK1:                       \
251           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
252           break;                                              \
253         case RCC_USART3CLKSOURCE_HSI:                         \
254           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
255           break;                                              \
256         case RCC_USART3CLKSOURCE_SYSCLK:                      \
257           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
258           break;                                              \
259         case RCC_USART3CLKSOURCE_LSE:                         \
260           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
261           break;                                              \
262         default:                                              \
263           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
264           break;                                              \
265       }                                                       \
266     }                                                         \
267     else if((__HANDLE__)->Instance == UART4)                  \
268     {                                                         \
269       switch(__HAL_RCC_GET_UART4_SOURCE())                    \
270       {                                                       \
271         case RCC_UART4CLKSOURCE_PCLK1:                        \
272           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
273           break;                                              \
274         case RCC_UART4CLKSOURCE_HSI:                          \
275           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
276           break;                                              \
277         case RCC_UART4CLKSOURCE_SYSCLK:                       \
278           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
279           break;                                              \
280         case RCC_UART4CLKSOURCE_LSE:                          \
281           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
282           break;                                              \
283         default:                                              \
284           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
285           break;                                              \
286       }                                                       \
287     }                                                         \
288     else if((__HANDLE__)->Instance == UART5)                  \
289     {                                                         \
290       switch(__HAL_RCC_GET_UART5_SOURCE())                    \
291       {                                                       \
292         case RCC_UART5CLKSOURCE_PCLK1:                        \
293           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
294           break;                                              \
295         case RCC_UART5CLKSOURCE_HSI:                          \
296           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
297           break;                                              \
298         case RCC_UART5CLKSOURCE_SYSCLK:                       \
299           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
300           break;                                              \
301         case RCC_UART5CLKSOURCE_LSE:                          \
302           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
303           break;                                              \
304         default:                                              \
305           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
306           break;                                              \
307       }                                                       \
308     }                                                         \
309     else if((__HANDLE__)->Instance == LPUART1)                \
310     {                                                         \
311       switch(__HAL_RCC_GET_LPUART1_SOURCE())                  \
312       {                                                       \
313         case RCC_LPUART1CLKSOURCE_PCLK1:                      \
314           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
315           break;                                              \
316         case RCC_LPUART1CLKSOURCE_HSI:                        \
317           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
318           break;                                              \
319         case RCC_LPUART1CLKSOURCE_SYSCLK:                     \
320           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
321           break;                                              \
322         case RCC_LPUART1CLKSOURCE_LSE:                        \
323           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
324           break;                                              \
325         default:                                              \
326           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
327           break;                                              \
328       }                                                       \
329     }                                                         \
330     else                                                      \
331     {                                                         \
332       (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;         \
333     }                                                         \
334   } while(0U)
335 
336 /** @brief  Report the UART mask to apply to retrieve the received data
337   *         according to the word length and to the parity bits activation.
338   * @note   If PCE = 1, the parity bit is not included in the data extracted
339   *         by the reception API().
340   *         This masking operation is not carried out in the case of
341   *         DMA transfers.
342   * @param  __HANDLE__ specifies the UART Handle.
343   * @retval None, the mask to apply to UART RDR register is stored in (__HANDLE__)->Mask field.
344   */
345 #define UART_MASK_COMPUTATION(__HANDLE__)                             \
346   do {                                                                \
347     if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_9B)          \
348     {                                                                 \
349       if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
350       {                                                               \
351         (__HANDLE__)->Mask = 0x01FFU ;                                \
352       }                                                               \
353       else                                                            \
354       {                                                               \
355         (__HANDLE__)->Mask = 0x00FFU ;                                \
356       }                                                               \
357     }                                                                 \
358     else if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_8B)     \
359     {                                                                 \
360       if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
361       {                                                               \
362         (__HANDLE__)->Mask = 0x00FFU ;                                \
363       }                                                               \
364       else                                                            \
365       {                                                               \
366         (__HANDLE__)->Mask = 0x007FU ;                                \
367       }                                                               \
368     }                                                                 \
369     else if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_7B)     \
370     {                                                                 \
371       if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
372       {                                                               \
373         (__HANDLE__)->Mask = 0x007FU ;                                \
374       }                                                               \
375       else                                                            \
376       {                                                               \
377         (__HANDLE__)->Mask = 0x003FU ;                                \
378       }                                                               \
379     }                                                                 \
380     else                                                              \
381     {                                                                 \
382       (__HANDLE__)->Mask = 0x0000U;                                   \
383     }                                                                 \
384   } while(0U)
385 
386 /**
387   * @brief Ensure that UART frame length is valid.
388   * @param __LENGTH__ UART frame length.
389   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
390   */
391 #define IS_UART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == UART_WORDLENGTH_7B) || \
392                                          ((__LENGTH__) == UART_WORDLENGTH_8B) || \
393                                          ((__LENGTH__) == UART_WORDLENGTH_9B))
394 
395 /**
396   * @brief Ensure that UART wake-up address length is valid.
397   * @param __ADDRESS__ UART wake-up address length.
398   * @retval SET (__ADDRESS__ is valid) or RESET (__ADDRESS__ is invalid)
399   */
400 #define IS_UART_ADDRESSLENGTH_DETECT(__ADDRESS__) (((__ADDRESS__) == UART_ADDRESS_DETECT_4B) || \
401                                                    ((__ADDRESS__) == UART_ADDRESS_DETECT_7B))
402 
403 /**
404   * @brief Ensure that UART TXFIFO threshold level is valid.
405   * @param __THRESHOLD__ UART TXFIFO threshold level.
406   * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid)
407   */
408 #define IS_UART_TXFIFO_THRESHOLD(__THRESHOLD__) (((__THRESHOLD__) == UART_TXFIFO_THRESHOLD_1_8) || \
409                                                  ((__THRESHOLD__) == UART_TXFIFO_THRESHOLD_1_4) || \
410                                                  ((__THRESHOLD__) == UART_TXFIFO_THRESHOLD_1_2) || \
411                                                  ((__THRESHOLD__) == UART_TXFIFO_THRESHOLD_3_4) || \
412                                                  ((__THRESHOLD__) == UART_TXFIFO_THRESHOLD_7_8) || \
413                                                  ((__THRESHOLD__) == UART_TXFIFO_THRESHOLD_8_8))
414 
415 /**
416   * @brief Ensure that UART RXFIFO threshold level is valid.
417   * @param __THRESHOLD__ UART RXFIFO threshold level.
418   * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid)
419   */
420 #define IS_UART_RXFIFO_THRESHOLD(__THRESHOLD__) (((__THRESHOLD__) == UART_RXFIFO_THRESHOLD_1_8) || \
421                                                  ((__THRESHOLD__) == UART_RXFIFO_THRESHOLD_1_4) || \
422                                                  ((__THRESHOLD__) == UART_RXFIFO_THRESHOLD_1_2) || \
423                                                  ((__THRESHOLD__) == UART_RXFIFO_THRESHOLD_3_4) || \
424                                                  ((__THRESHOLD__) == UART_RXFIFO_THRESHOLD_7_8) || \
425                                                  ((__THRESHOLD__) == UART_RXFIFO_THRESHOLD_8_8))
426 
427 /**
428   * @}
429   */
430 
431 /* Private functions ---------------------------------------------------------*/
432 
433 /**
434   * @}
435   */
436 
437 /**
438   * @}
439   */
440 
441 #ifdef __cplusplus
442 }
443 #endif
444 
445 #endif /* STM32L5xx_HAL_UART_EX_H */
446 
447