1 /**
2   ******************************************************************************
3   * @file    stm32g4xx_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) 2019 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 STM32G4xx_HAL_USART_EX_H
21 #define STM32G4xx_HAL_USART_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32g4xx_hal_def.h"
29 
30 /** @addtogroup STM32G4xx_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 /** @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   */
115 
116 /* Private macros ------------------------------------------------------------*/
117 /** @defgroup USARTEx_Private_Macros USARTEx Private Macros
118   * @{
119   */
120 
121 /** @brief  Compute the USART mask to apply to retrieve the received data
122   *         according to the word length and to the parity bits activation.
123   * @note   If PCE = 1, the parity bit is not included in the data extracted
124   *         by the reception API().
125   *         This masking operation is not carried out in the case of
126   *         DMA transfers.
127   * @param  __HANDLE__ specifies the USART Handle.
128   * @retval None, the mask to apply to USART RDR register is stored in (__HANDLE__)->Mask field.
129   */
130 #define USART_MASK_COMPUTATION(__HANDLE__)                            \
131   do {                                                                \
132     if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_9B)         \
133     {                                                                 \
134       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
135       {                                                               \
136         (__HANDLE__)->Mask = 0x01FFU;                                 \
137       }                                                               \
138       else                                                            \
139       {                                                               \
140         (__HANDLE__)->Mask = 0x00FFU;                                 \
141       }                                                               \
142     }                                                                 \
143     else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_8B)    \
144     {                                                                 \
145       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
146       {                                                               \
147         (__HANDLE__)->Mask = 0x00FFU;                                 \
148       }                                                               \
149       else                                                            \
150       {                                                               \
151         (__HANDLE__)->Mask = 0x007FU;                                 \
152       }                                                               \
153     }                                                                 \
154     else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_7B)    \
155     {                                                                 \
156       if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE)             \
157       {                                                               \
158         (__HANDLE__)->Mask = 0x007FU;                                 \
159       }                                                               \
160       else                                                            \
161       {                                                               \
162         (__HANDLE__)->Mask = 0x003FU;                                 \
163       }                                                               \
164     }                                                                 \
165     else                                                              \
166     {                                                                 \
167       (__HANDLE__)->Mask = 0x0000U;                                   \
168     }                                                                 \
169   } while(0U)
170 
171 /**
172   * @brief Ensure that USART frame length is valid.
173   * @param __LENGTH__ USART frame length.
174   * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid)
175   */
176 #define IS_USART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == USART_WORDLENGTH_7B) || \
177                                           ((__LENGTH__) == USART_WORDLENGTH_8B) || \
178                                           ((__LENGTH__) == USART_WORDLENGTH_9B))
179 
180 /**
181   * @brief Ensure that USART Negative Slave Select (NSS) pin management is valid.
182   * @param __NSS__ USART Negative Slave Select pin management.
183   * @retval SET (__NSS__ is valid) or RESET (__NSS__ is invalid)
184   */
185 #define IS_USART_NSS(__NSS__) (((__NSS__) == USART_NSS_HARD) || \
186                                ((__NSS__) == USART_NSS_SOFT))
187 
188 /**
189   * @brief Ensure that USART Slave Mode is valid.
190   * @param __STATE__ USART Slave Mode.
191   * @retval SET (__STATE__ is valid) or RESET (__STATE__ is invalid)
192   */
193 #define IS_USART_SLAVEMODE(__STATE__)   (((__STATE__) == USART_SLAVEMODE_DISABLE ) || \
194                                          ((__STATE__) == USART_SLAVEMODE_ENABLE))
195 
196 /**
197   * @brief Ensure that USART FIFO mode is valid.
198   * @param __STATE__ USART FIFO mode.
199   * @retval SET (__STATE__ is valid) or RESET (__STATE__ is invalid)
200   */
201 #define IS_USART_FIFO_MODE_STATE(__STATE__) (((__STATE__) == USART_FIFOMODE_DISABLE ) || \
202                                              ((__STATE__) == USART_FIFOMODE_ENABLE))
203 
204 /**
205   * @brief Ensure that USART TXFIFO threshold level is valid.
206   * @param __THRESHOLD__ USART TXFIFO threshold level.
207   * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid)
208   */
209 #define IS_USART_TXFIFO_THRESHOLD(__THRESHOLD__)  (((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_8)  || \
210                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_4)  || \
211                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_2)  || \
212                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_3_4)  || \
213                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_7_8)  || \
214                                                    ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_8_8))
215 
216 /**
217   * @brief Ensure that USART RXFIFO threshold level is valid.
218   * @param __THRESHOLD__ USART RXFIFO threshold level.
219   * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid)
220   */
221 #define IS_USART_RXFIFO_THRESHOLD(__THRESHOLD__)  (((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_8)  || \
222                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_4)  || \
223                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_2)  || \
224                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_3_4)  || \
225                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_7_8)  || \
226                                                    ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_8_8))
227 /**
228   * @}
229   */
230 
231 /* Exported functions --------------------------------------------------------*/
232 /** @addtogroup USARTEx_Exported_Functions
233   * @{
234   */
235 
236 /** @addtogroup USARTEx_Exported_Functions_Group1
237   * @{
238   */
239 
240 /* IO operation functions *****************************************************/
241 void HAL_USARTEx_RxFifoFullCallback(USART_HandleTypeDef *husart);
242 void HAL_USARTEx_TxFifoEmptyCallback(USART_HandleTypeDef *husart);
243 
244 /**
245   * @}
246   */
247 
248 /** @addtogroup USARTEx_Exported_Functions_Group2
249   * @{
250   */
251 
252 /* Peripheral Control functions ***********************************************/
253 HAL_StatusTypeDef HAL_USARTEx_EnableSlaveMode(USART_HandleTypeDef *husart);
254 HAL_StatusTypeDef HAL_USARTEx_DisableSlaveMode(USART_HandleTypeDef *husart);
255 HAL_StatusTypeDef HAL_USARTEx_ConfigNSS(USART_HandleTypeDef *husart, uint32_t NSSConfig);
256 HAL_StatusTypeDef HAL_USARTEx_EnableFifoMode(USART_HandleTypeDef *husart);
257 HAL_StatusTypeDef HAL_USARTEx_DisableFifoMode(USART_HandleTypeDef *husart);
258 HAL_StatusTypeDef HAL_USARTEx_SetTxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold);
259 HAL_StatusTypeDef HAL_USARTEx_SetRxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold);
260 
261 /**
262   * @}
263   */
264 
265 /**
266   * @}
267   */
268 
269 /**
270   * @}
271   */
272 
273 /**
274   * @}
275   */
276 
277 #ifdef __cplusplus
278 }
279 #endif
280 
281 #endif /* STM32G4xx_HAL_USART_EX_H */
282 
283