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

📄 usb.c

📁 USBN9604接口芯片.HID类完全通讯版
💻 C
📖 第 1 页 / 共 2 页
字号:
      {
      if(desc_typ==34)
       iii++;
       GETDESC=0;
       FLUSHTX0();
       FLUSHRX0();
      }
    write_9604(RXC0,RX_EN);
   }
}
/*tx0*/
void tx_0(void)
{
 //uchar txstat=0X00;
 int lim;

 lim=0;
 bitclr(TXC0,TX_EN);
 txstat=read_9604(TXS0);
 if(txstat&TX_DONE)                 /*transfer complete*/
   {
    FLUSHTX0();
    FLUSHRX0();
    if(GETDESC)                  /*in get_descriptor status*/
      {
       if(desc_typ==34)
       iii++;
       if(desc_typ==DEVICE)
       jjj++;
       lim=desc_idx+8;           /*go on transfer another 8byte descriptor*/
       for(;((desc_idx<lim)&&((desc_idx-originalindex)<desc_sze)); desc_idx++)
          get_desc();
       TXEN0_PID();              /*Exchange PID*/
       if((desc_idx-originalindex)==desc_sze)
         GETDESC=0;              /*descriprtor transfer complete ,out get_descriptor status*/
      }
    if(txstat&ACK_STAT)          /*transfer complete and receive ACK*/
      {
       if(address)
          {write_9604(FAR,address|AD_EN);address=0; }
       write_9604(RXC0,RX_EN);
      }
   }
}
/*rx3*/
void rx_3(void)
{
 //uchar bagman;
 uchar rxstat3;
 uchar count;

 rxstat3=0;
 count=0;
 bitclr(RXC3,RX_EN);
 rxstat3=read_9604(RXS3);
 FLUSHTX3();
 for(count=1;count<=64;count++)
    {
     bagman = read_9604(RXD3);
     write_9604(TXD3,bagman+2);
    }
 write_9604(TXC3,TX_EN+TX_LAST);
 FLUSHRX3();
 write_9604(RXC3,RX_EN);                                //enable RX3
}
/*tx3*/
void tx_3(void)
{
 uchar txstat3;

 txstat3=0X00;
 write_9604(TXC3,0X00);
 txstat3=read_9604(TXS3);
 if((txstat3&ACK_STAT)&&(txstat3&TX_DONE))
    write_9604(TXC3,TX_TOGGLE+TX_EN+TX_LAST);
}
/*Alter thing*/
void usb_alt(void)
{
 //uchar data altevent;

 //altevent=100;

 event = read_9604(ALTEV);
 //altevent = event;
 if(event&ALT_RESET)
      {
        write_9604(NFSR,NFS_RESET);             /*reset*/
        delayus(10);
        write_9604(FAR,AD_EN);                  /*address 0*/
        write_9604(EPC0,0X00);                  /**/
        FLUSHRX0();
        FLUSHTX0();
        write_9604(RXC0,RX_EN);                 /*enable endpoint 0 receive*/
        write_9604(NFSR,NFS_OPERATIONAL);       /*operation status*/
      }
 else if(event&ALT_SD3)
      {
        write_9604(ALTMSK,ALT_RESUME+ALT_RESET);/*RESUME+SESET*/
        write_9604(NFSR,NFS_SUSPEND);           /*suspend*/
      }
 else if(event&ALT_RESUME)
      {
        write_9604(ALTMSK,ALT_SD3+ALT_RESET);   /*RESUME+SESET*/
        FLUSHTX0();
        FLUSHRX0();
        write_9604(RXC0,RX_EN);                 /*enable endpoint 0 receive*/
        write_9604(NFSR,NFS_OPERATIONAL);       /*operation status*/
        delayms(1);
      }
}
/*NAK*/
void outnak_0(void)
{
 if(GETDESC)
   {
    GETDESC=0;
    FLUSHTX0();
    FLUSHRX0();
    write_9604(RXC0,RX_EN);
   }
}
//USB requre/////////////////////////////////////////////////////////////////
/*GET_STATUS*/
void getstatus(void)
{
 switch(req_buf[0]&0x03)         /*Device 0,Interface 1,Endpoint2*/
       {
        case 0:
                write_9604(TXD0,0X02);//self power
                break;
        case 1:
                write_9604(TXD0,0X00);
                break;
        case 2:
                switch(req_buf[4]&0x0F)   //no endpoint can be halt status
                      {
                       case 0:
                               write_9604(TXD0,0X00);
                               break;
                       case 1:
                               write_9604(TXD0,0X00);
                               break;
                       case 2:
                               write_9604(TXD0,0X00);
                               break;
                       case 3:
                               write_9604(TXD0,0X00);
                               break;
                       case 4:
                               write_9604(TXD0,0X00);
                               break;
                       case 5:
                               write_9604(TXD0,0X00);
                               break;
                       case 6:
                               write_9604(TXD0,0X00);
                               break;
                       default:
                               break;
                      }
                break;
        default:
                write_9604(TXD0,0X00);
                break;
       }
 write_9604(TXD0,0X00);
}
/*SETADDRESS*/
void setaddress(void)
{
 address=req_buf[2];
}
/*GET_DESCRIPTOR*/
void getdescriptor(void)
{
 uchar once8byte;

 once8byte=0;
 GETDESC=1;      /*enter getdescriptor status*/
 desc_idx=0;     /*index of descriptor except string descriptor*/
 originalindex=0;//index of descriptor at begin except string descriptor
 desc_typ = req_buf[3];   /*store the type requested*/
 switch(desc_typ)
       {
        case DEVICE:
                            desc_sze=18;
                            usbenumerating=1;  //enter enumerate status,enumerate begin
                            //TR0=1;             //allow T0
                            //ET1=0;             //disable T1
                            break;
        case CONFIGURATION:
                            desc_sze=41;
                            break;
        case REPORT:
                            desc_sze=28;
                            break;
        case STRING:
                            desc_idx=req_buf[2];
                            originalindex=desc_idx;
                            desc_sze=STRING_DESC[desc_idx];
                            break;
        default:
                            desc_sze=0;
                            break; /*other descriptor*/
       }
 if(desc_sze>req_buf[6])
   desc_sze = req_buf[6];/*for Configuration descriptor ,the first time only need 9 byte*/
 for(once8byte=0;((once8byte<8)&&((desc_idx-originalindex)<desc_sze));once8byte++,desc_idx++)
    get_desc();
 if((desc_idx-originalindex)==desc_sze)
    GETDESC=0; //only for string descriptor
}
/*getdescriptor subfunction*/
void get_desc(void)
{
 if(desc_typ==DEVICE)
    desc_dta=DEV_DESC[desc_idx];
 if(desc_typ==CONFIGURATION)
    desc_dta=CONF_DESC[desc_idx];
 if(desc_typ==REPORT)
    desc_dta=REPORT_DESC[desc_idx];
 if(desc_typ==STRING)
    desc_dta=STRING_DESC[desc_idx];
 write_9604(TXD0,desc_dta); 
}
/*SET_CONFIGURATION*/
void setconfiguration(void)
{
 usb_cfg=req_buf[2];                       /*save the configuration number to usb_cfg*/
 if((usb_cfg!=0)&&(usb_cfg==0XFF))         /*check configuration*/
   {
    TGL0PID=0;                           /*FIRST PID is DATA0*/
    bitclr(EPC0,STALL);                  /*nothing stalled*/
    bitclr(EPC5,STALL);
    bitclr(EPC6,STALL);
    FLUSHTX3();
    FLUSHTX3();
    write_9604(EPC5,EP_EN+5);              /*enable EP5 */
    FLUSHRX3();
    FLUSHRX3();
    write_9604(EPC6,EP_EN+6);              /*enable EP6*/
    write_9604(RXC3,RX_EN+IGN_SETUP);      /*enable RX3*/
    setconfigurationfinish=1;
    //FLUSHTX0();
    //write_9604(TXC0,TX_EN);
    
   }
 else                                      /*unconfigure the device  */
   {
    write_9604(EPC1,0X00);                 /*disable EP1*/
    write_9604(EPC2,0X00);                 /*disable EP2*/
    write_9604(EPC3,0X00);                 /*disable EP3*/
    write_9604(EPC4,0X00);                 /*disable EP4*/
    write_9604(EPC5,0X00);                 /*disable EP5*/
    write_9604(EPC6,0X00);                 /*disable EP6*/
   }
}


//other///////////////////////////////////////////////////////////////////////
/*FLUSH*/
void FLUSHTX0(void)
{
 write_9604(TXC0,FLUSH);
}
void FLUSHRX0(void)
{
 write_9604(RXC0,FLUSH);
}
void FLUSHTX3(void)
{
 write_9604(TXC3,FLUSH);
}
void FLUSHRX3(void)
{
 write_9604(RXC3,FLUSH);
}
/*delayus*/
void delayus(uint num)
{
 for(num=num*2;num>0;num--);      //for 24MHz crystal,0.5uS
}
/*delayms*/
void delayms(uint num)
{
 int num1;

 num1=2000;
 for(;num>0;num--)
    for(num1=2000;num1>0;num1--); //for 24MHz crystal,0.5uS
}
/*read9604*/
uchar read_9604(uint addr)
{
 uchar rdata;

 rdata=0; 
 
 *(USBN9604+1) = addr;
 rdata = *(USBN9604);
 return(rdata);
// rdata=XBYTE[USBN9604+addr];
// return(rdata);
}
/*write9604*/
void write_9604(uint addr,uchar wdata)
{
 *(USBN9604+1) = addr;
 *(USBN9604) = wdata;
 //XBYTE[USBN9604+addr]=wdata;
}
/*bitset*/
void bitset(uint addr,uchar bitval)
{
 write_9604(addr,read_9604(addr)|bitval);
}
/*bitclr*/
void bitclr(uint addr,uchar bitval)
{
 write_9604(addr,read_9604(addr)&(~bitval));
}
/*Exchange PID*/
void TXEN0_PID(void)
{
 if(TGL0PID)
   write_9604(TXC0,TX_TOGGLE+TX_EN);
 else
   write_9604(TXC0,TX_EN);
 TGL0PID=!TGL0PID;
}
void usb_icont(int no)
{
    unsigned char i;
//	INTC_IPRA |= 0x00f0;
	no = no<<4;
	i  = INTC_IPRA;
	i &= 0xff0f;
	INTC_IPRA =i|no;				
}

⌨️ 快捷键说明

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