📄 main.pre
字号:
break;
case 1:
ctrl_trf_session_owner = 1;
break;
case 2:
ctrl_trf_session_owner = 1;
pDst.bRam = (byte*)&ep[0].out+(SetupPkt.j.EPNum*8)+(SetupPkt.j.EPDir*4);
if(*pDst.bRam & 0x04)
CtrlTrfData.b._byte0=0x01;
break;
}
if(ctrl_trf_session_owner == 1)
{
pSrc.bRam = (byte*)&CtrlTrfData;
usb_stat.a.ctrl_trf_mem = 0;
((wCount).d.v[0]) = 2;
}
}
void USBStdFeatureReq(void)
{
if((SetupPkt.d.bFeature == 0x01)&&
((SetupPkt.b.bmRequestType & 0x1F) == 0))
{
ctrl_trf_session_owner = 1;
if(SetupPkt.b.bRequest == 3)
usb_stat.a.RemoteWakeup = 1;
else
usb_stat.a.RemoteWakeup = 0;
}
if ((SetupPkt.d.bFeature == 0x00)&&
((SetupPkt.b.bmRequestType & 0x1F) == 2)&&
((SetupPkt.a._byte[4] & 0x0F) != 0))
{
ctrl_trf_session_owner = 1;
pDst.bRam = (byte*)&ep[0].out+(SetupPkt.j.EPNum*8)+(SetupPkt.j.EPDir*4);
if(SetupPkt.b.bRequest == 3)
*pDst.bRam = 0x80|0x04;
else
{
if(SetupPkt.j.EPDir == 1)
*pDst.bRam = 0x00;
else
*pDst.bRam = 0x80|0x00|0x08;
}
}
}
void USBCtrlEPService(void)
{
if(USTAT == (0x00<<3)|(0<<2))
{
if(ep[0].out.a.Status.c.PID == 0b00001101)
USBCtrlTrfSetup();
else
USBCtrlTrfOut();
}
else if(USTAT == (0x00<<3)|(1<<2))
USBCtrlTrfIn();
}
void USBCtrlTrfSetup(void)
{
ctrl_trf_state = 0;
ctrl_trf_session_owner = 0;
wCount._word = 0;
USBCheckStdRequest();
USBCtrlEPServiceComplete();
}
void USBCtrlTrfOut(void)
{
if(ctrl_trf_state == 2)
{
USBCtrlTrfRxService();
if(ep[0].out.a.Status.a.DTS == 0)
ep[0].out.a.Status._byte = 0x80|0x40|0x08;
else
ep[0].out.a.Status._byte = 0x80|0x00|0x08;
}
else
USBPrepareForNextSetupTrf();
}
void USBCtrlTrfIn(void)
{
if(usb_device_state==4) { UADDR = SetupPkt.f.bDevADR._byte; if(UADDR > 0) usb_device_state=5; else usb_device_state=3; };
if(ctrl_trf_state == 1)
{
USBCtrlTrfTxService();
if(ep[0].in.a.Status.a.DTS == 0)
ep[0].in.a.Status._byte = 0x80|0x40|0x08;
else
ep[0].in.a.Status._byte = 0x80|0x00|0x08;
}
else
USBPrepareForNextSetupTrf();
}
void USBCtrlTrfTxService(void)
{
WORD byte_to_send;
if(wCount._word < 64)
byte_to_send._word = wCount._word;
else
byte_to_send._word = 64;
ep[0].in.a.Status.a.BC9 = 0;
ep[0].in.a.Status.a.BC8 = 0;
ep[0].in.a.Status._byte |= ((byte_to_send).d.v[1]);
ep[0].in._byte[1] = ((byte_to_send).d.v[0]);
wCount._word = wCount._word - byte_to_send._word;
pDst.bRam = (byte*)&CtrlTrfData;
if(usb_stat.a.ctrl_trf_mem == 1)
{
while(byte_to_send._word)
{
*pDst.bRam = *pSrc.bRom;
pDst.bRam++;
pSrc.bRom++;
byte_to_send._word--;
}
}
else
{
while(byte_to_send._word)
{
*pDst.bRam = *pSrc.bRam;
pDst.bRam++;
pSrc.bRam++;
byte_to_send._word--;
}
}
}
void USBCtrlTrfRxService(void)
{
WORD byte_to_read;
((byte_to_read).d.v[1]) = 0x03 & ep[0].out.a.Status._byte;
((byte_to_read).d.v[0]) = ep[0].out.a.Cnt;
wCount._word = wCount._word + byte_to_read._word;
pSrc.bRam = (byte*)&CtrlTrfData;
while(byte_to_read._word)
{
*pDst.bRam = *pSrc.bRam;
pDst.bRam++;
pSrc.bRam++;
byte_to_read._word--;
}
}
void USBCtrlEPServiceComplete(void)
{
if(ctrl_trf_session_owner == 0)
{
ep[0].out.a.Cnt = 64;
ep[0].out.b.ADR = (byte*)&SetupPkt;
ep[0].out.a.Status._byte = 0x80|0x04;
ep[0].in.a.Status._byte = 0x80|0x04;
}
else
{
if(SetupPkt.d.DataDir == 1)
{
if(SetupPkt.b.wLength < wCount._word)
wCount._word = SetupPkt.b.wLength;
USBCtrlTrfTxService();
ctrl_trf_state = 1;
ep[0].out.a.Cnt = 64;
ep[0].out.b.ADR = (byte*)&SetupPkt;
ep[0].out.a.Status._byte = 0x80;
ep[0].in.b.ADR = (byte*)&CtrlTrfData;
ep[0].in.a.Status._byte = 0x80|0x40|0x08;
}
else
{
ctrl_trf_state = 2;
ep[0].in.a.Cnt = 0;
ep[0].in.a.Status._byte = 0x80|0x40|0x08;
ep[0].out.a.Cnt = 64;
ep[0].out.b.ADR = (byte*)&CtrlTrfData;
ep[0].out.a.Status._byte = 0x80|0x40|0x08;
}
}
PKTDIS = 0;
}
void USBPrepareForNextSetupTrf(void)
{
ctrl_trf_state = 0;
ep[0].out.a.Cnt = 64;
ep[0].out.b.ADR = (byte*)&SetupPkt;
ep[0].out.a.Status._byte = 0x80|0x00|0x08;
ep[0].in.a.Status._byte = 0x00;
}
byte usb_device_state;
USB_DEVICE_STATUS usb_stat;
byte usb_active_cfg;
byte usb_alt_intf[1];
void USBCheckBusStatus(void)
{
if(1 == 1)
{
if(USBEN == 0)
USBModuleEnable();
}
else
{
if(USBEN == 1)
USBModuleDisable();
}
if(usb_device_state == 1)
{
if(!SE0)
{
UIR = 0;
UIE = 0;
URSTIE = 1;
IDLEIE = 1;
usb_device_state = 2;
}
}
}
void USBModuleEnable(void)
{
UCON = 0;
UIE = 0;
USBEN = 1;
usb_device_state = 1;
}
void USBModuleDisable(void)
{
UCON = 0;
UIE = 0;
usb_device_state = 0;
}
void USBSoftDetach(void)
{
USBModuleDisable();
}
void USBDriverService(void)
{
if(usb_device_state == 0) return;
if(ACTVIF && ACTVIE) USBWakeFromSuspend();
if(SUSPND==1) return;
if(URSTIF && URSTIE) USBProtocolReset();
if(IDLEIF && IDLEIE) USBSuspend();
if(SOFIF && SOFIE) USB_SOF_();
if(STALLIF && STALLIE) USBStall();
if(UERRIF && UERRIE) USBError();
if(usb_device_state < 3) return;
if(TRNIF && TRNIE)
{
USBCtrlEPService();
TRNIF = 0;
}
}
void USBSuspend(void)
{
ACTVIE = 1;
IDLEIF = 0;
SUSPND = 1;
}
void USBWakeFromSuspend(void)
{
SUSPND = 0;
ACTVIE = 0;
ACTVIF = 0;
}
void USBRemoteWakeup(void)
{
if(usb_stat.a.RemoteWakeup == 1)
{
USBWakeFromSuspend();
RESUME = 1;
RESUME = 0;
}
}
void USB_SOF_(void)
{
SOFIF = 0;
}
void USBStall(void)
{
if(UEP0bits.EPSTALL == 1)
{
USBPrepareForNextSetupTrf();
UEP0bits.EPSTALL = 0;
}
STALLIF = 0;
}
void USBError(void)
{
UERRIF = 0;
}
void USBProtocolReset(void)
{
UEIR = 0;
UIR = 0;
UEIE = 0b10011111;
UIE = 0b01111011;
UADDR = 0x00;
ClearArray((byte*)&UEP1,15);;
UEP0 = 0x06|0x10;
while(TRNIF == 1) TRNIF = 0;
PKTDIS = 0;
USBPrepareForNextSetupTrf();
usb_stat.a.RemoteWakeup = 0;
usb_active_cfg = 0;
usb_device_state = 3;
}
void ClearArray(byte* startAdr,byte count)
{
byte* pTemp = startAdr;
while(count)
{
*pTemp++ = 0;
# 1175
count--;
}
}
byte usbgen_rx_len;
extern byte usbgen_rx_len;
void USBGenInitEP(void)
{
usbgen_rx_len = 0;
UEP1 = 0x0E|0x10;
ep[1].out.a.Cnt = sizeof(usbgen_out);
ep[1].out.b.ADR = (byte*)usbgen_out;
ep[1].out.a.Status._byte = 0x80|0x00|0x08;
ep[1].in.b.ADR = (byte*)usbgen_in;
ep[1].in.a.Status._byte = 0x00|0x40;
}
void USBGenWrite(byte *buffer, byte len)
{
byte i;
if(len > 64)
len = 64;
for (i = 0; i < len; i++)
usbgen_in[i] = buffer[i];
ep[1].in.a.Cnt = len;
{ ep[1].in.a.Status._byte &= 0x40; ep[1].in.a.Status.a.DTS = !ep[1].in.a.Status.a.DTS; ep[1].in.a.Status._byte |= 0x80|0x08; };
}
byte USBGenRead(byte *buffer, byte len)
{
usbgen_rx_len = 0;
if( !ep[1].out.a.Status.b.UOWN )
{
if(len > ep[1].out.a.Cnt)
len = ep[1].out.a.Cnt;
for(usbgen_rx_len = 0; usbgen_rx_len < len; usbgen_rx_len++)
buffer[usbgen_rx_len] = usbgen_out[usbgen_rx_len];
ep[1].out.a.Cnt = sizeof(usbgen_out);
{ ep[1].out.a.Status._byte &= 0x40; ep[1].out.a.Status.a.DTS = !ep[1].out.a.Status.a.DTS; ep[1].out.a.Status._byte |= 0x80|0x08; };
}
return usbgen_rx_len;
}
void USBTasks(void)
{
USBCheckBusStatus();
if(UTEYE!=1) USBDriverService();
}
typedef union DATA_PACKET
{
byte _byte[64];
} DATA_PACKET;
# 12 "C:\Documents and Settings\Thai Hoa\Desktop\USB\main.c"
asm("\tpsect config,class=CONFIG"); asm("global config_word" "1"); asm("\torg (""1""-1)*2"); asm("config_word" "1"":"); asm("\tdw ""0xFEFF & 0xFFFF & 0xFFFC & 0xFFE7 & 0xBFFF & 0x7FFF");
asm("\tpsect config,class=CONFIG"); asm("global config_word" "2"); asm("\torg (""2""-1)*2"); asm("config_word" "2"":"); asm("\tdw ""0xFFFF & 0xFFFF & 0xFFFF & 0xFFFF & 0xFEFF & 0xFFFF");
asm("\tpsect config,class=CONFIG"); asm("global config_word" "3"); asm("\torg (""3""-1)*2"); asm("config_word" "3"":"); asm("\tdw ""0xFFFF & 0xFDFF & 0xFBFF & 0xFFFF");
asm("\tpsect config,class=CONFIG"); asm("global config_word" "4"); asm("\torg (""4""-1)*2"); asm("config_word" "4"":"); asm("\tdw ""0xFFFF & 0xFFFB & 0xFFBF & 0xFFFF");
asm("\tpsect config,class=CONFIG"); asm("global config_word" "5"); asm("\torg (""5""-1)*2"); asm("config_word" "5"":"); asm("\tdw ""0xFFFF");
asm("\tpsect config,class=CONFIG"); asm("global config_word" "6"); asm("\torg (""6""-1)*2"); asm("config_word" "6"":"); asm("\tdw ""0xFFFF");
asm("\tpsect config,class=CONFIG"); asm("global config_word" "7"); asm("\torg (""7""-1)*2"); asm("config_word" "7"":"); asm("\tdw ""0xFFFF");
void Init(void);
void USBTasks(void);
void UserTasks(void);
void main(void)
{
Init();
while(1)
{
USBTasks();
UserTasks();
}
}
void Init(void)
{
ADCON1 |= 0x0F;
TRISC=0x00;
PORTC=0x00;
{UCFG = 0x10|0x00|0x04|0x00; usb_device_state = 0; usb_stat._byte = 0x00; usb_active_cfg = 0x00;};
}
void UserTasks(void)
{
DATA_PACKET dataPacket;
if((usb_device_state < 6)||(SUSPND==1)) return;
if(USBGenRead((byte*)&dataPacket,sizeof(dataPacket)))
{
if (dataPacket._byte[1]==1) RC0=!RC0;
if (dataPacket._byte[1]==2) RC1=!RC1;
if (dataPacket._byte[1]==3) RC2=!RC2;
}
dataPacket._byte[0]='A';
dataPacket._byte[1]='B';
if(!ep[1].in.a.Status.b.UOWN) USBGenWrite((byte*)&dataPacket,2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -