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

📄 udevctl.v

📁 实现USB接口功能的VHDL和verilog完整源代码
💻 V
📖 第 1 页 / 共 4 页
字号:
		((ctlst == CTLWAIT4ACK) && rcvdataout) ||
		((ctlst == CTLWAIT4ACK) && rcvsetup) ||
		((ctlst == CTLWAIT4DATA) && turnaround) ||
		((ctlst == CTLWAIT4SETUP) && turnaround))  
         datapacketnotok <= 1'b1;
      else
         datapacketnotok <= 1'b0;

      if (usbreset)
         datapacketok <= 1'b0;
      //else if ((ctlst == CTLGRABDATA) && rcvpacketok)
      else if ( ( (ctlst == CTLGRABDATA) && rcvpacketok) || ((ctlst == CTLWAIT4ACK) && rcvack))
         datapacketok <= 1'b1;
      else
         datapacketok <= 1'b0;
    end
  // for endpoint0 to transfer read && write 
 // wire sendzerolengthpktd = (corecommand &&((ctlst == CTLDIN && rcvpacketok && addressmatch && (setupdirection == HOSTTODEV)) ||
  //                               (ctlst == CTLDOUT && nextctlst == CTLWAIT4DATA && (setupdirection == DEVTOHOST))) &&
   //                             setupcycle && endpoint0active) ||
                                   // hang on to it until we sent the CRC
    //                               (sendzerolengthpkt && ctlst != CTLIDLE);
  wire sendzerolengthpktd = ( ((ctlst == CTLDIN && rcvpacketok && addressmatch && (setupdirection == HOSTTODEV)) ||
                                 (ctlst == CTLDOUT && nextctlst == CTLWAIT4DATA && (setupdirection == DEVTOHOST))) &&
                                setupcycle && endpoint0active) ||
                                   (sendzerolengthpkt && ctlst != CTLIDLE);
 
  wire loaddesclengthlo = (setupcycle && endpoint0active && (ctlst == CTLSENDDATA)) &&
                          (((totalbytecount == 4'h2) && getcfgdesccycleflag) ||
                          ((totalbytecount == 4'h0) && getnoncfgdesccycleflag));
 
// if the descriptor isn't a config descriptor, we need to set the hi byte to 0
  
  wire clrdesclengthhi = (setupcycle && endpoint0active && (ctlst == CTLSENDDATA)) &&
                          (totalbytecount == 4'h0) && getnoncfgdesccycleflag;
 
  wire loaddesclengthhi = setupcycle && endpoint0active && (ctlst == CTLSENDDATA) &&
                         (totalbytecount == 4'h3) && getcfgdesccycleflag;

  wire setupwindexin = setupwindexlo[7];

 
 // capture the setup bytes as they come in
  always @(posedge usbclock)
    begin
      setupdirection <= (usbreset) ? 1'b0 : 
                           (grabsetup && (bytecount == 0)) ? rcvdatabyte[7] : setupdirection;
      setuptype <= (usbreset) ? 2'h0 : 
      			(grabsetup && (bytecount == 0)) ? rcvdatabyte[6:5] : setuptype;
      setuprecipient <= (grabsetup && (bytecount == 0)) ? rcvdatabyte[1:0] : setuprecipient;
      setuprequest <= (usbreset ) ? 8'h0 : 
      			(grabsetup && (bytecount == 1)) ? rcvdatabyte[7:0] : setuprequest;
      setupvalue0 <= (grabsetup && (bytecount == 2)) ? rcvdatabyte[7:0] : setupvalue0;
      setupvalue1 <= (grabsetup && (bytecount == 3)) ? rcvdatabyte[7:0] : setupvalue1;
      setupwindexlo <= (grabsetup && (bytecount == 4)) ? rcvdatabyte[7:0] : setupwindexlo;
      setupwindexhi <= (grabsetup && (bytecount == 5)) ? rcvdatabyte[7:0] : setupwindexhi;
      setuplengthlo <= (grabsetup && (bytecount == 6)) ? rcvdatabyte[7:0] : setuplengthlo;
      setuplengthhi <= (grabsetup && (bytecount == 7)) ? rcvdatabyte[7:0] : setuplengthhi;
      sendzerolengthpkt <= (usbreset) ? 1'b0 : sendzerolengthpktd;

	if (ctlst == CTLWAIT4SETUP)
		cfgdesclengthlo <= 8'hff;
	else if (loaddesclengthlo)
		cfgdesclengthlo <= romdevsetupdata;
	else
		cfgdesclengthlo <= cfgdesclengthlo;
 
        // need to handle the length hi differently since DEVICE descriptors only have single-byte length
	if (ctlst == CTLWAIT4SETUP)
		cfgdesclengthhi <= 8'hff;
	else if (clrdesclengthhi)
		cfgdesclengthhi <= 8'h00;
	else if (loaddesclengthhi)
		cfgdesclengthhi <= romdevsetupdata;
	else
		cfgdesclengthhi <= cfgdesclengthhi;

      getcfgdesccycleflag <= (setupvalue1[3:0] == TYPECONFIG) && (setuprequest == GETDESCRIPTOR);
      getnoncfgdesccycleflag <= (setupvalue1[3:0] == TYPEDEVICE) && (setuprequest == GETDESCRIPTOR);
    end 

  always @(posedge usbclock)
    begin
      if (usbreset)
        ouraddress <= 7'h00;   // be sure we don't match to random at reset/power-up
      else if (endpoint0active && (ctlst == CTLWAIT4ACK) && rcvack &&    // toggle once good data has been sent and seen
               (setuprequest == SETADDRESS) && corecommand)
        ouraddress <= setupvalue0[6:0];
      
      if (usbreset)
        currentalternatesetting <= 8'h00;   // be sure we don't match to random at reset/power-up
      else if ((ctlst == CTLGRABSETUP) && (nextctlst == CTLSENDACK) && 
               (setuprequest == SETINTERFACE) && corecommand)
        currentalternatesetting <= setupvalue0[7:0];
      
      if (usbreset)
        ourconfiguration <= 1'b0;              // must come out of reset unconfigured
      if ((ctlst == CTLGRABSETUP) && (nextctlst == CTLSENDACK) && 
         (setuprequest == SETCONFIGURATION) && corecommand)   //JAKE
	 ourconfiguration <= setupvalue0[0];
      
      if ((ctlst == CTLGRABSETUP) && (nextctlst == CTLSENDACK) && 
          (setuprequest == GETDESCRIPTOR) && corecommand)
        devromdescriptorindex <= {setupvalue1[2:0],setupvalue0[3:0]};
      
	  if (usbreset)
        romen <= 1'b0;
      else
        romen <= (ctlst == CTLSENDDATA) && endpoint0active;
    
// SET Feature and Clear Feature STALL
     if(usbreset)
	   begin
		   endp1wrstall <= 1'b0;
		   endp2rdstall <= 1'b0;
		   endp3rdstall <= 1'b0;
       end
     else if((ctlst == CTLGRABSETUP) && (nextctlst == CTLSENDACK) && (setuprequest == CLEARFEATURE)
			 && corecommand)
       begin
		   clearfeature <=  1'b1;
		   endp1wrstall <= !(setupwindexlo[1:0] == 2'b01 || setupwindexlo[1:0]== 2'b00
							 || setupwindexlo[1:0] == 2'b11 || setupwindexlo[1:0]== 2'b10 || setupwindexlo[3:2] != 2'b00 ); 
		   endp2rdstall <= !(setupwindexlo[1:0] == 2'b01 || setupwindexlo[1:0]== 2'b00
							 || setupwindexlo[1:0] == 2'b11 || setupwindexlo[1:0]== 2'b10 || setupwindexlo[3:2] != 2'b00); 
		   endp3rdstall <= !(setupwindexlo[1:0] == 2'b01 || setupwindexlo[1:0]== 2'b00
							 || setupwindexlo[1:0] == 2'b11 || setupwindexlo[1:0]== 2'b10 || setupwindexlo[3:2] != 2'b00); 
      end
     else if((ctlst == CTLGRABSETUP) && (nextctlst == CTLSENDACK) && (setuprequest == SETFEATURE)
			 && corecommand)
       begin
		   endp1wrstall <=  (setupwindexlo[3:0] == 4'b0001); 
		   endp2rdstall <=  (setupwindexlo[3:0] == 4'b0010);
		   endp3rdstall <=  (setupwindexlo[3:0] == 4'b0011);
      end
     else if((ctlst == CTLGRABSETUP) && (nextctlst == CTLSENDACK) && 
			 (setuprequest == SETINTERFACE || setuprequest == SETCONFIGURATION)
			 && corecommand)
      begin
		   endp1wrstall <=  1'b0; 
		   endp2rdstall <=  1'b0;
		   endp3rdstall <=  1'b0;
	  end
     else
	   begin
		  endp1wrstall <= endp1wrstall;
          endp2rdstall <= endp2rdstall;
		  endp3rdstall <= endp3rdstall;
          clearfeature <=1'b0;
        end
	end

reg vidflag;
// SeT Descriptor Flag setting
always @(posedge usbclock)
   begin
	   if(syncreset)
		  vidflag <=1'h0;
       else if ((ctlst == CTLGRABSETUP) && (nextctlst == CTLSENDACK) && 
					  (setuprequest == SETVID) && (setuptype ==TYPEVENDOR) )
          vidflag <=1'h1;
      end 

reg[7:0] vidreg0;
reg[7:0] vidreg1;
reg[7:0] pidreg0;
reg[7:0] pidreg1;

// Decoding Vendor CMD
  always@(posedge usbclock)
	begin
	   if(syncreset)
		  begin
			vidreg0 <='h00;
			vidreg1 <='h00;
			pidreg0 <='h00;
			pidreg1 <='h00;
          end
	   else if((ctlst == CTLGRABSETUP) && (nextctlst == CTLSENDACK) && 
               (setuprequest == SETVID) && (setuptype ==TYPEVENDOR) )
          begin
			vidreg0 <= setupvalue0;
			vidreg1 <= setupvalue1;
			pidreg0 <= setupwindexlo;
			pidreg1 <= setupwindexhi;
          end
	  
	  /*if(usbreset)
		  setcmd <=1'b0;
      else if( (ctlst== CTLGRABSETUP)&& (nextctlst == CTLSENDACK) &&
			   (setuptype ==TYPEVENDOR) && (setuprequest == SETCMD) )
          setcmd <=1'b1;
      else if( (setuprequest != SETCMD) || (setuptype !=TYPEVENDOR) ||(ctlst ==CTLWAIT4ACK))
		  setcmd <=1'b0;
      */
	if(usbreset)
		  getstatus <=1'b0;
      else if( (ctlst== CTLGRABSETUP)&& (nextctlst == CTLSENDACK) &&
			   (setuptype ==TYPEVENDOR) && (setuprequest == GETSTATUSV) )
          getstatus <=1'b1;
      else if((setuprequest != GETSTATUSV) || (setuptype !=TYPEVENDOR))
		  getstatus <=1'b0;
    
	if(usbreset)
		  getstatuscheck <=1'b0;
      else if( (ctlst== CTLGRABSETUP)&& (nextctlst == CTLSENDACK) &&
			   (setuptype ==TYPEVENDOR) && (setuprequest == GETSTATUSCHECK) )
          getstatuscheck <=1'b1;
      else if((setuprequest != GETSTATUSCHECK) || (setuptype !=TYPEVENDOR))
		  getstatuscheck <=1'b0;
	
	if(usbreset)
		  getdata <=1'b0;
      else if( (ctlst== CTLGRABSETUP)&& (nextctlst == CTLSENDACK) &&
			   (setuptype ==TYPEVENDOR) && (setuprequest == GETDATA) )
          getdata <=1'b1;
      else if((setuprequest != GETDATA) || (setuptype !=TYPEVENDOR))
		  getdata <=1'b0;
	
	if(usbreset)
		  setcmdrd <=1'b0;
      else if( (ctlst== CTLWAIT4ACK)&& 
			   (setuptype ==TYPEVENDOR) && (setuprequest == SETCMDRD) )
          setcmdrd <=1'b1;
      else if(  (setuprequest != SETCMDRD) || (setuptype !=TYPEVENDOR) 
				 || ( (ctlst == CTLSETUP) && (nextctlst == CTLWAIT4SETUP)) )
		  setcmdrd <=1'b0;

/* multiple wr and busy read */
/*	
	if(usbreset)
		  setcmdwr <=1'b0;
      else if( (ctlst== CTLDIN) && (nextctlst == CTLSENDNAK) && (currentendp == 4'b0010) && 
			   (setuptype ==TYPEVENDOR) && (setuprequest == SETCMDWR) )
          setcmdwr <=1'b1;
      else if( (setuprequest != SETCMDWR) || (setuptype !=TYPEVENDOR)
			   ||  ( (ctlst == CTLSETUP) && (nextctlst == CTLWAIT4SETUP)) )
		  setcmdwr <=1'b0;
*/

     if(usbreset)
		  setcmdwr <=1'b0;
	 else if( (ctlst== CTLGRABSETUP) && (nextctlst == CTLSENDACK) &&
		   (setuptype ==TYPEVENDOR) && (setuprequest == SETCMDWR) )
	      setcmdwr <=1'b1;
	 else if( (setuprequest != SETCMDWR) || (setuptype !=TYPEVENDOR) )
	     setcmdwr <=1'b0;

	if(usbreset)
		  gpiord <=1'b0;
      else if( (ctlst== CTLGRABSETUP)&& (nextctlst == CTLSENDACK) &&
			   (setuptype ==TYPEVENDOR) && (setuprequest == GPIORD) )
          gpiord <=1'b1;
      else if((setuprequest != GPIORD) || (setuptype !=TYPEVENDOR))
		  gpiord <=1'b0;
	
	if(usbreset)
		  setmode <=3'b000;
	else if( (ctlst== CTLGRABSETUP)&& (nextctlst == CTLSENDACK) &&
		   (setuptype ==TYPEVENDOR) && (setuprequest == SETMODE) )
          setmode <= setupvalue0[2:0];
	
	if(usbreset)
		   begin
			   wpb   <= 1'b1; 
			   gpiopin3 <=1'b0;
			   gpiopin4 <=1'b0;
			   gpiopin5 <=1'b0;
			   gpiopin6 <=1'b0;
			   gpiopin7 <=1'b0;
           end
      else if( (ctlst== CTLGRABSETUP)&& (nextctlst == CTLSENDACK) &&
			   (setuptype ==TYPEVENDOR) && (setuprequest == GPIOWR))
           begin
			   wpb      <= (setupvalue0 == 8'h08) ? setupvalue1[0] : wpb;
               gpiopin3 <= (setupvalue0 == 8'h03) ? setupvalue1[0] : gpiopin3;
               gpiopin4 <= (setupvalue0 == 8'h04) ? setupvalue1[0] : gpiopin4;
               gpiopin5 <= (setupvalue0 == 8'h05) ? setupvalue1[0] : gpiopin5;
               gpiopin6 <= (setupvalue0 == 8'h06) ? setupvalue1[0] : gpiopin6;
               gpiopin7 <= (setupvalue0 == 8'h07) ? setupvalue1[0] : gpiopin7;
           end
	
	
	if(usbreset)
		  clockrate <=3'b010;
      else if( (ctlst== CTLGRABSETUP)&& (nextctlst == CTLSENDACK) &&
			   (setuptype ==TYPEVENDOR) && (setuprequest == CLOCKRATE) )
          clockrate <= setupvalue0[2:0];
      else
		  clockrate <= clockrate;
	
	if(usbreset)
		   getbusy <=1'b0;
      else if( (ctlst== CTLGRABSETUP)&& (nextctlst == CTLSENDACK) &&
			   (setuptype ==TYPEVENDOR) && (setuprequest == GETBUSY) )
           getbusy <=1'b1;
      else if( ((setuprequest != GETBUSY) || (setuptype !=TYPEVENDOR))

⌨️ 快捷键说明

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