1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_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 STM32L0xx_HAL_IRDA_EX_H
21 #define STM32L0xx_HAL_IRDA_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #if !defined (STM32L010x4) && !defined (STM32L010x6) && !defined (STM32L010x8)
28 /* Includes ------------------------------------------------------------------*/
29 #include "stm32l0xx_hal_def.h"
30 
31 /** @addtogroup STM32L0xx_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 #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 /**
53   * @}
54   */
55 
56 /**
57   * @}
58   */
59 
60 /* Exported macros -----------------------------------------------------------*/
61 
62 /* Private macros ------------------------------------------------------------*/
63 
64 /** @defgroup IRDAEx_Private_Macros IRDAEx Private Macros
65   * @{
66   */
67 
68 /** @brief  Report the IRDA clock source.
69   * @param  __HANDLE__ specifies the IRDA Handle.
70   * @param  __CLOCKSOURCE__ output variable.
71   * @retval IRDA clocking source, written in __CLOCKSOURCE__.
72   */
73 #if defined (STM32L031xx) || defined (STM32L041xx) || defined (STM32L011xx) || defined (STM32L021xx) \
74  || defined (STM32L010xB) || defined (STM32L010x8)
75 #define IRDA_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
76   do {                                                        \
77     if((__HANDLE__)->Instance == USART2)                      \
78     {                                                         \
79       switch(__HAL_RCC_GET_USART2_SOURCE())                   \
80       {                                                       \
81         case RCC_USART2CLKSOURCE_PCLK1:                       \
82           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
83           break;                                              \
84         case RCC_USART2CLKSOURCE_HSI:                         \
85           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
86           break;                                              \
87         case RCC_USART2CLKSOURCE_SYSCLK:                      \
88           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
89           break;                                              \
90         case RCC_USART2CLKSOURCE_LSE:                         \
91           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
92           break;                                              \
93         default:                                              \
94           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
95           break;                                              \
96       }                                                       \
97     }                                                         \
98     else if((__HANDLE__)->Instance == LPUART1)                \
99     {                                                         \
100       switch(__HAL_RCC_GET_LPUART1_SOURCE())                  \
101       {                                                       \
102         case RCC_LPUART1CLKSOURCE_PCLK1:                      \
103           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
104           break;                                              \
105         case RCC_LPUART1CLKSOURCE_HSI:                        \
106           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
107           break;                                              \
108         case RCC_LPUART1CLKSOURCE_SYSCLK:                     \
109           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
110           break;                                              \
111         case RCC_LPUART1CLKSOURCE_LSE:                        \
112           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
113           break;                                              \
114         default:                                              \
115           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
116           break;                                              \
117       }                                                       \
118     }                                                         \
119     else                                                      \
120     {                                                         \
121       (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;         \
122     }                                                         \
123   } while(0U)
124 
125 #else /* (STM32L031xx) || (STM32L041xx) || (STM32L011xx) || (STM32L021xx) || (STM32L010xB) || (STM32L010x8)*/
126 
127 #define IRDA_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \
128   do {                                                        \
129     if((__HANDLE__)->Instance == USART1)                      \
130     {                                                         \
131       switch(__HAL_RCC_GET_USART1_SOURCE())                   \
132       {                                                       \
133         case RCC_USART1CLKSOURCE_PCLK2:                       \
134           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK2;         \
135           break;                                              \
136         case RCC_USART1CLKSOURCE_HSI:                         \
137           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
138           break;                                              \
139         case RCC_USART1CLKSOURCE_SYSCLK:                      \
140           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
141           break;                                              \
142         case RCC_USART1CLKSOURCE_LSE:                         \
143           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
144           break;                                              \
145         default:                                              \
146           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
147           break;                                              \
148       }                                                       \
149     }                                                         \
150     else if((__HANDLE__)->Instance == USART2)                 \
151     {                                                         \
152       switch(__HAL_RCC_GET_USART2_SOURCE())                   \
153       {                                                       \
154         case RCC_USART2CLKSOURCE_PCLK1:                       \
155           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
156           break;                                              \
157         case RCC_USART2CLKSOURCE_HSI:                         \
158           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
159           break;                                              \
160         case RCC_USART2CLKSOURCE_SYSCLK:                      \
161           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
162           break;                                              \
163         case RCC_USART2CLKSOURCE_LSE:                         \
164           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
165           break;                                              \
166         default:                                              \
167           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
168           break;                                              \
169       }                                                       \
170     }                                                         \
171     else if((__HANDLE__)->Instance == LPUART1)                \
172     {                                                         \
173       switch(__HAL_RCC_GET_LPUART1_SOURCE())                  \
174       {                                                       \
175         case RCC_LPUART1CLKSOURCE_PCLK1:                      \
176           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
177           break;                                              \
178         case RCC_LPUART1CLKSOURCE_HSI:                        \
179           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
180           break;                                              \
181         case RCC_LPUART1CLKSOURCE_SYSCLK:                     \
182           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
183           break;                                              \
184         case RCC_LPUART1CLKSOURCE_LSE:                        \
185           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
186           break;                                              \
187         default:                                              \
188           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
189           break;                                              \
190       }                                                       \
191     }                                                         \
192     else                                                      \
193     {                                                         \
194       (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;         \
195     }                                                         \
196   } while(0U)
197 #endif /* (STM32L031xx) || (STM32L041xx) || (STM32L011xx) || (STM32L021xx) || (STM32L010xB) || (STM32L010x8)*/
198 
199 
200 /** @brief  Compute the mask to apply to retrieve the received data
201   *         according to the word length and to the parity bits activation.
202   * @param  __HANDLE__ specifies the IRDA Handle.
203   * @retval None, the mask to apply to the associated UART RDR register is stored in (__HANDLE__)->Mask field.
204   */
205 #define IRDA_MASK_COMPUTATION(__HANDLE__)                             \
206   do {                                                                \
207     if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_9B)          \
208     {                                                                 \
209       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
210       {                                                               \
211         (__HANDLE__)->Mask = 0x01FFU ;                                \
212       }                                                               \
213       else                                                            \
214       {                                                               \
215         (__HANDLE__)->Mask = 0x00FFU ;                                \
216       }                                                               \
217     }                                                                 \
218     else if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_8B)     \
219     {                                                                 \
220       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
221       {                                                               \
222         (__HANDLE__)->Mask = 0x00FFU ;                                \
223       }                                                               \
224       else                                                            \
225       {                                                               \
226         (__HANDLE__)->Mask = 0x007FU ;                                \
227       }                                                               \
228     }                                                                 \
229     else if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_7B)     \
230     {                                                                 \
231       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
232       {                                                               \
233         (__HANDLE__)->Mask = 0x007FU ;                                \
234       }                                                               \
235       else                                                            \
236       {                                                               \
237         (__HANDLE__)->Mask = 0x003FU ;                                \
238       }                                                               \
239     }                                                                 \
240     else                                                              \
241     {                                                                 \
242       (__HANDLE__)->Mask = 0x0000U;                                   \
243     }                                                                 \
244   } while(0U)
245 
246 /** @brief Ensure that IRDA frame length is valid.
247   * @param __LENGTH__ IRDA frame length.
248   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
249   */
250 #define IS_IRDA_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == IRDA_WORDLENGTH_7B) || \
251                                          ((__LENGTH__) == IRDA_WORDLENGTH_8B) || \
252                                          ((__LENGTH__) == IRDA_WORDLENGTH_9B))
253 /**
254   * @}
255   */
256 
257 /* Exported functions --------------------------------------------------------*/
258 
259 /**
260   * @}
261   */
262 
263 /**
264   * @}
265   */
266 #endif /* !defined (STM32L010x4) && !defined (STM32L010x6) && !defined (STM32L010x8) */
267 
268 #ifdef __cplusplus
269 }
270 #endif
271 
272 #endif /* STM32L0xx_HAL_IRDA_EX_H */
273 
274