1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_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 STM32F3xx_HAL_UART_EX_H
21 #define STM32F3xx_HAL_UART_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32f3xx_hal_def.h"
29 
30 /** @addtogroup STM32F3xx_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 #if defined(USART_CR1_M1)
72 #define UART_WORDLENGTH_7B          USART_CR1_M1   /*!< 7-bit long UART frame */
73 #endif /* USART_CR1_M1 */
74 #define UART_WORDLENGTH_8B          0x00000000U    /*!< 8-bit long UART frame */
75 #if defined (USART_CR1_M0)
76 #define UART_WORDLENGTH_9B          USART_CR1_M0   /*!< 9-bit long UART frame */
77 #else
78 #define UART_WORDLENGTH_9B          USART_CR1_M   /*!< 9-bit long UART frame */
79 #endif /* USART_CR1_M0 */
80 /**
81   * @}
82   */
83 
84 /** @defgroup UARTEx_WakeUp_Address_Length UARTEx WakeUp Address Length
85   * @{
86   */
87 #define UART_ADDRESS_DETECT_4B      0x00000000U      /*!< 4-bit long wake-up address */
88 #define UART_ADDRESS_DETECT_7B      USART_CR2_ADDM7  /*!< 7-bit long wake-up address */
89 /**
90   * @}
91   */
92 
93 /**
94   * @}
95   */
96 
97 /* Exported macros -----------------------------------------------------------*/
98 /* Exported functions --------------------------------------------------------*/
99 /** @addtogroup UARTEx_Exported_Functions
100   * @{
101   */
102 
103 /** @addtogroup UARTEx_Exported_Functions_Group1
104   * @{
105   */
106 
107 /* Initialization and de-initialization functions  ****************************/
108 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,
109                                    uint32_t DeassertionTime);
110 
111 /**
112   * @}
113   */
114 
115 /** @addtogroup UARTEx_Exported_Functions_Group2
116   * @{
117   */
118 
119 void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart);
120 
121 /**
122   * @}
123   */
124 
125 /** @addtogroup UARTEx_Exported_Functions_Group3
126   * @{
127   */
128 
129 /* Peripheral Control functions  **********************************************/
130 HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
131 HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart);
132 HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart);
133 
134 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength);
135 
136 
137 HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen,
138                                            uint32_t Timeout);
139 HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
140 HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
141 
142 HAL_UART_RxEventTypeTypeDef HAL_UARTEx_GetRxEventType(const UART_HandleTypeDef *huart);
143 
144 
145 /**
146   * @}
147   */
148 
149 /**
150   * @}
151   */
152 
153 /* Private macros ------------------------------------------------------------*/
154 /** @defgroup UARTEx_Private_Macros UARTEx Private Macros
155   * @{
156   */
157 
158 /** @brief  Report the UART clock source.
159   * @param  __HANDLE__ specifies the UART Handle.
160   * @param  __CLOCKSOURCE__ output variable.
161   * @retval UART clocking source, written in __CLOCKSOURCE__.
162   */
163 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || defined(STM32F302xC) \
164  || defined(STM32F303xC) || defined(STM32F358xx)
165 #define UART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
166   do {                                                        \
167     if((__HANDLE__)->Instance == USART1)                      \
168     {                                                         \
169       switch(__HAL_RCC_GET_USART1_SOURCE())                   \
170       {                                                       \
171         case RCC_USART1CLKSOURCE_PCLK2:                       \
172           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK2;         \
173           break;                                              \
174         case RCC_USART1CLKSOURCE_HSI:                         \
175           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
176           break;                                              \
177         case RCC_USART1CLKSOURCE_SYSCLK:                      \
178           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
179           break;                                              \
180         case RCC_USART1CLKSOURCE_LSE:                         \
181           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
182           break;                                              \
183         default:                                              \
184           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
185           break;                                              \
186       }                                                       \
187     }                                                         \
188     else if((__HANDLE__)->Instance == USART2)                 \
189     {                                                         \
190       switch(__HAL_RCC_GET_USART2_SOURCE())                   \
191       {                                                       \
192         case RCC_USART2CLKSOURCE_PCLK1:                       \
193           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
194           break;                                              \
195         case RCC_USART2CLKSOURCE_HSI:                         \
196           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
197           break;                                              \
198         case RCC_USART2CLKSOURCE_SYSCLK:                      \
199           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
200           break;                                              \
201         case RCC_USART2CLKSOURCE_LSE:                         \
202           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
203           break;                                              \
204         default:                                              \
205           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
206           break;                                              \
207       }                                                       \
208     }                                                         \
209     else if((__HANDLE__)->Instance == USART3)                 \
210     {                                                         \
211       switch(__HAL_RCC_GET_USART3_SOURCE())                   \
212       {                                                       \
213         case RCC_USART3CLKSOURCE_PCLK1:                       \
214           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
215           break;                                              \
216         case RCC_USART3CLKSOURCE_HSI:                         \
217           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
218           break;                                              \
219         case RCC_USART3CLKSOURCE_SYSCLK:                      \
220           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
221           break;                                              \
222         case RCC_USART3CLKSOURCE_LSE:                         \
223           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
224           break;                                              \
225         default:                                              \
226           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
227           break;                                              \
228       }                                                       \
229     }                                                         \
230     else if((__HANDLE__)->Instance == UART4)                  \
231     {                                                         \
232       switch(__HAL_RCC_GET_UART4_SOURCE())                    \
233       {                                                       \
234         case RCC_UART4CLKSOURCE_PCLK1:                        \
235           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
236           break;                                              \
237         case RCC_UART4CLKSOURCE_HSI:                          \
238           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
239           break;                                              \
240         case RCC_UART4CLKSOURCE_SYSCLK:                       \
241           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
242           break;                                              \
243         case RCC_UART4CLKSOURCE_LSE:                          \
244           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
245           break;                                              \
246         default:                                              \
247           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
248           break;                                              \
249       }                                                       \
250     }                                                         \
251     else if ((__HANDLE__)->Instance == UART5)                 \
252     {                                                         \
253       switch(__HAL_RCC_GET_UART5_SOURCE())                    \
254       {                                                       \
255         case RCC_UART5CLKSOURCE_PCLK1:                        \
256           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
257           break;                                              \
258         case RCC_UART5CLKSOURCE_HSI:                          \
259           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
260           break;                                              \
261         case RCC_UART5CLKSOURCE_SYSCLK:                       \
262           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
263           break;                                              \
264         case RCC_UART5CLKSOURCE_LSE:                          \
265           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
266           break;                                              \
267         default:                                              \
268           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
269           break;                                              \
270       }                                                       \
271     }                                                         \
272     else                                                      \
273     {                                                         \
274       (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;         \
275     }                                                         \
276   } while(0U)
277 #elif defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || defined(STM32F301x8) \
278   || defined(STM32F302x8) || defined(STM32F318xx)
279 #define UART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
280   do {                                                        \
281     if((__HANDLE__)->Instance == USART1)                      \
282     {                                                         \
283       switch(__HAL_RCC_GET_USART1_SOURCE())                   \
284       {                                                       \
285         case RCC_USART1CLKSOURCE_PCLK1:                       \
286           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
287           break;                                              \
288         case RCC_USART1CLKSOURCE_HSI:                         \
289           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
290           break;                                              \
291         case RCC_USART1CLKSOURCE_SYSCLK:                      \
292           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
293           break;                                              \
294         case RCC_USART1CLKSOURCE_LSE:                         \
295           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
296           break;                                              \
297         default:                                              \
298           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
299           break;                                              \
300       }                                                       \
301     }                                                         \
302     else if((__HANDLE__)->Instance == USART2)                 \
303     {                                                         \
304       (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;             \
305     }                                                         \
306     else if((__HANDLE__)->Instance == USART3)                 \
307     {                                                         \
308       (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;             \
309     }                                                         \
310     else                                                      \
311     {                                                         \
312       (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;         \
313     }                                                         \
314   } while(0U)
315 #else
316 #define UART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
317   do {                                                        \
318     if((__HANDLE__)->Instance == USART1)                      \
319     {                                                         \
320       switch(__HAL_RCC_GET_USART1_SOURCE())                   \
321       {                                                       \
322         case RCC_USART1CLKSOURCE_PCLK2:                       \
323           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK2;         \
324           break;                                              \
325         case RCC_USART1CLKSOURCE_HSI:                         \
326           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
327           break;                                              \
328         case RCC_USART1CLKSOURCE_SYSCLK:                      \
329           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
330           break;                                              \
331         case RCC_USART1CLKSOURCE_LSE:                         \
332           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
333           break;                                              \
334         default:                                              \
335           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
336           break;                                              \
337       }                                                       \
338     }                                                         \
339     else if((__HANDLE__)->Instance == USART2)                 \
340     {                                                         \
341       switch(__HAL_RCC_GET_USART2_SOURCE())                   \
342       {                                                       \
343         case RCC_USART2CLKSOURCE_PCLK1:                       \
344           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
345           break;                                              \
346         case RCC_USART2CLKSOURCE_HSI:                         \
347           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
348           break;                                              \
349         case RCC_USART2CLKSOURCE_SYSCLK:                      \
350           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
351           break;                                              \
352         case RCC_USART2CLKSOURCE_LSE:                         \
353           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
354           break;                                              \
355         default:                                              \
356           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
357           break;                                              \
358       }                                                       \
359     }                                                         \
360     else if((__HANDLE__)->Instance == USART3)                 \
361     {                                                         \
362       switch(__HAL_RCC_GET_USART3_SOURCE())                   \
363       {                                                       \
364         case RCC_USART3CLKSOURCE_PCLK1:                       \
365           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_PCLK1;         \
366           break;                                              \
367         case RCC_USART3CLKSOURCE_HSI:                         \
368           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_HSI;           \
369           break;                                              \
370         case RCC_USART3CLKSOURCE_SYSCLK:                      \
371           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_SYSCLK;        \
372           break;                                              \
373         case RCC_USART3CLKSOURCE_LSE:                         \
374           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_LSE;           \
375           break;                                              \
376         default:                                              \
377           (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;     \
378           break;                                              \
379       }                                                       \
380     }                                                         \
381     else                                                      \
382     {                                                         \
383       (__CLOCKSOURCE__) = UART_CLOCKSOURCE_UNDEFINED;         \
384     }                                                         \
385   } while(0U)
386 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || STM32F302xC || STM32F303xC || STM32F358xx  */
387 
388 /** @brief  Report the UART mask to apply to retrieve the received data
389   *         according to the word length and to the parity bits activation.
390   * @note   If PCE = 1, the parity bit is not included in the data extracted
391   *         by the reception API().
392   *         This masking operation is not carried out in the case of
393   *         DMA transfers.
394   * @param  __HANDLE__ specifies the UART Handle.
395   * @retval None, the mask to apply to UART RDR register is stored in (__HANDLE__)->Mask field.
396   */
397 #if defined (USART_CR1_M1)
398 #define UART_MASK_COMPUTATION(__HANDLE__)                             \
399   do {                                                                \
400     if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_9B)          \
401     {                                                                 \
402       if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
403       {                                                               \
404         (__HANDLE__)->Mask = 0x01FFU ;                                \
405       }                                                               \
406       else                                                            \
407       {                                                               \
408         (__HANDLE__)->Mask = 0x00FFU ;                                \
409       }                                                               \
410     }                                                                 \
411     else if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_8B)     \
412     {                                                                 \
413       if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
414       {                                                               \
415         (__HANDLE__)->Mask = 0x00FFU ;                                \
416       }                                                               \
417       else                                                            \
418       {                                                               \
419         (__HANDLE__)->Mask = 0x007FU ;                                \
420       }                                                               \
421     }                                                                 \
422     else if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_7B)     \
423     {                                                                 \
424       if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
425       {                                                               \
426         (__HANDLE__)->Mask = 0x007FU ;                                \
427       }                                                               \
428       else                                                            \
429       {                                                               \
430         (__HANDLE__)->Mask = 0x003FU ;                                \
431       }                                                               \
432     }                                                                 \
433     else                                                              \
434     {                                                                 \
435       (__HANDLE__)->Mask = 0x0000U;                                   \
436     }                                                                 \
437   } while(0U)
438 
439 #else
440 #define UART_MASK_COMPUTATION(__HANDLE__)                             \
441   do {                                                                \
442     if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_9B)          \
443     {                                                                 \
444       if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
445       {                                                               \
446         (__HANDLE__)->Mask = 0x01FFU ;                                \
447       }                                                               \
448       else                                                            \
449       {                                                               \
450         (__HANDLE__)->Mask = 0x00FFU ;                                \
451       }                                                               \
452     }                                                                 \
453     else if ((__HANDLE__)->Init.WordLength == UART_WORDLENGTH_8B)     \
454     {                                                                 \
455       if ((__HANDLE__)->Init.Parity == UART_PARITY_NONE)              \
456       {                                                               \
457         (__HANDLE__)->Mask = 0x00FFU ;                                \
458       }                                                               \
459       else                                                            \
460       {                                                               \
461         (__HANDLE__)->Mask = 0x007FU ;                                \
462       }                                                               \
463     }                                                                 \
464     else                                                              \
465     {                                                                 \
466       (__HANDLE__)->Mask = 0x0000U;                                   \
467     }                                                                 \
468   } while(0U)
469 
470 #endif /* USART_CR1_M1 */
471 
472 /**
473   * @brief Ensure that UART frame length is valid.
474   * @param __LENGTH__ UART frame length.
475   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
476   */
477 #if defined (USART_CR1_M1)
478 #define IS_UART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == UART_WORDLENGTH_7B) || \
479                                          ((__LENGTH__) == UART_WORDLENGTH_8B) || \
480                                          ((__LENGTH__) == UART_WORDLENGTH_9B))
481 #else
482 #define IS_UART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == UART_WORDLENGTH_8B) || \
483                                          ((__LENGTH__) == UART_WORDLENGTH_9B))
484 #endif /* USART_CR1_M1 */
485 
486 /**
487   * @brief Ensure that UART wake-up address length is valid.
488   * @param __ADDRESS__ UART wake-up address length.
489   * @retval SET (__ADDRESS__ is valid) or RESET (__ADDRESS__ is invalid)
490   */
491 #define IS_UART_ADDRESSLENGTH_DETECT(__ADDRESS__) (((__ADDRESS__) == UART_ADDRESS_DETECT_4B) || \
492                                                    ((__ADDRESS__) == UART_ADDRESS_DETECT_7B))
493 
494 /**
495   * @}
496   */
497 
498 /* Private functions ---------------------------------------------------------*/
499 
500 /**
501   * @}
502   */
503 
504 /**
505   * @}
506   */
507 
508 #ifdef __cplusplus
509 }
510 #endif
511 
512 #endif /* STM32F3xx_HAL_UART_EX_H */
513 
514