/********************************** (C) COPYRIGHT  *******************************
* File Name          : ch32v30x_usbhs_host.h
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/06/06
* Description        : This file contains all the functions prototypes for the USB 
*                      Host firmware library.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/ 
#ifndef __CH32V30x_USBHS_HOST_H
#define __CH32V30x_USBHS_HOST_H

#include "debug.h"
#include "string.h"

#ifdef __cplusplus
 extern "C" {
#endif

#ifndef NULL
#define NULL                    0
#endif

#ifndef VOID
#define VOID                    void
#endif
#ifndef CONST
#define CONST                   const
#endif
#ifndef BOOL
typedef unsigned char           BOOL;
#endif
#ifndef BOOLEAN
typedef unsigned char           BOOLEAN;
#endif
#ifndef CHAR
typedef char                    CHAR;
#endif
#ifndef INT8
typedef char                    INT8;
#endif
#ifndef INT16
typedef short                   INT16;
#endif
#ifndef INT32
typedef long                    INT32;
#endif
#ifndef UINT8
typedef unsigned char           UINT8;
#endif
#ifndef UINT16
typedef unsigned short          UINT16;
#endif
#ifndef UINT32
typedef unsigned long           UINT32;
#endif
#ifndef UINT8V
typedef unsigned char volatile  UINT8V;
#endif
#ifndef UINT16V
typedef unsigned short volatile UINT16V;
#endif
#ifndef UINT32V
typedef unsigned long volatile  UINT32V;
#endif

#ifndef PVOID
typedef void                    *PVOID;
#endif
#ifndef PCHAR
typedef char                    *PCHAR;
#endif
#ifndef PCHAR
typedef const char              *PCCHAR;
#endif
#ifndef PINT8
typedef char                    *PINT8;
#endif
#ifndef PINT16
typedef short                   *PINT16;
#endif
#ifndef PINT32
typedef long                    *PINT32;
#endif
#ifndef PUINT8
typedef unsigned char           *PUINT8;
#endif
#ifndef PUINT16
typedef unsigned short          *PUINT16;
#endif
#ifndef PUINT32
typedef unsigned long           *PUINT32;
#endif
#ifndef PUINT8V
typedef volatile unsigned char  *PUINT8V;
#endif
#ifndef PUINT16V
typedef volatile unsigned short *PUINT16V;
#endif
#ifndef PUINT32V
typedef volatile unsigned long  *PUINT32V;
#endif


/* USB constant and structure define */

/* USB PID */
#ifndef USB_PID_SETUP
#define USB_PID_NULL            0x00    /* reserved PID */
#define USB_PID_SOF             0x05
#define USB_PID_SETUP           0x0D
#define USB_PID_IN              0x09
#define USB_PID_OUT             0x01
#define USB_PID_ACK             0x02
#define USB_PID_NYET            0x06
#define USB_PID_NAK             0x0A
#define USB_PID_STALL           0x0E
#define USB_PID_DATA0           0x03
#define USB_PID_DATA1           0x0B
#define USB_PID_PRE             0x0C
#endif

/* USB standard device request code */
#ifndef USB_GET_DESCRIPTOR
#define USB_GET_STATUS          0x00
#define USB_CLEAR_FEATURE       0x01
#define USB_SET_FEATURE         0x03
#define USB_SET_ADDRESS         0x05
#define USB_GET_DESCRIPTOR      0x06
#define USB_SET_DESCRIPTOR      0x07
#define USB_GET_CONFIGURATION   0x08
#define USB_SET_CONFIGURATION   0x09
#define USB_GET_INTERFACE       0x0A
#define USB_SET_INTERFACE       0x0B
#define USB_SYNCH_FRAME         0x0C
#endif

/* USB hub class request code */
#ifndef HUB_GET_DESCRIPTOR
#define HUB_GET_STATUS          0x00
#define HUB_CLEAR_FEATURE       0x01
#define HUB_GET_STATE           0x02
#define HUB_SET_FEATURE         0x03
#define HUB_GET_DESCRIPTOR      0x06
#define HUB_SET_DESCRIPTOR      0x07
#endif

/* USB HID class request code */
#ifndef HID_GET_REPORT
#define HID_GET_REPORT          0x01
#define HID_GET_IDLE            0x02
#define HID_GET_PROTOCOL        0x03
#define HID_SET_REPORT          0x09
#define HID_SET_IDLE            0x0A
#define HID_SET_PROTOCOL        0x0B
#endif

/* Bit define for USB request type */
#ifndef USB_REQ_TYP_MASK
#define USB_REQ_TYP_IN          0x80            /* control IN, device to host */
#define USB_REQ_TYP_OUT         0x00            /* control OUT, host to device */
#define USB_REQ_TYP_READ        0x80            /* control read, device to host */
#define USB_REQ_TYP_WRITE       0x00            /* control write, host to device */
#define USB_REQ_TYP_MASK        0x60            /* bit mask of request type */
#define USB_REQ_TYP_STANDARD    0x00
#define USB_REQ_TYP_CLASS       0x20
#define USB_REQ_TYP_VENDOR      0x40
#define USB_REQ_TYP_RESERVED    0x60
#define USB_REQ_RECIP_MASK      0x1F            /* bit mask of request recipient */
#define USB_REQ_RECIP_DEVICE    0x00
#define USB_REQ_RECIP_INTERF    0x01
#define USB_REQ_RECIP_ENDP      0x02
#define USB_REQ_RECIP_OTHER     0x03
#endif

/* USB request type for hub class request */
#ifndef HUB_GET_HUB_DESCRIPTOR
#define HUB_CLEAR_HUB_FEATURE   0x20
#define HUB_CLEAR_PORT_FEATURE  0x23
#define HUB_GET_BUS_STATE       0xA3
#define HUB_GET_HUB_DESCRIPTOR  0xA0
#define HUB_GET_HUB_STATUS      0xA0
#define HUB_GET_PORT_STATUS     0xA3
#define HUB_SET_HUB_DESCRIPTOR  0x20
#define HUB_SET_HUB_FEATURE     0x20
#define HUB_SET_PORT_FEATURE    0x23
#endif

/* Hub class feature selectors */
#ifndef HUB_PORT_RESET
#define HUB_C_HUB_LOCAL_POWER   0
#define HUB_C_HUB_OVER_CURRENT  1
#define HUB_PORT_CONNECTION     0
#define HUB_PORT_ENABLE         1
#define HUB_PORT_SUSPEND        2
#define HUB_PORT_OVER_CURRENT   3
#define HUB_PORT_RESET          4
#define HUB_PORT_POWER          8
#define HUB_PORT_LOW_SPEED      9
#define HUB_C_PORT_CONNECTION   16
#define HUB_C_PORT_ENABLE       17
#define HUB_C_PORT_SUSPEND      18
#define HUB_C_PORT_OVER_CURRENT 19
#define HUB_C_PORT_RESET        20
#endif

/* USB descriptor type */
#ifndef USB_DESCR_TYP_DEVICE
#define USB_DESCR_TYP_DEVICE    0x01
#define USB_DESCR_TYP_CONFIG    0x02
#define USB_DESCR_TYP_STRING    0x03
#define USB_DESCR_TYP_INTERF    0x04
#define USB_DESCR_TYP_ENDP      0x05
#define USB_DESCR_TYP_QUALIF    0x06
#define USB_DESCR_TYP_SPEED     0x07
#define USB_DESCR_TYP_OTG       0x09
#define USB_DESCR_TYP_BOS       0X0F
#define USB_DESCR_TYP_HID       0x21
#define USB_DESCR_TYP_REPORT    0x22
#define USB_DESCR_TYP_PHYSIC    0x23
#define USB_DESCR_TYP_CS_INTF   0x24
#define USB_DESCR_TYP_CS_ENDP   0x25
#define USB_DESCR_TYP_HUB       0x29
#endif

/* USB device class */
#ifndef USB_DEV_CLASS_HUB
#define USB_DEV_CLASS_RESERVED  0x00
#define USB_DEV_CLASS_AUDIO     0x01
#define USB_DEV_CLASS_COMMUNIC  0x02
#define USB_DEV_CLASS_HID       0x03
#define USB_DEV_CLASS_MONITOR   0x04
#define USB_DEV_CLASS_PHYSIC_IF 0x05
#define USB_DEV_CLASS_POWER     0x06
#define USB_DEV_CLASS_PRINTER   0x07
#define USB_DEV_CLASS_STORAGE   0x08
#define USB_DEV_CLASS_HUB       0x09
#define USB_DEV_CLASS_VEN_SPEC  0xFF
#endif

/* USB endpoint type and attributes */
#ifndef USB_ENDP_TYPE_MASK
#define USB_ENDP_DIR_MASK       0x80
#define USB_ENDP_ADDR_MASK      0x0F
#define USB_ENDP_TYPE_MASK      0x03
#define USB_ENDP_TYPE_CTRL      0x00
#define USB_ENDP_TYPE_ISOCH     0x01
#define USB_ENDP_TYPE_BULK      0x02
#define USB_ENDP_TYPE_INTER     0x03
#endif

#ifndef USB_DEVICE_ADDR
#define USB_DEVICE_ADDR         0x02
#endif
#ifndef DEFAULT_ENDP0_SIZE
#define DEFAULT_ENDP0_SIZE      8       /* default maximum packet size for endpoint 0 */
#endif
#ifndef MAX_PACKET_SIZE
#define MAX_PACKET_SIZE         512      /* maximum packet size */
#endif
#ifndef USB_BO_CBW_SIZE
#define USB_BO_CBW_SIZE         0x1F
#define USB_BO_CSW_SIZE         0x0D
#endif
#ifndef USB_BO_CBW_SIG0
#define USB_BO_CBW_SIG0         0x55
#define USB_BO_CBW_SIG1         0x53
#define USB_BO_CBW_SIG2         0x42
#define USB_BO_CBW_SIG3         0x43
#define USB_BO_CSW_SIG0         0x55
#define USB_BO_CSW_SIG1         0x53
#define USB_BO_CSW_SIG2         0x42
#define USB_BO_CSW_SIG3         0x53
#endif

#ifndef __PACKED
#define __PACKED            __attribute__((packed))
#endif

typedef struct __PACKED _USB_SETUP_REQ {
    UINT8 bRequestType;
    UINT8 bRequest;
    UINT16 wValue;
    UINT16 wIndex;
    UINT16 wLength;
} USB_SETUP_REQ, *PUSB_SETUP_REQ;


typedef struct __PACKED _USB_DEVICE_DESCR {
    UINT8 bLength;
    UINT8 bDescriptorType;
    UINT16 bcdUSB;
    UINT8 bDeviceClass;
    UINT8 bDeviceSubClass;
    UINT8 bDeviceProtocol;
    UINT8 bMaxPacketSize0;
    UINT16 idVendor;
    UINT16 idProduct;
    UINT16 bcdDevice;
    UINT8 iManufacturer;
    UINT8 iProduct;
    UINT8 iSerialNumber;
    UINT8 bNumConfigurations;
} USB_DEV_DESCR, *PUSB_DEV_DESCR;


typedef struct __PACKED _USB_CONFIG_DESCR {
    UINT8 bLength;
    UINT8 bDescriptorType;
    UINT16 wTotalLength;
    UINT8 bNumInterfaces;
    UINT8 bConfigurationValue;
    UINT8 iConfiguration;
    UINT8 bmAttributes;
    UINT8 MaxPower;
} USB_CFG_DESCR, *PUSB_CFG_DESCR;


typedef struct __PACKED _USB_INTERF_DESCR {
    UINT8 bLength;
    UINT8 bDescriptorType;
    UINT8 bInterfaceNumber;
    UINT8 bAlternateSetting;
    UINT8 bNumEndpoints;
    UINT8 bInterfaceClass;
    UINT8 bInterfaceSubClass;
    UINT8 bInterfaceProtocol;
    UINT8 iInterface;
} USB_ITF_DESCR, *PUSB_ITF_DESCR;


typedef struct __PACKED _USB_ENDPOINT_DESCR {
    UINT8 bLength;
    UINT8 bDescriptorType;
    UINT8 bEndpointAddress;
    UINT8 bmAttributes;
    UINT16 wMaxPacketSize;
    UINT8 bInterval;
} USB_ENDP_DESCR, *PUSB_ENDP_DESCR;


typedef struct __PACKED _USB_CONFIG_DESCR_LONG {
    USB_CFG_DESCR   cfg_descr;
    USB_ITF_DESCR   itf_descr;
    USB_ENDP_DESCR  endp_descr[1];
} USB_CFG_DESCR_LONG, *PUSB_CFG_DESCR_LONG;


typedef struct __PACKED _USB_HUB_DESCR {
    UINT8 bDescLength;
    UINT8 bDescriptorType;
    UINT8 bNbrPorts;
    UINT8 wHubCharacteristicsL;
    UINT8 wHubCharacteristicsH;
    UINT8 bPwrOn2PwrGood;
    UINT8 bHubContrCurrent;
    UINT8 DeviceRemovable;
    UINT8 PortPwrCtrlMask;
} USB_HUB_DESCR, *PUSB_HUB_DESCR;


typedef struct __PACKED _USB_HID_DESCR {
    UINT8 bLength;
    UINT8 bDescriptorType;
    UINT16 bcdHID;
    UINT8 bCountryCode;
    UINT8 bNumDescriptors;
    UINT8 bDescriptorTypeX;
    UINT8 wDescriptorLengthL;
    UINT8 wDescriptorLengthH;
} USB_HID_DESCR, *PUSB_HID_DESCR;


typedef struct __PACKED _UDISK_BOC_CBW {/* command of BulkOnly USB-FlashDisk */
    UINT32 mCBW_Sig;
    UINT32 mCBW_Tag;
    UINT32 mCBW_DataLen;                /* uppest byte of data length, always is 0 */
    UINT8 mCBW_Flag;                    /* transfer direction and etc. */
    UINT8 mCBW_LUN;
    UINT8 mCBW_CB_Len;                  /* length of command block */
    UINT8 mCBW_CB_Buf[16];              /* command block buffer */
} UDISK_BOC_CBW, *PXUDISK_BOC_CBW;


typedef struct __PACKED _UDISK_BOC_CSW {/* status of BulkOnly USB-FlashDisk */
    UINT32 mCBW_Sig;
    UINT32 mCBW_Tag;
    UINT32 mCSW_Residue;                /* return: remainder bytes */         /* uppest byte of remainder length, always is 0 */
    UINT8 mCSW_Status;                  /* return: result status */
} UDISK_BOC_CSW, *PXUDISK_BOC_CSW;


/******************************************************************************/
/*                         USB OTG Peripheral Register map                              */
/******************************************************************************/


/******************************************************************************/
#if 0
/* USBOTG_FS DEVICE USB_CONTROL */
/* BASE USB_CTRL */
//#define USBHD_BASE_CTRL     (*((PUINT32V)(0x50000000))) // USB control & interrupt enable & device address
#define USBHD_BASE_CTRL         (*((PUINT8V)(0x50000000)))  // USB base control
#define     USBHD_UC_HOST_MODE     0x80      // enable USB host mode: 0=device mode, 1=host mode
#define     USBHD_UC_LOW_SPEED     0x40      // enable USB low speed: 0=12Mbps, 1=1.5Mbps
#define     USBHD_UC_DEV_PU_EN     0x20      // USB device enable and internal pullup resistance enable
#define     USBHD_UC_SYS_CTRL1     0x20      // USB system control high bit
#define     USBHD_UC_SYS_CTRL0     0x10      // USB system control low bit
#define     USBHD_UC_SYS_CTRL_MASK    0x30      // bit mask of USB system control
// bUC_HOST_MODE & bUC_SYS_CTRL1 & bUC_SYS_CTRL0: USB system control
//   0 00: disable USB device and disable internal pullup resistance
//   0 01: enable USB device and disable internal pullup resistance, need external pullup resistance
//   0 1x: enable USB device and enable internal pullup resistance
//   1 00: enable USB host and normal status
//   1 01: enable USB host and force UDP/UDM output SE0 state
//   1 10: enable USB host and force UDP/UDM output J state
//   1 11: enable USB host and force UDP/UDM output resume or K state
#define     USBHD_UC_INT_BUSY      0x08      // enable automatic responding busy for device mode or automatic pause for host mode during interrupt flag UIF_TRANSFER valid
#define     USBHD_UC_RESET_SIE     0x04      // force reset USB SIE, need software clear
#define     USBHD_UC_CLR_ALL       0x02      // force clear FIFO and count of USB
#define     USBHD_UC_DMA_EN        0x01      // DMA enable and DMA interrupt enable for USB

#define USBHD_UDEV_CTRL        (*((PUINT8V)(0x50000001)))  // USB device physical prot control
#define     USBHD_UD_PD_DIS        0x80      // disable USB UDP/UDM pulldown resistance: 0=enable pulldown, 1=disable
#define     USBHD_UD_DP_PIN        0x20      // ReadOnly: indicate current UDP pin level
#define     USBHD_UD_DM_PIN        0x10      // ReadOnly: indicate current UDM pin level
#define     USBHD_UD_LOW_SPEED     0x04      // enable USB physical port low speed: 0=full speed, 1=low speed
#define     USBHD_UD_GP_BIT        0x02      // general purpose bit
#define     USBHD_UD_PORT_EN       0x01      // enable USB physical port I/O: 0=disable, 1=enable


#define USBHD_UHOST_CTRL     (*((PUINT8V)(0x50000001)))      // USB host physical prot control
#define     USBHD_UH_PD_DIS        0x80      // disable USB UDP/UDM pulldown resistance: 0=enable pulldown, 1=disable
#define     USBHD_UH_DP_PIN        0x20      // ReadOnly: indicate current UDP pin level
#define     USBHD_UH_DM_PIN        0x10      // ReadOnly: indicate current UDM pin level
#define     USBHD_UH_LOW_SPEED     0x04      // enable USB port low speed: 0=full speed, 1=low speed
#define     USBHD_UH_BUS_RESET     0x02      // control USB bus reset: 0=normal, 1=force bus reset
#define     USBHD_UH_PORT_EN       0x01      // enable USB port: 0=disable, 1=enable port, automatic disabled if USB device detached

#define USBHD_INT_EN       (*((PUINT8V)(0x50000002)))  // USB interrupt enable
#define     USBHD_UIE_DEV_SOF      0x80      // enable interrupt for SOF received for USB device mode
#define     USBHD_UIE_DEV_NAK      0x40      // enable interrupt for NAK responded for USB device mode
#define     USBHD_UIE_FIFO_OV      0x10      // enable interrupt for FIFO overflow
#define     USBHD_UIE_HST_SOF      0x08      // enable interrupt for host SOF timer action for USB host mode
#define     USBHD_UIE_SUSPEND      0x04      // enable interrupt for USB suspend or resume event
#define     USBHD_UIE_TRANSFER     0x02      // enable interrupt for USB transfer completion
#define     USBHD_UIE_DETECT       0x01      // enable interrupt for USB device detected event for USB host mode
#define     USBHD_UIE_BUS_RST      0x01      // enable interrupt for USB bus reset event for USB device mode

#define USBHD_DEV_ADDR       (*((PUINT8V)(0x50000003)))  // USB device address
#define     USBHD_UDA_GP_BIT       0x80      // general purpose bit
#define     USBHD_USB_ADDR_MASK       0x7F      // bit mask for USB device address

#define USBHD_MIS_ST       (*((PUINT8V)(0x50000005)))  // USB miscellaneous status
#define     USBHD_UMS_SOF_PRES     0x80      // RO, indicate host SOF timer presage status
#define     USBHD_UMS_SOF_ACT      0x40      // RO, indicate host SOF timer action status for USB host
#define     USBHD_UMS_SIE_FREE     0x20      // RO, indicate USB SIE free status
#define     USBHD_UMS_R_FIFO_RDY   0x10      // RO, indicate USB receiving FIFO ready status (not empty)
#define     USBHD_UMS_BUS_RESET    0x08      // RO, indicate USB bus reset status
#define     USBHD_UMS_SUSPEND      0x04      // RO, indicate USB suspend status
#define     USBHD_UMS_DM_LEVEL     0x02      // RO, indicate UDM level saved at device attached to USB host
#define     USBHD_UMS_DEV_ATTACH   0x01      // RO, indicate device attached status on USB host

#define USBHD_INT_FG       (*((PUINT8V)(0x50000006)))  // USB interrupt flag
#define     USBHD_U_IS_NAK         0x80    // RO, indicate current USB transfer is NAK received
#define     USBHD_U_TOG_OK         0x40    // RO, indicate current USB transfer toggle is OK
#define     USBHD_U_SIE_FREE       0x20    // RO, indicate USB SIE free status
#define     USBHD_UIF_FIFO_OV      0x10    // FIFO overflow interrupt flag for USB, direct bit address clear or write 1 to clear
#define     USBHD_UIF_HST_SOF      0x08    // host SOF timer interrupt flag for USB host, direct bit address clear or write 1 to clear
#define     USBHD_UIF_SUSPEND      0x04    // USB suspend or resume event interrupt flag, direct bit address clear or write 1 to clear
#define     USBHD_UIF_TRANSFER     0x02    // USB transfer completion interrupt flag, direct bit address clear or write 1 to clear
#define     USBHD_UIF_DETECT       0x01    // device detected event interrupt flag for USB host mode, direct bit address clear or write 1 to clear
#define     USBHD_UIF_BUS_RST      0x01    // bus reset event interrupt flag for USB device mode, direct bit address clear or write 1 to clear

#define USBHD_INT_ST       (*((PUINT8V)(0x50000007)))  // USB interrupt status
#define     USBHD_UIS_IS_NAK       0x80      // RO, indicate current USB transfer is NAK received for USB device mode
#define     USBHD_UIS_TOG_OK       0x40      // RO, indicate current USB transfer toggle is OK
#define     USBHD_UIS_TOKEN1       0x20      // RO, current token PID code bit 1 received for USB device mode
#define     USBHD_UIS_TOKEN0       0x10      // RO, current token PID code bit 0 received for USB device mode
#define     USBHD_UIS_TOKEN_MASK      0x30      // RO, bit mask of current token PID code received for USB device mode
#define     USBHD_UIS_TOKEN_OUT       0x00
#define     USBHD_UIS_TOKEN_SOF       0x10
#define     USBHD_UIS_TOKEN_IN        0x20
#define     USBHD_UIS_TOKEN_SETUP     0x30
// bUIS_TOKEN1 & bUIS_TOKEN0: current token PID code received for USB device mode
//   00: OUT token PID received
//   01: SOF token PID received
//   10: IN token PID received
//   11: SETUP token PID received
#define     MASK_UIS_ENDP       0x0F      // RO, bit mask of current transfer endpoint number for USB device mode
#define     MASK_UIS_H_RES      0x0F      // RO, bit mask of current transfer handshake response for USB host mode: 0000=no response, time out from device, others=handshake response PID received


#define R16_USB_RX_LEN       (*((PUINT16V)(0x50000008)))  // USB receiving length
#define USBHD_UEP4_1_MOD       (*((PUINT8V)(0x5000000c)))  // endpoint 4/1 mode
#define     USBHD_UEP1_RX_EN       0x80      // enable USB endpoint 1 receiving (OUT)
#define     USBHD_UEP1_TX_EN       0x40      // enable USB endpoint 1 transmittal (IN)
#define     USBHD_UEP1_BUF_MOD     0x10      // buffer mode of USB endpoint 1
// bUEPn_RX_EN & bUEPn_TX_EN & bUEPn_BUF_MOD: USB endpoint 1/2/3 buffer mode, buffer start address is UEPn_DMA
//   0 0 x:  disable endpoint and disable buffer
//   1 0 0:  64 bytes buffer for receiving (OUT endpoint)
//   1 0 1:  dual 64 bytes buffer by toggle bit bUEP_R_TOG selection for receiving (OUT endpoint), total=128bytes
//   0 1 0:  64 bytes buffer for transmittal (IN endpoint)
//   0 1 1:  dual 64 bytes buffer by toggle bit bUEP_T_TOG selection for transmittal (IN endpoint), total=128bytes
//   1 1 0:  64 bytes buffer for receiving (OUT endpoint) + 64 bytes buffer for transmittal (IN endpoint), total=128bytes
//   1 1 1:  dual 64 bytes buffer by bUEP_R_TOG selection for receiving (OUT endpoint) + dual 64 bytes buffer by bUEP_T_TOG selection for transmittal (IN endpoint), total=256bytes
#define     USBHD_UEP4_RX_EN       0x08      // enable USB endpoint 4 receiving (OUT)
#define     USBHD_UEP4_TX_EN       0x04      // enable USB endpoint 4 transmittal (IN)
// bUEP4_RX_EN & bUEP4_TX_EN: USB endpoint 4 buffer mode, buffer start address is UEP0_DMA
//   0 0:  single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint)
//   1 0:  single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint) + 64 bytes buffer for endpoint 4 receiving (OUT endpoint), total=128bytes
//   0 1:  single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint) + 64 bytes buffer for endpoint 4 transmittal (IN endpoint), total=128bytes
//   1 1:  single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint)
//           + 64 bytes buffer for endpoint 4 receiving (OUT endpoint) + 64 bytes buffer for endpoint 4 transmittal (IN endpoint), total=192bytes

#define USBHD_UEP2_3_MOD       (*((PUINT8V)(0x5000000d)))  // endpoint 2/3 mode
#define     USBHD_UEP3_RX_EN       0x80      // enable USB endpoint 3 receiving (OUT)
#define     USBHD_UEP3_TX_EN       0x40      // enable USB endpoint 3 transmittal (IN)
#define     USBHD_UEP3_BUF_MOD     0x10      // buffer mode of USB endpoint 3
#define     USBHD_UEP2_RX_EN       0x08      // enable USB endpoint 2 receiving (OUT)
#define     USBHD_UEP2_TX_EN       0x04      // enable USB endpoint 2 transmittal (IN)
#define     USBHD_UEP2_BUF_MOD     0x01      // buffer mode of USB endpoint 2

//#define R8_UH_EP_MOD        USBHD_UEP2_3_MOD               //host endpoint mode
#define USBHD_UH_EP_MOD     USBHD_UEP2_3_MOD
#define     USBHD_UH_EP_TX_EN      0x40      // enable USB host OUT endpoint transmittal
#define     USBHD_UH_EP_TBUF_MOD   0x10      // buffer mode of USB host OUT endpoint
// bUH_EP_TX_EN & bUH_EP_TBUF_MOD: USB host OUT endpoint buffer mode, buffer start address is UH_TX_DMA
//   0 x:  disable endpoint and disable buffer
//   1 0:  64 bytes buffer for transmittal (OUT endpoint)
//   1 1:  dual 64 bytes buffer by toggle bit bUH_T_TOG selection for transmittal (OUT endpoint), total=128bytes
#define     USBHD_UH_EP_RX_EN      0x08      // enable USB host IN endpoint receiving
#define     USBHD_UH_EP_RBUF_MOD   0x01      // buffer mode of USB host IN endpoint
// bUH_EP_RX_EN & bUH_EP_RBUF_MOD: USB host IN endpoint buffer mode, buffer start address is UH_RX_DMA
//   0 x:  disable endpoint and disable buffer
//   1 0:  64 bytes buffer for receiving (IN endpoint)
//   1 1:  dual 64 bytes buffer by toggle bit bUH_R_TOG selection for receiving (IN endpoint), total=128bytes

#define USBHD_UEP5_6_MOD       (*((PUINT8V)(0x5000000e)))  // endpoint 5/6 mode
#define USBHD_UEP6_RX_EN       0x80      // enable USB endpoint 6 receiving (OUT)
#define USBHD_UEP6_TX_EN       0x40      // enable USB endpoint 6 transmittal (IN)
#define USBHD_UEP6_BUF_MOD     0x10      // buffer mode of USB endpoint 6
#define USBHD_UEP5_RX_EN       0x08      // enable USB endpoint 5 receiving (OUT)
#define USBHD_UEP5_TX_EN       0x04      // enable USB endpoint 5 transmittal (IN)
#define USBHD_UEP5_BUF_MOD     0x01      // buffer mode of USB endpoint 5

#define USBHD_UEP7_MOD         (*((PUINT8V)(0x5000000f)))  // endpoint 7 mode
#define USBHD_UEP7_RX_EN       0x08      // enable USB endpoint 7 receiving (OUT)
#define USBHD_UEP7_TX_EN       0x04      // enable USB endpoint 7 transmittal (IN)
#define USBHD_UEP7_BUF_MOD     0x01      // buffer mode of USB endpoint 7


#define USBHD_UEP0_DMA        (*((PUINT32V)(0x50000010))) // endpoint 0 DMA buffer address
#define USBHD_UEP1_DMA        (*((PUINT32V)(0x50000014))) // endpoint 1 DMA buffer address
#define USBHD_UEP2_DMA        (*((PUINT32V)(0x50000018))) // endpoint 2 DMA buffer address
#define USBHD_UH_RX_DMA       USBHD_UEP2_DMA                // host rx endpoint buffer high address
#define USBHD_UEP3_DMA        (*((PUINT32V)(0x5000001c))) // endpoint 3 DMA buffer address

#define USBHD_UEP4_DMA        (*((PUINT32V)(0x50000020))) // endpoint 4 DMA buffer address
#define USBHD_UEP5_DMA        (*((PUINT32V)(0x50000024))) // endpoint 5 DMA buffer address
#define USBHD_UEP6_DMA        (*((PUINT32V)(0x50000028))) // endpoint 6 DMA buffer address
#define USBHD_UEP7_DMA        (*((PUINT32V)(0x5000002c))) // endpoint 7 DMA buffer address

#define USBHD_UH_TX_DMA         USBHD_UEP3_DMA    // host tx endpoint buffer high address
#define USBHD_UEP0_T_LEN       (*((PUINT8V)(0x50000030)))  // endpoint 0 transmittal length
#define USBHD_UEP0_TX_CTRL     (*((PUINT8V)(0x50000032)))  // endpoint 0 control
#define USBHD_UEP0_RX_CTRL     (*((PUINT8V)(0x50000033)))  // endpoint 0 control
#define USBHD_UEP1_T_LEN       (*((PUINT8V)(0x50000034)))  // endpoint 1 transmittal length
#define USBHD_UEP1_TX_CTRL     (*((PUINT8V)(0x50000036)))  // endpoint 1 control
#define USBHD_UEP1_RX_CTRL     (*((PUINT8V)(0x50000037)))  // endpoint 1 control

#define USBHD_UEP_AUTO_TOG     0x08      // enable automatic toggle after successful transfer completion on endpoint 1/2/3: 0=manual toggle, 1=automatic toggle
#define USBHD_UEP_R_TOG        0x04      // expected data toggle flag of USB endpoint X receiving (OUT): 0=DATA0, 1=DATA1
#define USBHD_UEP_T_TOG        0x04      // prepared data toggle flag of USB endpoint X transmittal (IN): 0=DATA0, 1=DATA1

#define USBHD_UEP_R_RES1       0x02      // handshake response type high bit for USB endpoint X receiving (OUT)
#define USBHD_UEP_R_RES0       0x01      // handshake response type low bit for USB endpoint X receiving (OUT)
#define USBHD_UEP_R_RES_MASK      0x03      // bit mask of handshake response type for USB endpoint X receiving (OUT)
#define USBHD_UEP_R_RES_ACK       0x00
#define USBHD_UEP_R_RES_TOUT      0x01
#define USBHD_UEP_R_RES_NAK       0x02
#define USBHD_UEP_R_RES_STALL     0x03
// RB_UEP_R_RES1 & RB_UEP_R_RES0: handshake response type for USB endpoint X receiving (OUT)
//   00: ACK (ready)
//   01: no response, time out to host, for non-zero endpoint isochronous transactions
//   10: NAK (busy)
//   11: STALL (error)
#define USBHD__UEP_T_RES1         0x02      // handshake response type high bit for USB endpoint X transmittal (IN)
#define USBHD__UEP_T_RES0         0x01      // handshake response type low bit for USB endpoint X transmittal (IN)
#define USBHD_UEP_T_RES_MASK      0x03      // bit mask of handshake response type for USB endpoint X transmittal (IN)
#define USBHD_UEP_T_RES_ACK       0x00
#define USBHD_UEP_T_RES_TOUT      0x01
#define USBHD_UEP_T_RES_NAK       0x02
#define USBHD_UEP_T_RES_STALL     0x03
// bUEP_T_RES1 & bUEP_T_RES0: handshake response type for USB endpoint X transmittal (IN)
//   00: DATA0 or DATA1 then expecting ACK (ready)
//   01: DATA0 or DATA1 then expecting no response, time out from host, for non-zero endpoint isochronous transactions
//   10: NAK (busy)
//   11: STALL (error)
              // host aux setup
#define USBHD_UH_SETUP      USBHD_UEP1_TX_CTRL
#define USBHD_UH_PRE_PID_EN    0x0400      // USB host PRE PID enable for low speed device via hub
#define USBHD_UH_SOF_EN        0x04        // USB host automatic SOF enable


#define USBHD_UEP2_T_LEN       (*((PUINT8V)(0x50000038)))  // endpoint 2 transmittal length
#define USBHD_UEP2_TX_CTRL     (*((PUINT8V)(0x5000003a)))  // endpoint 2 tx control
#define USBHD_UEP2_RX_CTRL     (*((PUINT8V)(0x5000003b)))  // endpoint 2 rx control
              // host endpoint and PID
#define USBHD_UH_EP_PID     USBHD_UEP2_T_LEN
#define USBHD_UH_TOKEN_MASK       0xF0      // bit mask of token PID for USB host transfer
#define USBHD_UH_ENDP_MASK        0x0F      // bit mask of endpoint number for USB host transfer

#define USBHD_UH_RX_CTRL    USBHD_UEP2_RX_CTRL  // host receiver endpoint control
#define USBHD_UH_R_AUTO_TOG    0x08      // enable automatic toggle after successful transfer completion: 0=manual toggle, 1=automatic toggle
#define USBHD_UH_R_TOG         0x04      // expected data toggle flag of host receiving (IN): 0=DATA0, 1=DATA1
#define USBHD_UH_R_RES         0x01      // prepared handshake response type for host receiving (IN): 0=ACK (ready), 1=no response, time out to device, for isochronous transactions


#define USBHD_UEP3_T_LEN       (*((PUINT16V)(0x5000003c))) // endpoint 3 transmittal length
#define USBHD_UEP3_TX_CTRL     (*((PUINT8V)(0x5000003e)))  // endpoint 3 tx control
#define USBHD_UEP3_RX_CTRL     (*((PUINT8V)(0x5000003f)))  // endpoint 3 rx control

#define USBHD_UH_TX_LEN     USBHD_UEP3_T_LEN

#define USBHD_UH_TX_CTRL       USBHD_UEP3_TX_CTRL      // host transmittal endpoint control
#define USBHD_UH_T_AUTO_TOG    0x08                    // enable automatic toggle after successful transfer completion: 0=manual toggle, 1=automatic toggle
#define USBHD_UH_T_TOG         0x04                    // prepared data toggle flag of host transmittal (SETUP/OUT): 0=DATA0, 1=DATA1
#define USBHD_UH_T_RES         0x01                    // expected handshake response type for host transmittal (SETUP/OUT): 0=ACK (ready), 1=no response, time out from device, for isochronous transactions


#define USBHD_UEP4_T_LEN       (*((PUINT8V)(0x50000040)))  // endpoint 4 transmittal length
#define USBHD_UEP4_TX_CTRL     (*((PUINT8V)(0x50000042)))  // endpoint 4 tx control
#define USBHD_UEP4_RX_CTRL     (*((PUINT8V)(0x50000043)))  // endpoint 4 rx control


#define USBHD_UEP5_T_LEN       (*((PUINT8V)(0x50000044)))  // endpoint 5 transmittal length
#define USBHD_UEP5_TX_CTRL     (*((PUINT8V)(0x50000046)))  // endpoint 5 tx control
#define USBHD_UEP5_RX_CTRL     (*((PUINT8V)(0x50000047)))  // endpoint 5 rx control

#define USBHD_UEP6_T_LEN       (*((PUINT8V)(0x50000048)))  // endpoint 6 transmittal length
#define USBHD_UEP6_TX_CTRL     (*((PUINT8V)(0x5000004a)))  // endpoint 6 tx control
#define USBHD_UEP6_RX_CTRL     (*((PUINT8V)(0x5000004b)))  // endpoint 6 rx control


#define USBHD_UEP7_T_LEN       (*((PUINT8V)(0x5000004c)))  // endpoint 7 transmittal length
#define USBHD_UEP7_TX_CTRL     (*((PUINT8V)(0x5000004e)))  // endpoint 7 tx control
#define USBHD_UEP7_RX_CTRL     (*((PUINT8V)(0x5000004f)))  // endpoint 7 rx control



#define  USB_OTG_CR           (*((PUINT32V)(0x50000054)))  // usb otg control
#define     USB_OTG_CR_SESS           0x20               // usb otg control,会话电平使能
#define     USB_OTG_CR_VBUS           0x10               // usb otg control,VBUS电平使能
#define     USB_OTG_CR_OTG_EN         0x08               // usb otg control,OTG功能使能
#define     USB_OTG_CR_IDPU           0x04               // usb otg control,ID引脚上拉
#define     USB_OTG_CR_CHARGEVBUS     0x02               // usb otg control,VBUS充电
#define     USB_OTG_CR_DISCHARGEVBUS  0x01               // usb otg control,VBUS放电

#define  USB_OTG_SR          (*((PUINT32V)(0x50000058)))  // usb otg status
#define     USB_OTG_SR_VBUS_VLD       0x01               // usb otg status,A设备VBUS输入电平有效
#define     USB_OTG_SR_SESS_VLD       0x02               // usb otg status,会话有效电平有效标志
#define     USB_OTG_SR_SESS_END       0x04               // usb otg status,会话结束电平有效标志
#define     USB_OTG_SR_ID_DIG         0x08               // usb otg status,ID电平

#endif
/* USBHS PHY Clock Config (RCC_CFGR2) 使用USBHS PHY时钟的情况下，需要用到这些宏定义 */
#ifndef  USBHS_EXIST
#define USB_48M_CLK_SRC_MASK   (1<<31)
#define USB_48M_CLK_SRC_SYS    (0<<31)
#define USB_48M_CLK_SRC_PHY    (1<<31)

#define USBHS_PLL_ALIVE        (1<<30)

#define USBHS_PLL_CKREF_MASK   (3<<28)
#define USBHS_PLL_CKREF_3M     (0<<28)
#define USBHS_PLL_CKREF_4M     (1<<28)
#define USBHS_PLL_CKREF_8M     (2<<28)
#define USBHS_PLL_CKREF_5M     (3<<28)

#define USBHS_PLL_SRC_MASK     (1<<27)
#define USBHS_PLL_SRC_HSE      (0<<27)
#define USBHS_PLL_SRC_HSI      (1<<27)

#define USBHS_PLL_SRC_PRE_MASK (7<<24)
#define USBHS_PLL_SRC_PRE_DIV1 (0<<24)
#define USBHS_PLL_SRC_PRE_DIV2 (1<<24)
#define USBHS_PLL_SRC_PRE_DIV3 (2<<24)
#define USBHS_PLL_SRC_PRE_DIV4 (3<<24)
#define USBHS_PLL_SRC_PRE_DIV5 (4<<24)
#define USBHS_PLL_SRC_PRE_DIV6 (5<<24)
#define USBHS_PLL_SRC_PRE_DIV7 (6<<24)


 /*******************RCC_CFG2***********************/
 #define USBHS_CLK_SRC_PHY     (1<<31)
 #define USBHS_CLK_SRC_SYS     (0<<31)
 #define USBHS_PLLALIVE        (1<<30)

 #define USBHS_PLL_CKREF_3M    (0<<28)
 #define USBHS_PLL_CKREF_4M    (1<<28)
 #define USBHS_PLL_CKREF_8M    (2<<28)
 #define USBHS_PLL_CKREF_5M    (3<<28)

 #define USBHS_PLL_SRC_HSE      (0<<27)
 #define USBHS_PLL_SRC_HSI      (1<<27)

 #define USBHS_PLL_SRC_PRE_DIV1 (0<<24)
 #define USBHS_PLL_SRC_PRE_DIV2 (1<<24)
 #define USBHS_PLL_SRC_PRE_DIV3 (2<<24)
 #define USBHS_PLL_SRC_PRE_DIV4 (3<<24)
 #define USBHS_PLL_SRC_PRE_DIV5 (4<<24)
 #define USBHS_PLL_SRC_PRE_DIV6 (5<<24)
 #define USBHS_PLL_SRC_PRE_DIV7 (6<<24)
 #define USBHS_PLL_SRC_PRE_DIV8 (7<<24)
 /*******************RCC_AHBEBR***********************/
 #define USBHS_CLK_EN        (1<<11)


/******************************************************************************/
/*                         USB HD Host Mode Peripheral Register map                              */
/******************************************************************************/
 /********************************************/
 // R8_USB_CTRL
 #define DMA_EN                  (1<<0)
 #define USB_ALL_CLR             (1<<1)
 #define USB_FORCE_RST           (1<<2)
 #define INT_BUSY_EN             (1<<3)
 #define DEV_PU_EN               (1<<4)
 #define FULL_SPEED              (0<<5)
 #define HIGH_SPEED              (1<<5)
 #define LOW_SPEED               (2<<5)
 #define HOST_MODE               (1<<7)

//R8_UHOST_CTRL
 #define SEND_BUS_RESET          (1<<0)
 #define SEND_BUS_SUSPEND        (1<<1)
 #define SEND_BUS_RESUME         (1<<2)
 #define REMOTE_WAKE             (1<<3)
 #define PHY_SUSPENDM            (1<<4)
 #define UH_SOFT_FREE            (1<<6)
 #define SEND_SOF_EN             (1<<7)

// R8_USB_INT_EN
#define USBHS_DETECT_EN          (1<<0)
#define USBHS_ACT_EN             (1<<1)
#define USBHS_SUSP_EN            (1<<2)
#define USBHS_SOF_EN             (1<<3)
#define USBHS_OVER_EN            (1<<4)
#define USBHS_SETUP_EN           (1<<5)
#define USBHS_ISO_EN             (1<<6)
#define USBHS_DEV_NAK_EN         (1<<7)

// USB SUSPENED
#define HOST_TESTMODE_MASK       (3<<0)
#define DEV_REMOTE_WAKEUP        (1<<2)
#define USB_LINESTATE            (3<<4)

// R8_USB_SPEED_TYPE
#define USBSPEED_MASK            (0x03)

// R8_USB_MIS_ST
#define USBHS_SPLIT_CAN          (1<<0)
#define USBHS_ATTCH              (1<<1)
#define USBHS_SUSPEND            (1<<2)
#define USBHS_BUS_RESET          (1<<3)
#define USBHS_FIFO_RDY           (1<<4)
#define USBHS_SIE_FREE           (1<<5)
#define USBHS_SOF_ACT            (1<<6)
#define USBHS_SOF_PRES           (1<<7)

// R8_USB_INT_FLAG
#define USBHS_DETECT_FLAG        (1<<0)
#define USBHS_ACT_FLAG           (1<<1)
#define USBHS_SUSP_FLAG          (1<<2)
#define USBHS_SOF_FLAG           (1<<3)
#define USBHS_OVER_FLAG          (1<<4)
#define USBHS_SETUP_FLAG         (1<<5)
#define USBHS_ISO_FLAG           (1<<6)

// R8_USUB_INT_ST
#define USBHS_TOGGLE_OK          (0x40)
#define USBHS_HOST_RES           (0x0f)

// R32_UH_EP_MOD
 #define HOST_TX_EN           (1<<3)
 #define HOST_RX_EN           (1<<18)

// HOST_EP_TYPE
#define USBHS_ENDP_TX_ISO     (1<<3)
#define USBHS_ENDP_RX_ISO     (1<<(16+2))

// R32_UH_EP_PID
 #define HOST_MASK_TOKEN      (0x0f)
 #define HOST_MASK_ENDP       (0x0f<<4)

//R8_UH_RX_CTRL
 #define EP_R_RES_MASK        (3<<0)
 #define EP_R_RES_ACK         (0<<0)
 #define EP_R_RES_NYET        (1<<0)
 #define EP_R_RES_NAK         (2<<0)
 #define EP_R_RES_STALL       (3<<0)

 #define UH_R_RES_NO          (1<<2)
 #define UH_R_TOG_1           (1<<3)
 #define UH_R_TOG_2           (2<<3)
 #define UH_R_TOG_3           (3<<3)
 #define UH_R_TOG_AUTO        (1<<5)
 #define UH_R_DATA_NO         (1<<6)
//R8_UH_TX_CTRL
 #define UH_T_RES_MASK        (3<<0)
 #define UH_T_RES_ACK         (0<<0)
 #define UH_T_RES_NYET        (1<<0)
 #define UH_T_RES_NAK         (2<<0)
 #define UH_T_RES_STALL       (3<<0)

 #define UH_T_RES_NO          (1<<2)
 #define UH_T_TOG_1           (1<<3)
 #define UH_T_TOG_2           (2<<3)
 #define UH_T_TOG_3           (3<<3)
 #define UH_T_TOG_AUTO        (1<<5)
 #define UH_T_DATA_NO         (1<<6)


#define DEV_TYPE_KEYBOARD     (USB_DEV_CLASS_HID | 0x20)
#define DEV_TYPE_MOUSE        (USB_DEV_CLASS_HID | 0x30)
#define DEF_AOA_DEVICE        0xF0
#define DEV_TYPE_UNKNOW       0xFF
/******************************************************/
 //endpoint
#define DEF_ENDP_0     0
#define DEF_ENDP_1     1
#define DEF_ENDP_2     2
#define DEF_ENDP_3     3
#define DEF_ENDP_4     4
#define DEF_ENDP_5     5
#define DEF_ENDP_6     6
#define DEF_ENDP_7     7
#define DEF_ENDP_8     8
#define DEF_ENDP_9     9
#define DEF_ENDP_10    10
#define DEF_ENDP_11    11
#define DEF_ENDP_12    12
#define DEF_ENDP_13    13
#define DEF_ENDP_14    14
#define DEF_ENDP_15    15

#define USBHS_MAX_PACK_SIZE     512
#define WAIT_TIME               1000000
#define WAIT_USB_TOUT_200US     3000

#ifndef ERR_SUCCESS
#define ERR_SUCCESS           (0x00)
#define ERR_USB_CONNECT       (0x15)
#define ERR_USB_DISCON        (0x16)
#define ERR_USB_BUF_OVER      (0x17)
#define ERR_USB_DISK_ERR      (0x1F)
#define ERR_USB_TRANSFER      (0x20)
#define ERR_USB_UNSUPPORT     (0xFB)
#define ERR_USB_UNKNOWN       (0xFE)
#define ERR_AOA_PROTOCOL      (0x41)
#endif

typedef struct
{
    UINT16  OutEndpMaxSize;
    UINT16  InEndpMaxSize;          // IN  端点最大包大小
    UINT8   InEndpNum;              // IN  端点号
    UINT8   Intog;                  // IN  端点同步标志位
    UINT8   InEndpCount;            // IN  端点数量
    UINT8   OutEndpNum;             // OUT 端点号
    UINT8   Outtog;                 // OUT 端点同步标志位
    UINT8   OutEndpCount;           // OUT 端点数量
}DEVENDP;

typedef struct  __attribute__((packed))  _DEV_INFO
{
     DEVENDP DevEndp;
     UINT8   DeviceStatus;           // 设备状态,0-无设备,1-有设备但尚未初始化,2-有设备但初始化枚举失败,3-有设备且初始化枚举成功
     UINT8   DeviceAddress;          // 设备被分配的USB地址
     UINT8   DeviceSpeed;            // 0为全速,1为高速，2为低速
     UINT8   DeviceType;             // 设备类型：08-磁盘   0x30-HID  0x31-KEYBOARD  0x32-MOUSE
     UINT8   DeviceEndp0Size;        // USB0设备的端点0的最大包尺寸
     UINT8   DeviceCongValue;        // 设备配置值
 }USBDEV_INFO,*pUSBDEV_INFO;

/*****************************变量外扩****************************/


 extern UINT8   FoundNewDev;
 extern UINT8   DevEnum;
 extern UINT8   Endp0MaxSize ;
 extern UINT8   UsbDevEndp0Size;
 extern __attribute__ ((aligned(4))) UINT8   endpTxBuf[  ];  // OUT, must even address
 extern __attribute__ ((aligned(4))) UINT8   endpRxBuf[  ];  // OUT, must even address

 #define pSetupReq       ((PUSB_SETUP_REQ)endpTXbuf)
 #define USB_HIGH_SPEED   1

 extern const UINT8 GetDevDescrptor[];     //get device descriptor
 extern const UINT8 GetConfigDescrptor[];  //get configuration descriptor
 extern const UINT8 SetAddress[];          //set address of device
 extern const UINT8 SetConfig[];           //set configuration of device
 extern const UINT8 Clear_EndpStall[];     //clear stall

 extern __attribute__ ((aligned(4))) UINT8   endpTXbuf[ MAX_PACKET_SIZE ];  // OUT, must even address
 extern __attribute__ ((aligned(4))) UINT8   endpRXbuf[ MAX_PACKET_SIZE ];  // OUT, must even addres
 extern __attribute__ ((aligned(4))) const UINT8  HIDSetReport[ ];
 extern __attribute__ ((aligned(4))) const UINT8  HIDGetReport[ ];
 extern UINT8 DeviceDescriptor[ 18 ];
 extern UINT8 ConfigDescriptor[ 512 ];
 /*****************************函数外扩****************************/
 void   CopySetupReqPkg( const UINT8 *pReqPkt );
 void   USB20_RCC_Init( void );
 void   USBHS_HostInit (FunctionalState sta);
 void   USBHS_CurrentAddr( UINT8 address );
 void   Analysis_Descr(pUSBDEV_INFO pusbdev,PUINT8 pdesc, UINT16 l);
 void   SetBusReset(void);

 UINT8  USBHS_HostEnum( UINT8 *Databuf );
 UINT8  CtrlGetDevDescr(UINT8 *Databuf);
 UINT8  CtrlGetConfigDescr(UINT8 *Databuf);
 UINT8  CtrlSetAddress(UINT8 addr);
 UINT8  CtrlSetUsbConfig(UINT8 cfg_val);
 UINT8  CtrlClearEndpStall( UINT8 endp );
 UINT8  CtrlSetUsbIntercace( UINT8 cfg );
 UINT8  HubGetPortStatus( UINT8 HubPortIndex );
 UINT8  HubSetPortFeature( UINT8 HubPortIndex, UINT8 FeatureSelt );
 UINT8  HubClearPortFeature( UINT8 HubPortIndex, UINT8 FeatureSelt );
 UINT8  HostCtrlTransfer(PUINT8 databuf,PUINT8 len);
 UINT8  USBHostTransact(UINT8 endp_pid,UINT8 toggle,UINT32 timeout);
#ifdef __cplusplus
}
#endif


#endif  


#endif
