#ifndef __USB_H
#define __USB_H
#include "OB90A64U1.h"
void USB_SetFifoSize(void);
void USB_Open(void);
void USB_SetCon(uint32_t EndPoint, uint32_t Type);
void USBGetFiFo(uint8_t* pBuff, uint32_t EndPoint, uint32_t *Length);
void USBSetReady(uint32_t EndPoint);
uint32_t USB_WaitInterrupt(uint32_t EndPoint, uint32_t IN_OUT, uint32_t Timeout, uint32_t *nLength);
void USB_WriteFifo(uint8_t* pBuff, uint32_t EndPoint, uint32_t Length);
uint32_t USB_GetFifoSize(uint32_t EndPoint);
uint32_t USB_GetInterruptTimerOut(uint32_t EndPoint);
uint32_t USB_Transfer_In(uint8_t* pBuff, uint32_t EndPonit, uint32_t McuLength, uint32_t PcLength);
uint32_t USB_Transfer_Out(uint8_t* pBuff, uint32_t EndPonit, uint32_t PcLength);
uint32_t USB_ControlIn(uint8_t* pBuff, uint32_t DataLen);
void USB_StallEp(uint32_t EndPoint);
void USB_ProcCtrlPkg(void);
void USB_AutoTrim48M(void);
void USB_SetClockSource(void)	;
#define USB_FIFO_SIZE_EP0   0x08
#define USB_FIFO_SIZE_EP1   0x16
#define USB_FIFO_SIZE_EP2   0x16
#define USB_FIFO_SIZE_EP3   0x16
#define USB_FIFO_SIZE_EP4   0x16
#define USB_FIFO_SIZE_EP5   0x16
#define USB_FIFO_SIZE_EP6   0x40
#define USB_FIFO_SIZE_EP7   0x200

#define USB_TRANSFER_SETUP         0x00
#define USB_TRANSFER_ISO_OUT       0x02
#define USB_TRANSFER_ISO_IN        0x03
#define USB_TRANSFER_BULK_OUT      0x04
#define USB_TRANSFER_BULK_IN       0x05 
#define USB_TRANSFER_INTERRUPT_OUT 0x06
#define USB_TRANSFER_INTERRUPT_IN  0x07

#define USB_END_POINT_0 (0x01 << 0x00)
#define USB_END_POINT_1 (0x01 << 0x01)
#define USB_END_POINT_2 (0x01 << 0x02)
#define USB_END_POINT_3 (0x01 << 0x03)
#define USB_END_POINT_4 (0x01 << 0x04)
#define USB_END_POINT_5 (0x01 << 0x05)
#define USB_END_POINT_6 (0x01 << 0x06)
#define USB_END_POINT_7 (0x01 << 0x07)

#define USB_CONTROL_POINT           USB_END_POINT_0
#define USB_BULK_IN_POINT           USB_END_POINT_6
#define USB_BULK_OUT_POINT          USB_END_POINT_7
#define USB_INTERRUPT_IN_POINT      USB_END_POINT_3
#define USB_INTERRUPT_OUT_POINT     USB_END_POINT_3
#define USB_ISO_IN_POINT            USB_END_POINT_3
#define USB_ISO_OUT_POINT           USB_END_POINT_3


#define USB_STATUS_TIMER_OUT    0
#define USB_STATUS_OK           1
#define USB_STATUS_RESET        2
#define USB_STATUS_OTHER_POINT  3

#define USB_TIMER_OUT_SETUP      1000
#define USB_TIMER_OUT_BULK       4000
#define USB_TIMER_OUT_INTERRUPT  4000
#define USB_TIMER_OUT_ISO        4000

#define USB_DIR_IN  0x01
#define USB_DIR_OUT 0x00


#define USB_INTERTUPT_EN_RESET    (1 <<  8)
#define USB_INTERTUPT_EN_SUSPEND  (1 <<  9)
#define USB_INTERTUPT_EN_SOF      (1 << 10)
#define USB_INTERTUPT_EN_CONFIG   (1 << 11)
#define USB_INTERTUPT_EN_USB      (1 << 12)
#define USB_INTERTUPT_EN_RESUME   (1 << 13)

#define USB_CONTROL1_IP_EN         (1 << 0)
#define USB_CONTROL1_UDC_EN        (1 << 1)

#define USB_CONTROL2_R1_CLOSE      (1 << 0)
#define USB_CONTROL2_R2_CLOSE      (1 << 1)
#endif
