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