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

📄 et21x130c_usb_lib.c

📁 在台湾义统公司
💻 C
📖 第 1 页 / 共 5 页
字号:
//////////////////////////////////////////////////////////
//
//  USB Library main file 
//  You can revise the sub function in the prjName.c
//  Revise this file may cause usb error!
//
//////////////////////////////////////////////////////////
   
//if MCU speed is set 24MHz or 48MHz, please define "ET_MCU_HI_SPEED" 
#define ET_MCU_HI_SPEED

#include <usb_reg.h>

//Use lib generator to create ET21x130C_c_setting.h
#include "ET21x130C_c_setting.h"

#include <interrupt.h>

#include <c_macro.h>

#include "ET21x130C_usb_lib.h"

                 
                         
                         
                         
                         

void UsbIntPre() using USBBANK
{
#if _USB_AUTO_SET_BANK
   M_BANK(USB_BANK);
#endif
 
#if _USB_AUTO_STORE_IAP0
   STACK_IAP0 = IAP0;
#endif
  
}

////////////////////////////////////////////////////////////////
// fun: UsbInitial
// Initial usb register.
// If you want to add code when usb initial,revise AddUsbInitialWorkSub(),
// Call Function: AddUsbInitialWorkSub();
///////////////////////////////////////////////////////////////
// UsbInital() revised by USB_INITAL
void UsbInitial() using USBBANK
{
    // 
    IAPADR = 0x05;

    // enable usb function
    M_BS6(PRIE);
    
//    M_BANK(USB_BANK);    
//hub endpoint transmit: 0: not transmit yet  1:transmit ok, 

    M_BC3(HUB_STATUS);
	USB_STATUS = 0;
	STATUS_BUF = 0;
	A_INTERFACE = 0;

// enable embedd function interrupt
    EPINTE = 0x3f;
	USB_Lib_Delay();

//_ENABLE USB Bus Reset Event 
	STAINTE = 0x01;
	USB_Lib_Delay();

//Hub initilize: interrupt INT__ENABLE
	HINTE = 0xCF;
	USB_Lib_Delay();



//Address clear  
	ADDRESS_BUFh = 0;
	ADDRESS_BUFf = 0;
	
	
	FAR = 0xFF;
	USB_Lib_Delay();
	HAR = 0xFF;
	USB_Lib_Delay();
	
//USB SPEED setting
#if _USB_FULL_SPEED
    M_BS7(GCNTR);
#else
    M_BC7(GCNTR);
#endif 
       
    USB_Lib_Delay();
    
//Start transmition: plug-in    
    M_BS1(GCNTR);
        
    USB_Lib_Delay();
    
    //. call macro  _ADD_USB_INITIAL_WORK_SUB      
    AddUsbInitialWorkSub();
    
    return;
  
}


///////////////////////////////////////////////////////////
//. RST_ISR interrupt function,interrupt vector 0x98
//  run when USB Bus Reset Event Detect
//  If you want to add code when usb reset,revise AddUsbBusResetSub(),
//  Call Function: AddUsbBusResetSub();
///////////////////////////////////////////////////////////
void Int_RstInte() interrupt INT_RSTINTE using USBBANK
{
#if(_USB_AUTO_SET_BANK ||_USB_AUTO_STORE_IAP0)
   UsbIntPre();
#endif

   // software reset usb device 
   M_BS0(GCNTR);
   USB_Lib_Delay();

   // clear usb bus reset event detect

//**Remote-wakeup disable,power switch disable   
   
   //HGSR = 0;
   _asm
     MOV A,#0x00
     MOV _HGSR,A
   _endasm;  


   // set Hub address
//   HAR = 0x00;
   USB_Lib_Delay();
   
   // usb device address
   HAR = 0;
   USB_Lib_Delay();
   FAR = 0;
   USB_Lib_Delay();
   
   STAINTR = 0xFE;//#0b11111110
   USB_Lib_Delay();
  
   nAddrBuf = 0;
   HEP1_CNT = 0;
   USB_STATUS = 0;
   STATUS_BUF = 0;
   
#if !_USB_HUB
   {
      HAR = 127;
      USB_Lib_Delay();
      M_BS5(HGSR);
      USB_Lib_Delay();
      HPCONR = 145;//#0b10010001
      USB_Lib_Delay();
      M_BC0(HPSCR1);
            
      USB_Lib_Delay();
      M_BS5(GCNTR);
      USB_Lib_Delay();
      HPCONR = 137; //#0b10001001
      USB_Lib_Delay();
      M_BC4(HPSCR1);   
      USB_Lib_Delay();
   }
#endif   
   //. call macro _ADD_USB_BUS_RESET_SUB
   AddUsbBusResetSub();

 Back_To_Mloop: 
#if(_USB_AUTO_STORE_IAP0)
   {
      IAP0=STACK_IAP0;
      USB_Lib_Delay();        
   }
#endif
   
#if(_USB_DIS_ANO_INT)
      M_ENI;
#endif
      
   return;  
}


///////////////////////////////////////////////////////////
//. RX0_ISR interrupt function,interrupt vector 0x88
//  run when USB EP0 RX Event
//  If you want to add code when USB EP0 RX Event occur ,revise AddUsbSetReportSub(),
//  Call Function:AddUsbSetReportSub();
///////////////////////////////////////////////////////////
//RX0_ISR 2004/01/27 Create by Brooklin
void Int_Int0Rxe(void) interrupt INT_INT0RXE using USBBANK
{
	
#if(_USB_AUTO_SET_BANK ||_USB_AUTO_STORE_IAP0)
    UsbIntPre();
#endif
    
    if(M_ISBS0(EP0RXTR))    // JBS	EP0RXTR,OUT,f_out_package
    {//. f_out_package
        EP0RXTR = 0xFE; //#0b11111110
        USB_Lib_Delay();
        //. JBC 	FC_STATUS,DATA_OUT,next_f_inout   					
        if(M_ISBC2(FC_STATUS))
            goto Next_F_Inout;
        else
        {
            M_BC2(FC_STATUS);   
            //macro  _ADD_USB_SET_REPORT_SUB
            AddUsbSetReportSub();  
            
            nwLength = 0;     
            //wLengthL = 0;
            //wLengthH = 0;
            
            //. next_f_inout
        }
    }
    //. f_setup_package
    else
    {
        if(M_ISBS1(EP0RXTR))
        {
            EP0RXTR = 0xFD; //#0b11111101        
        }
        else
        {
            EP0RXTR = 0xFB; //#0b11111011
        }

        _asm 
          NOP
          MOV A,#_bmRequestType
          MOV _IAP0,A
          NOP
        _endasm;

        
        while(M_ISBC3(IAP0))
        {
           IAC0 = EP0RXDAR;
        }

// replace wLengthH and wLengthL by nwLength 
//        if(!wLengthH && !wLengthL)
        if(!nwLength)
        {
           EP0TXCTR = 0;
           USB_Lib_Delay();
        }    
        
        M_BC1(USB_STATUS);
        M_BC0(USB_STATUS);
        M_BC2(USB_STATUS);
        M_BC5(USB_STATUS);
        
//        _asm
//          MOV A,#_bmRequestType
//        _endasm;
        StageOne();
         
        if(M_ISBS0(USB_STATUS)) 
        {//.f_stall
           M_BS1(EP0RXCSR);
           USB_Lib_Delay();
           M_BS1(EP0TXCSR); 
           USB_Lib_Delay();
           goto Back_To_Mloop;       
        }    
    } 
 
 Next_F_Inout:   
   M_BC1(EP0RXCSR);
   USB_Lib_Delay();
   M_BC1(EP0TXCSR);
   USB_Lib_Delay();
   M_BS0(EP0RXCSR);
   USB_Lib_Delay();
   M_BS0(EP0TXCSR); 

  // BACK_TO_MLOOP
Back_To_Mloop: 
#if(_USB_AUTO_STORE_IAP0)
   {
      IAP0=STACK_IAP0;
      USB_Lib_Delay();        
   }
#endif
   
#if(_USB_DIS_ANO_INT)
      M_ENI;
#endif

   return;        
}

///////////////////////////////////////////////////////////
//. IN0_ISR interrupt function,interrupt vector 0x88
//  run when EP0 USB IN Token Event occur
//  
//  Call Function: 
///////////////////////////////////////////////////////////
void Int_Int0Ine(void) interrupt INT_INT0INE using USBBANK
{
	
#if(_USB_AUTO_SET_BANK ||_USB_AUTO_STORE_IAP0)
   UsbIntPre();
#endif
      
 //Clear interrupt flag   
   EPINTR = 251;//#0b11111011
   USB_Lib_Delay();

#if(_USB_AUTO_STORE_IAP0)
   {
      IAP0=STACK_IAP0;
      USB_Lib_Delay();        
   }
#endif
   
#if(_USB_DIS_ANO_INT)
      M_ENI;
#endif

   return;  
}



////////////////////////////////////////////////////////////////////////////////////////
//. TX0_ISR interrupt function,interrupt vector 0x88
//  run when USB EP0 TX Event
//  If you want to add code when USB EP0 TX Event occur ,revise AddUsbGetReportExpSub(),
//  Call Function:AddUsbGetReportExpSub();
////////////////////////////////////////////////////////////////////////////////////////
void Int_Int0Txe(void) interrupt INT_INT0TXE using USBBANK
{
#if(_USB_AUTO_SET_BANK ||_USB_AUTO_STORE_IAP0)
   UsbIntPre();
#endif
 //Clear interrupt INT_flag   
   EPINTR = 253;//#0b11111101
   USB_Lib_Delay();
   
   FAR = ADDRESS_BUFf;
   USB_Lib_Delay();
   if(!TBLL)
   {//. exit_tx01
      EP0TXCTR=0;
      USB_Lib_Delay();
   }
   else if(nwLength)
   {//. TX0_ISR1
      if(M_ISBS5(FC_STATUS))
      {//. USB_GET_REPORT_SUB
       //. call macro  _ADD_USB_GET_REPORT_EXP_SUB
         AddUsbGetReportExpSub();
      }
      else
      {
         nTable = nRomAdr;
         //HTBL = ROMADRH;
         //LTBL = ROMADRL;
         USB_Lib_Delay();
         fTransmitTable();
      }  
   }

 Next_F_Inout:   
   M_BC1(EP0RXCSR);
   USB_Lib_Delay();
   M_BC1(EP0TXCSR);
   USB_Lib_Delay();
   M_BS0(EP0RXCSR);
   USB_Lib_Delay();
   M_BS0(EP0TXCSR); 
}


////////////////////////////////////////////////////////////////////////////////////////
//. EP1_ISR interrupt function,interrupt vector 0x90
//  run when EP1 Interrupt occur
//  If you want to add code when EP1 Interrupt occur ,revise AddUsbEp1Sub(),
//  Call Function:AddUsbEp1Sub();
////////////////////////////////////////////////////////////////////////////////////////
void Int_Int1e() interrupt INT_INT1E using USBBANK
{
   //UsbIntPre();
   //M_BC1(PORTB);
   EPINTR = 247; // #0b11110111
   USB_Lib_Delay();
   //. call macro _ADD_USB_EP1_SUB
   AddUsbEp1Sub();

 Back_To_Mloop: 
#if(_USB_AUTO_STORE_IAP0)
   {
      IAP0=STACK_IAP0;
      USB_Lib_Delay();        
   }
#endif
   
#if(_USB_DIS_ANO_INT)
      M_ENI;
      
#endif
      
   return;  
}


////////////////////////////////////////////////////////////////////////////////////////
//. EP2_ISR interrupt function,interrupt vector 0x90
//  run when EP2 Interrupt occur
//  If you want to add code when EP2 Interrupt occur ,revise AddUsbEp2Sub(),
//  Call Function:AddUsbEp2Sub();
////////////////////////////////////////////////////////////////////////////////////////
void Int_Int2e() interrupt INT_INT2E using USBBANK
{
   UsbIntPre();
   EPINTR = 239; // #0b11101111
   USB_Lib_Delay();
   //. call macro _ADD_USB_EP2_SUB
   AddUsbEp2Sub();


 Back_To_Mloop: 
#if(_USB_AUTO_STORE_IAP0)
   {
      IAP0=STACK_IAP0;
      USB_Lib_Delay();        
   }
#endif
   
#if(_USB_DIS_ANO_INT)
      M_ENI;
#endif

   return; 
}




////////////////////////////////////////////////////////////////////////////////////////
//. EP3_ISR interrupt function,interrupt vector 0x90
//  run when EP3 Interrupt occur
//  If you want to add code when EP3 Interrupt occur ,revise AddUsbEp3Sub(),
//  Call Function:AddUsbEp3Sub();
////////////////////////////////////////////////////////////////////////////////////////
void Int_Int3e() interrupt INT_INT3E using USBBANK
{
   UsbIntPre();
   EPINTR = 223; // #0b11011111
   USB_Lib_Delay();
   //. call macro _ADD_USB_EP3_SUB
   AddUsbEp3Sub();


Back_To_Mloop: 
#if(_USB_AUTO_STORE_IAP0)
   {

⌨️ 快捷键说明

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