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

📄 d12ci._c

📁 基于D12的USB固件驱动
💻 _C
字号:
#include <macros.h>    /* for CLI() SEI() _NOP() macro */
#include "mainloop.h"

extern EPPFLAGS bEPPflags;

void outportCommand(unsigned char Data);
void outportData(unsigned char Data);
unsigned char inportData(void);

/*###################################################################
         
  Give Definiens of  Command Interface of all visited D12 functions    
  
  ###################################################################*/

/*********************************************************************
  Function:     D12_SetAddressEnable 
  Description:  Set D12 address and enable it                      
  Calls:        CLI(); SEI(); outportCommand(); outportData();
  Called By:    set_address();                       
  Input:        unsigned char bAddress, unsigned char bEnable                       
  Output:                       
  Return:       void                     
  Others:                            
*********************************************************************/
void D12_SetAddressEnable(unsigned char bAddress, unsigned char bEnable)
{ 
    if (bEPPflags.bits.in_isr == 0)
    {
        /*DisableInterrupts;*/
	    CLI();                      
    } 
   
    outportCommand(0xD0);         
   
    if (bEnable)
    {
        /*bit 7 of bAddress is enable bit,bit 0 ~ bit 6 are Address bits*/
	    bAddress |= 0x80;
   
        outportData(bAddress);
   
        if (bEPPflags.bits.in_isr == 0)
        {
	        /*EnableInterrupts;*/
		    SEI();
	    }       
    }
}

/*********************************************************************
  Function:       D12_SetEndpointEnable
  Description:    Set Endpoint Enable
  Calls:          CLI(); SEI(); outportCommand(); outportData();
  Called By:      init_unconfig(); init_config();
  Input:          unsigned char bEnable 
  Output:        
  Return:         void
  Others:         
*********************************************************************/
void D12_SetEndpointEnable(unsigned char bEnable)
{
    if (bEPPflags.bits.in_isr == 0)
    {
        /*DisableInterrupts;*/
	    CLI();
    }
       
    outportCommand(0xD8);         
 
    /*bit 0 of bEnable is Enable bit*/
    if (bEnable)
    {
        outportData(1);   
    }
    else
    {
        outportData(0);
    }
      
    if (bEPPflags.bits.in_isr == 0)
    {
        /*EnableInterrupts;*/
	    SEI();
    }
}

/*********************************************************************
  Function:       D12_SetMode
  Description:    SetMode
  Calls:          CLI(); SEI(); outportCommand(); outportData();
  Called By:      reconnect_USB(); 
  Input:          unsigned char bConfig,unsigned char bClkDiv
  Output:         
  Return:         void
  Others:         
*********************************************************************/
void D12_SetMode(unsigned char bConfig, unsigned char bClkDiv)
{
    if (bEPPflags.bits.in_isr == 0)
    {
        /*DisableInterrupts;*/
	    CLI();
    }                            
 
    outportCommand(0xF3);         
 
    outportData(bConfig);  
 
    outportData(bClkDiv);
   
    if (bEPPflags.bits.in_isr == 0)
    {
        /*EnableInterrupts;*/
	    SEI();
    }  
}

/*
Set DMA
void D12_SetDMA(unsigned char bMode)
{
 if(bEPPflags.bits.in_isr==0)
 {       
     
	 CLI();
 }

 outportCommand(0xFB);

 outportData(bMode);
 
 if(bEPPflags.bits.in_isr==0)
 {
     
	 SEI();
 }  
}
*/


/*********************************************************************
  Function:       D12_ReadInterruptRegister
  Description:    Read Interrupt Register
  Calls:          outportCommand(); inportData();
  Called By:      int3_usb_isr(); 
  Input:          void
  Output:         
  Return:         unsigned short
  Others:         
*********************************************************************/
unsigned short D12_ReadInterruptRegister(void)
{
    unsigned char b1;
    unsigned int  j;
   
    outportCommand(0xF4);
   
    b1 = inportData();
   
    j = inportData();
   
    j = (unsigned char)j << 8;
    j += b1;
   
    return j;
}

/*********************************************************************
  Function:       D12_SelectEndpoint
  Description:    Select Endpoint
  Calls:          CLI(); SEI(); outportCommand(); inportData();
  Called By:      get_status(); 
  Input:          unsigned char bEndp
  Output:         
  Return:         unsigned char
  Others:         
*********************************************************************/
unsigned char D12_SelectEndpoint(unsigned char bEndp)
{
    unsigned char c;
   
    if(bEPPflags.bits.in_isr==0)
    {
        /*DisableInterrupts;*/
        CLI();
    }
 
    outportCommand(bEndp);
 
    c = inportData();
   
    if(bEPPflags.bits.in_isr == 0)
    {
        /*EnableInterrupts;*/
	    SEI();
    }    
 
    return c;
}

/*********************************************************************
  Function:       D12_ReadLastTransactionStatus
  Description:    Read LastTransaction Status to clear relative interrupt
  Calls:          outportCommand(); inportData();
  Called By:      ep0_rxdone(); ep0_txdone(); ep1_rxdone(); ep1_txdone(); 
                  main_rxdone(); main_txdone();   
  Input:          unsigned char bEndp 
  Output:          
  Return:         unsigned char LastTransactionStatus  
  Others:          
*********************************************************************/
unsigned char D12_ReadLastTransactionStatus(unsigned char bEndp)
{
    outportCommand(0x40 + bEndp);
 
    return inportData();  
}

/*********************************************************************
  Function:       D12_ReadEndpointStatus
  Description:    Read Endpoint Status
  Calls:          CLI(); SEI(); outportCommand(); inportData();
  Called By:      main_rxdone(); trans_data(); 
  Input:          unsigned char bEndp
  Output:         
  Return:         unsigned char Endpoint Status
  Others:          
*********************************************************************/
unsigned char D12_ReadEndpointStatus(unsigned char bEndp)
{
    unsigned char c;
 
    if (bEPPflags.bits.in_isr == 0)
    {
        /*DisableInterrupts;*/
        CLI();
    }
       
    outportCommand(0x80 + bEndp);
 
    c = inportData();
   
    if (bEPPflags.bits.in_isr == 0)
    {
        /*EnableInterrupts;*/
	    SEI();
    }
       
    return c;
}
 
/*********************************************************************
  Function:       D12_SetEndpointStatus
  Description:    Set Endpoint Status
  Calls:          CLI(); SEI(); outportCommand(); outportData();
  Called By:      clear_feature(); set_feature(); ep0_rxdone(); stall_ep0();
  Input:          unsigned char bEndp,unsigned char bStalled
  Output:          
  Return:         void 
  Others:          
*********************************************************************/
void D12_SetEndpointStatus(unsigned char bEndp, unsigned char bStalled)
{
    if (bEPPflags.bits.in_isr == 0)
    {
        /*DisableInterrupts;*/
	    CLI();
    }
 
    outportCommand(0x40+bEndp);
 
    outportData(bStalled);
   
    if (bEPPflags.bits.in_isr == 0)
    {
        /*EnableInterrupts;*/
	    SEI();
    }
}

/*********************************************************************
  Function:       D12_SendResume
  Description:    Send Resume
  Calls:          outportCommand();  
  Called By:      
  Input:          void 
  Output:         
  Return:         void  
  Others:          
*********************************************************************/
void D12_SendResume(void)
{
    outportCommand(0xF6);
}

/*********************************************************************
  Function:       D12_ReadCurrentFrameNumber
  Description:    Read Current Frame Number
  Calls:          CLI(); SEI(); outportCommand(); inportData();
  Called By:      
  Input:          void 
  Output:        
  Return:         unsigned short Current Frame Number
  Others:         
*********************************************************************/
unsigned short D12_ReadCurrentFrameNumber(void)
{
    unsigned short i = 0, j = 0;
   
    if (bEPPflags.bits.in_isr == 0)
    {
        /*DisableInterrupts;*/
	    CLI();
    }
   
    outportCommand(0xF5);
 
    i = inportData();
    j = inportData();
    i += (j << 8);
   
    if (bEPPflags.bits.in_isr == 0)
    {
        /*EnableInterrupts;*/
	    SEI();
    }
  
    return i;
   
}

/*********************************************************************
  Function:       D12_ReadChipID
  Description:    Read Chip ID
  Calls:           
  Called By:       
  Input:           
  Output:         
  Return:         
  Others:        
*********************************************************************/
unsigned short D12_ReadChipID(void)
{
    unsigned short i,j;
  
    if (bEPPflags.bits.in_isr == 0)
    {
        /*DisableInterrupts;*/
	    CLI();
    }
   
    outportCommand(0xFD);
  
    i=inportData();
 
    j=inportData();
   
    i += (j << 8);
  
    if (bEPPflags.bits.in_isr==0)
    {
        /*EnableInterrupts;*/
	    SEI();
    }
      
    return i;
}
   
/*********************************************************************
  Function:       D12_ReadEndpoint
  Description:    Read Endpoint
  Calls:          CLI(); SEI(); outportCommand(); inportData(); 
  Called By:      ep0_rxdone();  ep1_rxdone(); main_rxdone(); trans_data();
  Input:          unsigned char endp,unsigned char len,unsigned char *buf
  Output:          
  Return:         unsigned char
  Others:          
*********************************************************************/
unsigned char D12_ReadEndpoint(unsigned char endp, unsigned char len, unsigned char *buf)
{
    unsigned char i, j;
   
    if (bEPPflags.bits.in_isr == 0)
    {
        /*DisableInterrupts;*/
	    CLI();
    }
   
    outportCommand(endp);
   
    if ((inportData() & D12_FULLEMPTY) == 0)
    {
        return 0;
    }
       
    outportCommand(0x80 + endp);
 
    i = inportData();

    i = i & 0x60;
 
    if (endp == 2)
    {
        if (i == 0x60)
        {
	        bEPPflags.bits.ep1buf_full=1;
	    }
        else
        {
	        bEPPflags.bits.ep1buf_full=0;
	    }
    }
    if (endp == 4)
    {
        if (i == 0x60)
        {
	        bEPPflags.bits.ep2buf_full = 1;
	    }
        else
        {
	        bEPPflags.bits.ep2buf_full = 0;
	    }
    }
   
    outportCommand(0xF0);
 
    j = inportData();
 
    j = inportData();
   
    if (j > len)     
    {
        j = len;
    }
    for (i = 0;i < j;i++)
    {
        *(buf + i) = inportData();
    }
       
    outportCommand(0xF2);
   
    if (bEPPflags.bits.in_isr == 0)
    {
        /*EnableInterrupts;*/
	    SEI();
    }
      
    return j;
}


/*********************************************************************
  Function:       D12_WriteEndpoint
  Description:    Write Endpoint
  Calls:          CLI(); SEI(); outportCommand(); inportData(); outportData();
  Called By:      single_transmit(); code_transmit(); ep0_txdone(); trans_data(); 
  Input:          unsigned char endp,unsigned char len,unsigned char *buf
  Output:        
  Return:         unsigned char
  Others:          
*********************************************************************/
unsigned char D12_WriteEndpoint(unsigned char endp, unsigned char len, unsigned char *buf)
{
    unsigned char i;
   
    if (bEPPflags.bits.in_isr == 0)
    {
        /*DisableInterrupts;*/
	    CLI();
    }
   
    outportCommand(endp);
 
    inportData();   
   
    outportCommand(0xF0);
 
    outportData(0);
 
    outportData(len);
   
    for (i = 0; i < len; i++)
    {    
        outportData(* (buf + i));
    }   
 
    outportCommand(0xFA);
   
    if(bEPPflags.bits.in_isr==0)
    {
         /*EnableInterrupts;*/
	     SEI();
    }
       
    return len;
}
   
   
/*********************************************************************
  Function:       D12_AcknowledgeEndpoint
  Description:    Acknowledge Endpoint
  Calls:          outportCommand(); 
  Called By:      ep0_rxdone(); 
  Input:          unsigned char endp 
  Output:         
  Return:         void 
  Others:         
*********************************************************************/
void D12_AcknowledgeEndpoint(unsigned char endp)
{
   outportCommand(endp);
   
   outportCommand(0xF1);
   
   if (endp == 0)
   {
       outportCommand(0xF2);
   }       
} 

⌨️ 快捷键说明

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