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

📄 chap_9._c

📁 基于D12的USB固件驱动
💻 _C
📖 第 1 页 / 共 2 页
字号:
#include <macros.h>    /* for CLI() SEI() _NOP() macro */
#include "usb100.h"
#include "mainloop.h"
#include "d12ci.h"
/*below are used only by chap_9.c*/
#define NUM_ENDPOINTS 4
#define CONFIG_DESCRIPTOR_LENGTH sizeof(ConfigDescr)+sizeof(InterfaceDescr)+(NUM_ENDPOINTS*0x07)				 
unsigned char DescrData[46] = {0,};	
const unsigned char DeviceDescr[18] = {0x12, 0x01, 0x10, 0x01, 0x08, 0x00, 0x00, 0x10, 0x71,
                                       0x04, 0x66, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01};
const unsigned char ConfigDescr[9] = {0x09, 0x02, 0x2e, 0x00, 0x01, 0x01, 0x00, 0xa0, 0x32};
const unsigned char InterfaceDescr[9] = {0x09, 0x04, 0x00, 0x00, 0x04, 0x08, 0x06, 0x50, 0x00};
const unsigned char EP1_TXDDescr[7] = {0x07, 0x05, 0x81, 0x03, 0x10, 0x00, 0x0a};
const unsigned char EP1_RXDDescr[7] = {0x07, 0x05, 0x01, 0x03, 0x10, 0x00, 0x0a};
const unsigned char EP2_TXDDescr[7] = {0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x0a};
const unsigned char EP2_RXDDescr[7] = {0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x0a};
/*extern*/
extern void stall_ep0(void);
void D12_SetEndpointEnable(unsigned char bEnable);
unsigned char D12_WriteEndpoint(unsigned char endp, unsigned char len, unsigned char *buf);
unsigned char D12_SelectEndpoint(unsigned char bEndp);
void D12_SetEndpointStatus(unsigned char bEndp, unsigned char bStalled);
void D12_SetAddressEnable(unsigned char bAddress, unsigned char bEnable);

/*###################################################################
        
                          USB Protocol Layer  
          Sub Routine Recalled by USB Standard Request Routine

  ###################################################################*/


/*********************************************************************
  Function:     reserved
  Description:  stall_ep0       
  Calls:        stall_ep0();
  Called By:           
  Input:        void                   
  Output:                       
  Return:       void                     
  Others:                            
*********************************************************************/
void reserved(void)
{
    stall_ep0();
}


/*********************************************************************
  Function:     init_unconfig
  Description:  init_unconfig           
  Calls:        D12_SetEndpointEnable();
  Called By:    set_configuration();
  Input:        void
  Output:                       
  Return:       void                     
  Others:                            
*********************************************************************/
void init_unconfig(void)
{
    D12_SetEndpointEnable(0);
}


/*********************************************************************
  Function:     init_config
  Description:  Set D12 address and enable it                      
  Calls:        D12_SetEndpointEnable();
  Called By:    set_address();                       
  Input:        unsigned char bAddress, unsigned char bEnable                       
  Output:                       
  Return:       void                     
  Others:                            
*********************************************************************/
void init_config(void)
{
    /*Enable Endp Transmit and Receive*/
    D12_SetEndpointEnable(1);
}


/*********************************************************************
  Function:     single_transmit
  Description:  Transmit Data(DATA TYPE) by Endp1                 
  Calls:        D12_WriteEndpoint();
  Called By:    get_status(); clear_feature(); set_feature(); set_address();
                set_configuration(); get_configuration(); get_interface(); 
				set_interface(); get_fireware_version(); get_buffer_size(); 
				trans_data();
  Input:        unsigned char * buf, unsigned char len                      
  Output:                       
  Return:       void                     
  Others:                            
*********************************************************************/
void single_transmit(unsigned char * buf, unsigned char len)
{
    if (len <= EP0_PACKET_SIZE)
     {
         D12_WriteEndpoint(1, len, buf);
     }
}


/*********************************************************************
  Function:     code_transmit
  Description:  Transmit Data(CODE TYPE) by Endp1            
  Calls:        CLI(); SEI(); D12_WriteEndpoint();
  Called By:    get_descriptor();              
  Input:        unsigned char * pRomData, unsigned short len              
  Output:                       
  Return:       void                     
  Others:                            
*********************************************************************/
void code_transmit(unsigned char * pRomData, unsigned short len)
{
    ControlData.wCount = 0;
   
    if (ControlData.wLength > len)
    {
	    ControlData.wLength = len;
	}
    
	ControlData.pData = pRomData;
    
    if (ControlData.wLength >= EP0_PACKET_SIZE)
    {
        D12_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData);
       
	    ControlData.wCount += EP0_PACKET_SIZE;
       
	    CLI();
      
	    bEPPflags.bits.control_state = USB_TRANSMIT;
      
	    SEI();
    }
    else
    {
        D12_WriteEndpoint(1, (unsigned char)ControlData.wLength, pRomData);
        
	    ControlData.wCount += ControlData.wLength;
        
		CLI();
        
		bEPPflags.bits.control_state = USB_IDLE;
        
		SEI();
    }
}
 


/*###################################################################
        
                      USB standard device requests   

  ###################################################################*/


/*********************************************************************
  Function:     get_status
  Description:  get_status               
  Calls:        single_transmit(); D12_SelectEndpoint(); stall_ep0();
  Called By:    StandardDeviceRequest();         
  Input:        void                  
  Output:                       
  Return:       void                     
  Others:                            
*********************************************************************/
void get_status(void)
{
    unsigned char endp, txdat[2];
    
	unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
    
	unsigned char c;
    
	if (bRecipient == USB_RECIPIENT_DEVICE)
    {                                          
        if (bEPPflags.bits.remote_wakeup == 1)     
        {
		    /*Remote Wakeup,Self Power*/
			txdat[0] = 3;
		}                        
        else
        {
		    txdat[0] = 1;
		}
        
		txdat[1] = 0;
        
		single_transmit(txdat, 2);
    }

    else if (bRecipient == USB_RECIPIENT_INTERFACE)
    {                    
        txdat[0] = 0;
        
		txdat[1] = 0;
       
	    single_transmit(txdat, 2); 
    }
   
    else if (bRecipient == USB_RECIPIENT_ENDPOINT)
    {   
        endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);                                        //Return Endpoint
       
	    if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK) 
        {
		    /*Read Input Endp Status*/
			c = D12_SelectEndpoint(endp * 2 + 1);     
		}	
		else
        {
		    /*Read Output Endp Status*/
		    c = D12_SelectEndpoint(endp * 2); 
		}      
        if (c & D12_STALL)
        {
		    /*Endp Stall*/
			txdat[0] = 1;
		}
        else
        {
		    /*Endp Enable*/
		    txdat[0] = 0; 
		}                        
        
		txdat[1] = 0;
        
		single_transmit(txdat, 2); 
    }
   
    /*Unstandard Request,Transmit STALL*/
	else    
	{
	    stall_ep0();
	} 
}

/*********************************************************************
  Function:     clear_feature
  Description:  Clear Device Remote Wakeup ,Clear Stall Status of EPn     
  Calls:        CLI(); SEI(); single_transmit(); D12_SetEndpointStatus(); stall_ep0(); 
  Called By:    StandardDeviceRequest();
  Input:        void               
  Output:                       
  Return:       void                     
  Others:                            
*********************************************************************/
void clear_feature(void)
{
    unsigned char endp;
  
    unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
  
    if (bRecipient == USB_RECIPIENT_DEVICE
        && ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP)
    {
        CLI();
       
	    /*Clear Remote Wakeup*/
	    bEPPflags.bits.remote_wakeup=0;        
       
	    SEI();
      
	    /*Return Acknowledge*/
	    single_transmit(0,0);                 
    }
    else if (bRecipient == USB_RECIPIENT_ENDPOINT
        && ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL)
    {   
        endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);                                        //Return Endpoint

⌨️ 快捷键说明

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