1 /**
2   ******************************************************************************
3   * @file    stm32f7xx_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) 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_IRDA_EX_H
21 #define STM32F7xx_HAL_IRDA_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 /** @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 #define IRDA_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
73   do {                                                        \
74     if((__HANDLE__)->Instance == USART1)                      \
75     {                                                         \
76       switch(__HAL_RCC_GET_USART1_SOURCE())                   \
77       {                                                       \
78         case RCC_USART1CLKSOURCE_PCLK2:                       \
79           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK2;         \
80           break;                                              \
81         case RCC_USART1CLKSOURCE_HSI:                         \
82           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
83           break;                                              \
84         case RCC_USART1CLKSOURCE_SYSCLK:                      \
85           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
86           break;                                              \
87         case RCC_USART1CLKSOURCE_LSE:                         \
88           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
89           break;                                              \
90         default:                                              \
91           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
92           break;                                              \
93       }                                                       \
94     }                                                         \
95     else if((__HANDLE__)->Instance == USART2)                 \
96     {                                                         \
97       switch(__HAL_RCC_GET_USART2_SOURCE())                   \
98       {                                                       \
99         case RCC_USART2CLKSOURCE_PCLK1:                       \
100           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
101           break;                                              \
102         case RCC_USART2CLKSOURCE_HSI:                         \
103           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
104           break;                                              \
105         case RCC_USART2CLKSOURCE_SYSCLK:                      \
106           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
107           break;                                              \
108         case RCC_USART2CLKSOURCE_LSE:                         \
109           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
110           break;                                              \
111         default:                                              \
112           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
113           break;                                              \
114       }                                                       \
115     }                                                         \
116     else if((__HANDLE__)->Instance == USART3)                 \
117     {                                                         \
118       switch(__HAL_RCC_GET_USART3_SOURCE())                   \
119       {                                                       \
120         case RCC_USART3CLKSOURCE_PCLK1:                       \
121           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
122           break;                                              \
123         case RCC_USART3CLKSOURCE_HSI:                         \
124           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
125           break;                                              \
126         case RCC_USART3CLKSOURCE_SYSCLK:                      \
127           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
128           break;                                              \
129         case RCC_USART3CLKSOURCE_LSE:                         \
130           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
131           break;                                              \
132         default:                                              \
133           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
134           break;                                              \
135       }                                                       \
136     }                                                         \
137     else if((__HANDLE__)->Instance == UART4)                  \
138     {                                                         \
139       switch(__HAL_RCC_GET_UART4_SOURCE())                    \
140       {                                                       \
141         case RCC_UART4CLKSOURCE_PCLK1:                        \
142           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
143           break;                                              \
144         case RCC_UART4CLKSOURCE_HSI:                          \
145           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
146           break;                                              \
147         case RCC_UART4CLKSOURCE_SYSCLK:                       \
148           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
149           break;                                              \
150         case RCC_UART4CLKSOURCE_LSE:                          \
151           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
152           break;                                              \
153         default:                                              \
154           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
155           break;                                              \
156       }                                                       \
157     }                                                         \
158     else if ((__HANDLE__)->Instance == UART5)                 \
159     {                                                         \
160       switch(__HAL_RCC_GET_UART5_SOURCE())                    \
161       {                                                       \
162         case RCC_UART5CLKSOURCE_PCLK1:                        \
163           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
164           break;                                              \
165         case RCC_UART5CLKSOURCE_HSI:                          \
166           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
167           break;                                              \
168         case RCC_UART5CLKSOURCE_SYSCLK:                       \
169           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
170           break;                                              \
171         case RCC_UART5CLKSOURCE_LSE:                          \
172           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
173           break;                                              \
174         default:                                              \
175           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
176           break;                                              \
177       }                                                       \
178     }                                                         \
179     else if((__HANDLE__)->Instance == USART6)                 \
180     {                                                         \
181       switch(__HAL_RCC_GET_USART6_SOURCE())                   \
182       {                                                       \
183         case RCC_USART6CLKSOURCE_PCLK2:                       \
184           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK2;         \
185           break;                                              \
186         case RCC_USART6CLKSOURCE_HSI:                         \
187           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
188           break;                                              \
189         case RCC_USART6CLKSOURCE_SYSCLK:                      \
190           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
191           break;                                              \
192         case RCC_USART6CLKSOURCE_LSE:                         \
193           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
194           break;                                              \
195         default:                                              \
196           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
197           break;                                              \
198       }                                                       \
199     }                                                         \
200     else if ((__HANDLE__)->Instance == UART7)                 \
201     {                                                         \
202       switch(__HAL_RCC_GET_UART7_SOURCE())                    \
203       {                                                       \
204         case RCC_UART7CLKSOURCE_PCLK1:                        \
205           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
206           break;                                              \
207         case RCC_UART7CLKSOURCE_HSI:                          \
208           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
209           break;                                              \
210         case RCC_UART7CLKSOURCE_SYSCLK:                       \
211           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
212           break;                                              \
213         case RCC_UART7CLKSOURCE_LSE:                          \
214           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
215           break;                                              \
216         default:                                              \
217           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
218           break;                                              \
219       }                                                       \
220     }                                                         \
221     else if ((__HANDLE__)->Instance == UART8)                 \
222     {                                                         \
223       switch(__HAL_RCC_GET_UART8_SOURCE())                    \
224       {                                                       \
225         case RCC_UART8CLKSOURCE_PCLK1:                        \
226           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
227           break;                                              \
228         case RCC_UART8CLKSOURCE_HSI:                          \
229           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
230           break;                                              \
231         case RCC_UART8CLKSOURCE_SYSCLK:                       \
232           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
233           break;                                              \
234         case RCC_UART8CLKSOURCE_LSE:                          \
235           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
236           break;                                              \
237         default:                                              \
238           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
239           break;                                              \
240       }                                                       \
241     }                                                         \
242     else                                                      \
243     {                                                         \
244       (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;         \
245     }                                                         \
246   } while(0U)
247 
248 /** @brief  Compute the mask to apply to retrieve the received data
249   *         according to the word length and to the parity bits activation.
250   * @param  __HANDLE__ specifies the IRDA Handle.
251   * @retval None, the mask to apply to the associated UART RDR register is stored in (__HANDLE__)->Mask field.
252   */
253 #define IRDA_MASK_COMPUTATION(__HANDLE__)                             \
254   do {                                                                \
255     if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_9B)          \
256     {                                                                 \
257       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
258       {                                                               \
259         (__HANDLE__)->Mask = 0x01FFU ;                                \
260       }                                                               \
261       else                                                            \
262       {                                                               \
263         (__HANDLE__)->Mask = 0x00FFU ;                                \
264       }                                                               \
265     }                                                                 \
266     else if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_8B)     \
267     {                                                                 \
268       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
269       {                                                               \
270         (__HANDLE__)->Mask = 0x00FFU ;                                \
271       }                                                               \
272       else                                                            \
273       {                                                               \
274         (__HANDLE__)->Mask = 0x007FU ;                                \
275       }                                                               \
276     }                                                                 \
277     else if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_7B)     \
278     {                                                                 \
279       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
280       {                                                               \
281         (__HANDLE__)->Mask = 0x007FU ;                                \
282       }                                                               \
283       else                                                            \
284       {                                                               \
285         (__HANDLE__)->Mask = 0x003FU ;                                \
286       }                                                               \
287     }                                                                 \
288     else                                                              \
289     {                                                                 \
290       (__HANDLE__)->Mask = 0x0000U;                                   \
291     }                                                                 \
292   } while(0U)
293 
294 /** @brief Ensure that IRDA frame length is valid.
295   * @param __LENGTH__ IRDA frame length.
296   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
297   */
298 #define IS_IRDA_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == IRDA_WORDLENGTH_7B) || \
299                                          ((__LENGTH__) == IRDA_WORDLENGTH_8B) || \
300                                          ((__LENGTH__) == IRDA_WORDLENGTH_9B))
301 /**
302   * @}
303   */
304 
305 /* Exported functions --------------------------------------------------------*/
306 
307 /**
308   * @}
309   */
310 
311 /**
312   * @}
313   */
314 
315 #ifdef __cplusplus
316 }
317 #endif
318 
319 #endif /* STM32F7xx_HAL_IRDA_EX_H */
320 
321