📄 usbdirq.c
字号:
#include "usb.h"
#define TBR 0x50
#define LSR 0x55
extern unsigned char DelKeyQueueEx(void);
void Usb_Stop(void)
{
UsbSetMode(0x06,0x04);
}
void Usb_Start(void)
{
unsigned short i;
unsigned char buf[512];
UDISK_PARA* pPara;
/*FsInitWorkBuf(&g_epctl.fs, CacheBuf1, 0);*/
/*读入坏块*/
nfreadpage(31,buf);/*第31pgae*/
pPara = (UDISK_PARA*)buf;
for ( i = 0; i < BADBLOCKNUM; i++ )
g_epctl.nBadBlocks[i] = pPara->nBadBlocks[i];
g_epctl.nBadBlockCount = pPara->nBadBlockCount;
g_epctl.CbwCmd = 0xff;
g_epctl.recflag = 0;
UsbSetEndpointStatus(0,0);
UsbSetEndpointStatus(1,0);
UsbSetEndpointStatus(2,0);
UsbSetEndpointStatus(3,0);
UsbSetEndpointStatus(4,0);
UsbSetEndpointStatus(5,0);
i = UsbReadEndpointStatus(0);
i = UsbReadEndpointStatus(1);
i = UsbReadEndpointStatus(2);
i = UsbReadEndpointStatus(3);
i = UsbReadEndpointStatus(4);
i = UsbReadEndpointStatus(5);
UsbReadLastTransactionStatus(0);
UsbReadLastTransactionStatus(1);
UsbReadLastTransactionStatus(2);
UsbReadLastTransactionStatus(3);
UsbReadLastTransactionStatus(4);
UsbReadLastTransactionStatus(5);
/*设置地址使能*/
UsbSetDMA(0);
UsbSetAddressEnable(0x00,1);
UsbSetMode(0x16,0x04);
/*设置端点使能*/
UsbSetEndpointEnable(1);
isr = UsbReadInterruptRegister();
/*_UsbInit();*/
}
void ep0_rxdone(void)
{
unsigned char ep_last;
DEVICE_REQUEST *d_request;
ep_last = UsbReadLastTransactionStatus(0); /* Clear interrupt flag*/
if (ep_last & D12_SETUPPACKET)/*接收设置包*/
{
if( UsbReadEndpoint(0, (unsigned char *)g_epctl.dataReBufer,
8) != 8 )
{
UsbSetEndpointStatus(0, 1);/*端点设置为停止*/
UsbSetEndpointStatus(1, 1);
g_epctl.state= USB_IDLE;
g_epctl.request= 0;/*none*/
return;
}
/* Acknowledge setup here to unlock in/out endp*/
UsbAcknowledgeEndpoint(0);
UsbAcknowledgeEndpoint(1);
d_request = (DEVICE_REQUEST *)g_epctl.dataReBufer;
g_epctl.ep0sendlen= d_request->wLength;
if (g_epctl.ep0sendlen >1000)
{
UsbSetEndpointStatus(0, 1);
UsbSetEndpointStatus(1, 1);
g_epctl.state= USB_IDLE;
g_epctl.request= 0;/*none*/
return;
}
else
{
/*正确请求数据放入g_request*/
MemoryCopy ((U8 *)&g_usbrequest,(U8 *)g_epctl.dataReBufer,8);
g_epctl.request = 1;
g_epctl.state= USB_TRANSMIT;
}
}
}
void ep0_txdone(void)
{
unsigned short i ;
//unsigned char isr;
isr = UsbReadLastTransactionStatus(1); /* Clear interrupt flag*/
i = g_epctl.ep0sendlen;
if (g_epctl.state != USB_TRANSMIT)
return;
if( i >= EP0_PACKET_SIZE)
{
UsbWriteEndpoint(1, g_epctl.ep0pData, EP0_PACKET_SIZE);
g_epctl.ep0pData+=EP0_PACKET_SIZE;
g_epctl.ep0sendlen =i - EP0_PACKET_SIZE;
g_epctl.state= USB_TRANSMIT;
}
else if( i != 0)
{
UsbWriteEndpoint(1, g_epctl.ep0pData, i);
g_epctl.ep0sendlen = 0;
g_epctl.state = USB_TRANSMIT;
}
else if (i == 0)
{
UsbWriteEndpoint(1, 0, 0); /* Send zero packet at the end */
g_epctl.state = USB_IDLE;
}
}
void ep1_txdone(void)
{
UsbReadLastTransactionStatus(3); /* Clear interrupt flag */
}
void ep1_rxdone(void)
{
unsigned char len;
unsigned char buf[16];
UsbReadLastTransactionStatus(2); /* Clear interrupt flag */
len = UsbReadEndpoint(2, buf, 16);
}
void main_txdone(void)
{
//unsigned char isr;
isr = UsbReadLastTransactionStatus(5); /* Clear interrupt flag */
if(g_epctl.recflag == 1)
{
g_epctl.ep5isr = 1;
}
}
void main_rxdone(void)
{
unsigned char len,*p;
//unsigned char isr;
isr = UsbReadLastTransactionStatus(4); /* Clear interrupt flag */
if (g_epctl.recflag == 1)
{
g_epctl.ep4isr = 1;
return;
}
len = UsbReadMainEndpoint(g_epctl.mainData);
p = g_epctl.mainData;
if ( p[0] != 'U' || p[1] != 'S'
|| p[2] != 'B' || p[3] != 'C' )
{
return;
}
/*设置新命令,保存命令*/
MemoryCopy((void *)g_epctl.CBW,p,31);
g_epctl.CbwCmd = p[15];
if(g_epctl.CbwCmd == 0x2A && len>0x1f)
{
g_epctl.Pageptr = len - 0x1f;
}
else
{
g_epctl.Pageptr = 0;
}
g_epctl.recflag = 0;
return;
}
void bus_reset(void)
{
g_epctl.state = USB_IDLE;
g_epctl.suspend = SU_NOMAL;
g_epctl.request =0;
g_epctl.ep0wantlen = 0;
g_epctl.ep0sendlen = 0;
g_epctl.ep5isr = 0;
g_epctl.ep4isr = 0;
g_epctl.recflag =0;
irqcount = 0;
}
void Usb_irq(void)
{
unsigned short i_st;
//WrUTXH0( 'i' ) ;
i_st = UsbReadInterruptRegister(); //读取中断寄存器
isr = i_st;
if(i_st != 0)
{
if(i_st & D12_INT_BUSRESET) //D12_INT_BUSRESET 0x0040
{
bus_reset();
g_UsbConFlags[intcount ++] = 0x75; //("u");
}
else
{
if(i_st & D12_INT_SUSPENDCHANGE) //D12_INT_SUSPENDCHANGE 0x0080
{
if(g_epctl.suspend == SU_NOMAL)
{
g_epctl.suspend = SU_SUSPEND;
g_UsbConFlags[intcount ++] = 0x78; //("x");
}
else
{
g_epctl.suspend = SU_NOMAL;
g_UsbConFlags[intcount ++] = 0x40;//("@");
}
}
if(i_st & D12_INT_ENDP0IN) //D12_INT_ENDP0IN 0x0002
{
ep0_txdone();
}
if(i_st & D12_INT_ENDP0OUT)
{
ep0_rxdone();
}
if(i_st & D12_INT_ENDP1IN)
{
ep1_txdone();
}
if(i_st & D12_INT_ENDP1OUT)
{
ep1_rxdone();
}
if(i_st & D12_INT_ENDP2IN)
{
main_txdone();
}
if(i_st & D12_INT_ENDP2OUT)
{
main_rxdone();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -