1 /**
2   ******************************************************************************
3   * @file    stm32g0xx_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) 2018 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 STM32G0xx_HAL_IRDA_EX_H
21 #define STM32G0xx_HAL_IRDA_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32g0xx_hal_def.h"
29 
30 /** @addtogroup STM32G0xx_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(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx)
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_PCLK1:                       \
80           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
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                                                      \
139     {                                                         \
140       (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;         \
141     }                                                         \
142   } while(0U)
143 #elif defined(STM32G081xx) || defined(STM32G071xx) || defined(STM32G070xx)
144 #define IRDA_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
145   do {                                                        \
146     if((__HANDLE__)->Instance == USART1)                      \
147     {                                                         \
148       switch(__HAL_RCC_GET_USART1_SOURCE())                   \
149       {                                                       \
150         case RCC_USART1CLKSOURCE_PCLK1:                       \
151           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
152           break;                                              \
153         case RCC_USART1CLKSOURCE_HSI:                         \
154           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
155           break;                                              \
156         case RCC_USART1CLKSOURCE_SYSCLK:                      \
157           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
158           break;                                              \
159         case RCC_USART1CLKSOURCE_LSE:                         \
160           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
161           break;                                              \
162         default:                                              \
163           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
164           break;                                              \
165       }                                                       \
166     }                                                         \
167     else if((__HANDLE__)->Instance == USART2)                 \
168     {                                                         \
169       switch(__HAL_RCC_GET_USART2_SOURCE())                   \
170       {                                                       \
171         case RCC_USART2CLKSOURCE_PCLK1:                       \
172           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
173           break;                                              \
174         case RCC_USART2CLKSOURCE_HSI:                         \
175           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
176           break;                                              \
177         case RCC_USART2CLKSOURCE_SYSCLK:                      \
178           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
179           break;                                              \
180         case RCC_USART2CLKSOURCE_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 #else
194 #define IRDA_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
195   do {                                                        \
196     if((__HANDLE__)->Instance == USART1)                      \
197     {                                                         \
198       switch(__HAL_RCC_GET_USART1_SOURCE())                   \
199       {                                                       \
200         case RCC_USART1CLKSOURCE_PCLK1:                       \
201           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK1;         \
202           break;                                              \
203         case RCC_USART1CLKSOURCE_HSI:                         \
204           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;           \
205           break;                                              \
206         case RCC_USART1CLKSOURCE_SYSCLK:                      \
207           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;        \
208           break;                                              \
209         case RCC_USART1CLKSOURCE_LSE:                         \
210           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;           \
211           break;                                              \
212         default:                                              \
213           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;     \
214           break;                                              \
215       }                                                       \
216     }                                                         \
217     else                                                      \
218     {                                                         \
219       (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;         \
220     }                                                         \
221   } while(0U)
222 #endif /* STM32G0C1xx || STM32G0B1xx || STM32G0B0xx */
223 
224 /** @brief  Compute the mask to apply to retrieve the received data
225   *         according to the word length and to the parity bits activation.
226   * @param  __HANDLE__ specifies the IRDA Handle.
227   * @retval None, the mask to apply to the associated UART RDR register is stored in (__HANDLE__)->Mask field.
228   */
229 #define IRDA_MASK_COMPUTATION(__HANDLE__)                             \
230   do {                                                                \
231     if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_9B)          \
232     {                                                                 \
233       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
234       {                                                               \
235         (__HANDLE__)->Mask = 0x01FFU ;                                \
236       }                                                               \
237       else                                                            \
238       {                                                               \
239         (__HANDLE__)->Mask = 0x00FFU ;                                \
240       }                                                               \
241     }                                                                 \
242     else if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_8B)     \
243     {                                                                 \
244       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
245       {                                                               \
246         (__HANDLE__)->Mask = 0x00FFU ;                                \
247       }                                                               \
248       else                                                            \
249       {                                                               \
250         (__HANDLE__)->Mask = 0x007FU ;                                \
251       }                                                               \
252     }                                                                 \
253     else if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_7B)     \
254     {                                                                 \
255       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
256       {                                                               \
257         (__HANDLE__)->Mask = 0x007FU ;                                \
258       }                                                               \
259       else                                                            \
260       {                                                               \
261         (__HANDLE__)->Mask = 0x003FU ;                                \
262       }                                                               \
263     }                                                                 \
264     else                                                              \
265     {                                                                 \
266       (__HANDLE__)->Mask = 0x0000U;                                   \
267     }                                                                 \
268   } while(0U)
269 
270 /** @brief Ensure that IRDA frame length is valid.
271   * @param __LENGTH__ IRDA frame length.
272   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
273   */
274 #define IS_IRDA_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == IRDA_WORDLENGTH_7B) || \
275                                          ((__LENGTH__) == IRDA_WORDLENGTH_8B) || \
276                                          ((__LENGTH__) == IRDA_WORDLENGTH_9B))
277 /**
278   * @}
279   */
280 
281 /* Exported functions --------------------------------------------------------*/
282 
283 /**
284   * @}
285   */
286 
287 /**
288   * @}
289   */
290 
291 #ifdef __cplusplus
292 }
293 #endif
294 
295 #endif /* STM32G0xx_HAL_IRDA_EX_H */
296 
297