1 /**
2   ******************************************************************************
3   * @file    stm32u5xx_hal_hcd.h
4   * @author  MCD Application Team
5   * @brief   Header file of HCD HAL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2021 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 STM32U5xx_HAL_HCD_H
21 #define STM32U5xx_HAL_HCD_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32u5xx_ll_usb.h"
29 
30 #if defined (USB_OTG_FS) || defined (USB_OTG_HS) || defined (USB_DRD_FS)
31 /** @addtogroup STM32U5xx_HAL_Driver
32   * @{
33   */
34 
35 /** @addtogroup HCD HCD
36   * @{
37   */
38 
39 /* Exported types ------------------------------------------------------------*/
40 /** @defgroup HCD_Exported_Types HCD Exported Types
41   * @{
42   */
43 
44 /** @defgroup HCD_Exported_Types_Group1 HCD State Structure definition
45   * @{
46   */
47 typedef enum
48 {
49   HAL_HCD_STATE_RESET    = 0x00,
50   HAL_HCD_STATE_READY    = 0x01,
51   HAL_HCD_STATE_ERROR    = 0x02,
52   HAL_HCD_STATE_BUSY     = 0x03,
53   HAL_HCD_STATE_TIMEOUT  = 0x04
54 } HCD_StateTypeDef;
55 
56 #if defined (USB_DRD_FS)
57 typedef USB_DRD_TypeDef         HCD_TypeDef;
58 typedef USB_DRD_CfgTypeDef      HCD_InitTypeDef;
59 typedef USB_DRD_HCTypeDef       HCD_HCTypeDef;
60 typedef USB_DRD_URBStateTypeDef HCD_URBStateTypeDef;
61 typedef USB_DRD_HCStateTypeDef  HCD_HCStateTypeDef;
62 #else
63 typedef USB_OTG_GlobalTypeDef   HCD_TypeDef;
64 typedef USB_OTG_CfgTypeDef      HCD_InitTypeDef;
65 typedef USB_OTG_HCTypeDef       HCD_HCTypeDef;
66 typedef USB_OTG_URBStateTypeDef HCD_URBStateTypeDef;
67 typedef USB_OTG_HCStateTypeDef  HCD_HCStateTypeDef;
68 #endif /* defined (USB_DRD_FS) */
69 #if defined (USB_DRD_FS)
70 typedef enum
71 {
72   HCD_HCD_STATE_DISCONNECTED = 0x00U,
73   HCD_HCD_STATE_CONNECTED    = 0x01U,
74   HCD_HCD_STATE_RESETED      = 0x02U,
75   HCD_HCD_STATE_RUN          = 0x03U,
76   HCD_HCD_STATE_SUSPEND      = 0x04U,
77   HCD_HCD_STATE_RESUME       = 0x05U,
78 } HCD_HostStateTypeDef;
79 
80 /* PMA lookup Table size depending on PMA Size
81  * 8Bytes each Block 32Bit in each word
82  */
83 #define PMA_BLOCKS        ((USB_DRD_PMA_SIZE) / (8U * 32U))
84 #endif /* defined (USB_DRD_FS) */
85 /**
86   * @}
87   */
88 
89 /** @defgroup HCD_Exported_Types_Group2 HCD Handle Structure definition
90   * @{
91   */
92 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
93 typedef struct __HCD_HandleTypeDef
94 #else
95 typedef struct
96 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
97 {
98   HCD_TypeDef               *Instance;  /*!< Register base address    */
99   HCD_InitTypeDef           Init;       /*!< HCD required parameters  */
100   HCD_HCTypeDef             hc[16];     /*!< Host channels parameters */
101 #if defined (USB_DRD_FS)
102   uint32_t                  ep0_PmaAllocState;  /*!< EP0 PMA allocation State (allocated, virtual Ch, EP0 direction) */
103   uint16_t                  phy_chin_state[8];  /*!< Physical Channel in State (Used/Free) */
104   uint16_t                  phy_chout_state[8]; /*!< Physical Channel out State (Used/Free)*/
105   uint32_t                  PMALookupTable[PMA_BLOCKS]; /*PMA LookUp Table */
106   HCD_HostStateTypeDef      HostState; /*!< USB current state DICONNECT/CONNECT/RUN/SUSPEND/RESUME */
107 #endif /* defined (USB_DRD_FS) */
108   HAL_LockTypeDef           Lock;       /*!< HCD peripheral status    */
109   __IO HCD_StateTypeDef     State;      /*!< HCD communication state  */
110   __IO  uint32_t            ErrorCode;  /*!< HCD Error code           */
111   void                      *pData;     /*!< Pointer Stack Handler    */
112 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
113   void (* SOFCallback)(struct __HCD_HandleTypeDef *hhcd);                               /*!< USB OTG HCD SOF callback                */
114   void (* ConnectCallback)(struct __HCD_HandleTypeDef *hhcd);                           /*!< USB OTG HCD Connect callback            */
115   void (* DisconnectCallback)(struct __HCD_HandleTypeDef *hhcd);                        /*!< USB OTG HCD Disconnect callback         */
116   void (* PortEnabledCallback)(struct __HCD_HandleTypeDef *hhcd);                       /*!< USB OTG HCD Port Enable callback        */
117   void (* PortDisabledCallback)(struct __HCD_HandleTypeDef *hhcd);                      /*!< USB OTG HCD Port Disable callback       */
118   void (* HC_NotifyURBChangeCallback)(struct __HCD_HandleTypeDef *hhcd, uint8_t chnum,
119                                       HCD_URBStateTypeDef urb_state);                   /*!< USB OTG HCD Host Channel Notify URB Change callback  */
120 
121   void (* MspInitCallback)(struct __HCD_HandleTypeDef *hhcd);                           /*!< USB OTG HCD Msp Init callback           */
122   void (* MspDeInitCallback)(struct __HCD_HandleTypeDef *hhcd);                         /*!< USB OTG HCD Msp DeInit callback         */
123 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
124 } HCD_HandleTypeDef;
125 /**
126   * @}
127   */
128 
129 /**
130   * @}
131   */
132 
133 /* Exported constants --------------------------------------------------------*/
134 /** @defgroup HCD_Exported_Constants HCD Exported Constants
135   * @{
136   */
137 
138 /** @defgroup HCD_Speed HCD Speed
139   * @{
140   */
141 #define HCD_SPEED_HIGH               USBH_HS_SPEED
142 #define HCD_SPEED_FULL               USBH_FSLS_SPEED
143 #define HCD_SPEED_LOW                USBH_FSLS_SPEED
144 /**
145   * @}
146   */
147 
148 /** @defgroup HCD_Device_Speed HCD Device Speed
149   * @{
150   */
151 #define HCD_DEVICE_SPEED_HIGH               0U
152 #define HCD_DEVICE_SPEED_FULL               1U
153 #define HCD_DEVICE_SPEED_LOW                2U
154 /**
155   * @}
156   */
157 
158 /** @defgroup HCD_PHY_Module HCD PHY Module
159   * @{
160   */
161 #define HCD_PHY_ULPI                 1U
162 #define HCD_PHY_EMBEDDED             2U
163 /**
164   * @}
165   */
166 
167 /** @defgroup HCD_Error_Code_definition HCD Error Code definition
168   * @brief  HCD Error Code definition
169   * @{
170   */
171 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
172 #define  HAL_HCD_ERROR_INVALID_CALLBACK                        (0x00000010U)    /*!< Invalid Callback error  */
173 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
174 
175 /**
176   * @}
177   */
178 
179 /**
180   * @}
181   */
182 
183 /* Exported macro ------------------------------------------------------------*/
184 /** @defgroup HCD_Exported_Macros HCD Exported Macros
185   *  @brief macros to handle interrupts and specific clock configurations
186   * @{
187   */
188 #define __HAL_HCD_ENABLE(__HANDLE__)                   (void)USB_EnableGlobalInt ((__HANDLE__)->Instance)
189 #define __HAL_HCD_DISABLE(__HANDLE__)                  (void)USB_DisableGlobalInt ((__HANDLE__)->Instance)
190 
191 #define __HAL_HCD_GET_FLAG(__HANDLE__, __INTERRUPT__)      ((USB_ReadInterrupts((__HANDLE__)->Instance)\
192                                                              & (__INTERRUPT__)) == (__INTERRUPT__))
193 #if defined (USB_DRD_FS)
194 #define __HAL_HCD_CLEAR_FLAG(__HANDLE__, __INTERRUPT__)    (((__HANDLE__)->Instance->ISTR) &= ~(__INTERRUPT__))
195 #else
196 #define __HAL_HCD_GET_CH_FLAG(__HANDLE__, __chnum__, __INTERRUPT__) \
197   ((USB_ReadChInterrupts((__HANDLE__)->Instance, (__chnum__)) & (__INTERRUPT__)) == (__INTERRUPT__))
198 
199 #define __HAL_HCD_CLEAR_FLAG(__HANDLE__, __INTERRUPT__)    (((__HANDLE__)->Instance->GINTSTS) = (__INTERRUPT__))
200 #endif /* defined (USB_DRD_FS) */
201 #define __HAL_HCD_IS_INVALID_INTERRUPT(__HANDLE__)         (USB_ReadInterrupts((__HANDLE__)->Instance) == 0U)
202 
203 #if defined (USB_DRD_FS)
204 #define __HAL_HCD_GET_CHNUM(__HANDLE__)                    (((__HANDLE__)->Instance->ISTR) & USB_ISTR_IDN)
205 #define __HAL_HCD_GET_CHDIR(__HANDLE__)                    (((__HANDLE__)->Instance->ISTR) & USB_ISTR_DIR)
206 #else
207 #define __HAL_HCD_CLEAR_HC_INT(chnum, __INTERRUPT__)  (USBx_HC(chnum)->HCINT = (__INTERRUPT__))
208 #define __HAL_HCD_MASK_HALT_HC_INT(chnum)             (USBx_HC(chnum)->HCINTMSK &= ~USB_OTG_HCINTMSK_CHHM)
209 #define __HAL_HCD_UNMASK_HALT_HC_INT(chnum)           (USBx_HC(chnum)->HCINTMSK |= USB_OTG_HCINTMSK_CHHM)
210 #define __HAL_HCD_MASK_ACK_HC_INT(chnum)              (USBx_HC(chnum)->HCINTMSK &= ~USB_OTG_HCINTMSK_ACKM)
211 #define __HAL_HCD_UNMASK_ACK_HC_INT(chnum)            (USBx_HC(chnum)->HCINTMSK |= USB_OTG_HCINTMSK_ACKM)
212 #define __HAL_HCD_SET_HC_CSPLT(chnum)                 (USBx_HC(chnum)->HCSPLT   |= USB_OTG_HCSPLT_COMPLSPLT)
213 #define __HAL_HCD_CLEAR_HC_CSPLT(chnum)               (USBx_HC(chnum)->HCSPLT   &= ~USB_OTG_HCSPLT_COMPLSPLT)
214 #define __HAL_HCD_CLEAR_HC_SSPLT(chnum)               (USBx_HC(chnum)->HCSPLT   &= ~USB_OTG_HCSPLT_SPLITEN)
215 #endif /* defined (USB_DRD_FS) */
216 /**
217   * @}
218   */
219 
220 /* Exported functions --------------------------------------------------------*/
221 /** @addtogroup HCD_Exported_Functions HCD Exported Functions
222   * @{
223   */
224 
225 /** @defgroup HCD_Exported_Functions_Group1 Initialization and de-initialization functions
226   * @{
227   */
228 HAL_StatusTypeDef HAL_HCD_Init(HCD_HandleTypeDef *hhcd);
229 HAL_StatusTypeDef HAL_HCD_DeInit(HCD_HandleTypeDef *hhcd);
230 HAL_StatusTypeDef HAL_HCD_HC_Init(HCD_HandleTypeDef *hhcd, uint8_t ch_num,
231                                   uint8_t epnum, uint8_t dev_address,
232                                   uint8_t speed, uint8_t ep_type, uint16_t mps);
233 
234 HAL_StatusTypeDef HAL_HCD_HC_Halt(HCD_HandleTypeDef *hhcd, uint8_t ch_num);
235 #if defined (USB_DRD_FS)
236 HAL_StatusTypeDef HAL_HCD_HC_Close(HCD_HandleTypeDef *hhcd, uint8_t ch_num);
237 #endif /* defined (USB_DRD_FS) */
238 void              HAL_HCD_MspInit(HCD_HandleTypeDef *hhcd);
239 void              HAL_HCD_MspDeInit(HCD_HandleTypeDef *hhcd);
240 
241 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
242 /** @defgroup HAL_HCD_Callback_ID_enumeration_definition HAL USB OTG HCD Callback ID enumeration definition
243   * @brief  HAL USB OTG HCD Callback ID enumeration definition
244   * @{
245   */
246 typedef enum
247 {
248   HAL_HCD_SOF_CB_ID            = 0x01,       /*!< USB HCD SOF callback ID           */
249   HAL_HCD_CONNECT_CB_ID        = 0x02,       /*!< USB HCD Connect callback ID       */
250   HAL_HCD_DISCONNECT_CB_ID     = 0x03,       /*!< USB HCD Disconnect callback ID    */
251   HAL_HCD_PORT_ENABLED_CB_ID   = 0x04,       /*!< USB HCD Port Enable callback ID   */
252   HAL_HCD_PORT_DISABLED_CB_ID  = 0x05,       /*!< USB HCD Port Disable callback ID  */
253 
254   HAL_HCD_MSPINIT_CB_ID        = 0x06,       /*!< USB HCD MspInit callback ID       */
255   HAL_HCD_MSPDEINIT_CB_ID      = 0x07        /*!< USB HCD MspDeInit callback ID     */
256 
257 } HAL_HCD_CallbackIDTypeDef;
258 /**
259   * @}
260   */
261 
262 /** @defgroup HAL_HCD_Callback_pointer_definition HAL USB OTG HCD Callback pointer definition
263   * @brief  HAL USB OTG HCD Callback pointer definition
264   * @{
265   */
266 
267 typedef void (*pHCD_CallbackTypeDef)(HCD_HandleTypeDef *hhcd);                   /*!< pointer to a common USB OTG HCD callback function  */
268 typedef void (*pHCD_HC_NotifyURBChangeCallbackTypeDef)(HCD_HandleTypeDef *hhcd,
269                                                        uint8_t epnum,
270                                                        HCD_URBStateTypeDef urb_state);   /*!< pointer to USB OTG HCD host channel  callback */
271 /**
272   * @}
273   */
274 
275 HAL_StatusTypeDef HAL_HCD_RegisterCallback(HCD_HandleTypeDef *hhcd,
276                                            HAL_HCD_CallbackIDTypeDef CallbackID,
277                                            pHCD_CallbackTypeDef pCallback);
278 
279 HAL_StatusTypeDef HAL_HCD_UnRegisterCallback(HCD_HandleTypeDef *hhcd,
280                                              HAL_HCD_CallbackIDTypeDef CallbackID);
281 
282 HAL_StatusTypeDef HAL_HCD_RegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd,
283                                                              pHCD_HC_NotifyURBChangeCallbackTypeDef pCallback);
284 
285 HAL_StatusTypeDef HAL_HCD_UnRegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd);
286 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
287 /**
288   * @}
289   */
290 
291 /* I/O operation functions  ***************************************************/
292 /** @addtogroup HCD_Exported_Functions_Group2 Input and Output operation functions
293   * @{
294   */
295 HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd, uint8_t ch_num,
296                                            uint8_t direction, uint8_t ep_type,
297                                            uint8_t token, uint8_t *pbuff,
298                                            uint16_t length, uint8_t do_ping);
299 
300 HAL_StatusTypeDef HAL_HCD_HC_SetHubInfo(HCD_HandleTypeDef *hhcd, uint8_t ch_num,
301                                         uint8_t addr, uint8_t PortNbr);
302 
303 HAL_StatusTypeDef HAL_HCD_HC_ClearHubInfo(HCD_HandleTypeDef *hhcd, uint8_t ch_num);
304 
305 /* Non-Blocking mode: Interrupt */
306 void HAL_HCD_IRQHandler(HCD_HandleTypeDef *hhcd);
307 void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd);
308 void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd);
309 void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd);
310 void HAL_HCD_PortEnabled_Callback(HCD_HandleTypeDef *hhcd);
311 void HAL_HCD_PortDisabled_Callback(HCD_HandleTypeDef *hhcd);
312 #if defined (USB_DRD_FS)
313 void HAL_HCD_SuspendCallback(HCD_HandleTypeDef *hhcd);
314 void HAL_HCD_ResumeCallback(HCD_HandleTypeDef *hhcd);
315 #endif /* defined (USB_DRD_FS) */
316 void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum,
317                                          HCD_URBStateTypeDef urb_state);
318 /**
319   * @}
320   */
321 
322 /* Peripheral Control functions  **********************************************/
323 /** @addtogroup HCD_Exported_Functions_Group3 Peripheral Control functions
324   * @{
325   */
326 HAL_StatusTypeDef HAL_HCD_ResetPort(HCD_HandleTypeDef *hhcd);
327 HAL_StatusTypeDef HAL_HCD_Start(HCD_HandleTypeDef *hhcd);
328 HAL_StatusTypeDef HAL_HCD_Stop(HCD_HandleTypeDef *hhcd);
329 #if defined (USB_DRD_FS)
330 HAL_StatusTypeDef HAL_HCD_Suspend(HCD_HandleTypeDef *hhcd);
331 HAL_StatusTypeDef HAL_HCD_Resume(HCD_HandleTypeDef *hhcd);
332 HAL_StatusTypeDef HAL_HCD_ResumePort(HCD_HandleTypeDef *hhcd);
333 #endif /* defined (USB_DRD_FS) */
334 /**
335   * @}
336   */
337 
338 /* Peripheral State functions  ************************************************/
339 /** @addtogroup HCD_Exported_Functions_Group4 Peripheral State functions
340   * @{
341   */
342 HCD_StateTypeDef        HAL_HCD_GetState(HCD_HandleTypeDef const *hhcd);
343 HCD_URBStateTypeDef     HAL_HCD_HC_GetURBState(HCD_HandleTypeDef const *hhcd, uint8_t chnum);
344 HCD_HCStateTypeDef      HAL_HCD_HC_GetState(HCD_HandleTypeDef const *hhcd, uint8_t chnum);
345 uint32_t                HAL_HCD_HC_GetXferCount(HCD_HandleTypeDef const *hhcd, uint8_t chnum);
346 uint32_t                HAL_HCD_GetCurrentFrame(HCD_HandleTypeDef *hhcd);
347 uint32_t                HAL_HCD_GetCurrentSpeed(HCD_HandleTypeDef *hhcd);
348 
349 #if defined (USB_DRD_FS)
350 /* PMA Allocation functions  **********************************************/
351 /** @addtogroup PMA Allocation
352   * @{
353   */
354 HAL_StatusTypeDef  HAL_HCD_PMAlloc(HCD_HandleTypeDef *hhcd, uint8_t ch_num,
355                                    uint16_t ch_kind, uint16_t mps);
356 
357 HAL_StatusTypeDef  HAL_HCD_PMADeAlloc(HCD_HandleTypeDef *hhcd, uint8_t ch_num);
358 HAL_StatusTypeDef  HAL_HCD_PMAReset(HCD_HandleTypeDef *hhcd);
359 
360 /**
361   * @}
362   */
363 #endif /* defined (USB_DRD_FS) */
364 
365 /**
366   * @}
367   */
368 
369 /* Private macros ------------------------------------------------------------*/
370 /** @defgroup HCD_Private_Macros HCD Private Macros
371   * @{
372   */
373 #if defined (USB_DRD_FS)
374 #define HCD_MIN(a, b)  (((a) < (b)) ? (a) : (b))
375 #define HCD_MAX(a, b)  (((a) > (b)) ? (a) : (b))
376 
377 /** @defgroup HCD_LOGICAL_CHANNEL HCD Logical Channel
378   * @{
379   */
380 #define HCD_LOGICAL_CH_NOT_OPENED             0xFFU
381 #define HCD_FREE_CH_NOT_FOUND                 0xFFU
382 /**
383   * @}
384   */
385 
386 /** @defgroup HCD_ENDP_Kind HCD Endpoint Kind
387   * @{
388   */
389 #define HCD_SNG_BUF                            0U
390 #define HCD_DBL_BUF                            1U
391 /**
392   * @}
393   */
394 
395 /* Set Channel */
396 #define HCD_SET_CHANNEL                        USB_DRD_SET_CHEP
397 
398 /* Get Channel Register */
399 #define HCD_GET_CHANNEL                        USB_DRD_GET_CHEP
400 
401 
402 /**
403   * @brief free buffer used from the application realizing it to the line
404   *         toggles bit SW_BUF in the double buffered endpoint register
405   * @param USBx USB device.
406   * @param   bChNum, bDir
407   * @retval None
408   */
409 #define HCD_FREE_USER_BUFFER                   USB_DRD_FREE_USER_BUFFER
410 
411 /**
412   * @brief Set the Setup bit in the corresponding channel, when a Setup
413      transaction is needed.
414   * @param USBx USB device.
415   * @param   bChNum
416   * @retval None
417   */
418 #define HAC_SET_CH_TX_SETUP                    USB_DRD_CHEP_TX_SETUP
419 
420 /**
421   * @brief  sets the status for tx transfer (bits STAT_TX[1:0]).
422   * @param  USBx USB peripheral instance register address.
423   * @param  bChNum Endpoint Number.
424   * @param  wState new state
425   * @retval None
426   */
427 #define HCD_SET_CH_TX_STATUS                   USB_DRD_SET_CHEP_TX_STATUS
428 
429 /**
430   * @brief  sets the status for rx transfer (bits STAT_TX[1:0])
431   * @param  USBx USB peripheral instance register address.
432   * @param  bChNum Endpoint Number.
433   * @param  wState new state
434   * @retval None
435   */
436 #define HCD_SET_CH_RX_STATUS                   USB_DRD_SET_CHEP_RX_STATUS
437 /**
438   * @brief  gets the status for tx/rx transfer (bits STAT_TX[1:0]
439   *         /STAT_RX[1:0])
440   * @param  USBx USB peripheral instance register address.
441   * @param  bChNum Endpoint Number.
442   * @retval status
443   */
444 #define HCD_GET_CH_TX_STATUS                   USB_DRD_GET_CHEP_TX_STATUS
445 #define HCD_GET_CH_RX_STATUS                   USB_DRD_GET_CHEP_RX_STATUS
446 /**
447   * @brief  Sets/clears CH_KIND bit in the Channel register.
448   * @param  USBx USB peripheral instance register address.
449   * @param  bChNum Endpoint Number.
450   * @retval None
451   */
452 #define HCD_SET_CH_KIND                        USB_DRD_SET_CH_KIND
453 #define HCD_CLEAR_CH_KIND                      USB_DRD_CLEAR_CH_KIND
454 #define HCD_SET_BULK_CH_DBUF                   HCD_SET_CH_KIND
455 #define HCD_CLEAR_BULK_CH_DBUF                 HCD_CLEAR_CH_KIND
456 
457 /**
458   * @brief  Clears bit ERR_RX in the Channel register
459   * @param  USBx USB peripheral instance register address.
460   * @param  bChNum Endpoint Number.
461   * @retval None
462   */
463 #define HCD_CLEAR_RX_CH_ERR                    USB_DRD_CLEAR_CHEP_RX_ERR
464 
465 /**
466   * @brief  Clears bit ERR_TX in the Channel register
467   * @param  USBx USB peripheral instance register address.
468   * @param  bChNum Endpoint Number.
469   * @retval None
470   */
471 #define HCD_CLEAR_TX_CH_ERR                    USB_DRD_CLEAR_CHEP_TX_ERR
472 /**
473   * @brief  Clears bit CTR_RX / CTR_TX in the endpoint register.
474   * @param  USBx USB peripheral instance register address.
475   * @param  bChNum Endpoint Number.
476   * @retval None
477   */
478 #define HCD_CLEAR_RX_CH_CTR                    USB_DRD_CLEAR_RX_CHEP_CTR
479 #define HCD_CLEAR_TX_CH_CTR                    USB_DRD_CLEAR_TX_CHEP_CTR
480 
481 /**
482   * @brief  Toggles DTOG_RX / DTOG_TX bit in the endpoint register.
483   * @param  USBx USB peripheral instance register address.
484   * @param  bChNum Endpoint Number.
485   * @retval None
486   */
487 #define HCD_RX_DTOG                            USB_DRD_RX_DTOG
488 #define HCD_TX_DTOG                            USB_DRD_TX_DTOG
489 /**
490   * @brief  Clears DTOG_RX / DTOG_TX bit in the endpoint register.
491   * @param  USBx USB peripheral instance register address.
492   * @param  bChNum Endpoint Number.
493   * @retval None
494   */
495 #define HCD_CLEAR_RX_DTOG                      USB_DRD_CLEAR_RX_DTOG
496 #define HCD_CLEAR_TX_DTOG                      USB_DRD_CLEAR_TX_DTOG
497 
498 /**
499   * @brief  sets counter for the tx/rx buffer.
500   * @param  USBx USB peripheral instance register address.
501   * @param  bChNum Endpoint Number.
502   * @param  wCount Counter value.
503   * @retval None
504   */
505 #define HCD_SET_CH_TX_CNT                      USB_DRD_SET_CHEP_TX_CNT
506 #define HCD_SET_CH_RX_CNT                      USB_DRD_SET_CHEP_RX_CNT
507 
508 /**
509   * @brief  gets counter of the tx buffer.
510   * @param  USBx USB peripheral instance register address.
511   * @param  bChNum channel Number.
512   * @retval Counter value
513   */
514 #define HCD_GET_CH_TX_CNT                      USB_DRD_GET_CHEP_TX_CNT
515 
516 /**
517   * @brief  gets counter of the rx buffer.
518   * @param  Instance USB peripheral instance register address.
519   * @param  bChNum channel Number.
520   * @retval Counter value
521   */
HCD_GET_CH_RX_CNT(HCD_TypeDef * Instance,uint16_t bChNum)522 __STATIC_INLINE uint16_t HCD_GET_CH_RX_CNT(HCD_TypeDef *Instance, uint16_t bChNum)
523 {
524   uint32_t HostCoreSpeed;
525   __IO uint32_t count = 10U;
526 
527   /* Get Host core Speed */
528   HostCoreSpeed = USB_GetHostSpeed(Instance);
529 
530   /* Count depends on device LS */
531   if (HostCoreSpeed == USB_DRD_SPEED_LS)
532   {
533     count = (70U * (HAL_RCC_GetHCLKFreq() / 1000000U)) / 100U;
534   }
535 
536   if (count > 15U)
537   {
538     count = HCD_MAX(10U, (count - 15U));
539   }
540 
541   /* WA: few cycles for RX PMA descriptor to update */
542   while (count > 0U)
543   {
544     count--;
545   }
546 
547   return (uint16_t)USB_DRD_GET_CHEP_RX_CNT((Instance), (bChNum));
548 }
549 
550 /**
551   * @brief  Gets buffer 0/1 address of a double buffer endpoint.
552   * @param  USBx USB peripheral instance register address.
553   * @param  bChNum Endpoint Number.
554   * @param  bDir endpoint dir  EP_DBUF_OUT = OUT
555   *         EP_DBUF_IN  = IN
556   * @param  wCount: Counter value
557   * @retval None
558   */
559 #define HCD_SET_CH_DBUF0_CNT                   USB_DRD_SET_CHEP_DBUF0_CNT
560 #define HCD_SET_CH_DBUF1_CNT                   USB_DRD_SET_CHEP_DBUF1_CNT
561 #define HCD_SET_CH_DBUF_CNT                    USB_DRD_SET_CHEP_DBUF_CNT
562 
563 
564 /**
565   * @brief  gets counter of the rx buffer0.
566   * @param  Instance USB peripheral instance register address.
567   * @param  bChNum channel Number.
568   * @retval Counter value
569   */
HCD_GET_CH_DBUF0_CNT(const HCD_TypeDef * Instance,uint16_t bChNum)570 __STATIC_INLINE uint16_t HCD_GET_CH_DBUF0_CNT(const HCD_TypeDef *Instance, uint16_t bChNum)
571 {
572   UNUSED(Instance);
573   __IO uint32_t count = 10U;
574 
575   /* WA: few cycles for RX PMA descriptor to update */
576   while (count > 0U)
577   {
578     count--;
579   }
580 
581   return (uint16_t)USB_DRD_GET_CHEP_DBUF0_CNT((Instance), (bChNum));
582 }
583 
584 /**
585   * @brief  gets counter of the rx buffer1.
586   * @param  Instance USB peripheral instance register address.
587   * @param  bChNum channel Number.
588   * @retval Counter value
589   */
HCD_GET_CH_DBUF1_CNT(const HCD_TypeDef * Instance,uint16_t bChNum)590 __STATIC_INLINE uint16_t HCD_GET_CH_DBUF1_CNT(const HCD_TypeDef *Instance, uint16_t bChNum)
591 {
592   UNUSED(Instance);
593   __IO uint32_t count = 10U;
594 
595   /* WA: few cycles for RX PMA descriptor to update */
596   while (count > 0U)
597   {
598     count--;
599   }
600 
601   return (uint16_t)USB_DRD_GET_CHEP_DBUF1_CNT((Instance), (bChNum));
602 }
603 #endif /* defined (USB_DRD_FS) */
604 
605 /**
606   * @}
607   */
608 /* Private functions prototypes ----------------------------------------------*/
609 
610 /**
611   * @}
612   */
613 /**
614   * @}
615   */
616 /**
617   * @}
618   */
619 #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) || defined (USB_DRD_FS) */
620 
621 #ifdef __cplusplus
622 }
623 #endif
624 
625 #endif /* STM32U5xx_HAL_HCD_H */
626