1 /**
2   ******************************************************************************
3   * @file    stm32g4xx_hal_irda_ex.h
4   * @author  MCD Application Team
5   * @brief   Header file of IRDA 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 STM32G4xx_HAL_IRDA_EX_H
21 #define STM32G4xx_HAL_IRDA_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32g4xx_hal_def.h"
29 
30 /** @addtogroup STM32G4xx_HAL_Driver
31   * @{
32   */
33 
34 /** @defgroup IRDAEx IRDAEx
35   * @brief IRDA Extended HAL module driver
36   * @{
37   */
38 
39 /* Exported types ------------------------------------------------------------*/
40 /* Exported constants --------------------------------------------------------*/
41 /** @defgroup IRDAEx_Extended_Exported_Constants IRDAEx Extended Exported Constants
42   * @{
43   */
44 
45 /** @defgroup IRDAEx_Word_Length IRDAEx Word Length
46   * @{
47   */
48 #define IRDA_WORDLENGTH_7B                  USART_CR1_M1   /*!< 7-bit long frame */
49 #define IRDA_WORDLENGTH_8B                  0x00000000U    /*!< 8-bit long frame */
50 #define IRDA_WORDLENGTH_9B                  USART_CR1_M0   /*!< 9-bit long frame */
51 /**
52   * @}
53   */
54 
55 /**
56   * @}
57   */
58 
59 /* Exported macros -----------------------------------------------------------*/
60 
61 /* Private macros ------------------------------------------------------------*/
62 
63 /** @defgroup IRDAEx_Private_Macros IRDAEx Private Macros
64   * @{
65   */
66 
67 /** @brief  Report the IRDA clock source.
68   * @param  __HANDLE__ specifies the IRDA Handle.
69   * @param  __CLOCKSOURCE__ output variable.
70   * @retval IRDA clocking source, written in __CLOCKSOURCE__.
71   */
72 #if defined(UART5)
73 #define IRDA_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)        \
74   do {                                                         \
75     if((__HANDLE__)->Instance == USART1)                       \
76     {                                                          \
77       switch(__HAL_RCC_GET_USART1_SOURCE())                    \
78       {                                                        \
79         case RCC_USART1CLKSOURCE_PCLK2:                        \
80           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK2;          \
81           break;                                               \
82         case RCC_USART1CLKSOURCE_HSI:                          \
83           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
84           break;                                               \
85         case RCC_USART1CLKSOURCE_SYSCLK:                       \
86           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
87           break;                                               \
88         case RCC_USART1CLKSOURCE_LSE:                          \
89           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
90           break;                                               \
91         default:                                               \
92           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
93           break;                                               \
94       }                                                        \
95     }                                                          \
96     else if((__HANDLE__)->Instance == USART2)                  \
97     {                                                          \
98       switch(__HAL_RCC_GET_USART2_SOURCE())                    \
99       {                                                        \
100         case RCC_USART2CLKSOURCE_PCLK1:                        \
101           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;          \
102           break;                                               \
103         case RCC_USART2CLKSOURCE_HSI:                          \
104           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
105           break;                                               \
106         case RCC_USART2CLKSOURCE_SYSCLK:                       \
107           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
108           break;                                               \
109         case RCC_USART2CLKSOURCE_LSE:                          \
110           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
111           break;                                               \
112         default:                                               \
113           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
114           break;                                               \
115       }                                                        \
116     }                                                          \
117     else if((__HANDLE__)->Instance == USART3)                  \
118     {                                                          \
119       switch(__HAL_RCC_GET_USART3_SOURCE())                    \
120       {                                                        \
121         case RCC_USART3CLKSOURCE_PCLK1:                        \
122           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;          \
123           break;                                               \
124         case RCC_USART3CLKSOURCE_HSI:                          \
125           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
126           break;                                               \
127         case RCC_USART3CLKSOURCE_SYSCLK:                       \
128           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
129           break;                                               \
130         case RCC_USART3CLKSOURCE_LSE:                          \
131           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
132           break;                                               \
133         default:                                               \
134           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
135           break;                                               \
136       }                                                        \
137     }                                                          \
138     else if((__HANDLE__)->Instance == UART4)                   \
139     {                                                          \
140       switch(__HAL_RCC_GET_UART4_SOURCE())                     \
141       {                                                        \
142         case RCC_UART4CLKSOURCE_PCLK1:                         \
143           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;          \
144           break;                                               \
145         case RCC_UART4CLKSOURCE_HSI:                           \
146           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
147           break;                                               \
148         case RCC_UART4CLKSOURCE_SYSCLK:                        \
149           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
150           break;                                               \
151         case RCC_UART4CLKSOURCE_LSE:                           \
152           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
153           break;                                               \
154         default:                                               \
155           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
156           break;                                               \
157       }                                                        \
158     }                                                          \
159     else if((__HANDLE__)->Instance == UART5)                   \
160     {                                                          \
161       switch(__HAL_RCC_GET_UART5_SOURCE())                     \
162       {                                                        \
163         case RCC_UART5CLKSOURCE_PCLK1:                         \
164           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;          \
165           break;                                               \
166         case RCC_UART5CLKSOURCE_HSI:                           \
167           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
168           break;                                               \
169         case RCC_UART5CLKSOURCE_SYSCLK:                        \
170           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
171           break;                                               \
172         case RCC_UART5CLKSOURCE_LSE:                           \
173           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
174           break;                                               \
175         default:                                               \
176           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
177           break;                                               \
178       }                                                        \
179     }                                                          \
180     else                                                       \
181     {                                                          \
182       (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;          \
183     }                                                          \
184   } while(0U)
185 #elif defined(UART4)
186 #define IRDA_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)        \
187   do {                                                         \
188     if((__HANDLE__)->Instance == USART1)                       \
189     {                                                          \
190       switch(__HAL_RCC_GET_USART1_SOURCE())                    \
191       {                                                        \
192         case RCC_USART1CLKSOURCE_PCLK2:                        \
193           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK2;          \
194           break;                                               \
195         case RCC_USART1CLKSOURCE_HSI:                          \
196           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
197           break;                                               \
198         case RCC_USART1CLKSOURCE_SYSCLK:                       \
199           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
200           break;                                               \
201         case RCC_USART1CLKSOURCE_LSE:                          \
202           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
203           break;                                               \
204         default:                                               \
205           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
206           break;                                               \
207       }                                                        \
208     }                                                          \
209     else if((__HANDLE__)->Instance == USART2)                  \
210     {                                                          \
211       switch(__HAL_RCC_GET_USART2_SOURCE())                    \
212       {                                                        \
213         case RCC_USART2CLKSOURCE_PCLK1:                        \
214           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;          \
215           break;                                               \
216         case RCC_USART2CLKSOURCE_HSI:                          \
217           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
218           break;                                               \
219         case RCC_USART2CLKSOURCE_SYSCLK:                       \
220           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
221           break;                                               \
222         case RCC_USART2CLKSOURCE_LSE:                          \
223           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
224           break;                                               \
225         default:                                               \
226           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
227           break;                                               \
228       }                                                        \
229     }                                                          \
230     else if((__HANDLE__)->Instance == USART3)                  \
231     {                                                          \
232       switch(__HAL_RCC_GET_USART3_SOURCE())                    \
233       {                                                        \
234         case RCC_USART3CLKSOURCE_PCLK1:                        \
235           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;          \
236           break;                                               \
237         case RCC_USART3CLKSOURCE_HSI:                          \
238           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
239           break;                                               \
240         case RCC_USART3CLKSOURCE_SYSCLK:                       \
241           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
242           break;                                               \
243         case RCC_USART3CLKSOURCE_LSE:                          \
244           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
245           break;                                               \
246         default:                                               \
247           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
248           break;                                               \
249       }                                                        \
250     }                                                          \
251     else if((__HANDLE__)->Instance == UART4)                   \
252     {                                                          \
253       switch(__HAL_RCC_GET_UART4_SOURCE())                     \
254       {                                                        \
255         case RCC_UART4CLKSOURCE_PCLK1:                         \
256           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;          \
257           break;                                               \
258         case RCC_UART4CLKSOURCE_HSI:                           \
259           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
260           break;                                               \
261         case RCC_UART4CLKSOURCE_SYSCLK:                        \
262           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
263           break;                                               \
264         case RCC_UART4CLKSOURCE_LSE:                           \
265           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
266           break;                                               \
267         default:                                               \
268           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
269           break;                                               \
270       }                                                        \
271     }                                                          \
272     else                                                       \
273     {                                                          \
274       (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;          \
275     }                                                          \
276   } while(0U)
277 #else
278 #define IRDA_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)        \
279   do {                                                         \
280     if((__HANDLE__)->Instance == USART1)                       \
281     {                                                          \
282       switch(__HAL_RCC_GET_USART1_SOURCE())                    \
283       {                                                        \
284         case RCC_USART1CLKSOURCE_PCLK2:                        \
285           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK2;          \
286           break;                                               \
287         case RCC_USART1CLKSOURCE_HSI:                          \
288           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
289           break;                                               \
290         case RCC_USART1CLKSOURCE_SYSCLK:                       \
291           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
292           break;                                               \
293         case RCC_USART1CLKSOURCE_LSE:                          \
294           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
295           break;                                               \
296         default:                                               \
297           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
298           break;                                               \
299       }                                                        \
300     }                                                          \
301     else if((__HANDLE__)->Instance == USART2)                  \
302     {                                                          \
303       switch(__HAL_RCC_GET_USART2_SOURCE())                    \
304       {                                                        \
305         case RCC_USART2CLKSOURCE_PCLK1:                        \
306           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;          \
307           break;                                               \
308         case RCC_USART2CLKSOURCE_HSI:                          \
309           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
310           break;                                               \
311         case RCC_USART2CLKSOURCE_SYSCLK:                       \
312           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
313           break;                                               \
314         case RCC_USART2CLKSOURCE_LSE:                          \
315           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
316           break;                                               \
317         default:                                               \
318           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
319           break;                                               \
320       }                                                        \
321     }                                                          \
322     else if((__HANDLE__)->Instance == USART3)                  \
323     {                                                          \
324       switch(__HAL_RCC_GET_USART3_SOURCE())                    \
325       {                                                        \
326         case RCC_USART3CLKSOURCE_PCLK1:                        \
327           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;          \
328           break;                                               \
329         case RCC_USART3CLKSOURCE_HSI:                          \
330           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
331           break;                                               \
332         case RCC_USART3CLKSOURCE_SYSCLK:                       \
333           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
334           break;                                               \
335         case RCC_USART3CLKSOURCE_LSE:                          \
336           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
337           break;                                               \
338         default:                                               \
339           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
340           break;                                               \
341       }                                                        \
342     }                                                          \
343     else                                                       \
344     {                                                          \
345       (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;          \
346     }                                                          \
347   } while(0U)
348 #endif /* UART5 */
349 
350 /** @brief  Compute the mask to apply to retrieve the received data
351   *         according to the word length and to the parity bits activation.
352   * @param  __HANDLE__ specifies the IRDA Handle.
353   * @retval None, the mask to apply to the associated UART RDR register is stored in (__HANDLE__)->Mask field.
354   */
355 #define IRDA_MASK_COMPUTATION(__HANDLE__)                             \
356   do {                                                                \
357     if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_9B)          \
358     {                                                                 \
359       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
360       {                                                               \
361         (__HANDLE__)->Mask = 0x01FFU ;                                \
362       }                                                               \
363       else                                                            \
364       {                                                               \
365         (__HANDLE__)->Mask = 0x00FFU ;                                \
366       }                                                               \
367     }                                                                 \
368     else if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_8B)     \
369     {                                                                 \
370       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
371       {                                                               \
372         (__HANDLE__)->Mask = 0x00FFU ;                                \
373       }                                                               \
374       else                                                            \
375       {                                                               \
376         (__HANDLE__)->Mask = 0x007FU ;                                \
377       }                                                               \
378     }                                                                 \
379     else if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_7B)     \
380     {                                                                 \
381       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
382       {                                                               \
383         (__HANDLE__)->Mask = 0x007FU ;                                \
384       }                                                               \
385       else                                                            \
386       {                                                               \
387         (__HANDLE__)->Mask = 0x003FU ;                                \
388       }                                                               \
389     }                                                                 \
390     else                                                              \
391     {                                                                 \
392       (__HANDLE__)->Mask = 0x0000U;                                   \
393     }                                                                 \
394   } while(0U)
395 
396 /** @brief Ensure that IRDA frame length is valid.
397   * @param __LENGTH__ IRDA frame length.
398   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
399   */
400 #define IS_IRDA_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == IRDA_WORDLENGTH_7B) || \
401                                          ((__LENGTH__) == IRDA_WORDLENGTH_8B) || \
402                                          ((__LENGTH__) == IRDA_WORDLENGTH_9B))
403 /**
404   * @}
405   */
406 
407 /* Exported functions --------------------------------------------------------*/
408 
409 /**
410   * @}
411   */
412 
413 /**
414   * @}
415   */
416 
417 #ifdef __cplusplus
418 }
419 #endif
420 
421 #endif /* STM32G4xx_HAL_IRDA_EX_H */
422 
423