📄 inthandler.c
字号:
#include "Def.h"
#include "Uart.h"
#include "UsbAddr.h"
#include "UsbLib.h"
#include "UsbInit.h"
#include "IntHandler.h"
#include "InOutHandler.h"
extern volatile int iEpInState;
extern volatile int iEpOutState;
//volatile int isUsbdSetConfiguration;
#define CLR_EP0_OUT_PKT_RDY() rEP0_CSR=( (ep0_csr & (~EP0_WR_BITS))| EP0_SERVICED_OUT_PKT_RDY )
#define CLR_EP0_OUTPKTRDY_DATAEND() rEP0_CSR=( (ep0_csr & (~EP0_WR_BITS))|(EP0_SERVICED_OUT_PKT_RDY|EP0_DATA_END) )
#define SET_EP0_IN_PKT_RDY() rEP0_CSR=( (ep0_csr & (~EP0_WR_BITS))|(EP0_IN_PKT_READY) )
#define SET_EP0_INPKTRDY_DATAEND() rEP0_CSR=( (ep0_csr & (~EP0_WR_BITS))|(EP0_IN_PKT_READY|EP0_DATA_END) )
#define CLR_EP0_SETUP_END() rEP0_CSR=( (ep0_csr & (~EP0_WR_BITS))|(EP0_SERVICED_SETUP_END) )
#define CLR_EP0_SENT_STALL() rEP0_CSR=( (ep0_csr & (~EP0_WR_BITS))&(~EP0_SENT_STALL) )
#define FLUSH_EP0_FIFO() {while(rOUT_FIFO_CNT1_REG)rEP0_FIFO;}
struct USB_SETUP_DATA descSetup;
struct USB_DEVICE_DESCRIPTOR descDev;
struct USB_CONFIGURATION_DESCRIPTOR descConf;
struct USB_INTERFACE_DESCRIPTOR descIf;
struct USB_ENDPOINT_DESCRIPTOR descEndpt0;
struct USB_ENDPOINT_DESCRIPTOR descEndpt1;
struct USB_CONFIGURATION_SET ConfigSet;
struct USB_INTERFACE_GET InterfaceGet;
struct USB_GET_STATUS StatusGet; //={0,0,0,0,0};
U32 ep0State;
static const U8 descStr0[]={
4,STRING_TYPE,LANGID_US_L,LANGID_US_H, //codes representing languages
};
static const U8 descStr1[]={ //Manufacturer
(0x14+2),STRING_TYPE,
'S',0x0,'y',0x0,'s',0x0,'t',0x0,'e',0x0,'m',0x0,' ',0x0,'M',0x0,
'C',0x0,'U',0x0,
};
static const U8 descStr2[]={ //Product
(0x2a+2),STRING_TYPE,
'S',0x0,'E',0x0,'C',0x0,' ',0x0,'S',0x0,'3',0x0,'C',0x0,'2',0x0,
'4',0x0,'1',0x0,'0',0x0,'X',0x0,' ',0x0,'T',0x0,'e',0x0,'s',0x0,
't',0x0,' ',0x0,'B',0x0,'/',0x0,'D',0x0
};
void IRQ_IsrUsbd(void)
{
U8 usbdIntpnd,epIntpnd;
U8 saveIndexReg=rINDEX_REG;
usbdIntpnd=rUSB_INT_REG;
epIntpnd=rEP_INT_REG;
// UartPrintf( "[INT:EP_I=%x,USBI=%x]\r\n",epIntpnd,usbdIntpnd );
if(usbdIntpnd&SUSPEND_INT)
{
rUSB_INT_REG=SUSPEND_INT;
UartPrintf( "<SUS挂起]\r\n");
}
if(usbdIntpnd&RESUME_INT)
{
rUSB_INT_REG=RESUME_INT;
UartPrintf("<RSM唤醒]\r\n");
}
if(usbdIntpnd&RESET_INT)
{
UartPrintf( "<RST重启]\r\n");
//ResetUsbd();
ConfigUsbd();
rUSB_INT_REG=RESET_INT; //RESET_INT should be cleared after ResetUsbd().
// PrepareEp1Fifo();
}
if(epIntpnd&EP0_INT)
{
rEP_INT_REG=EP0_INT;
//UartPrintf("EP0-<");
Ep0Handler();
}
if(epIntpnd&EP1_INT)
{
rEP_INT_REG=EP1_INT;
//UartPrintf("EP1->\r\n");
Ep1Handler();
}
if(epIntpnd&EP2_INT)
{
rEP_INT_REG=EP2_INT;
//UartPrintf("<2:TBD]"); //not implemented yet
//Ep2Handler();
while(1);
}
if(epIntpnd&EP3_INT)
{
rEP_INT_REG=EP3_INT;
//UartPrintf("EP3_INT\r\n");
Ep3Handler();
}
if(epIntpnd&EP4_INT)
{
rEP_INT_REG=EP4_INT;
//UartPrintf("<4:TBD]"); //not implemented yet
//Ep4Handler();
while(1);
}
///
ClearPending(BIT_USBD);
rINDEX_REG=saveIndexReg; //还原端口索引
return;
}
void InitDescriptorTable(void)
{
//Standard device descriptor//设备描述符
descDev.bLength=0x12; //设备描述表的长度EP0_DEV_DESC_SIZE=0x12 bytes
descDev.bDescriptorType=DEVICE_TYPE; //设备描述符类型
descDev.bcdUSBL=0x10; //版本号(低位)Ver 1.10
descDev.bcdUSBH=0x01; //版本号(高位)Ver 1.10
descDev.bDeviceClass=0x0; //设备类码//0xFF; //0x0
descDev.bDeviceSubClass=0x0; //子类码
descDev.bDeviceProtocol=0x0; //协议码
descDev.bMaxPacketSize0=0x8; //端口最大包
descDev.idVendorL=0x45; //厂商标志(低位)
descDev.idVendorH=0x53; //厂商标志(高位)
descDev.idProductL=0x34; //产品标志(低位)
descDev.idProductH=0x12; //产品标志(高位)
descDev.bcdDeviceL=0x00; //设备发行号BCD码(低位)
descDev.bcdDeviceH=0x01; //设备发行号BCD码(高位)
descDev.iManufacturer=0x1; //index of string descriptor厂商信息字符串的索引
descDev.iProduct=0x2; //index of string descriptor产品信息字符串的索引
descDev.iSerialNumber=0x0; //设备信息字符串的索引
descDev.bNumConfigurations=0x1; //可能的配置数
//Standard configuration descriptor//配置描述符
descConf.bLength=0x9; //描述符的长度
descConf.bDescriptorType=CONFIGURATION_TYPE; //配置描述符类型
descConf.wTotalLengthL=0x20; //配置信息总长<cfg desc>+<if desc>+<endp0 desc>+<endp1 desc>
descConf.wTotalLengthH=0; //配置信息总长(高位)
descConf.bNumInterfaces=1; //此配置信息支持的接口个数
descConf.bConfigurationValue=1; //配置号
descConf.iConfiguration=0; //配置的字符串描述表索引
descConf.bmAttributes=CONF_ATTR_DEFAULT|CONF_ATTR_SELFPOWERED; //特性(位图) bus powered only.
descConf.maxPower=25; //所需功率 draws 50mA current from the USB bus.
//Standard interface descriptor//接口描述符
descIf.bLength=0x9; //接口描述符的长度
descIf.bDescriptorType=INTERFACE_TYPE; //接口描述符的类型
descIf.bInterfaceNumber=0x0; //接口数
descIf.bAlternateSetting=0x0; //可选设置索引值
descIf.bNumEndpoints=2; //端口0以外的端口数
descIf.bInterfaceClass=0x8; //设备类码--0x8:海量存储设备
descIf.bInterfaceSubClass=0x6; //设备子类码6=SCSI //0x0;
descIf.bInterfaceProtocol=0x50; //协议码,bulk 0nly 传输 //0x0;
descIf.iInterface=0x0; //接口的字符串描述表的索引值
//Standard endpoint0 descriptor//端口1描述符
descEndpt0.bLength=0x7; //端口描述符长度
descEndpt0.bDescriptorType=ENDPOINT_TYPE; //端口描述符类
descEndpt0.bEndpointAddress=1|EP_ADDR_IN; //端点地址,Bit7:控制端口方向(出/入),2400Xendpoint 1 is IN endpoint.
descEndpt0.bmAttributes=EP_ATTR_BULK; //bit[1:0]10=批传输,其它位保留
descEndpt0.wMaxPacketSizeL=EP1_PKT_SIZE; //包大小:64
descEndpt0.wMaxPacketSizeH=0x0; //高位
descEndpt0.bInterval=0x0; //访问端点的时间间隔,批传输此值忽略
//Standard endpoint1 descriptor//端口3描述符
descEndpt1.bLength=0x7; //端口描述符长度
descEndpt1.bDescriptorType=ENDPOINT_TYPE; //端口描述符类
descEndpt1.bEndpointAddress=3|EP_ADDR_OUT; //端点地址,Bit7:控制端口方向(出/入),2400Xendpoint 1 is IN endpoint.
descEndpt1.bmAttributes=EP_ATTR_BULK; //bit[1:0]10=批传输,其它位保留
descEndpt1.wMaxPacketSizeL=EP3_PKT_SIZE; //包大小:64
descEndpt1.wMaxPacketSizeH=0x0; //高位
descEndpt1.bInterval=0x0; //访问端点的时间间隔,批传输此值忽略
}//InitDescriptorTable()
void Ep0Handler(void)
{
volatile U8 Rwuen;
volatile U8 Selfpwr=TRUE;
static int ep0SubState;
U8 ep0_csr;
rINDEX_REG=0; //索引指向0端口
ep0_csr=rEP0_CSR; //端口0控制状态寄存器(判断端口0将要进行的操作)
//printk("csr_ep0=%x ",ep0_csr);
//DATAEND interrupt(ep0_csr==0x0) will be ignored
//because ep0State==EP0_STATE_INIT when the DATAEND interrupt is issued.
if(ep0_csr & EP0_SETUP_END) //0x10
{
// Host may end GET_DESCRIPTOR operation without completing the IN data stage.
// If host does that, SETUP_END bit will be set.
// OUT_PKT_RDY has to be also cleared because status stage sets OUT_PKT_RDY to 1.
//printk("[SETUPEND]");
CLR_EP0_SETUP_END();
if(ep0_csr & EP0_OUT_PKT_READY)
{
FLUSH_EP0_FIFO(); //(???)
//I think this isn't needed because EP0 flush is done automatically.
CLR_EP0_OUT_PKT_RDY();
}
ep0State=EP0_STATE_INIT;
return;
}
//I think that EP0_SENT_STALL will not be set to 1.
if(ep0_csr & EP0_SENT_STALL) //0x04
{
//printk("[STALL]");
CLR_EP0_SENT_STALL();
if(ep0_csr & EP0_OUT_PKT_READY)
{
CLR_EP0_OUT_PKT_RDY();
}
ep0State=EP0_STATE_INIT;
return;
}
if((ep0_csr & EP0_OUT_PKT_READY) && (ep0State==EP0_STATE_INIT)) //0x01 0x00
{
//printk("[ep0State=%d]",ep0State);
RdPktEp0((U8 *)&descSetup,EP0_PKT_SIZE); //接收0端口数据
//PrintEp0Pkt((U8 *)(&descSetup)); //打印0端口数据
switch(descSetup.bRequest)
{
case GET_DESCRIPTOR: //6
switch(descSetup.bValueH)
{
case DEVICE_TYPE: //1
//printk("[设备]");
CLR_EP0_OUT_PKT_RDY();
ep0State=EP0_STATE_GD_DEV_0; //10
break;
case CONFIGURATION_TYPE: //2
//printk("[配置]");
CLR_EP0_OUT_PKT_RDY();
if((descSetup.bLengthL+(descSetup.bLengthH<<8))>0x9)
//bLengthH should be used for bLength=0x209 at WIN2K.
ep0State=EP0_STATE_GD_CFG_0; //for WIN98,WIN2K
else
ep0State=EP0_STATE_GD_CFG_ONLY_0; //for WIN2K
break;
case STRING_TYPE: //3
//printk("[字符串]");
CLR_EP0_OUT_PKT_RDY();
switch(descSetup.bValueL)
{
case 0:
ep0State=EP0_STATE_GD_STR_I0;
break;
case 1:
ep0State=EP0_STATE_GD_STR_I1;
break;
case 2:
ep0State=EP0_STATE_GD_STR_I2;
break;
default:
//printk("[无字符串]");
break;
}
ep0SubState=0;
break;
case INTERFACE_TYPE: //4
//printk("[接口]");
CLR_EP0_OUT_PKT_RDY();
ep0State=EP0_STATE_GD_IF_ONLY_0; //for WIN98
break;
case ENDPOINT_TYPE: //5
//printk("[端点]");
CLR_EP0_OUT_PKT_RDY();
switch(descSetup.bValueL&0xf)
{
case 0:
ep0State=EP0_STATE_GD_EP0_ONLY_0;
break;
case 1:
ep0State=EP0_STATE_GD_EP1_ONLY_0;
break;
default:
//printk("[无端点]");
break;
}
break;
default:
//printk("[无描述符类型]");
break;
}
break;
case SET_ADDRESS: //0x5
//printk("[SA:%d]",descSetup.bValueL);
rFUNC_ADDR_REG=descSetup.bValueL | 0x80; //设置设备地址
CLR_EP0_OUTPKTRDY_DATAEND(); //Because of no data control transfers.
ep0State=EP0_STATE_INIT;
break;
case SET_CONFIGURATION: //9
//printk("[SC]");
ConfigSet.ConfigurationValue=descSetup.bValueL;
CLR_EP0_OUTPKTRDY_DATAEND(); //Because of no data control transfers.
ep0State=EP0_STATE_INIT;
//isUsbdSetConfiguration=1;
break;
//////////////////////// For chapter 9 test ////////////////////
case CLEAR_FEATURE: //清除特性请求 //1
switch (descSetup.bmRequestType)
{
case DEVICE_RECIPIENT:
if (descSetup.bValueL == 1)
Rwuen = FALSE;
break;
case ENDPOINT_RECIPIENT:
if (descSetup.bValueL == 0)
{
if((descSetup.bIndexL & 0x7f) == 0x00){
StatusGet.Endpoint0= 0;
}
if((descSetup.bIndexL & 0x8f) == 0x81){ // IN Endpoint 1
StatusGet.Endpoint1= 0;
}
if((descSetup.bIndexL & 0x8f) == 0x03){ // OUT Endpoint 3
StatusGet.Endpoint3= 0;
}
}
break;
default:
break;
}
CLR_EP0_OUTPKTRDY_DATAEND();
ep0State=EP0_STATE_INIT;
break;
case GET_CONFIGURATION: //读取配置请求 //8
CLR_EP0_OUT_PKT_RDY();
ep0State=EP0_CONFIG_SET;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -