1 /**
2   ******************************************************************************
3   * @file    stm32wbaxx_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) 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_USART_EX_H
21 #define STM32WBAxx_HAL_USART_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 /** @addtogroup USARTEx
35   * @{
36   */
37 
38 /* Exported types ------------------------------------------------------------*/
39 /** @defgroup USARTEx_Exported_Types USARTEx Exported Types
40   * @{
41   */
42 /**
43   * @brief  USART Autonomous mode parameters
44   */
45 typedef struct
46 {
47   uint32_t AutonomousModeState;  /*!< Specifies the autonomous mode state.This parameter can be a value of
48                                       @ref USARTEx_Autonomous_mode.*/
49 
50   uint32_t TriggerSelection;     /*!< Specifies which trigger will activate the Transmission automatically.
51                                       This parameter can be a value of @ref USARTEx_Autonomous_Trigger_selection */
52 
53   uint32_t TriggerPolarity;      /*!< Specifies the autonomous mode trigger signal polarity.
54                                       This parameter can be a value of @ref USARTEx_Autonomous_Trigger_Polarity */
55 
56   uint32_t DataSize;             /*!< Specifies the transmitted data size in byte */
57 
58   uint32_t IdleFrame;            /*!< Specifies whether the IDLE frame transmission is enabled or disabled.
59                                       This parameter can be a value of @ref USARTEx_Autonomous_IDLE_FRAME. */
60 
61 } USART_AutonomousModeConfTypeDef;
62 /**
63   * @}
64   */
65 
66 /* Exported constants --------------------------------------------------------*/
67 /** @defgroup USARTEx_Exported_Constants USARTEx Exported Constants
68   * @{
69   */
70 
71 /** @defgroup USARTEx_Word_Length USARTEx Word Length
72   * @{
73   */
74 #define USART_WORDLENGTH_7B                  (USART_CR1_M1)   /*!< 7-bit long USART frame */
75 #define USART_WORDLENGTH_8B                  (0x00000000U)              /*!< 8-bit long USART frame */
76 #define USART_WORDLENGTH_9B                  (USART_CR1_M0)   /*!< 9-bit long USART frame */
77 /**
78   * @}
79   */
80 
81 /** @defgroup USARTEx_Slave_Select_management USARTEx Slave Select Management
82   * @{
83   */
84 #define USART_NSS_HARD                        0x00000000U          /*!< SPI slave selection depends on NSS input pin              */
85 #define USART_NSS_SOFT                        USART_CR2_DIS_NSS    /*!< SPI slave is always selected and NSS input pin is ignored */
86 /**
87   * @}
88   */
89 
90 
91 /** @defgroup USARTEx_Slave_Mode USARTEx Synchronous Slave mode enable
92   * @brief    USART SLAVE mode
93   * @{
94   */
95 #define USART_SLAVEMODE_DISABLE   0x00000000U     /*!< USART SPI Slave Mode Enable  */
96 #define USART_SLAVEMODE_ENABLE    USART_CR2_SLVEN /*!< USART SPI Slave Mode Disable */
97 /**
98   * @}
99   */
100 
101 /** @defgroup USARTEx_FIFO_mode USARTEx FIFO  mode
102   * @brief    USART FIFO  mode
103   * @{
104   */
105 #define USART_FIFOMODE_DISABLE        0x00000000U                   /*!< FIFO mode disable */
106 #define USART_FIFOMODE_ENABLE         USART_CR1_FIFOEN              /*!< FIFO mode enable  */
107 /**
108   * @}
109   */
110 
111 /** @defgroup USARTEx_TXFIFO_threshold_level USARTEx TXFIFO threshold level
112   * @brief    USART TXFIFO level
113   * @{
114   */
115 #define USART_TXFIFO_THRESHOLD_1_8   0x00000000U                               /*!< TXFIFO reaches 1/8 of its depth */
116 #define USART_TXFIFO_THRESHOLD_1_4   USART_CR3_TXFTCFG_0                       /*!< TXFIFO reaches 1/4 of its depth */
117 #define USART_TXFIFO_THRESHOLD_1_2   USART_CR3_TXFTCFG_1                       /*!< TXFIFO reaches 1/2 of its depth */
118 #define USART_TXFIFO_THRESHOLD_3_4   (USART_CR3_TXFTCFG_0|USART_CR3_TXFTCFG_1) /*!< TXFIFO reaches 3/4 of its depth */
119 #define USART_TXFIFO_THRESHOLD_7_8   USART_CR3_TXFTCFG_2                       /*!< TXFIFO reaches 7/8 of its depth */
120 #define USART_TXFIFO_THRESHOLD_8_8   (USART_CR3_TXFTCFG_2|USART_CR3_TXFTCFG_0) /*!< TXFIFO becomes empty            */
121 /**
122   * @}
123   */
124 
125 /** @defgroup USARTEx_RXFIFO_threshold_level USARTEx RXFIFO threshold level
126   * @brief    USART RXFIFO level
127   * @{
128   */
129 #define USART_RXFIFO_THRESHOLD_1_8   0x00000000U                               /*!< RXFIFO FIFO reaches 1/8 of its depth */
130 #define USART_RXFIFO_THRESHOLD_1_4   USART_CR3_RXFTCFG_0                       /*!< RXFIFO FIFO reaches 1/4 of its depth */
131 #define USART_RXFIFO_THRESHOLD_1_2   USART_CR3_RXFTCFG_1                       /*!< RXFIFO FIFO reaches 1/2 of its depth */
132 #define USART_RXFIFO_THRESHOLD_3_4   (USART_CR3_RXFTCFG_0|USART_CR3_RXFTCFG_1) /*!< RXFIFO FIFO reaches 3/4 of its depth */
133 #define USART_RXFIFO_THRESHOLD_7_8   USART_CR3_RXFTCFG_2                       /*!< RXFIFO FIFO reaches 7/8 of its depth */
134 #define USART_RXFIFO_THRESHOLD_8_8   (USART_CR3_RXFTCFG_2|USART_CR3_RXFTCFG_0) /*!< RXFIFO FIFO becomes full             */
135 /**
136   * @}
137   */
138 
139 /** @defgroup USARTEx_Autonomous_mode USARTEx Autonomous Mode
140   * @brief    USART Autonomous mode
141   * @{
142   */
143 #define USART_AUTONOMOUS_MODE_DISABLE  0x00000000U          /*!< Autonomous mode disable */
144 #define USART_AUTONOMOUS_MODE_ENABLE   USART_AUTOCR_TRIGEN  /*!< Autonomous mode enable  */
145 /**
146   * @}
147   */
148 
149 /** @defgroup USARTEx_Autonomous_Trigger_Polarity USARTEx Autonomous Trigger Polarity
150   * @brief    USART Trigger polarity edge selection
151   * @{
152   */
153 #define USART_TRIG_POLARITY_RISING   0x00000000U           /*!< USART triggered on rising edge  */
154 #define USART_TRIG_POLARITY_FALLING  USART_AUTOCR_TRIGPOL  /*!< USART triggered on falling edge */
155 /**
156   * @}
157   */
158 
159 /** @defgroup USARTEx_Autonomous_IDLE_FRAME USARTEx Autonomous IDLE Frame
160   * @brief    USART IDLE frame transmission
161   * @{
162   */
163 #define USART_IDLE_FRAME_ENABLE      0x00000000U          /*!< IDLE Frame sent after enabling the transmitter */
164 #define USART_IDLE_FRAME_DISABLE     USART_AUTOCR_IDLEDIS /*!< IDLE Frame not sent after enabling the transmitter */
165 /**
166   * @}
167   */
168 
169 /** @defgroup USARTEx_Autonomous_Trigger_selection USARTEx Autonomous trigger selection
170   * @brief    USART Autonomous Trigger selection
171   * @{
172   */
173 #define USART_GPDMA1_CH0_TCF_TRG   0U   /*!< USART GPDMA1 channel0 Internal Trigger  */
174 #define USART_GPDMA1_CH1_TCF_TRG   1U   /*!< USART GPDMA1 channel1 Internal Trigger  */
175 #define USART_GPDMA1_CH2_TCF_TRG   2U   /*!< USART GPDMA1 channel2 Internal Trigger  */
176 #define USART_GPDMA1_CH3_TCF_TRG   3U   /*!< USART GPDMA1 channel3 Internal Trigger  */
177 #define USART_EXTI_LINE6_TRG       4U   /*!< USART EXTI line 6 Internal Trigger      */
178 #define USART_EXTI_LINE9_TRG       5U   /*!< USART EXTI line 9 Internal Trigger      */
179 #define USART_LPTIM1_OUT_TRG       6U   /*!< USART LPTIM1 out Internal Trigger       */
180 #if defined(LPTIM2)
181 #define USART_LPTIM2_OUT_TRG       7U   /*!< USART LPTIM2 out Internal Trigger       */
182 #endif /* LPTIM2 */
183 #if defined(COMP12_COMMON)
184 #define USART_COMP1_OUT_TRG        8U   /*!< USART COMP1 out Internal Trigger        */
185 #define USART_COMP2_OUT_TRG        9U   /*!< USART COMP2 out Internal Trigger        */
186 #endif /* COMP12_COMMON */
187 #define USART_RTC_ALRA_TRG         10U  /*!< USART RTC alarm Internal Trigger        */
188 #define USART_RTC_WUT_TRG          11U  /*!< USART RTC wakeup Internal Trigger       */
189 /**
190   * @}
191   */
192 
193 /**
194   * @}
195   */
196 
197 /* Private macros ------------------------------------------------------------*/
198 /** @defgroup USARTEx_Private_Macros USARTEx Private Macros
199   * @{
200   */
201 
202 /** @brief  Compute the USART mask to apply to retrieve the received data
203   *         according to the word length and to the parity bits activation.
204   * @note   If PCE = 1, the parity bit is not included in the data extracted
205   *         by the reception API().
206   *         This masking operation is not carried out in the case of
207   *         DMA transfers.
208   * @param  __HANDLE__ specifies the USART Handle.
209   * @retval None, the mask to apply to USART RDR register is stored in (__HANDLE__)->Mask field.
210   */
211 #define USART_MASK_COMPUTATION(__HANDLE__)                            \
212   do {                                                                \
213     if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_9B)         \
214     {                                                                 \
215       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
216       {                                                               \
217         (__HANDLE__)->Mask = 0x01FFU;                                 \
218       }                                                               \
219       else                                                            \
220       {                                                               \
221         (__HANDLE__)->Mask = 0x00FFU;                                 \
222       }                                                               \
223     }                                                                 \
224     else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_8B)    \
225     {                                                                 \
226       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
227       {                                                               \
228         (__HANDLE__)->Mask = 0x00FFU;                                 \
229       }                                                               \
230       else                                                            \
231       {                                                               \
232         (__HANDLE__)->Mask = 0x007FU;                                 \
233       }                                                               \
234     }                                                                 \
235     else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_7B)    \
236     {                                                                 \
237       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
238       {                                                               \
239         (__HANDLE__)->Mask = 0x007FU;                                 \
240       }                                                               \
241       else                                                            \
242       {                                                               \
243         (__HANDLE__)->Mask = 0x003FU;                                 \
244       }                                                               \
245     }                                                                 \
246     else                                                              \
247     {                                                                 \
248       (__HANDLE__)->Mask = 0x0000U;                                   \
249     }                                                                 \
250   } while(0U)
251 
252 /**
253   * @brief Ensure that USART frame length is valid.
254   * @param __LENGTH__ USART frame length.
255   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
256   */
257 #define IS_USART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == USART_WORDLENGTH_7B) || \
258                                           ((__LENGTH__) == USART_WORDLENGTH_8B) || \
259                                           ((__LENGTH__) == USART_WORDLENGTH_9B))
260 
261 /**
262   * @brief Ensure that USART Negative Slave Select (NSS) pin management is valid.
263   * @param __NSS__ USART Negative Slave Select pin management.
264   * @retval SET (__NSS__ is valid) or RESET (__NSS__ is invalid)
265   */
266 #define IS_USART_NSS(__NSS__) (((__NSS__) == USART_NSS_HARD) || \
267                                ((__NSS__) == USART_NSS_SOFT))
268 
269 /**
270   * @brief Ensure that USART Slave Mode is valid.
271   * @param __STATE__ USART Slave Mode.
272   * @retval SET (__STATE__ is valid) or RESET (__STATE__ is invalid)
273   */
274 #define IS_USART_SLAVEMODE(__STATE__)   (((__STATE__) == USART_SLAVEMODE_DISABLE ) || \
275                                          ((__STATE__) == USART_SLAVEMODE_ENABLE))
276 
277 /**
278   * @brief Ensure that USART FIFO mode is valid.
279   * @param __STATE__ USART FIFO mode.
280   * @retval SET (__STATE__ is valid) or RESET (__STATE__ is invalid)
281   */
282 #define IS_USART_FIFO_MODE_STATE(__STATE__) (((__STATE__) == USART_FIFOMODE_DISABLE ) || \
283                                              ((__STATE__) == USART_FIFOMODE_ENABLE))
284 
285 /**
286   * @brief Ensure that USART TXFIFO threshold level is valid.
287   * @param __THRESHOLD__ USART TXFIFO threshold level.
288   * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid)
289   */
290 #define IS_USART_TXFIFO_THRESHOLD(__THRESHOLD__)  (((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_8)  || \
291                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_4)  || \
292                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_2)  || \
293                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_3_4)  || \
294                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_7_8)  || \
295                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_8_8))
296 
297 /**
298   * @brief Ensure that USART RXFIFO threshold level is valid.
299   * @param __THRESHOLD__ USART RXFIFO threshold level.
300   * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid)
301   */
302 #define IS_USART_RXFIFO_THRESHOLD(__THRESHOLD__)  (((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_8)  || \
303                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_4)  || \
304                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_2)  || \
305                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_3_4)  || \
306                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_7_8)  || \
307                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_8_8))
308 /**
309   * @brief Ensure that USART Trigger polarity state is valid.
310   * @param __POLARITY__ USART Trigger polarity.
311   * @retval SET (__POLARITY__ is valid) or RESET (__POLARITY__ is invalid)
312   */
313 #define IS_USART_TRIGGER_POLARITY(__POLARITY__)  (((__POLARITY__) == USART_TRIG_POLARITY_RISING) ||\
314                                                   ((__POLARITY__) == USART_TRIG_POLARITY_FALLING))
315 
316 /**
317   * @brief Ensure that USART IDLE Frame Transmit state is valid.
318   * @param __IDLE__ USART IDLE Frame Transmit state.
319   * @retval SET (__IDLE__ is valid) or RESET (__IDLE__ is invalid)
320   */
321 #define IS_USART_IDLE_FRAME_TRANSMIT(__IDLE__)  (((__IDLE__) == USART_IDLE_FRAME_ENABLE) ||\
322                                                  ((__IDLE__) == USART_IDLE_FRAME_DISABLE))
323 
324 /**
325   * @brief Ensure that USART Trigger source selection is valid.
326   * @param __SOURCE__ USART Trigger source selection.
327   * @retval SET (__SOURCE__ is valid) or RESET (__SOURCE__ is invalid)
328   */
329 #define IS_USART_TRIGGER_SELECTION(__SOURCE__)    ((__SOURCE__) <= 11U)
330 
331 /**
332   * @brief Ensure that the number of transferred data is valid.
333   * @param __SOURCE__ USART TX data size.
334   * @retval SET (__SOURCE__ is valid) or RESET (__SOURCE__ is invalid)
335   */
336 #define IS_USART_TX_DATA_SIZE(__SOURCE__)  ((__SOURCE__) <= 0xFFFFU)
337 
338 /**
339   * @}
340   */
341 
342 /* Exported functions --------------------------------------------------------*/
343 /** @addtogroup USARTEx_Exported_Functions
344   * @{
345   */
346 
347 /** @addtogroup USARTEx_Exported_Functions_Group1
348   * @{
349   */
350 
351 /* IO operation functions *****************************************************/
352 void HAL_USARTEx_RxFifoFullCallback(USART_HandleTypeDef *husart);
353 void HAL_USARTEx_TxFifoEmptyCallback(USART_HandleTypeDef *husart);
354 
355 /**
356   * @}
357   */
358 
359 /** @addtogroup USARTEx_Exported_Functions_Group2
360   * @{
361   */
362 
363 /* Peripheral Control functions ***********************************************/
364 HAL_StatusTypeDef HAL_USARTEx_EnableSlaveMode(USART_HandleTypeDef *husart);
365 HAL_StatusTypeDef HAL_USARTEx_DisableSlaveMode(USART_HandleTypeDef *husart);
366 HAL_StatusTypeDef HAL_USARTEx_ConfigNSS(USART_HandleTypeDef *husart, uint32_t NSSConfig);
367 HAL_StatusTypeDef HAL_USARTEx_EnableFifoMode(USART_HandleTypeDef *husart);
368 HAL_StatusTypeDef HAL_USARTEx_DisableFifoMode(USART_HandleTypeDef *husart);
369 HAL_StatusTypeDef HAL_USARTEx_SetTxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold);
370 HAL_StatusTypeDef HAL_USARTEx_SetRxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold);
371 HAL_StatusTypeDef HAL_USARTEx_SetConfigAutonomousMode(USART_HandleTypeDef *husart,
372                                                       const USART_AutonomousModeConfTypeDef *sConfig);
373 HAL_StatusTypeDef HAL_USARTEx_GetConfigAutonomousMode(const USART_HandleTypeDef *husart,
374                                                       USART_AutonomousModeConfTypeDef *sConfig);
375 HAL_StatusTypeDef HAL_USARTEx_ClearConfigAutonomousMode(USART_HandleTypeDef *husart);
376 
377 /**
378   * @}
379   */
380 
381 /**
382   * @}
383   */
384 
385 /**
386   * @}
387   */
388 
389 /**
390   * @}
391   */
392 
393 #ifdef __cplusplus
394 }
395 #endif
396 
397 #endif /* STM32WBAxx_HAL_USART_EX_H */
398 
399