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