1 /**
2   ******************************************************************************
3   * @file    stm32wbaxx_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) 2022 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 STM32WBAxx_HAL_IRDA_EX_H
21 #define STM32WBAxx_HAL_IRDA_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32wbaxx_hal_def.h"
29 
30 /** @addtogroup STM32WBAxx_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(USART2)
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_PCLK2:                        \
80           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK2;          \
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                                                       \
118     {                                                          \
119       (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;          \
120     }                                                          \
121   } while(0U)
122 #else
123 #define IRDA_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)        \
124   do {                                                         \
125     if((__HANDLE__)->Instance == USART1)                       \
126     {                                                          \
127       switch(__HAL_RCC_GET_USART1_SOURCE())                    \
128       {                                                        \
129         case RCC_USART1CLKSOURCE_PCLK2:                        \
130           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_PCLK2;          \
131           break;                                               \
132         case RCC_USART1CLKSOURCE_HSI:                          \
133           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_HSI;            \
134           break;                                               \
135         case RCC_USART1CLKSOURCE_SYSCLK:                       \
136           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_SYSCLK;         \
137           break;                                               \
138         case RCC_USART1CLKSOURCE_LSE:                          \
139           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_LSE;            \
140           break;                                               \
141         default:                                               \
142           (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;      \
143           break;                                               \
144       }                                                        \
145     }                                                          \
146     else                                                       \
147     {                                                          \
148       (__CLOCKSOURCE__) = IRDA_CLOCKSOURCE_UNDEFINED;          \
149     }                                                          \
150   } while(0U)
151 #endif /* USART2 */
152 
153 /** @brief  Compute the mask to apply to retrieve the received data
154   *         according to the word length and to the parity bits activation.
155   * @param  __HANDLE__ specifies the IRDA Handle.
156   * @retval None, the mask to apply to the associated UART RDR register is stored in (__HANDLE__)->Mask field.
157   */
158 #define IRDA_MASK_COMPUTATION(__HANDLE__)                             \
159   do {                                                                \
160     if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_9B)          \
161     {                                                                 \
162       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
163       {                                                               \
164         (__HANDLE__)->Mask = 0x01FFU ;                                \
165       }                                                               \
166       else                                                            \
167       {                                                               \
168         (__HANDLE__)->Mask = 0x00FFU ;                                \
169       }                                                               \
170     }                                                                 \
171     else if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_8B)     \
172     {                                                                 \
173       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
174       {                                                               \
175         (__HANDLE__)->Mask = 0x00FFU ;                                \
176       }                                                               \
177       else                                                            \
178       {                                                               \
179         (__HANDLE__)->Mask = 0x007FU ;                                \
180       }                                                               \
181     }                                                                 \
182     else if ((__HANDLE__)->Init.WordLength == IRDA_WORDLENGTH_7B)     \
183     {                                                                 \
184       if ((__HANDLE__)->Init.Parity == IRDA_PARITY_NONE)              \
185       {                                                               \
186         (__HANDLE__)->Mask = 0x007FU ;                                \
187       }                                                               \
188       else                                                            \
189       {                                                               \
190         (__HANDLE__)->Mask = 0x003FU ;                                \
191       }                                                               \
192     }                                                                 \
193     else                                                              \
194     {                                                                 \
195       (__HANDLE__)->Mask = 0x0000U;                                   \
196     }                                                                 \
197   } while(0U)
198 
199 /** @brief Ensure that IRDA frame length is valid.
200   * @param __LENGTH__ IRDA frame length.
201   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
202   */
203 #define IS_IRDA_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == IRDA_WORDLENGTH_7B) || \
204                                          ((__LENGTH__) == IRDA_WORDLENGTH_8B) || \
205                                          ((__LENGTH__) == IRDA_WORDLENGTH_9B))
206 /**
207   * @}
208   */
209 
210 /* Exported functions --------------------------------------------------------*/
211 
212 /**
213   * @}
214   */
215 
216 /**
217   * @}
218   */
219 
220 #ifdef __cplusplus
221 }
222 #endif
223 
224 #endif /* STM32WBAxx_HAL_IRDA_EX_H */
225 
226