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