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

📄 usb_drv.lst

📁 这是一个用C语言在VC6.0环境下编写的用于USB驱动的代码
💻 LST
📖 第 1 页 / 共 5 页
字号:
C51 COMPILER V7.06   USB_DRV                                                               08/05/2007 09:42:58 PAGE 1   


C51 COMPILER V7.06, COMPILATION OF MODULE USB_DRV
OBJECT MODULE PLACED IN USB_Drv.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE USB_Drv.c BROWSE DEBUG OBJECTEXTEND CODE

stmt level    source

   1          #include "usb_cfg.h"
   2          #include "usb_def.h"
   3          #include "usb_reg.h"
   4          #include "at89x52.h"
   5          #include "stdio.h"
   6          #include "disk_cfg.h"
   7          #include "INTRINS.H"
   8          #define nop _nop_()
   9          #define uint unsigned int
  10          #define uchar unsigned char
  11          /*_____ M A C R O S ________________________________________________________*/
  12          #define FLUSHTX0 {write_usb(TXC0,FLUSH);}
  13          /* Flush and disable the USB TX1 **************************************/
  14          #define FLUSHTX1 {write_usb(TXC1,FLUSH);}
  15          /* Flush and disable the USB TX2 **************************************/
  16          #define FLUSHTX2 {write_usb(TXC2,FLUSH);}
  17          /* Flush and disable the USB TX3 **************************************/
  18          #define FLUSHTX3 {write_usb(TXC3,FLUSH);}
  19          /* Flush and disable the USB RX0 **************************************/
  20          #define FLUSHRX0 {write_usb(RXC0,FLUSH);}
  21          /* Flush and disable the USB RX1 **************************************/
  22          #define FLUSHRX1 {write_usb(RXC1,FLUSH);}
  23          /* Flush and disable the USB RX2 **************************************/
  24          #define FLUSHRX2 {write_usb(RXC2,FLUSH);}
  25          /* Flush and disable the USB RX3 **************************************/
  26          #define FLUSHRX3 {write_usb(RXC3,FLUSH);}
  27          
  28          /*  Send data to TXD0 ****************************/
  29          #define send_desc_to_TXD0   \
  30                  for(desc_idx=0;desc_idx<EP_CONTROL_LENGTH&&desc_idx<data_to_transfer;desc_idx++)   \
  31              {                                                                                  \
  32                          TXD0=*pbuffer++;                                                               \
  33              }                                                                                  \
  34                  if(data_to_transfer>EP_CONTROL_LENGTH)                                             \
  35                  {                                                                                  \
  36                      data_to_transfer-=EP_CONTROL_LENGTH;                                           \
  37                  }                                                                                  \
  38                  else                                                                               \
  39                  {                                                                                  \
  40                     data_to_transfer=0;                                                             \
  41                  }                                                                                  \ 
  42          
  43          /* store the status byte in FIFO0 for the chosen endpoint *************/
  44          #define EPSTATUS(ep)                                                   \
  45                      case ep:                                                   \
  46                        if (stalled^(1<<ep)) write_usb(TXD0,1); else write_usb(TXD0,0);\     
  47                        break;  
  48          
  49          /* enable TX0, using the appropriate DATA PID *************************/
  50          
  51          
  52          #define TXEN0_PID    \
  53                    { if(dta_pid0) write_usb(TXC0,TX_TOGL+TX_EN);       /*DATA1*/\
  54                      else write_usb(TXC0,TX_EN);                       /*DATA0*/\
  55                      dta_pid0=!dta_pid0;} 
C51 COMPILER V7.06   USB_DRV                                                               08/05/2007 09:42:58 PAGE 2   

  56          
  57          /* enable TX1, using the appropriate DATA PID, but not toggling it ****/
  58          #define TXEN1_PID_NO_TGL  \
  59                { if(dta_pid1) write_usb(TXC1,TX_TOGL+TX_LAST+TX_EN);       /*DATA1*/\
  60                      else write_usb(TXC1,TX_LAST+TX_EN); }                      /*DATA0*/
  61          #define TXEN1_PID        \
  62                { TXEN1_PID_NO_TGL;  \
  63                      dta_pid1=!dta_pid1;}
  64          
  65          
  66          /* enable TX2, using the appropriate DATA PID, but not toggling it ****/
  67          #define TXEN2_PID_NO_TGL  \
  68                { if(dta_pid2) write_usb(TXC2,TX_TOGL+TX_LAST+TX_EN);       /*DATA1*/\
  69                      else write_usb(TXC2,TX_LAST+TX_EN); }                      /*DATA0*/
  70          #define TXEN2_PID        \
  71                { TXEN2_PID_NO_TGL;  \
  72                      dta_pid2=!dta_pid2;}
  73          
  74          /* enable TX3, using the appropriate DATA PID, but not toggling it ****/
  75          #define TXEN3_PID_NO_TGL  \
  76                { if(dta_pid3) write_usb(TXC3,TX_TOGL+TX_LAST+TX_EN);       /*DATA1*/\
  77                      else write_usb(TXC3,TX_LAST+TX_EN); }                      /*DATA0*/
  78          #define TXEN3_PID        \
  79                { TXEN3_PID_NO_TGL;  \
  80                      dta_pid3=!dta_pid3;}
  81          
  82          #define write_usb(USB_REG,i)   USB_REG=i
  83          #define read_usb(USB_REG)    USB_REG
  84          /*Fuction declare*******************************/
  85          extern void DelaymS(uint i);
  86          void rx_0();
  87          void rx_1();
  88          void rx_2();
  89          void rx_3();
  90          void tx_0();
  91          void tx_1();
  92          void tx_2();
  93          void tx_3();
  94          void usb_alt();
  95          void nak0();
  96          void nakO1();
  97          void nakI1();
  98          void nak2();
  99          void nak3();
 100          void usb_get_descriptor();
 101          void usb_set_configuration();
 102          void usb_clear_feature();
 103          void usb_set_feature();
 104          void usb_get_status();
 105          void usb_mass_storage_get_lun();
 106          void usb_mass_storage_reset();
 107          
 108          /*global ver define *********************/
 109              uchar rxstat;
 110                  uchar txstat;
 111                  uchar desc_idx;
 112                  uchar evnt;
 113                  uchar usb_buf[64];
 114                  char bdata dtapid;                  /* PID related status*/
 115              sbit dta_pid0= dtapid^0;
 116              sbit dta_pid1= dtapid^1;
 117              sbit dta_pid2= dtapid^2;
C51 COMPILER V7.06   USB_DRV                                                               08/05/2007 09:42:58 PAGE 3   

 118              sbit dta_pid3= dtapid^3;
 119                  char bdata stalled;                 /* indicate the stall status of endpoints0-6 */
 120              sbit stall_ep0 = stalled^0;
 121              sbit stall_ep1 = stalled^1;
 122              sbit stall_ep2 = stalled^2;
 123              sbit stall_ep3 = stalled^3;
 124              sbit stall_ep4 = stalled^4;
 125              sbit stall_ep5 = stalled^5;
 126              sbit stall_ep6 = stalled^6;
 127                  uchar bmRequestType;
 128                  uchar bmRequest;
 129                  uchar data_to_transfer;
 130              uchar code *pbuffer;
 131                  uchar zero_packet_flag;
 132                  uchar usb_cfg;
 133                  uchar flag;
 134                  
 135          
 136          
 137          
 138          void usb_init()
 139          {
 140   1           usb_cfg=0;
 141   1               data_to_transfer=0;
 142   1               pbuffer=NULL;
 143   1               zero_packet_flag=FALSE;
 144   1           RST=0;
 145   1               DelaymS(100);
 146   1               RST=1;
 147   1               DelaymS(100);
 148   1           write_usb(MCNTRL,SRST);   //reset
 149   1           while( read_usb(MCNTRL) & SRST);//wait for reset finish
 150   1           write_usb(MCNTRL,INT_L_P + VGE );//power on 3.3V
 151   1           write_usb(MAMSK, 0);   // disable all ints
 152   1           /*set up interrupt masks ****************************************/
 153   1           write_usb(NAKMSK,NAK_O0+NAK_O1+NAK_I1);                          /*NAK evnts*/  
 154   1           write_usb(TXMSK,TXFIFO0+TXFIFO1+TXFIFO2+TXFIFO3);          /*TX events*/
 155   1           write_usb(RXMSK,RXFIFO0+RXFIFO1+RXFIFO2+RXFIFO3);          /*RX events*/
 156   1           //write_usb(ALTMSK,SD3+RESET_A);                     /*ALT evnts*/
 157   1           /*this is modified in the */
 158   1           /*suspend-resume routines */
 159   1           /*so if any change is made*/
 160   1           /*here it needs to be ref-*/
 161   1           /*lected there too.       */
 162   1           write_usb(MAMSK,(INTR_E+RX_EV+NAK+TX_EV+ALT));
 163   1           write_usb(DMAMASK, 0);
 164   1            /*set default address, enable EP0 only **************************/
 165   1            write_usb(FAR,AD_EN+0);
 166   1            write_usb(EPC0, 0x00); 
 167   1            /*enable the receiver and go operational ************************/  
 168   1            FLUSHTX0;                           /*flush TX0 and disable   */  
 169   1            write_usb(RXC0,RX_EN);              /*enable the receiver     */
 170   1            write_usb(NFSR,OPR_ST);             /*go operational          */   
 171   1            write_usb(MCNTRL,INT_L_P+NAT+VGE);      /*set NODE ATTACH         */
 172   1      }
 173          
 174          void usb_isr()interrupt 0
 175          {
 176   1           
 177   1           evnt=read_usb(MAEV);
 178   1               if (evnt & RX_EV) 
 179   1           {
C51 COMPILER V7.06   USB_DRV                                                               08/05/2007 09:42:58 PAGE 4   

 180   2                evnt=read_usb(RXEV);            /*check the RX events     */  
 181   2                if      (evnt&RXFIFO0) rx_0();  /*endpoint 0              */   
 182   2                else if (evnt&RXFIFO1) rx_1();  /*endpoint 2              */ 
 183   2                else if (evnt&RXFIFO2) rx_2();  /*endpoint 4              */  
 184   2                else if (evnt&RXFIFO3) rx_3();  /*endpoint 6              */  
 185   2                else                            /*some other RX event     */
 186   2                { 
 187   3                }
 188   2           }
 189   1           else if (evnt & TX_EV) 
 190   1          {
 191   2                evnt=read_usb(TXEV);            /*check the TX events     */  
 192   2                if      (evnt&TXFIFO0) tx_0();  /*endpoint 0              */   
 193   2                else if (evnt&TXFIFO1) tx_1();  /*endpoint 1              */ 
 194   2                else if (evnt&TXFIFO2) tx_2();  /*endpoint 3              */  
 195   2                else if (evnt&TXFIFO3) tx_3();  /*endpoint 5              */  
 196   2                else                            /*some other TX event     */
 197   2                { 
 198   3                }
 199   2          }
 200   1          else if (evnt & ALT) usb_alt();     /*alternate event?        */   
 201   1          else if (evnt & NAK) 
 202   1          {
 203   2                evnt=read_usb(NAKEV);           /*check the NAK events    */ 
 204   2                if      (evnt & NAK_O0) nak0();   /*endpoint 0              */   
 205   2                else if (evnt & NAK_O1) nakO1();   /*endpoint 2              */
 206   2                else if (evnt & NAK_I1) nakI1(); 
 207   2                else if (evnt & NAK_O2) nak2();   /*endpoint 4              */  
 208   2                else if (evnt & NAK_O3) nak3();   /*endpoint 6              */  
 209   2                else                            /*some other TX event     */
 210   2                { 
 211   3                }
 212   2          }
 213   1          else                                /*spurious event!         */
 214   1          {
 215   2          }
 216   1      }
 217          
 218          void rx_0()
 219          {
 220   1          rxstat=read_usb(RXS0);              /*get receiver status     */
 221   1              /*is this a setup packet? ***************************************/  
 222   1          if(rxstat & SETUP_R)                   
 223   1          { 
 224   2              data_to_transfer=0;
 225   2                  pbuffer=NULL;
 226   2                  zero_packet_flag=FALSE;
 227   2              /*read data payload into buffer then flush/disble the RX ****/
 228   2                      
 229   2              for(desc_idx=0; desc_idx<8; desc_idx++)
 230   2              {
 231   3                   usb_buf[desc_idx] = read_usb(RXD0);
 232   3              }
 233   2                      FLUSHRX0;                     /*make sure the RX is off */
 234   2              FLUSHTX0;                     /*make sure the TX is off */ 
 235   2                      TI=1;printf("\n");
 236   2                      for(desc_idx=0; desc_idx<8; desc_idx++)
 237   2              {
 238   3                   printf("%2.2X,", (uint)usb_buf[desc_idx]);
 239   3      
 240   3              }
 241   2              bmRequestType=usb_buf[0];
C51 COMPILER V7.06   USB_DRV                                                               08/05/2007 09:42:58 PAGE 5   

 242   2                      bmRequest=usb_buf[1];        
 243   2              if ((usb_buf[0]&0x60)==0x00)    /*if a standard request   */
 244   2                      {
 245   3                      switch (usb_buf[1])           /*find request target     */

⌨️ 快捷键说明

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