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

📄 freescale

📁 Freescale 系列单片机常用模块与综合系统设计
💻
📖 第 1 页 / 共 2 页
字号:
   



/*********************************************************
* Name: USB_EP0_IN_Handler
* Desc: Handle USB EP0 IN Token
* Parameter: None
* Return: None            
**********************************************************/

 void USB_EP0_IN_Handler(void)
{
     
    if(vUSB_Dev_State == cADR_PENDING) {
        ADDR = Setup_Pkt.wValue_l;
        vUSB_Dev_State = cADDRESS;
    }
           

    EP0_Load();
          
    if(EP0Bi.Stat.McuCtlBit.DATA == 0)
        EP0Bi.Stat._byte = kUDATA1;
    else
        EP0Bi.Stat._byte = kUDATA0;

}


/*********************************************************
* Name: USB_EP0_OUT_Handler
* Desc: Handle USB EP0 OUT Token
*       EP0 OUT token is always Data1 and zero count
* Parameter: None
* Return: None            
**********************************************************/

void USB_EP0_OUT_Handler(void)
{
  
    EP0Bo.Cnt = cEP0_BUFF_SIZE; 
    EP0Bo.Stat._byte = kUDATA0;
    EP0Bi.Stat._byte = kUDATA1;      
}
      


/*********************************************************
* Name: USB_Setup_Handler
* Desc: Handle USB SETUP token
* Parameter: None
* Return: None            
**********************************************************/

void USB_Setup_Handler(void)
{
       
   vEP0IN_DataCnt= 0;
   
   if(0x00 == (Setup_Pkt.bmRequestType & 0x60))
    USB_StdReq_Handler();
   
   else if(0x20 == (Setup_Pkt.bmRequestType & 0x60))
    USB_ClsReq_Handler();       
    
   else
    EP0_Stall();  
   
   
   EP0Bi.Stat._byte = kUDATA1;  
   EP0Bo.Cnt  = cEP0_BUFF_SIZE;
   
   
   CTL_TSUSPEND = 0;
}



/*********************************************************
* Name: EP1_Load
* Desc: Load data to Endpoint 1 buffer
* Parameter: None
* Return: None            
**********************************************************/
void EP1_Load(void)
{
    byte i,counter;
    byte *pBuffer;
     
    pBuffer=(byte *)&EP1IN_Data;
    
    if(vEP1_Cnt > cEP1_BUFF_SIZE)
      counter = cEP1_BUFF_SIZE;
    else
      counter = (byte)vEP1_Cnt;
    
    for(i=0;i<counter;i++,vEP1Idx++)
      pBuffer[i]=vEP1Data[vEP1Idx];
       
    
    EP1Bi.Cnt = counter;
    vEP1State ^=0x40;  
    EP1Bi.Stat._byte= vEP1State;
    
    vEP1_Cnt = vEP1_Cnt - counter;
    
}


/*********************************************************
* Name: USB_EP1_IN_Handler
* Desc: Handle USB Endpoint1 IN token
* Parameter: None
* Return: None            
**********************************************************/
void USB_EP1_IN_Handler(void)
{
   
   if( vUSB_Trf_State == cCSW) 
   {
      Send_CSW();
   } 
   
   else if(vUSB_Trf_State == cEP1Tx)
   {
     
     EP1_Load();
     if(vEP1_Cnt == 0)
    {
      vUSB_Trf_State=cUSBWait; 
    } 
    
   }
   
   else
   {
      EP1Bi.Cnt = 0;
      EP1Bi.Stat._byte= kMCU;  
   }
    
}


/*********************************************************
* Name: USB_EP2_OUT_Handler
* Desc: Handle USB Endpoint2 OUT token
* Parameter: None
* Return: None            
**********************************************************/ 
void USB_EP2_OUT_Handler(void)
{
    byte i;
    byte *pCBW_Pkt;
    
    pCBW_Pkt=(byte *)&EP2OUT_Data;
      

    
    if(EP2Bo.Cnt ==31)        // CBW should be 31 bytes
    {
      vUSB_Trf_State = cCBW;
      vCBWBuf_flag=1;   
      
      for(i=0;i<EP2Bo.Cnt;i++)
      {
          vCBW_Buf[i]= pCBW_Pkt[i];
          
      }
             
      vEP2State ^=0x40;
      EP2Bo.Stat._byte=vEP2State;
      EP2Bo.Cnt = cEP2_BUFF_SIZE;
    }
      
    else
    {
         
      for(i=0;i<EP2Bo.Cnt;i++,vEP2Idx++)
      {
         
         vEP2Data[vEP2Idx]= pCBW_Pkt[i];
      }
         
      if(vEP2Idx == 512)
      {
        vUSB_Trf_State = cEP2Rx;
        EP2Bo.Cnt = cEP2_BUFF_SIZE;
        vEP2State ^=0x40;
        EP2Bo.Stat._byte= kMCU;
      } 
      else
      {
          
        vEP2State ^=0x40;
        EP2Bo.Stat._byte=vEP2State;
        EP2Bo.Cnt = cEP2_BUFF_SIZE;
      }  
        
    }
}




/*********************************************************
* Name: USB_Handler
* Desc: Handle all EP token request
* Parameter: None
* Return: None            
**********************************************************/

void USB_Handler(void) 
{
  byte stat;
  
  stat=STAT&0xF8;
      
  if(stat == mEP0_OUT) 
  {
    if(EP0Bo.Stat.RecPid.PID == mSETUP_TOKEN)
      USB_Setup_Handler();
    else
      USB_EP0_OUT_Handler();
  } 
  
  else if (stat==mEP0_IN) {
    USB_EP0_IN_Handler();
   
  }
  
  else if (stat==mEP1_IN){
    USB_EP1_IN_Handler();
    
  }
  
  else if (stat==mEP2_OUT) {
    USB_EP2_OUT_Handler();
    
  }
  
}


/*********************************************************
* Name: USB_Reset_Handler
* Desc: Handle USB reset command
* Parameter: None
* Return: None            
**********************************************************/

void USB_Reset_Handler(void)
{
    ERRSTAT = 0xFF;                 // clear USB error flag
    INTSTAT = 0xBF;                 // clear USB interrupt
    ERRENB = 0xBF;                  // disable all USB error interrupt sources
    INTENB = 0x88;                  // enable stall and token    
    
    ADDR = 0x00;                    // reset to default address 
  
    //Below is initialize the BDT
    EP0Bi.Stat._byte = kUDATA1;     // endpoint 0 IN buffer initialization
    EP0Bi.Cnt = 0x00;                      
    EP0Bi.Addr = cEP0INBuffAddr;                     
  
    EP0Bo.Stat._byte = kUDATA0;     // endpoint 0 OUT initialization
    EP0Bo.Cnt = cEP0_BUFF_SIZE;              
    EP0Bo.Addr = cEP0OUTBuffAddr;   // (EP0 OUT buffer-0x1860) >> 2;    

    
    EPCTL0 = 0x0D;                  // enable endpoint 0
    
  
    vUSB_Dev_State = cDEFAULT;
   
}



/*********************************************************
* Name: USB_Stall_Handler
* Desc: Handle USB stall info
* Parameter: None
* Return: None            
**********************************************************/

void USB_Stall_Handler(void) 
{
    
    if(EPCTL0_EPSTALL == 1)
        EPCTL0_EPSTALL = 0;
    
    INTSTAT_STALLF = 1;
}

/*********************************************************
* Name: USB_ISR
* Desc: USB interrupt service routine
* Parameter: None
* Return: None            
**********************************************************/

interrupt void USB_ISR(void) 
{
  
   if(INTSTAT_USBRSTF && INTENB_USBRST) 
    {
      
      USB_Reset_Handler();
    }
         
  if(INTSTAT_STALLF && INTENB_STALL )
      USB_Stall_Handler();
   
  
  if(INTSTAT_TOKDNEF && INTENB_TOKDNE)
    {
      USB_Handler();
        
      INTSTAT_TOKDNEF = 1;    
    }
}

⌨️ 快捷键说明

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