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

📄 usbsetup.c

📁 S3C2410学习的基础资料 大部分实验源码及工程
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************
 NAME: usbsetup.c
 DESC: process the USB setup stage operations.
 HISTORY:
 MAR.25.2002:purnnamu: S3C2400X usbsetup.c is ported for S3C2410X.
 AUG.20.2002:purnnamu: rEP0_CSR should be used instead of rOUT_CSR1_REG for EP0 macros.
 **************************************************************/
#include "def.h"
#include "config.h"
#include "board.h"
#include "2410addr.h"

#include "2410usb.h"
#include "usblib.h"
#include "usbsetup.h"
#include "usbout.h"
#include "usbin.h"
#include "usb.h"

#ifdef	USB_DOWNLOAD_SUPPORT


// *** End point information ***
//   EP0: control
//   EP1: bulk in end point
//   EP2: not used
//   EP3: bulk out end point
//   EP4: not used

// *** VERY IMPORTANT NOTE ***
// Every descriptor size of EP0 should be 8n+m(m=1~7).
// Otherwise, USB will not operate normally because the program
// doesn't prepare the case that the descriptor size is 8n+0.
// If the size of a descriptor is 8n, the 0 length packit should be sent. 
// Special thanks to E.S.Choi for reminding me of this USB specification.


// ===================================================================
// All following commands will operate only in case 
// - ep0_csr is valid.
// ===================================================================
#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() 		{register i;while(rOUT_FIFO_CNT1_REG)i=rEP0_FIFO;}

U32 ep0State;
U32 ep0SubState;

extern volatile int isUsbdSetConfiguration;

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;

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
    };

/*****************************************/
/*         Ep0中断处理开始               */    
/*        控制传输中断处理               */
/*****************************************/
void Ep0Handler(void)
{
    static int ep0SubState;
//    int i;
    U8 ep0_csr;

    rINDEX_REG=0;   //访问端点0控制状态寄存器EP0_CSR前,必须给索引寄存器INDEX_REG赋0
    ep0_csr=rEP0_CSR;  //端点0控制状态寄存器的值赋给变量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)   //当SETUP_END为1时,即一个控制传输结束
    {   
    	 // 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.
		CLR_EP0_SETUP_END();      //清除SETUP_END位
		if(ep0_csr & EP0_OUT_PKT_READY)  //一个有效的令牌包写入EP0 FIFO,USB对OUT_PKT_READY位置1
		{
		    FLUSH_EP0_FIFO();            //冲掉EP0_FIFO中的数据  
	    	//I think this isn't needed because EP0 flush is done automatically.   
	    	CLR_EP0_OUT_PKT_RDY();       //清除OUT_PKT_READY
		}
	
		ep0State=EP0_STATE_INIT;  //ep0State状态为0
		return;
    }	

    //I think that EP0_SENT_STALL will not be set to 1.
    if(ep0_csr & EP0_SENT_STALL)  //控制事务由于协议冲突而停止,USB对SENT_STALL位置1
    {   
//		DbgOut("[STALL]");
	   	CLR_EP0_SENT_STALL();     //清除SENT_STALL位
		if(ep0_csr & EP0_OUT_PKT_READY)   //如果输出包标志置位
		{
		    CLR_EP0_OUT_PKT_RDY(); //清除输出包有效标志
		}
	
		ep0State=EP0_STATE_INIT;
		return;
    }
    //一个有效的令牌包写入EP0 FIFO,并且ep0State=0
    if((ep0_csr & EP0_OUT_PKT_READY) && (ep0State==EP0_STATE_INIT))
    {	
		RdPktEp0((U8 *)&descSetup,EP0_PKT_SIZE);//把8字节SETUP数据包读入结构体descSetup
    
		switch(descSetup.bRequest)//根据请求命令代码分支跳转
    	{
    	case GET_DESCRIPTOR://请求描述符:代码=6
            switch(descSetup.bValueH) //wValue字段的高字节为描述符类型编号       
            {
            case DEVICE_TYPE://设备类:编号为1
// 	    		DbgOut("[GDD]");
	 	    	CLR_EP0_OUT_PKT_RDY();//清除OUT_PKT_READY
		    	ep0State=EP0_STATE_GD_DEV_0;//EP0的状态为请求描述符设备0阶段	        
	    		break;	
	    		
		    case CONFIGURATION_TYPE://配置类:编号为2
// 			   	DbgOut("[GDC]");
 	    		CLR_EP0_OUT_PKT_RDY();
	 	    	if((descSetup.bLengthL+(descSetup.bLengthH<<8))>0x9)//数据包大于0x9时,设置EP0的状态
 	    	  //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
// 		    	DbgOut("[GDS]");
 	    		CLR_EP0_OUT_PKT_RDY();
	    		switch(descSetup.bValueL)//wValue字段的低字节为描述符字符串索引
	    		{
	    	    case 0:
	    	    	ep0State=EP0_STATE_GD_STR_I0;//获得字符串0阶段
	    	    	break;
	    	    case 1:
       	    		ep0State=EP0_STATE_GD_STR_I1;//获得字符串1阶段
	    	    	break;
	    	    case 2:	
	    	    	ep0State=EP0_STATE_GD_STR_I2;//获得字符串2阶段
	    	    	break;
	    	    default:
//	    			DbgOut("[UE:STRI?]");
	    			break;
	    		}
		    	ep0SubState=0;
		    	break;
		    	
	    	case INTERFACE_TYPE://接口类:编号为4
// 	    		DbgOut("[GDI]");
	 	    	CLR_EP0_OUT_PKT_RDY();
		    	ep0State=EP0_STATE_GD_IF_ONLY_0; //for WIN98
	    		break;
	    		
		    case ENDPOINT_TYPE:	//端点类:编号为5    	
// 		    	DbgOut("[GDE]");
 	    		CLR_EP0_OUT_PKT_RDY();
 	    		switch(descSetup.bValueL&0xf)//wValue字段的低字节为描述符字符串索引
		    	{
		    	case 0:
	    		    ep0State=EP0_STATE_GD_EP0_ONLY_0;
	    		    break;
		    	case 1:
					ep0State=EP0_STATE_GD_EP1_ONLY_0;
	    		    break;
	    		default:
//	    	    	DbgOut("[UE:GDE?]");
		    	    break;
		    	}
	    		break;
		    default:
//		    	DbgOut("[UE:GD?]");
	    		break;
		    }	
   		    break;//wValue字段的低字节为描述符字符串索引
   		    
    	case SET_ADDRESS://设置地址:代码=5
//			DbgOut("[SA:%d]",descSetup.bValueL);
            rFUNC_ADDR_REG=descSetup.bValueL | 0x80;//地址值放在wValue字段的低字节,更新ADDR_UPDATA
		    CLR_EP0_OUTPKTRDY_DATAEND(); //没有数据控制传输,DATA_END位置1,数据传输结束.
            ep0State=EP0_STATE_INIT;
            break;    	
            
		case SET_CONFIGURATION://设置配置:代码=9
//			DbgOut("[SC]");
            CLR_EP0_OUTPKTRDY_DATAEND(); //没有数据控制传输,DATA_END位置1.
            ep0State=EP0_STATE_INIT;
            isUsbdSetConfiguration=1; 
    	    break;
    	    
  		default:
//			DbgOut("[UE:SETUP=%x]",descSetup.bRequest);
    	    CLR_EP0_OUTPKTRDY_DATAEND(); //没有数据控制传输,DATA_END位置1
		    ep0State=EP0_STATE_INIT;
	    	break;    	
        }
    }//一个有效的令牌包写入EP0 FIFO,并且ep0State=0处理结束
    
    switch(ep0State)//根据EP0的阶段状态标志ep0State分支跳转
    {	
	case EP0_STATE_INIT:
	    break; 

	//=== GET_DESCRIPTOR:DEVICE ===

⌨️ 快捷键说明

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