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

📄 mkit100.c

📁 isp1160_pci_usb_host_MP100.rar USB主机设备程序~!
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <bios.h>
#include <stdlib.h>

#include "philips.h"
#include "hc_comm.h"
#include "hw_acces.h"

unsigned char	g_host_IRQ_num;

void reset_usb(void)
{
 write_register16(Com16_HcHardwareConfiguration		, 0x002D    );
 write_register32(Com32_HcFmInterval 		  		, 0x25002EDF);
 write_register32(Com32_HcInterruptEnable 		  	, 0x0000000F);
 write_register32(Com32_HcControl					, 0x00000600);
}

void set_operational(void)
{
 write_register16(Com16_HcHardwareConfiguration		, 0x002D    );
 write_register32(Com32_HcFmInterval 		  		, 0x25002EDF);
 write_register32(Com32_HcInterruptEnable 		  	, 0x0000000F);
 write_register32(Com32_HcControl					, 0x00000680);
}

void enable_port(void)
{
 unsigned long dat32;

 write_register32(Com32_HcRhPortStatus1,0x00000102);
 write_register32(Com32_HcRhPortStatus2,0x00000102);
 write_register32(Com32_HcRhDescriptorA,0x05000B01);
 write_register32(Com32_HcRhDescriptorB,0x00000000);

 write_register32(Com32_HcRhPortStatus1,0x00000102);
 write_register32(Com32_HcRhPortStatus2,0x00000102);

 set_port_speed(0);

 dat32=read_register32(Com32_HcRhPortStatus1);
 if(((dat32)&(0x00000200))!=0)
 {
  if(  (((dat32)&(0x00000001)))!=0 ) {set_port_speed(1);}
 }

 dat32=read_register32(Com32_HcRhPortStatus2);
 if(((dat32)&(0x00000200))!=0)
 {
  if(  (((dat32)&(0x00000001)))!=0 ) {set_port_speed(1);}
 }
}

void port_monitor(void)
{
 unsigned long control,irq_status,status1,status2;
 unsigned int start_y=10;
 unsigned int user_in;

 clrscr();

 set_operational();

 write_register32(Com32_HcRhPortStatus1,0x00000102);
 write_register32(Com32_HcRhPortStatus2,0x00000102);
 write_register32(Com32_HcRhDescriptorA,0x05000B01);
 write_register32(Com32_HcRhDescriptorB,0x00000000);

 do
 {
  control   =read_register32(Com32_HcControl);
  irq_status=read_register32(Com32_HcInterruptStatus);
  status1   =read_register32(Com32_HcRhPortStatus1);
  status2   =read_register32(Com32_HcRhPortStatus2);

  if((status1&0x00000001)==1)
  {
   gotoxy(20,start_y+6);
   write_register32(Com32_HcRhPortStatus1,0x00000102);
   if((status1&0x00000200)!=0)
   {
	printf("LS Device connected to Port 1");
	set_port_speed(1);
   }
   else
   {
	printf("FS Device connected to Port 1");
	set_port_speed(0);
   }
  }

  else
  {
   gotoxy(20,start_y+6);
   printf("                                      ");
  }

  if((status2&0x00000001)==1)
  {
   gotoxy(20,start_y+7);
   write_register32(Com32_HcRhPortStatus2,0x00000102);
   if((status2&0x00000200)!=0)
   {
	printf("LS Device connected to Port 2");
	set_port_speed(1);
   }
   else
   {
	printf("FS Device connected to Port 2");
	set_port_speed(0);
   }
  }

  else
  {
   gotoxy(20,start_y+7);
   printf("                                      ");
  }

  gotoxy(20,start_y);
  printf("Port Status Monitor");
  gotoxy(20,start_y+2);
  printf("Control    Register = %8lX.",control);
  gotoxy(20,start_y+3);
  printf("Int Status Register = %8lX.",irq_status);
  gotoxy(20,start_y+4);
  printf("P1  Status Register = %8lX.",status1);
  gotoxy(20,start_y+5);
  printf("P2  Status Register = %8lX.",status2);
  gotoxy(20,start_y+8);
  printf("Press '1' to go back to main menu");

  user_in=read_key(0);
 }
 while(user_in!='1');
}

void soft_reset(void)
{
 write_register16(0xA9,0);
}

void decode_ccode(unsigned char ccode,char *message)
{
 switch (ccode)
 {
  case 0: strcpy(message,"Transaction Completed Sucessfully!");  break;
  case 1: strcpy(message,"CRC Checksum Error Detected!");  break;
  case 2: strcpy(message,"Bit Stuffing Violation!");  break;
  case 3: strcpy(message,"Data Toggle Mismatch!");  break;
  case 4: strcpy(message,"Target Endpoint Stalled!");  break;
  case 5: strcpy(message,"No Response From Device!");  break;
  case 6: strcpy(message,"PID Check Failure!");  break;
  case 7: strcpy(message,"Unexpected PID!");  break;
  case 8: strcpy(message,"Data Overrun!");  break;
  case 9: strcpy(message,"Data Underrun!");  break;

  default: strcpy(message,"Non 1-9 Error Occurred!");
 }
}

void decode_config(unsigned int *reply,char mode)
{
 unsigned int iInterface;

 if(mode==1)
 {
 printf("\n======Configuration Descriptor===");
 printf("\nDescriptor Type    : 0x%8X ",(reply[0]&0xFF00)>>8);
 printf("    Size  :%3d Bytes ",reply[0]&0x00FF);
 printf("\nTotal Length       :   %8d (%4X)",(reply[1]),(reply[1]) );
 printf("\nNo of Interface    :   %8d ",reply[2]&0x00FF);
 printf("\nConfig Value       : 0x%8X ", (reply[2]&0xFF00)>>8);
 printf("\niConfig            : 0x%8X ", reply[3]&0x00FF);
 printf("\nbmAttribute        : 0x%8X ",(reply[3]&0xFF00)>>8);

   {
	if( (((reply[3]&0xFF00)>>8)&0x0040)==0 )
	{
	 printf(" => Device is Bus-Powered");
	}
	else
	{
	 printf(" => Device is Self-Powered");
	}
   }

 printf("\nMaxPower(mA)       :   %8d ",(reply[4]&0x00FF)<<1 );

 printf("\n======Interface Descriptor=======");
 printf("\nDescriptor Type    : 0x%8X ",(reply[5]&0x00FF)    );
 printf("    Size  : %2d Bytes ",(reply[4]&0xFF00)>>8 );
 printf("\nNo of Interface    : 0x%8X ",(reply[5]&0xFF00)>>8 );
 printf("\nAlternate Setting  : 0x%8X ",(reply[6]&0x00FF)    );
 printf("\nNo of Endpoints    : 0x%8X ",(reply[6]&0xFF00)>>8 );
 printf("\nInterface Class    : 0x%8X ",(reply[7]&0x00FF)    );
 printf("\nInterface SubClass : 0x%8X ",(reply[7]&0xFF00)>>8 );
 printf("\nInterface Protocol : 0x%8X ",(reply[8]&0x00FF)    );
 printf("\nInterface Index    : 0x%8X ",(reply[8]&0xFF00)>>8 );
 }
}

void decode_dev(unsigned int *reply,char mode)
{
 if(mode==1)
 {
  printf("\nDescriptor Type : 0x%8X "       ,(reply[0]&0xFF00)>>8);
  printf("\nDescriptor Size :   %8d "       ,reply[0]&0x00FF);
  printf("\nUSB Device Type :      %2X.%2X ",(reply[1]&0xFF00)>>8,reply[1]&0x00FF);
  printf("\nClass           : 0x%8X "       ,reply[2]&0x00FF);
  printf("\nSubClass        : 0x%8X "       ,(reply[2]&0xFF00)>>8);
  printf("\nbDeviceProtocol : 0x%8X "       ,reply[3]&0x00FF);
  printf("\nbMaxPacSize 0   : 0x%8X "       ,(reply[3]&0xFF00)>>8);
  printf("\nVendorID        : 0x%8X "       ,reply[4]);
  printf("\nProductID       : 0x%8X "       ,reply[5]);
  printf("\nBCD Device      : 0x%8X "       ,reply[6]);
  printf("\niManufacturer   : 0x%8X "       ,(reply[7]&0xFF00)>>8);
  printf("\niProduct        : 0x%8X "       ,(reply[7]&0x00FF)   );
  printf("\nSerialNumber    : 0x%8X "       ,(reply[8]&0xFF00)>>8);
  printf("\nNo of Config    : 0x%8X "       ,(reply[8]&0x00FF));
 }
}

void decode_ep(unsigned int *reply,char mode)
{
 int temp;

 if(mode==1)
 {
  printf("\nDescriptor Type : 0x%8X ",(reply[0]&0xFF00)>>8);
  printf("\nDescriptor Size : 0x%8X ",(reply[0]&0x00FF)>>0);
  printf("\nEndpoint Address: 0x%8X ",(reply[1]&0x0F00)>>8);
  //Out or IN endpoint
  if( (reply[1]&0x0080)==0)
	   { printf("OUT"); }
  else { printf("IN"); }

  //Transfer Type
  temp=(reply[1]&0x0300)>>8;
  if(temp==0) {printf("\nThis is a Control Endpoint");       }
  if(temp==1) {printf("\nThis is an Isochronous Endpoint");   }
  if(temp==2) {printf("\nThis is a Bulk Endpoint");          }
  if(temp==3) {printf("\nThis is an Interrupt Endpoint");     }

  printf("\nMax Packet Size : 0x%8X ",(reply[2]&0x00FF)>>0);
  printf("\nPolling Interval:   %8dmS ",(reply[3]&0xFF00)>>8);
 }

 printf("\n\n\n\n\n\n\n\n");
}

void get_usb(void)
{
 unsigned int user_in;

 unsigned long *lp;
 unsigned long *data_ptr;
 unsigned int *reply_ptr;
 unsigned int reply[128];
 unsigned int cnt;
 unsigned int data_in[8];
 unsigned char usb_device_name[128];
 unsigned char *udn;
 unsigned int ccode;
 unsigned int temp;
 char message[50];

 unsigned int name_length;

 unsigned int iManufacturer=0,iProduct=0,iInterface=0;

 unsigned int dev_desc[128];
 unsigned int ep_desc[128];
 unsigned int config_desc[512];
 unsigned int str_desc[128];
 unsigned int i_desc[128];

 char m_udn[50]="NULL";
 char p_udn[50]="NULL";
 char i_udn[50]="NULL";

 int addr=3;

 reply_ptr=reply;

 clrscr();

 reset_usb();
 erase_all();
 set_operational();
 enable_port();
 delay(300);

 set_operational();
 enable_port();

 write_register32(Com32_HcRhPortStatus1,0x00000010);
 write_register32(Com32_HcRhPortStatus2,0x00000010);
 pwait(5000);

 set_address(0,addr);

 set_config(3,1);
 clrscr();

 //Get Device
 ccode=get_control(dev_desc,addr,'D');

 decode_ccode(ccode,message);
 printf("\n\nGetDescriptor... %s",message);
 if( (ccode==0) || (ccode>7) )
 {
  decode_dev(dev_desc,0);

  iProduct       =(dev_desc[7]&0xFF00)>>8;
  iManufacturer  =(dev_desc[7]&0x00FF)   ;
  device_info('W','M',iManufacturer);
  device_info('W','P',iProduct);
 }

 //Get Configuration
 ccode=get_control(config_desc,addr,'C');
 decode_ccode(ccode,message);
 printf("\n\nGetConfiguration... %s",message);
 if( (ccode==0) || (ccode>7) )
 {
  decode_config(config_desc,0);

  iInterface =(config_desc[8]&0xFF00)>>8;
  device_info('W','I',iInterface);
 }

 device_info('S',0,0);

 //Get String
 if(iProduct!=0)
 {
  ccode=get_control(reply,addr,'S',iProduct);
  decode_ccode(ccode,message);
  printf("\nGet Product... %s",message);

  name_length=( (*reply_ptr&0x00FF)>>1  );

  printf("\nName length is %d",name_length);

  if(name_length>0)
  {
   name_length--;

	if(  (   (ccode==0)||(ccode>7)   )    && (name_length>0) )
	{
	 cnt=0;

	 do
	 {
	  sprintf(p_udn+cnt,"%c",*(reply_ptr+cnt+1));
	  cnt++;
	 }
	 while(cnt<name_length);

	 printf("\nProduct : %s",p_udn);
	}
  }
 }

 if(iManufacturer!=0)
 {
  ccode=get_control(reply,3,'S',iManufacturer);
  ccode=get_control(reply,3,'S',iManufacturer);
  decode_ccode(ccode,message);
  printf("\nGet Manufacturer...%d  %s",iManufacturer,message);

  name_length=( (reply[0]&0x00FF)>>1 );

  if(name_length!=0)
  {
   printf("\nName length is %d",name_length);
   name_length--;

	if(  (   (ccode==0)||(ccode>7)   )    && (name_length>0) )
	{
	 cnt=0;

	 do
	 {

⌨️ 快捷键说明

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