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

📄 frm9602.c

📁 我自己写的用国半9603与PC并口通讯的例程
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "reg51.h"
#include "reg9602.h"
#include "usbspec.h"
#include "absacc.h"
#include "frm9602.h"

/////////////////////Variables Definication///////////////////////////
 USC cmd_length;
 USC evnt,usb_cfg,usb_buf[8],n;
 USC desc_typ,desc_idx,desc_sze,buf,txstat,rxstat;
 bit status_GETDESC,dtapid_TGL0PID,dtapid_TGL1PID,dtapid_TGL2PID,dtapid_TGL3PID;
 bit stall0,stall1,stall2,stall3,stall4,stall5,stall6;

#define DEV_DESC_SIZE 18
code USC DEV_DESC[] = {18,     /*length of this desc.    */
             0x01,              /*DEVICE descriptor       */
             0x00,0x01,         /*spec rev level (BCD)    */
             0x00,              /*device class            */
             0x00,              /*device subclass         */
             0x00,              /*device protocol         */  
             0x08,              /*max packet size         */   
             0x00,0x04,         /*vendor ID    */
//   the PID can be modified as your wish
             0x3c,0x00,         /*product ID   */  
             0x63,0x00,         /* revision ID  */  
             0,                 /*index of manuf. string  */   
             0,                 /*index of prod.  string  */  
             0,                 /*index of ser. # string  */   
             0x01               /*number of configs.      */ 
             };                

#define CFG_DESC_SIZE 60
code USC CFG_DESC[] = {0x09,              /*length of this desc.    */ 
             0x02,              /*CONFIGURATION descriptor*/  
             0x3c,0x00,         /*total length returned   */ 
             0x01,              /*number of interfaces    */ 
             0x01,              /*number of this config   */ 
             0x00,              /*index of config. string */  
             0x40,              /*attr.: self powered,remote wakeup*/   
             0x50,                /*max power (100 mA)      */  

             0x09,              /*length of this desc.    */  
             0x04,              /*INTERFACE descriptor    */  
             0x00,              /*interface number        */
             0x00,              /*alternate setting       */  
             0x06,              /*# of endpoints  */ 
             0x00,              /*interface class         */
             0x00,              /*interface subclass      */  
             0x00,              /*interface protocol      */  
             0x00,              /*index of intf. string   */ 

/*The WDM driver for this board references the endpoints by 'pipe'    */
/*number: 0, 1, 2 et al in order below:                               */

                /*Pipe 0                                       */
             0x07,              /*length of this desc.    */   
             0x05,              /*ENDPOINT descriptor     */ 
             0x81,              /*address (IN)            */  
             0x02,              /*attributes  (bulk) */    
             0x20,0x00,         /*max packet size (32)    */
             0x01,              /*interval (ms)           */

                /*Pipe 1                                       */ 
             0x07,              /*length of this desc.    */   
             0x05,              /*ENDPOINT descriptor     */ 
             0x02,              /*address (OUT)           */  
             0x02,              /*attributes  (BULK)      */    
             0x20,0x00,         /*max packet size (32)    */
             0x01,              /*interval (ms)           */

                /*Pipe 2                                       */
             0x07,              /*length of this desc.    */   
             0x05,              /*ENDPOINT descriptor     */ 
             0x83,              /*address (IN)            */  
             0x02,              /*attributes  (bulk) */    
             0x20,0x00,         /*max packet size (32)    */
             0x01,              /*interval (ms)           */

                /*Pipe 3                                       */ 
             0x07,              /*length of this desc.    */   
             0x05,              /*ENDPOINT descriptor     */ 
             0x04,              /*address (OUT)           */  
             0x02,              /*attributes  (BULK)      */    
             0x20,0x00,         /*max packet size (32)    */
             0x01,              /*interval (ms)           */

                /*Pipe 4                                       */
             0x07,              /*length of this desc.    */   
             0x05,              /*ENDPOINT descriptor     */ 
             0x85,              /*address (IN)            */  
             0x02,              /*attributes  (BULK) */    
             0x40,0x00,         /*max packet size (64)    */
             0x01,              /*interval (ms)           */

                /*Pipe 5                                       */ 
             0x07,              /*length of this desc.    */   
             0x05,              /*ENDPOINT descriptor     */ 
             0x06,              /*address (OUT)           */  
             0x02,              /*attributes  (BULK)      */    
             0x40,0x00,         /*max packet size (64)    */
             0x01};             /*interval (ms)           */

USC test=0,i,test_RXC1=0,test_DMA=0,test_RXS3=0;
/////////////////////////////////////////////////////////

void main(void)
{
     USC t=0;

     write_usb(MCNTRL,SRST);          //Software reset 9602
     init_51();

     init_sys();
     init_9602();

     EX0=1;     //Enable ext INT0
     EA=1;      //Enable CPU's interrupt function

     while(1)   //Waiting for interruptions
         {
//for(t=0;t<10;t++)
          Delay1();
/////////////////////////////
//nWRITE=0;
//test=read_usb(DMACNT);
//test=read_usb(DMAMSK);

/////////////////////////////

          }
} 

/***************************Initialization**********************************/
void init_51()
{
     //initiate 51
     IP=0x1;    //Set ext INT0 to be high priority
     EX0=0;     //Disable ext INT0
     IT0=0;     //Set ext INT0 to be low volt_level trigged
     EA=0;      //Enable ext INT0
}

void init_sys()
{
    USC t;
    
    P1=0xFF;
    nCHOOSE=1;
    usb_cs=1;
    nWRITE=0;
    nAddrStb=0;
    nDataStb=0;

    return;
}

void init_9602()
{
     USC t=0;
     status_GETDESC=0;
     usb_cfg = 0;
     write_usb(MCNTRL,SRST);          //Software reset 9602
     write_usb(MCNTRL,VGE+INT_L_P);   //Enable regulater,set INT to be low,push_pull
     write_usb(FAR,AD_EN+0);        //Set address to be 0x80
     write_usb(EPC0,0x00);          //Setup EP0
     write_usb(NAKMSK,NAK_O0);      //Enable OUT NACK envent 0
     write_usb(TXMSK,TXFIFO0+TXFIFO1+TXFIFO2+TXFIFO3);//Enable TX event reporting
     write_usb(RXC1,RX_EN);         //Enable EP2 reception
     write_usb(RXMSK,RXFIFO0+RXFIFO1+RXFIFO2+RXFIFO3);//Enable RX event reporting
     write_usb(ALTMSK,SD3+RESET_A+DMA); //Enable 3ms idle time reporting and DMA reporting
        //test=read_usb(ALTMSK);
     write_usb(MAMSK,(INTR_E+RX_EV+NAK+TX_EV+ALT));//Enable five INT causes
     FLUSHTX0;                      //Clear TX FIFO_1
     Delay1();
     write_usb(RXC0,RX_EN);         //Enable EP0 reception
     write_usb(NFSR,OPR_ST);        //Set operation state to be operating
     write_usb(MCNTRL,VGE+INT_L_P+NAT);//Attach node to USB bus
}

/*******************Entrance of interruption*******************************/
void Int_Event() interrupt 0
{
     nWRITE=0;

     evnt = read_usb(MAEV);              
     if (evnt & RX_EV) 
     {
         evnt=read_usb(RXEV);        
         if(evnt&RXFIFO0) 
                rxevent_0(); 
            else if (evnt & RXFIFO1) 
                rxevent_1();  
            else if (evnt & RXFIFO2) 
                rxevent_2();  
            else if (evnt & RXFIFO3) 
                rxevent_3();  
     }
     else if (evnt & TX_EV) 
     {
         evnt=read_usb(TXEV);        
         
             if (evnt&TXFIFO0) 
                txevent_0();  
             else if (evnt&TXFIFO1) 
                txevent_1(); 
             else if (evnt&TXFIFO2) 
                txevent_2(); 
             else if (evnt&TXFIFO3) 
                txevent_3();           
     }
     else if (evnt & NAK) 
     {
         evnt=read_usb(NAKEV); 
             if (evnt&NAK_O0) 
                nak0();   
     }
     else if (evnt & ALT) 
         usb_alt();    
/*// for debug only
    else if (evnt & FRAME)
    {
        //////////////////////////
        test=read_usb(DMAMSK);
        test=read_usb(DMACNT);
        if(test==0)test=read_usb(DMAEV);
        //////////////////////////
    }
 */
    nWRITE=1;
}
  
/**************************RX0 and TX0 event handling functions***************/
void rxevent_0(void)
{
    USC commandID;

    rxstat=read_usb(RXS0);
    if(rxstat & SETUP_R)  //If it is a SETUP package..                 
    { 
             for(desc_idx=0; desc_idx<8; desc_idx++) 
             usb_buf[desc_idx] = read_usb(RXD0);// Read content of FIFO_0

             FLUSHRX0; 
             FLUSHTX0;        
             bitclr(EPC0,STALL);        //????????
            
             if ((usb_buf[0]&0x60)==0x00)//If it is a standard SETUP package
                 switch (usb_buf[1])         //Do respective processing
                 {
                    case CLEAR_FEATURE:                  
                         clrfeature(); 
                         break;

                    case GET_CONFIGURATION:                   
                         write_usb(TXD0,usb_cfg);
                         break;

                    case GET_DESCRIPTOR: 
                         getdescriptor();                   
                         break;

                    case GET_STATUS: 
                         getstatus();   
                         break;

                    case SET_ADDRESS:
                         write_usb(EPC0,DEF);   
                         write_usb(FAR,usb_buf[2] | AD_EN);      
                         break;

                    case SET_CONFIGURATION:
                         setconfiguration(); 
                         break;

                    case SET_FEATURE: 
                         setfeature();  
                         break;
             
                    case GET_INTERFACE:
                         write_usb(TXD0,0);  
                         break;
             
                    case SET_INTERFACE:
                         if(usb_buf[2])      
                         bitset(EPC0,STALL); 
                         break;

                    default:            
                         break;
                  }
             else   //If it is a command defined by vendor..                     
             {
             }

             write_usb(TXC0,TX_TOGL+TX_EN);
             dtapid_TGL0PID=0;             
    }
    else   
    {
       if (status_GETDESC)      
           {
              if ((rxstat& 0x5F)!=0x10)
               {
               }
              status_GETDESC=0;          
              FLUSHTX0;                  
           }
       write_usb(RXC0,RX_EN);         
    }
    nWRITE=1;
}

void txevent_0(void)
{
    USC lim;

     txstat=read_usb(TXS0); 
     if ((txstat & ACK_STAT) && (txstat & TX_DONE))
     {
             FLUSHTX0; 
             if(status_GETDESC)
         {
            lim=desc_idx+8; 
            for(; ((desc_idx<lim)&&(desc_idx<desc_sze)); desc_idx++)
                get_desc();
            TXEN0_PID;    
             }
     }
     else
         write_usb(RXC0,RX_EN);  
}
/*********************Here are some basic functions**********************/
void Delay1()
{
     USC i;
     for(i=0;i<=10;i++){}
}    

void write_usb(USC addr,USC dat) 
{
     XBYTE[(0xfeff & addr)|0xfe00]=dat;
}

USC read_usb(USC addr)
{
     return XBYTE[0xfeff & addr];
}

void bitset(USC adr, USC mask)
  {
        write_usb(adr,(read_usb(adr)|(mask)));   /*set the bit (RMW)  */

⌨️ 快捷键说明

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