1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_usart_ex.h
4   * @author  MCD Application Team
5   * @brief   Header file of USART 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_USART_EX_H
21 #define STM32L0xx_HAL_USART_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32l0xx_hal_def.h"
29 
30 /** @addtogroup STM32L0xx_HAL_Driver
31   * @{
32   */
33 
34 /** @addtogroup USARTEx
35   * @{
36   */
37 
38 /* Exported types ------------------------------------------------------------*/
39 /* Exported constants --------------------------------------------------------*/
40 /** @defgroup USARTEx_Exported_Constants USARTEx Exported Constants
41   * @{
42   */
43 
44 /** @defgroup USARTEx_Word_Length USARTEx Word Length
45   * @{
46   */
47 #define USART_WORDLENGTH_7B                  (USART_CR1_M1)   /*!< 7-bit long USART frame */
48 #define USART_WORDLENGTH_8B                  (0x00000000U)              /*!< 8-bit long USART frame */
49 #define USART_WORDLENGTH_9B                  (USART_CR1_M0)   /*!< 9-bit long USART frame */
50 /**
51   * @}
52   */
53 
54 
55 /**
56   * @}
57   */
58 
59 /* Private macros ------------------------------------------------------------*/
60 /** @defgroup USARTEx_Private_Macros USARTEx Private Macros
61   * @{
62   */
63 
64 /** @brief  Report the USART clock source.
65   * @param  __HANDLE__ specifies the USART Handle.
66   * @param  __CLOCKSOURCE__ output variable.
67   * @retval the USART clocking source, written in __CLOCKSOURCE__.
68   */
69 #if defined(STM32L051xx) || defined(STM32L052xx) || defined(STM32L053xx) || defined(STM32L062xx) || defined(STM32L063xx)
70 
71 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
72   do {                                                         \
73     if((__HANDLE__)->Instance == USART1)                       \
74     {                                                          \
75       switch(__HAL_RCC_GET_USART1_SOURCE())                    \
76       {                                                        \
77         case RCC_USART1CLKSOURCE_PCLK2:                        \
78           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2;         \
79           break;                                               \
80         case RCC_USART1CLKSOURCE_HSI:                          \
81           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI;           \
82           break;                                               \
83         case RCC_USART1CLKSOURCE_SYSCLK:                       \
84           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK;        \
85           break;                                               \
86         case RCC_USART1CLKSOURCE_LSE:                          \
87           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE;           \
88           break;                                               \
89         default:                                               \
90           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;     \
91           break;                                               \
92       }                                                        \
93     }                                                          \
94     else if((__HANDLE__)->Instance == USART2)                  \
95     {                                                          \
96       switch(__HAL_RCC_GET_USART2_SOURCE())                    \
97       {                                                        \
98         case RCC_USART2CLKSOURCE_PCLK1:                        \
99           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1;         \
100           break;                                               \
101         case RCC_USART2CLKSOURCE_HSI:                          \
102           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI;           \
103           break;                                               \
104         case RCC_USART2CLKSOURCE_SYSCLK:                       \
105           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK;        \
106           break;                                               \
107         case RCC_USART2CLKSOURCE_LSE:                          \
108           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE;           \
109           break;                                               \
110         default:                                               \
111           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;     \
112           break;                                               \
113       }                                                        \
114     }                                                          \
115     else                                                       \
116     {                                                          \
117       (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;         \
118     }                                                          \
119   } while(0U)
120 
121 #elif defined(STM32L071xx) || defined (STM32L081xx) || defined(STM32L072xx) || defined (STM32L082xx) \
122    || defined(STM32L073xx) || defined (STM32L083xx)
123 
124 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
125   do {                                                         \
126     if((__HANDLE__)->Instance == USART1)                       \
127     {                                                          \
128       switch(__HAL_RCC_GET_USART1_SOURCE())                    \
129       {                                                        \
130         case RCC_USART1CLKSOURCE_PCLK2:                        \
131           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2;         \
132           break;                                               \
133         case RCC_USART1CLKSOURCE_HSI:                          \
134           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI;           \
135           break;                                               \
136         case RCC_USART1CLKSOURCE_SYSCLK:                       \
137           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK;        \
138           break;                                               \
139         case RCC_USART1CLKSOURCE_LSE:                          \
140           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE;           \
141           break;                                               \
142         default:                                               \
143           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;     \
144           break;                                               \
145       }                                                        \
146     }                                                          \
147     else if((__HANDLE__)->Instance == USART2)                  \
148     {                                                          \
149       switch(__HAL_RCC_GET_USART2_SOURCE())                    \
150       {                                                        \
151         case RCC_USART2CLKSOURCE_PCLK1:                        \
152           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1;         \
153           break;                                               \
154         case RCC_USART2CLKSOURCE_HSI:                          \
155           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI;           \
156           break;                                               \
157         case RCC_USART2CLKSOURCE_SYSCLK:                       \
158           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK;        \
159           break;                                               \
160         case RCC_USART2CLKSOURCE_LSE:                          \
161           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE;           \
162           break;                                               \
163         default:                                               \
164           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;     \
165           break;                                               \
166       }                                                        \
167     }                                                          \
168     else if((__HANDLE__)->Instance == USART4)                  \
169     {                                                          \
170       (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1;             \
171     }                                                          \
172     else if((__HANDLE__)->Instance == USART5)                  \
173     {                                                          \
174       (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1;             \
175     }                                                          \
176     else                                                       \
177     {                                                          \
178       (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;         \
179     }                                                          \
180   } while(0U)
181 
182 #else
183 
184 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
185   do {                                                         \
186     if((__HANDLE__)->Instance == USART2)                       \
187     {                                                          \
188       switch(__HAL_RCC_GET_USART2_SOURCE())                    \
189       {                                                        \
190         case RCC_USART2CLKSOURCE_PCLK1:                        \
191           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1;         \
192           break;                                               \
193         case RCC_USART2CLKSOURCE_HSI:                          \
194           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI;           \
195           break;                                               \
196         case RCC_USART2CLKSOURCE_SYSCLK:                       \
197           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK;        \
198           break;                                               \
199         case RCC_USART2CLKSOURCE_LSE:                          \
200           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE;           \
201           break;                                               \
202         default:                                               \
203           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;     \
204           break;                                               \
205       }                                                        \
206     }                                                          \
207     else                                                       \
208     {                                                          \
209       (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;         \
210     }                                                          \
211   } while(0U)
212 
213 #endif /* STM32L051xx || STM32L052xx || STM32L053xx || STM32L062xx || STM32L063xx */
214 
215 /** @brief  Compute the USART mask to apply to retrieve the received data
216   *         according to the word length and to the parity bits activation.
217   * @note   If PCE = 1, the parity bit is not included in the data extracted
218   *         by the reception API().
219   *         This masking operation is not carried out in the case of
220   *         DMA transfers.
221   * @param  __HANDLE__ specifies the USART Handle.
222   * @retval None, the mask to apply to USART RDR register is stored in (__HANDLE__)->Mask field.
223   */
224 #define USART_MASK_COMPUTATION(__HANDLE__)                            \
225   do {                                                                \
226     if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_9B)         \
227     {                                                                 \
228       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
229       {                                                               \
230         (__HANDLE__)->Mask = 0x01FFU;                                 \
231       }                                                               \
232       else                                                            \
233       {                                                               \
234         (__HANDLE__)->Mask = 0x00FFU;                                 \
235       }                                                               \
236     }                                                                 \
237     else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_8B)    \
238     {                                                                 \
239       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
240       {                                                               \
241         (__HANDLE__)->Mask = 0x00FFU;                                 \
242       }                                                               \
243       else                                                            \
244       {                                                               \
245         (__HANDLE__)->Mask = 0x007FU;                                 \
246       }                                                               \
247     }                                                                 \
248     else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_7B)    \
249     {                                                                 \
250       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
251       {                                                               \
252         (__HANDLE__)->Mask = 0x007FU;                                 \
253       }                                                               \
254       else                                                            \
255       {                                                               \
256         (__HANDLE__)->Mask = 0x003FU;                                 \
257       }                                                               \
258     }                                                                 \
259     else                                                              \
260     {                                                                 \
261       (__HANDLE__)->Mask = 0x0000U;                                   \
262     }                                                                 \
263   } while(0U)
264 
265 /**
266   * @brief Ensure that USART frame length is valid.
267   * @param __LENGTH__ USART frame length.
268   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
269   */
270 #define IS_USART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == USART_WORDLENGTH_7B) || \
271                                           ((__LENGTH__) == USART_WORDLENGTH_8B) || \
272                                           ((__LENGTH__) == USART_WORDLENGTH_9B))
273 
274 
275 /**
276   * @}
277   */
278 
279 /* Exported functions --------------------------------------------------------*/
280 /** @addtogroup USARTEx_Exported_Functions
281   * @{
282   */
283 
284 /** @addtogroup USARTEx_Exported_Functions_Group1
285   * @{
286   */
287 
288 /* IO operation functions *****************************************************/
289 
290 /**
291   * @}
292   */
293 
294 /** @addtogroup USARTEx_Exported_Functions_Group2
295   * @{
296   */
297 
298 /* Peripheral Control functions ***********************************************/
299 
300 /**
301   * @}
302   */
303 
304 /**
305   * @}
306   */
307 
308 /**
309   * @}
310   */
311 
312 /**
313   * @}
314   */
315 
316 #ifdef __cplusplus
317 }
318 #endif
319 
320 #endif /* STM32L0xx_HAL_USART_EX_H */
321 
322