1 /**
2   ******************************************************************************
3   * @file    stm32wlxx_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) 2020 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 STM32WLxx_HAL_USART_EX_H
21 #define STM32WLxx_HAL_USART_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32wlxx_hal_def.h"
29 
30 /** @addtogroup STM32WLxx_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                  ((uint32_t)USART_CR1_M1)   /*!< 7-bit long USART frame */
48 #define USART_WORDLENGTH_8B                  0x00000000U                /*!< 8-bit long USART frame */
49 #define USART_WORDLENGTH_9B                  ((uint32_t)USART_CR1_M0)   /*!< 9-bit long USART frame */
50 /**
51   * @}
52   */
53 
54 /** @defgroup USARTEx_Slave_Select_management USARTEx Slave Select Management
55   * @{
56   */
57 #define USART_NSS_HARD                        0x00000000U          /*!< SPI slave selection depends on NSS input pin              */
58 #define USART_NSS_SOFT                        USART_CR2_DIS_NSS    /*!< SPI slave is always selected and NSS input pin is ignored */
59 /**
60   * @}
61   */
62 
63 
64 /** @defgroup USARTEx_Slave_Mode USARTEx Synchronous Slave mode enable
65   * @brief    USART SLAVE mode
66   * @{
67   */
68 #define USART_SLAVEMODE_DISABLE   0x00000000U     /*!< USART SPI Slave Mode Enable  */
69 #define USART_SLAVEMODE_ENABLE    USART_CR2_SLVEN /*!< USART SPI Slave Mode Disable */
70 /**
71   * @}
72   */
73 
74 /** @defgroup USARTEx_FIFO_mode USARTEx FIFO  mode
75   * @brief    USART FIFO  mode
76   * @{
77   */
78 #define USART_FIFOMODE_DISABLE        0x00000000U                   /*!< FIFO mode disable */
79 #define USART_FIFOMODE_ENABLE         USART_CR1_FIFOEN              /*!< FIFO mode enable  */
80 /**
81   * @}
82   */
83 
84 /** @defgroup USARTEx_TXFIFO_threshold_level USARTEx TXFIFO threshold level
85   * @brief    USART TXFIFO level
86   * @{
87   */
88 #define USART_TXFIFO_THRESHOLD_1_8   0x00000000U                               /*!< TXFIFO reaches 1/8 of its depth */
89 #define USART_TXFIFO_THRESHOLD_1_4   USART_CR3_TXFTCFG_0                       /*!< TXFIFO reaches 1/4 of its depth */
90 #define USART_TXFIFO_THRESHOLD_1_2   USART_CR3_TXFTCFG_1                       /*!< TXFIFO reaches 1/2 of its depth */
91 #define USART_TXFIFO_THRESHOLD_3_4   (USART_CR3_TXFTCFG_0|USART_CR3_TXFTCFG_1) /*!< TXFIFO reaches 3/4 of its depth */
92 #define USART_TXFIFO_THRESHOLD_7_8   USART_CR3_TXFTCFG_2                       /*!< TXFIFO reaches 7/8 of its depth */
93 #define USART_TXFIFO_THRESHOLD_8_8   (USART_CR3_TXFTCFG_2|USART_CR3_TXFTCFG_0) /*!< TXFIFO becomes empty            */
94 /**
95   * @}
96   */
97 
98 /** @defgroup USARTEx_RXFIFO_threshold_level USARTEx RXFIFO threshold level
99   * @brief    USART RXFIFO level
100   * @{
101   */
102 #define USART_RXFIFO_THRESHOLD_1_8   0x00000000U                               /*!< RXFIFO FIFO reaches 1/8 of its depth */
103 #define USART_RXFIFO_THRESHOLD_1_4   USART_CR3_RXFTCFG_0                       /*!< RXFIFO FIFO reaches 1/4 of its depth */
104 #define USART_RXFIFO_THRESHOLD_1_2   USART_CR3_RXFTCFG_1                       /*!< RXFIFO FIFO reaches 1/2 of its depth */
105 #define USART_RXFIFO_THRESHOLD_3_4   (USART_CR3_RXFTCFG_0|USART_CR3_RXFTCFG_1) /*!< RXFIFO FIFO reaches 3/4 of its depth */
106 #define USART_RXFIFO_THRESHOLD_7_8   USART_CR3_RXFTCFG_2                       /*!< RXFIFO FIFO reaches 7/8 of its depth */
107 #define USART_RXFIFO_THRESHOLD_8_8   (USART_CR3_RXFTCFG_2|USART_CR3_RXFTCFG_0) /*!< RXFIFO FIFO becomes full             */
108 /**
109   * @}
110   */
111 /**
112   * @}
113   */
114 /* Private macros ------------------------------------------------------------*/
115 /** @defgroup USARTEx_Private_Macros USARTEx Private Macros
116   * @{
117   */
118 
119 /** @brief  Compute the USART mask to apply to retrieve the received data
120   *         according to the word length and to the parity bits activation.
121   * @note   If PCE = 1, the parity bit is not included in the data extracted
122   *         by the reception API().
123   *         This masking operation is not carried out in the case of
124   *         DMA transfers.
125   * @param  __HANDLE__ specifies the USART Handle.
126   * @retval None, the mask to apply to USART RDR register is stored in (__HANDLE__)->Mask field.
127   */
128 #define USART_MASK_COMPUTATION(__HANDLE__)                            \
129   do {                                                                \
130     if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_9B)         \
131     {                                                                 \
132       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
133       {                                                               \
134         (__HANDLE__)->Mask = 0x01FFU;                                 \
135       }                                                               \
136       else                                                            \
137       {                                                               \
138         (__HANDLE__)->Mask = 0x00FFU;                                 \
139       }                                                               \
140     }                                                                 \
141     else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_8B)    \
142     {                                                                 \
143       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
144       {                                                               \
145         (__HANDLE__)->Mask = 0x00FFU;                                 \
146       }                                                               \
147       else                                                            \
148       {                                                               \
149         (__HANDLE__)->Mask = 0x007FU;                                 \
150       }                                                               \
151     }                                                                 \
152     else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_7B)    \
153     {                                                                 \
154       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
155       {                                                               \
156         (__HANDLE__)->Mask = 0x007FU;                                 \
157       }                                                               \
158       else                                                            \
159       {                                                               \
160         (__HANDLE__)->Mask = 0x003FU;                                 \
161       }                                                               \
162     }                                                                 \
163     else                                                              \
164     {                                                                 \
165       (__HANDLE__)->Mask = 0x0000U;                                   \
166     }                                                                 \
167   } while(0U)
168 
169 
170 /**
171   * @brief Ensure that USART frame length is valid.
172   * @param __LENGTH__ USART frame length.
173   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
174   */
175 #define IS_USART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == USART_WORDLENGTH_7B) || \
176                                           ((__LENGTH__) == USART_WORDLENGTH_8B) || \
177                                           ((__LENGTH__) == USART_WORDLENGTH_9B))
178 
179 /**
180   * @brief Ensure that USART Negative Slave Select (NSS) pin management is valid.
181   * @param __NSS__ USART Negative Slave Select pin management.
182   * @retval SET (__NSS__ is valid) or RESET (__NSS__ is invalid)
183   */
184 #define IS_USART_NSS(__NSS__) (((__NSS__) == USART_NSS_HARD) || \
185                                ((__NSS__) == USART_NSS_SOFT))
186 
187 /**
188   * @brief Ensure that USART Slave Mode is valid.
189   * @param __STATE__ USART Slave Mode.
190   * @retval SET (__STATE__ is valid) or RESET (__STATE__ is invalid)
191   */
192 #define IS_USART_SLAVEMODE(__STATE__)   (((__STATE__) == USART_SLAVEMODE_DISABLE ) || \
193                                          ((__STATE__) == USART_SLAVEMODE_ENABLE))
194 
195 /**
196   * @brief Ensure that USART FIFO mode is valid.
197   * @param __STATE__ USART FIFO mode.
198   * @retval SET (__STATE__ is valid) or RESET (__STATE__ is invalid)
199   */
200 #define IS_USART_FIFO_MODE_STATE(__STATE__) (((__STATE__) == USART_FIFOMODE_DISABLE ) || \
201                                              ((__STATE__) == USART_FIFOMODE_ENABLE))
202 
203 /**
204   * @brief Ensure that USART TXFIFO threshold level is valid.
205   * @param __THRESHOLD__ USART TXFIFO threshold level.
206   * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid)
207   */
208 #define IS_USART_TXFIFO_THRESHOLD(__THRESHOLD__)  (((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_8)  || \
209                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_4)  || \
210                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_2)  || \
211                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_3_4)  || \
212                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_7_8)  || \
213                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_8_8))
214 
215 /**
216   * @brief Ensure that USART RXFIFO threshold level is valid.
217   * @param __THRESHOLD__ USART RXFIFO threshold level.
218   * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid)
219   */
220 #define IS_USART_RXFIFO_THRESHOLD(__THRESHOLD__)  (((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_8)  || \
221                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_4)  || \
222                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_2)  || \
223                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_3_4)  || \
224                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_7_8)  || \
225                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_8_8))
226 /**
227   * @}
228   */
229 
230 /* Exported functions --------------------------------------------------------*/
231 /** @addtogroup USARTEx_Exported_Functions
232   * @{
233   */
234 
235 /** @addtogroup USARTEx_Exported_Functions_Group1
236   * @{
237   */
238 
239 /* IO operation functions *****************************************************/
240 void HAL_USARTEx_RxFifoFullCallback(USART_HandleTypeDef *husart);
241 void HAL_USARTEx_TxFifoEmptyCallback(USART_HandleTypeDef *husart);
242 
243 /**
244   * @}
245   */
246 
247 /** @addtogroup USARTEx_Exported_Functions_Group2
248   * @{
249   */
250 
251 /* Peripheral Control functions ***********************************************/
252 HAL_StatusTypeDef HAL_USARTEx_EnableSlaveMode(USART_HandleTypeDef *husart);
253 HAL_StatusTypeDef HAL_USARTEx_DisableSlaveMode(USART_HandleTypeDef *husart);
254 HAL_StatusTypeDef HAL_USARTEx_ConfigNSS(USART_HandleTypeDef *husart, uint32_t NSSConfig);
255 HAL_StatusTypeDef HAL_USARTEx_EnableFifoMode(USART_HandleTypeDef *husart);
256 HAL_StatusTypeDef HAL_USARTEx_DisableFifoMode(USART_HandleTypeDef *husart);
257 HAL_StatusTypeDef HAL_USARTEx_SetTxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold);
258 HAL_StatusTypeDef HAL_USARTEx_SetRxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold);
259 /**
260   * @}
261   */
262 
263 /**
264   * @}
265   */
266 
267 /**
268   * @}
269   */
270 
271 /**
272   * @}
273   */
274 
275 #ifdef __cplusplus
276 }
277 #endif
278 
279 #endif /* STM32WLxx_HAL_USART_EX_H */
280 
281