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   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
10   *
11   * Redistribution and use in source and binary forms, with or without modification,
12   * are permitted provided that the following conditions are met:
13   *   1. Redistributions of source code must retain the above copyright notice,
14   *      this list of conditions and the following disclaimer.
15   *   2. Redistributions in binary form must reproduce the above copyright notice,
16   *      this list of conditions and the following disclaimer in the documentation
17   *      and/or other materials provided with the distribution.
18   *   3. Neither the name of STMicroelectronics nor the names of its contributors
19   *      may be used to endorse or promote products derived from this software
20   *      without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   *
33   ******************************************************************************
34   */
35 
36 /* Define to prevent recursive inclusion -------------------------------------*/
37 #ifndef __STM32L0xx_HAL_USART_EX_H
38 #define __STM32L0xx_HAL_USART_EX_H
39 
40 #ifdef __cplusplus
41  extern "C" {
42 #endif
43 
44 /* Includes ------------------------------------------------------------------*/
45 #include "stm32l0xx_hal_def.h"
46 
47 /** @addtogroup STM32L0xx_HAL_Driver
48   * @{
49   */
50 
51 /** @addtogroup USARTEx
52   * @{
53   */
54 
55 /* Exported types ------------------------------------------------------------*/
56 /* Exported constants --------------------------------------------------------*/
57 /** @defgroup USARTEx_Exported_Constants USARTEx Exported Constants
58   * @{
59   */
60 
61 /** @defgroup USARTEx_Word_Length USARTEx Word Length
62   * @{
63   */
64 #define USART_WORDLENGTH_7B                  ((uint32_t)USART_CR1_M1)   /*!< 7-bit long USART frame */
65 #define USART_WORDLENGTH_8B                  ((uint32_t)0x00000000U)    /*!< 8-bit long USART frame */
66 #define USART_WORDLENGTH_9B                  ((uint32_t)USART_CR1_M0)   /*!< 9-bit long USART frame */
67 /**
68   * @}
69   */
70 
71 /**
72   * @}
73   */
74 
75 /* Exported functions --------------------------------------------------------*/
76 
77 /* Private macros ------------------------------------------------------------*/
78 /** @defgroup USARTEx_Private_Macros USARTEx Private Macros
79   * @{
80   */
81 
82 /** @brief  Report the USART clock source.
83   * @param  __HANDLE__: specifies the USART Handle.
84   * @param  __CLOCKSOURCE__: output variable.
85   * @retval the USART clocking source, written in __CLOCKSOURCE__.
86   */
87 #if defined (STM32L051xx) || defined (STM32L052xx) || defined (STM32L053xx) || defined (STM32L061xx) || defined (STM32L062xx) || defined (STM32L063xx)
88 
89 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
90   do {                                                         \
91     if((__HANDLE__)->Instance == USART1)                       \
92     {                                                          \
93        switch(__HAL_RCC_GET_USART1_SOURCE())                   \
94        {                                                       \
95         case RCC_USART1CLKSOURCE_PCLK2:                        \
96           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2;         \
97           break;                                               \
98         case RCC_USART1CLKSOURCE_HSI:                          \
99           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI;           \
100           break;                                               \
101         case RCC_USART1CLKSOURCE_SYSCLK:                       \
102           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK;        \
103           break;                                               \
104         case RCC_USART1CLKSOURCE_LSE:                          \
105           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE;           \
106           break;                                               \
107         default:                                               \
108           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;     \
109           break;                                               \
110        }                                                       \
111     }                                                          \
112     else if((__HANDLE__)->Instance == USART2)                  \
113     {                                                          \
114        switch(__HAL_RCC_GET_USART2_SOURCE())                   \
115        {                                                       \
116         case RCC_USART2CLKSOURCE_PCLK1:                        \
117           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1;         \
118           break;                                               \
119         case RCC_USART2CLKSOURCE_HSI:                          \
120           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI;           \
121           break;                                               \
122         case RCC_USART2CLKSOURCE_SYSCLK:                       \
123           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK;        \
124           break;                                               \
125         case RCC_USART2CLKSOURCE_LSE:                          \
126           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE;           \
127           break;                                               \
128         default:                                               \
129           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;     \
130           break;                                               \
131        }                                                       \
132     }                                                          \
133   } while(0)
134 
135 #elif defined(STM32L071xx) || defined (STM32L081xx) || defined(STM32L072xx) || defined (STM32L082xx) || defined(STM32L073xx) || defined (STM32L083xx)
136 
137 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
138   do {                                                         \
139     if((__HANDLE__)->Instance == USART1)                       \
140     {                                                          \
141        switch(__HAL_RCC_GET_USART1_SOURCE())                   \
142        {                                                       \
143         case RCC_USART1CLKSOURCE_PCLK2:                        \
144           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2;         \
145           break;                                               \
146         case RCC_USART1CLKSOURCE_HSI:                          \
147           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI;           \
148           break;                                               \
149         case RCC_USART1CLKSOURCE_SYSCLK:                       \
150           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK;        \
151           break;                                               \
152         case RCC_USART1CLKSOURCE_LSE:                          \
153           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE;           \
154           break;                                               \
155         default:                                               \
156           (__CLOCKSOURCE__) = USART_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__) = USART_CLOCKSOURCE_PCLK1;         \
166           break;                                               \
167         case RCC_USART2CLKSOURCE_HSI:                          \
168           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI;           \
169           break;                                               \
170         case RCC_USART2CLKSOURCE_SYSCLK:                       \
171           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK;        \
172           break;                                               \
173         case RCC_USART2CLKSOURCE_LSE:                          \
174           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE;           \
175           break;                                               \
176         default:                                               \
177           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;     \
178           break;                                               \
179        }                                                       \
180     }                                                          \
181     else if((__HANDLE__)->Instance == USART4)                  \
182     {                                                          \
183       (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1;             \
184     }                                                          \
185     else if((__HANDLE__)->Instance == USART5)                  \
186     {                                                          \
187       (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1;             \
188     }                                                          \
189   } while(0)
190 
191 #else
192 
193 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__)       \
194   do {                                                         \
195     if((__HANDLE__)->Instance == USART2)                       \
196     {                                                          \
197        switch(__HAL_RCC_GET_USART2_SOURCE())                   \
198        {                                                       \
199         case RCC_USART2CLKSOURCE_PCLK1:                        \
200           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1;         \
201           break;                                               \
202         case RCC_USART2CLKSOURCE_HSI:                          \
203           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI;           \
204           break;                                               \
205         case RCC_USART2CLKSOURCE_SYSCLK:                       \
206           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK;        \
207           break;                                               \
208         case RCC_USART2CLKSOURCE_LSE:                          \
209           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE;           \
210           break;                                               \
211         default:                                               \
212           (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED;     \
213           break;                                               \
214        }                                                       \
215     }                                                          \
216   } while(0)
217 
218 #endif
219 
220 /** @brief  Compute the USART mask to apply to retrieve the received data
221   *         according to the word length and to the parity bits activation.
222   * @note   If PCE = 1, the parity bit is not included in the data extracted
223   *         by the reception API().
224   *         This masking operation is not carried out in the case of
225   *         DMA transfers.
226   * @param  __HANDLE__: specifies the USART Handle.
227   * @retval None, the mask to apply to USART RDR register is stored in (__HANDLE__)->Mask field.
228   */
229 #define USART_MASK_COMPUTATION(__HANDLE__)                            \
230   do {                                                                \
231   if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_9B)           \
232   {                                                                   \
233      if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)              \
234      {                                                                \
235         (__HANDLE__)->Mask = 0x01FF ;                                 \
236      }                                                                \
237      else                                                             \
238      {                                                                \
239         (__HANDLE__)->Mask = 0x00FF ;                                 \
240      }                                                                \
241   }                                                                   \
242   else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_8B)      \
243   {                                                                   \
244      if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)              \
245      {                                                                \
246         (__HANDLE__)->Mask = 0x00FF ;                                 \
247      }                                                                \
248      else                                                             \
249      {                                                                \
250         (__HANDLE__)->Mask = 0x007F ;                                 \
251      }                                                                \
252   }                                                                   \
253   else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_7B)      \
254   {                                                                   \
255      if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)              \
256      {                                                                \
257         (__HANDLE__)->Mask = 0x007F ;                                 \
258      }                                                                \
259      else                                                             \
260      {                                                                \
261         (__HANDLE__)->Mask = 0x003F ;                                 \
262      }                                                                \
263   }                                                                   \
264 } while(0)
265 
266 
267 /**
268   * @brief Ensure that USART frame length is valid.
269   * @param __LENGTH__: USART frame length.
270   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
271   */
272 #define IS_USART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == USART_WORDLENGTH_7B) || \
273                                           ((__LENGTH__) == USART_WORDLENGTH_8B) || \
274                                           ((__LENGTH__) == USART_WORDLENGTH_9B))
275 
276 /**
277   * @}
278   */
279 
280 /* Exported functions --------------------------------------------------------*/
281 
282 /**
283   * @}
284   */
285 
286 /**
287   * @}
288   */
289 
290 #ifdef __cplusplus
291 }
292 #endif
293 
294 #endif /* __STM32L0xx_HAL_USART_EX_H */
295 
296 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
297 
298