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

📄 c_usb.c

📁 isp1160_pci_usb_host_MP100.rar USB主机设备程序~!
💻 C
字号:
#include "hw_acces.h"
#include "hc_comm.h"

#define SETUP 0
#define OUT   1
#define IN    2

#define active 1
#define LAST   1
#define nLAST  0

#define retry     2000
#define wait_time   200

unsigned int port1speed=0;
unsigned int c_ptd[4];

void set_port_speed(unsigned int speed)
{
 port1speed=speed;
}

unsigned int device_info(char mode, char itype, char index)
{
 static int iManufacturer=0;
 static int iProduct=0;
 static int iInterface=0;

 if(mode=='W')
 {
  if(itype=='M') {iManufacturer=index;}
  if(itype=='P') {iProduct     =index;}
  if(itype=='I') {iInterface   =index;}
 }

 if(mode=='R')
 {
  if(itype=='M') {return(iManufacturer);}
  if(itype=='P') {return(iProduct)     ;}
  if(itype=='I') {return(iInterface)   ;}
 }

 if(mode=='S')
 {
  printf("\niManufacturer = %4X",iManufacturer);
  printf("\niProduct      = %4X",iProduct     );
  printf("\niInterface    = %4X",iInterface   );
 }

 return 0;
}

struct ptd_struct
{
 unsigned int c_code;
 unsigned int active_bit;
 unsigned int toggle;
 unsigned int actual_size;
 unsigned int endpoint;
 unsigned int last_ptd;
 unsigned int speed;
 unsigned int max_size;
 unsigned int pid;
 unsigned int total_size;
 unsigned int format;
 unsigned int func_addr;
 unsigned char fm;
}
ptd2send;

void send_control(unsigned int *a_ptr,unsigned int *r_ptr,unsigned int d0,unsigned int d1,unsigned int d2,unsigned int d3)
{
 unsigned int cnt=10;
 unsigned int *reply;
 unsigned int abuf[8];
 unsigned int *nptr;
 unsigned int active_bit;
 unsigned int buf_status;
 unsigned int rbuf[8];
 unsigned int UpInt;

 abuf[0]=*(a_ptr+0);
 abuf[0]|=0xF000;

 abuf[1]=*(a_ptr+1);
 abuf[2]=*(a_ptr+2);
 abuf[3]=*(a_ptr+3);
 abuf[4]=d0;
 abuf[5]=d1;
 abuf[6]=d2;
 abuf[7]=d3;

 nptr=abuf;
 write_atl(nptr,8);  //write 16 bytes
 do
  {
   delay(1);
   read_atl(r_ptr, 2);
   active_bit=(*r_ptr)&(0x0800);
   active_bit=active_bit>>11;
   cnt--;
  }
 while((cnt>1)   &&   (active_bit!=0));

 if(port1speed==1){read_atl(r_ptr, 8);}  //read 16 bytes
 if(port1speed==0){read_atl(r_ptr,36);}  //read 72 bytes

 if(cnt<=1)
 {
  *r_ptr=0xFFFF;
 }
}

void make_control_ptd(unsigned int *rptr, char type_ptd,char last,char ep,unsigned int max,char tog,char addr)
{
 ptd2send.c_code=0x0F;
 ptd2send.active_bit=1;
 ptd2send.toggle=tog;
 ptd2send.actual_size=0;
 ptd2send.endpoint=ep;
 ptd2send.last_ptd=1;
 ptd2send.speed=port1speed;
 ptd2send.max_size=max;
 ptd2send.total_size=max;
 ptd2send.pid= type_ptd;
 ptd2send.format=0;
 ptd2send.fm=0;
 ptd2send.func_addr=addr;

 *(rptr+0)=	(ptd2send.c_code     	&0x0000)<<12
		   |(ptd2send.active_bit 	&0x0001)<<11
		   |(ptd2send.toggle		&0x0001)<<10
		   |(ptd2send.actual_size   &0x03FF);

 *(rptr+1)=  (ptd2send.endpoint		&0x000F)<<12
		   |(ptd2send.last_ptd		&0x0001)<<11
		   |(ptd2send.speed			&0x0001)<<10
		   |(ptd2send.max_size		&0x03FF);

 *(rptr+2)=  (0x0000             	&0x000F)<<12
		   |(ptd2send.pid     		&0x0003)<<10
		   |(ptd2send.total_size	&0x03FF);

 *(rptr+3)=  (ptd2send.fm            &0x00FF)<<8
		   |(ptd2send.format		&0x0001)<<7
		   |(ptd2send.func_addr     &0x007F);
}

void make_bulk_ptd(unsigned int *r_ptr,char ep,unsigned int max,char tog,char addr)
{
 unsigned int vcnt;
 unsigned int max_pac=64;

 ptd2send.c_code=0x0F;
 ptd2send.active_bit=1;
 ptd2send.toggle=tog;
 ptd2send.actual_size=0;
 ptd2send.endpoint=ep;
 ptd2send.last_ptd=1;
 ptd2send.speed=0;

 if(max<=max_pac){ptd2send.max_size=max;}
 else {ptd2send.max_size=max_pac;}

 ptd2send.total_size=max;
 ptd2send.pid= OUT;
 ptd2send.format=0;
 ptd2send.fm=0;
 ptd2send.func_addr=addr;

 *(r_ptr+0)=(ptd2send.c_code     	&0x000F)<<12
		   |(ptd2send.active_bit 	&0x0001)<<11
		   |(ptd2send.toggle		&0x0001)<<10
		   |(ptd2send.actual_size   &0x03FF);

 *(r_ptr+1)=(ptd2send.endpoint		&0x000F)<<12
		   |(ptd2send.last_ptd		&0x0001)<<11
		   |(ptd2send.speed			&0x0001)<<10
		   |(ptd2send.max_size		&0x03FF);

 *(r_ptr+2)=(0x0001             	&0x0001)<<15
		   |(ptd2send.pid     		&0x0003)<<10
		   |(ptd2send.total_size	&0x03FF);

 *(r_ptr+3)=(ptd2send.fm            &0x00FF)<<8
		   |(ptd2send.format		&0x0001)<<7
		   |(ptd2send.func_addr     &0x007F);
}

void get_bulk_ptd(unsigned int *a_ptr,unsigned int word_size)
{
   read_atl(a_ptr,word_size);
}

void put_bulk_ptd(unsigned int *hd_ptr,unsigned int word_size,unsigned int *data_ptr)
{
 unsigned int cnt;

 write_register16(Com16_HcTransferCounter,(word_size*2)+8);
 outport(g_1161_command_address,Com16_HcATLBufferPort|0x80);
 cnt=0;
 do
  {
   outport(g_1161_data_address,*(hd_ptr+cnt));
   cnt++;
  }
 while(cnt<4);

 cnt=0;
 do
  {
   outport(g_1161_data_address,*(data_ptr+cnt));
   cnt++;
  }
 while(cnt<word_size);
}

unsigned int get_mouse(int addr,unsigned int *m_ptr)
{
 unsigned int ccode;

 unsigned int *cbuf_ptr;
 unsigned int cbuf[8];
 unsigned int *rb_ptr;
 unsigned int rb[80];
 unsigned int mouse_tog=0;

 cbuf_ptr=cbuf;
 rb_ptr=rb;

 make_control_ptd(cbuf_ptr,IN,1,1,4,mouse_tog,addr);
 send_control(cbuf_ptr,rb_ptr,0x0680,0x0302,0x0409,0x0040);

 *m_ptr     =*(rb_ptr+4);
 *(m_ptr+1) =*(rb_ptr+5);

 ccode=(rb[0]&0xF000)>>12;

 if(ccode==3)
 {
//  mouse_tog++;
  make_control_ptd(cbuf_ptr,IN,0,1,4,mouse_tog,addr);
  send_control(cbuf_ptr,rb_ptr,0x0680,0x0302,0x0409,0x0040);

  *m_ptr     =*(rb_ptr+4);
  *(m_ptr+1) =*(rb_ptr+5);
  mouse_tog++;
 }

// mouse_tog++;
 ccode=(rb[0]&0xF000)>>12;

 return(ccode);
}

void set_config(int addr,int config)
{
 unsigned int *cbuf_ptr;
 unsigned int cbuf[8];
 unsigned int *rb_ptr;
 unsigned int rb[80];

 cbuf_ptr=cbuf;
 rb_ptr=rb;

 //send out first setup packet
 make_control_ptd(cbuf_ptr,SETUP,0,0,8,0,addr);
 send_control(cbuf_ptr,rb_ptr,0x0900,config,0x0000,0x0000);

 //send out DATA IN packet
 make_control_ptd(cbuf_ptr,IN,0,0,0,1,addr);
 send_control(cbuf_ptr,rb_ptr,0x0000,0x0000,0x0000,0x0000);
}

void set_interface(int addr,int interface)
{
 unsigned int *cbuf_ptr;
 unsigned int cbuf[8];
 unsigned int *rb_ptr;
 unsigned int rb[80];

 cbuf_ptr=cbuf;
 rb_ptr=rb;

 //send out first setup packet
 make_control_ptd(cbuf_ptr,SETUP,0,0,8,0,addr);
 send_control(cbuf_ptr,rb_ptr,0x0B00,interface,0x0000,0x0000);

 //send out DATA IN packet
 make_control_ptd(cbuf_ptr,IN,0,0,0,1,addr);
 send_control(cbuf_ptr,rb_ptr,0x0000,0x0000,0x0000,0x0000);
}

void set_address(int old_addr,int new_addr)
{
 unsigned int *cbuf_ptr;
 unsigned int cbuf[8];
 unsigned int *rb_ptr;
 unsigned int rb[80];


 cbuf_ptr=cbuf;
 rb_ptr=rb;

 //send out first setup packet
 make_control_ptd(cbuf_ptr,SETUP,0,0,8,0,old_addr);
 send_control(cbuf_ptr,rb_ptr,0x0500,new_addr,0x0000,0x0000);

 //send out DATA IN packet
 make_control_ptd(cbuf_ptr,IN,0,0,0,1,old_addr);
 send_control(cbuf_ptr,rb_ptr,0x0000,0x0000,0x0000,0x0000);
}

unsigned int get_control(unsigned int *rptr,unsigned int addr,char control_type,unsigned int extra)
{
 unsigned int *cbuf_ptr;
 unsigned int cbuf[8];
 unsigned int *rb_ptr;
 unsigned int rb[128];
 unsigned int cnt=0,lcnt=0;
 unsigned int toggle_cnt=0;
 unsigned int word_size;
 unsigned int ccode=0;
 unsigned int DesSize,MaxSize;
 signed int rbytes;
 unsigned int active_bit;

 unsigned int ptd_retry=9;
 unsigned int probe_retry=9;
 unsigned int probe_ccode;

 cbuf_ptr=cbuf;
 rb_ptr=rb;

do
{
 //send out first setup packet to check descriptor size
 make_control_ptd(cbuf_ptr,SETUP,0,0,8,0,addr);
 if(control_type=='D') {send_control(cbuf_ptr,rb_ptr,0x0680,0x0100      ,0x0000,0x8);}
 if(control_type=='C') {send_control(cbuf_ptr,rb_ptr,0x0680,0x0200      ,0x0000,0x8);}
 if(control_type=='S') {send_control(cbuf_ptr,rb_ptr,0x0680,0x0300|extra,0x0409,0x8);}
 if(control_type=='I') {send_control(cbuf_ptr,rb_ptr,0x0680,0x0400      ,0x0000,0x8);}
 if(control_type=='E') {send_control(cbuf_ptr,rb_ptr,0x0680,0x0500      ,0x0000,0x8);}

 active_bit=(rb[0]&0x0800)>>11;
 ccode=(rb[0]&0xF000)>>12;
 printf("\nCommand stage ccode = %d active=%X",ccode,active_bit);

 toggle_cnt++;
 make_control_ptd(cbuf_ptr,IN,1,0,8,toggle_cnt%2,addr);
 send_control(cbuf_ptr,rb_ptr,0x0000,0x0000,0x0000,0x0000);

 probe_ccode=(rb[0]&0xF000)>>12;

 if(control_type!='C')
  {
   DesSize=((rb[4]&0x00FF));
  }

 if(control_type=='C')
  {
   DesSize=rb[5];
  }

 //send out DATA OUT packet
 make_control_ptd(cbuf_ptr,OUT,1,0,0,toggle_cnt%2,addr);
 send_control(cbuf_ptr,rb_ptr,0x0000,0x0000,0x0000,0x0000);

 probe_retry--;
}
while( (ccode!=0) && (probe_retry>0));

//END of Probe Phase

if(probe_ccode==0)
{
 make_control_ptd(cbuf_ptr,SETUP,0,0,8,0,addr);
 if(control_type=='D') {send_control(cbuf_ptr,rb_ptr,0x0680,0x0100      ,0x0000,DesSize);}
 if(control_type=='C') {send_control(cbuf_ptr,rb_ptr,0x0680,0x0200      ,0x0000,DesSize);}
 if(control_type=='S') {send_control(cbuf_ptr,rb_ptr,0x0680,0x0300|extra,0x0000,DesSize);}
 if(control_type=='I') {send_control(cbuf_ptr,rb_ptr,0x0680,0x0400      ,0x0000,DesSize);}
 if(control_type=='E') {send_control(cbuf_ptr,rb_ptr,0x0680,0x0500      ,0x0000,DesSize);}

 word_size=(DesSize+1)>>1;  //number of words to read
 rbytes=DesSize; //rbytes:remaining bytes to read

 toggle_cnt=0;
 cnt=0;
  do
  {
   //send out DATA IN packet
   if( (ccode==0) || (ccode>7) ) {toggle_cnt++;}

   if(rbytes>=8)
   {make_control_ptd(cbuf_ptr,IN,1,0,8,toggle_cnt%2,addr);   }
   else
   {make_control_ptd(cbuf_ptr,IN,1,0,rbytes,toggle_cnt%2,addr);   }

   send_control(cbuf_ptr,rb_ptr,0x0000,0x0000,0x0000,0x0000);

   ccode=(rb[0]&0xF000)>>12;

   //If transaction is sucessful, update counters and proceed
   if( (ccode==0) || (ccode>7) )
   {
	lcnt=0;
	do
	{
	 //Copy the 8 bytes of data located right after the 8 bytes PTD
	 *(rptr+cnt)=rb[4+lcnt];
	 cnt++;
	 lcnt++;
	 rbytes--;
	 rbytes--;
	}
	while((lcnt<4) && (rbytes>0) );
   }

   //If transaction is not sucessful, retry
   else
   {
	ptd_retry--;
   }

  }
  while((cnt<word_size)&& (rbytes>0) && (ptd_retry>1) );

 ccode=(rb[0]&0xF000)>>12;

 if( (ccode<1) || (ccode>7) )
 {
  //send out DATA OUT packet
  make_control_ptd(cbuf_ptr,OUT,0,0,0,toggle_cnt%2,addr);
  send_control(cbuf_ptr,rb_ptr,0x0000,0x0000,0x0000,0x0000);
 }
return(ccode);
}

else
return(probe_ccode);
}

void get_hid(unsigned int *r_ptr,unsigned int addr)
{
 unsigned int *cbuf_ptr;
 unsigned int cbuf[8];
 unsigned int *rb_ptr;
 unsigned int rb[80];
 unsigned int cnt=0,lcnt=0;
 unsigned int toggle_cnt=0;
 unsigned int word_size;

 cbuf_ptr=cbuf;
 rb_ptr=rb;

 //send out first setup packet
 make_control_ptd(cbuf_ptr,SETUP,0,0,8,0,addr);
 send_control(cbuf_ptr,rb_ptr,0x0A21,0x0000,0x0000,0x0000);

 make_control_ptd(cbuf_ptr,IN,0,0,0,toggle_cnt%2,addr);
 send_control(cbuf_ptr,rb_ptr,0x0000,0x0000,0x0000,0x0000);

 //send out first setup packet
 make_control_ptd(cbuf_ptr,SETUP,0,0,8,0,addr);
 send_control(cbuf_ptr,rb_ptr,0x0681,0x2200,0x0000,0x0084);

 do
 {
  //send out DATA IN packet
  toggle_cnt++;
  make_control_ptd(cbuf_ptr,IN,0,0,8,toggle_cnt%2,addr);
  send_control(cbuf_ptr,rb_ptr,0x0000,0x0000,0x0000,0x0000);

  lcnt=0;
   do
	{
	 if(cnt==0){word_size=(rb[4]&0x00FF)>>1;}
	 *(r_ptr+cnt)=rb[4+lcnt];
	 cnt++;
	 lcnt++;
	}
   while(lcnt<4);
 }
 while((cnt<32)&&(cnt<word_size));

 //send out DATA OUT packet
 make_control_ptd(cbuf_ptr,OUT,0,0,0,toggle_cnt%2,addr);
 send_control(cbuf_ptr,rb_ptr,0x0000,0x0000,0x0000,0x0000);
}

⌨️ 快捷键说明

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