⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.pre

📁 USB interface examples using CDC by HT-PICC 18
💻 PRE
📖 第 1 页 / 共 4 页
字号:
void USBCtrlTrfRxService(void);
void USBCtrlEPServiceComplete(void);

void USBPrepareForNextSetupTrf(void);
void USBCheckBusStatus(void);
void USBDriverService(void);
void USBRemoteWakeup(void);
void USBSoftDetach(void);
void ClearArray(byte* startAdr,byte count);
void USBModuleEnable(void);
void USBModuleDisable(void);
void USBSuspend(void);
void USBWakeFromSuspend(void);
void USBProtocolReset(void);
void USB_SOF_(void);
void USBStall(void);
void USBError(void);

void USBCtrlTrfSetup(void);
void USBCtrlTrfOut(void);
void USBCtrlTrfIn(void);

void USBStdGetDsc(void);
void USBStdSetCfg(void);
void USBStdGetStatus(void);
void USBStdFeatureReq(void);

void USBGenInitEP(void);
void USBGenWrite(byte *buffer, byte len);
byte USBGenRead(byte *buffer, byte len);

# 182
typedef union _CTRL_TRF_SETUP
{
struct
{
byte _byte[64];
} a;


struct
{
byte bmRequestType;
byte bRequest;
word wValue;
word wIndex;
word wLength;
} b;
struct
{
unsigned :8;
unsigned :8;
WORD W_Value;
WORD W_Index;
WORD W_Length;
} c;
struct
{
unsigned Recipient:5;
unsigned RequestType:2;
unsigned DataDir:1;
unsigned :8;
byte bFeature;
unsigned :8;
unsigned :8;
unsigned :8;
unsigned :8;
unsigned :8;
} d;
struct
{
unsigned :8;
unsigned :8;
byte bDscIndex;
byte bDscType;
word wLangID;
unsigned :8;
unsigned :8;
} e;
struct
{
unsigned :8;
unsigned :8;
BYTE bDevADR;
byte bDevADRH;
unsigned :8;
unsigned :8;
unsigned :8;
unsigned :8;
} f;
struct
{
unsigned :8;
unsigned :8;
byte bCfgValue;
byte bCfgRSD;
unsigned :8;
unsigned :8;
unsigned :8;
unsigned :8;
} g;
struct
{
unsigned :8;
unsigned :8;
byte bAltID;
byte bAltID_H;
byte bIntfID;
byte bIntfID_H;
unsigned :8;
unsigned :8;
} h;
struct
{
unsigned :8;
unsigned :8;
unsigned :8;
unsigned :8;
byte bEPID;
byte bEPID_H;
unsigned :8;
unsigned :8;
} i;
struct
{
unsigned :8;
unsigned :8;
unsigned :8;
unsigned :8;
unsigned EPNum:4;
unsigned :3;
unsigned EPDir:1;
unsigned :8;
unsigned :8;
unsigned :8;
} j;
} CTRL_TRF_SETUP;


typedef union _CTRL_TRF_DATA
{
struct
{
byte _byte[64];
} a;
struct
{
byte _byte0;
byte _byte1;
byte _byte2;
byte _byte3;
byte _byte4;
byte _byte5;
byte _byte6;
byte _byte7;
} b;
struct
{
word _word0;
word _word1;
word _word2;
word _word3;
} c;
} CTRL_TRF_DATA;

# 376
typedef struct _USB_DEV_DSC
{
byte bLength; byte bDscType; word bcdUSB;
byte bDevCls; byte bDevSubCls; byte bDevProtocol;
byte bMaxPktSize0; word idVendor; word idProduct;
word bcdDevice; byte iMFR; byte iProduct;
byte iSerialNum; byte bNumCfg;
} USB_DEV_DSC;


typedef struct _USB_CFG_DSC
{
byte bLength; byte bDscType; word wTotalLength;
byte bNumIntf; byte bCfgValue; byte iCfg;
byte bmAttributes; byte bMaxPower;
} USB_CFG_DSC;


typedef struct _USB_INTF_DSC
{
byte bLength; byte bDscType; byte bIntfNum;
byte bAltSetting; byte bNumEPs; byte bIntfCls;
byte bIntfSubCls; byte bIntfProtocol; byte iIntf;
} USB_INTF_DSC;


typedef struct _USB_EP_DSC
{
byte bLength; byte bDscType; byte bEPAdr;
byte bmAttributes; word wMaxPktSize; byte bInterval;
} USB_EP_DSC;

# 470
typedef struct
{
USB_CFG_DSC cd01;
USB_INTF_DSC i00a00;
USB_EP_DSC ep01o_i00a00;
USB_EP_DSC ep01i_i00a00;
} CFG01;

extern const USB_DEV_DSC device_dsc;

extern const unsigned char *const USB_CD_Ptr[];
extern const unsigned char *const USB_SD_Ptr[];

# 508
typedef union _USB_DEVICE_STATUS
{
byte _byte;
struct
{
unsigned RemoteWakeup:1;
unsigned ctrl_trf_mem:1;
} a;
} USB_DEVICE_STATUS;

typedef union _BD_STAT
{
byte _byte;
struct{
unsigned BC8:1;
unsigned BC9:1;
unsigned BSTALL:1;
unsigned DTSEN:1;
unsigned INCDIS:1;
unsigned KEN:1;
unsigned DTS:1;
unsigned UOWN:1;
} a;
struct{
unsigned BC8:1;
unsigned BC9:1;
unsigned PID0:1;
unsigned PID1:1;
unsigned PID2:1;
unsigned PID3:1;
unsigned :1;
unsigned UOWN:1;
} b;
struct{
unsigned :2;
unsigned PID:4;
unsigned :2;
} c;
} BD_STAT;

typedef union _BDT
{
byte _byte[4];

struct
{
BD_STAT Status;
byte Cnt;
byte ADRL;
byte ADRH;
} a;
struct
{
byte StatWhole;
byte Cnt;
byte* ADR;
} b;
} BDT;

typedef struct
{
BDT out;
BDT in;
} EndpointType;


extern byte usb_device_state;
extern USB_DEVICE_STATUS usb_stat;
extern byte usb_active_cfg;
extern byte usb_alt_intf[1];


static volatile EndpointType ep[2] @ 0x400;


static volatile CTRL_TRF_SETUP SetupPkt @ 0x480;
static volatile CTRL_TRF_DATA CtrlTrfData @ 0x4C0;

static volatile byte usbgen_out[64] @ 0x600;
static volatile byte usbgen_in[64] @ 0x700;


const USB_DEV_DSC device_dsc=
{
sizeof(USB_DEV_DSC),
0x01,
0x0200,
0x00,
0x00,
0x00,
64,
0x04D8,
0x0011,
0x0000,
0x01,
0x02,
0x00,
0x01
};
const CFG01 cfg01=
{
sizeof(USB_CFG_DSC),
0x02,
sizeof(cfg01),
1,
1,
0,
0x01<<7,
50,


sizeof(USB_INTF_DSC),
0x04,
0,
0,
2,
0x00,
0x00,
0x00,
0,


sizeof(USB_EP_DSC),0x05,0x01,0x02,64,32,
sizeof(USB_EP_DSC),0x05,0x81,0x02,64,32
};

const struct{byte bLength;byte bDscType;word string[1];}sd000={
sizeof(sd000),0x03,0x0409};

const struct{byte bLength;byte bDscType;word string[18];}sd001={
sizeof(sd001),0x03,
'P','h','a','m',' ','T','h','a','i',' ',
'H','o','a',' ','B','K','H','N'};

const struct{byte bLength;byte bDscType;word string[17];}sd002={
sizeof(sd002),0x03,
'G','i','a','o',' ','T','i','e','p',' ','U',
'S','B',' ','2','.','0'};

const unsigned char *const USB_CD_Ptr[]=
{
(const unsigned char *) &cfg01,
(const unsigned char *) &cfg01
};

const unsigned char *const USB_SD_Ptr[]=
{
(const unsigned char *) &sd000,
(const unsigned char *) &sd001,
(const unsigned char *) &sd002
};

# 676
byte ctrl_trf_state;
byte ctrl_trf_session_owner;

POINTER pSrc;
POINTER pDst;
WORD wCount;

# 709
void USBCheckStdRequest(void)
{
if(SetupPkt.d.RequestType != 0x00) return;
switch(SetupPkt.b.bRequest)
{
case 5:
ctrl_trf_session_owner = 1;
usb_device_state = 4;
break;
case 6:
USBStdGetDsc();
break;
case 9:
USBStdSetCfg();
break;
case 8:
ctrl_trf_session_owner = 1;
pSrc.bRam = (byte*)&usb_active_cfg;
usb_stat.a.ctrl_trf_mem = 0;
((wCount).d.v[0]) = 1;
break;
case 0:
USBStdGetStatus();
break;
case 1:
case 3:
USBStdFeatureReq();
break;
case 10:
ctrl_trf_session_owner = 1;
pSrc.bRam = (byte*)usb_alt_intf+SetupPkt.h.bIntfID;
usb_stat.a.ctrl_trf_mem = 0;
((wCount).d.v[0]) = 1;
break;
case 11:
ctrl_trf_session_owner = 1;
usb_alt_intf[SetupPkt.h.bIntfID] = SetupPkt.h.bAltID;
break;
case 7:
case 12:
default:
break;
}
}

void USBStdGetDsc(void)
{
if(SetupPkt.b.bmRequestType == 0x80)
{
switch(SetupPkt.e.bDscType)
{
case 0x01:
ctrl_trf_session_owner = 1;
pSrc.bRom = (const byte*)&device_dsc;
wCount._word = sizeof(device_dsc);
break;
case 0x02:
ctrl_trf_session_owner = 1;
pSrc.bRom = *(USB_CD_Ptr+SetupPkt.e.bDscIndex);
wCount._word = *(pSrc.wRom+1);
break;
case 0x03:
ctrl_trf_session_owner = 1;
pSrc.bRom = *(USB_SD_Ptr+SetupPkt.e.bDscIndex);
wCount._word = *pSrc.bRom;
break;
}
usb_stat.a.ctrl_trf_mem = 1;
}
}


void USBStdSetCfg(void)
{
ctrl_trf_session_owner = 1;
ClearArray((byte*)&UEP1,15);;
ClearArray((byte*)usb_alt_intf,1);
usb_active_cfg = SetupPkt.g.bCfgValue;
if(SetupPkt.g.bCfgValue == 0)
usb_device_state = 5;
else
{
usb_device_state = 6;
USBGenInitEP();
}
}


void USBStdGetStatus(void)
{
CtrlTrfData.b._byte0 = 0;
CtrlTrfData.b._byte1 = 0;
switch (SetupPkt.d.Recipient)
{
case 0:
ctrl_trf_session_owner = 1;
if(1 == 1)
CtrlTrfData.b._byte0|=0b00000001;
if(usb_stat.a.RemoteWakeup == 1)
CtrlTrfData.b._byte0|=0b00000010;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -