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

📄 usb.lst

📁 usb接口的移动硬盘
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V7.06   USB                                                                   03/02/2006 23:51:19 PAGE 1   


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

stmt level    source

   1          /******************************************************************
   2             本程序只供学习使用,未经作者许可,不得用于其它任何用途
   3                我的邮箱:computer-lov@tom.com
   4                  欢迎访问我的blog:  http://computer00.21ic.org
   5          
   6          USB.C  file
   7          
   8          Created by Computer-lov
   9          Date: 2004.9.21
  10          
  11          Edit date:2006.3.2
  12          
  13          Version V1.1
  14          Copyright(C) Computer-lov 2004-2014
  15          All rigths reserved
  16                       
  17          *******************************************************************/
  18          
  19          #include<AT89x52.H>
  20          #include "USB.H"
  21          #include "PDIUSBD12.H"
  22          #include "MASS_STORAGE.H"
  23          //#include<serial.h>
  24          #include "ide.h"
  25          
  26          #define USB_COMMAND_ADD           1
  27          #define USB_DATA_ADD              0
  28          
  29          #define USB_DATA                  P0
  30          #define USB_A0                    P3_5
  31          #define USB_WR                    P3_6
  32          #define USB_RD                    P3_7
  33          #define USB_INT                   P3_2
  34          
  35          
  36          #define ENDPOINT_NUMBER           2
  37          #define MAX_CONTROL_DATA_SIZE     16
  38          #define MAX_LUN                    0
  39          #define MAX_BULK_DATA_SIZE        64
  40          
  41          CONTROL_DATA_BUFF idata Control_Data;
  42          
  43          unsigned char idata buffer[64];
  44          
  45          CBW idata cbw;
  46          CSW idata csw;
  47          
  48          
  49          /*union 
  50          {
  51           unsigned char Register;
  52           struct
  53            {
  54             unsigned char Command                :1;
  55             unsigned char Data                   :1;
C51 COMPILER V7.06   USB                                                                   03/02/2006 23:51:19 PAGE 2   

  56            }Status;
  57          }SCSI;*/
  58          
  59          
  60          unsigned char bdata SCSI_Register;
  61          sbit SCSI_Command    =   SCSI_Register^0;
  62          sbit SCSI_Data       =   SCSI_Register^1;
  63          
  64          
  65          unsigned char *pData;
  66          
  67          unsigned char code DISK_INF[36]
  68          =                                      //磁盘信息
  69          {
  70           0x00,
  71           0x00,
  72           0x02,
  73           0x02,
  74           0x1F,
  75           0x00,0x00,0x00,
  76           0x49,0x44,0x45,0xA1,0xFA,0x55,0x53,0x42, 
  77           0xD7,0xD4,0xBC,0xBA,0xD7,0xF6,0xB5,0xC4,0xD2,0xC6,0xB6,0xAF,0xD3,0xB2,0xC5,0xCC,
  78           0x31,0x2E,0x30,0x31
  79          };
  80          
  81          unsigned char DISK_CAPACITY[8]=                                  //磁盘容量
  82          {
  83           0x00,0x01,0xff,0x00,
  84           0x00,0x00,0x02,0x00
  85          };
  86          
  87          code unsigned char SENSE[0x12]=                                      //模式探测返回数据
  88          {
  89          0x70, 0x00, 0x05, 0x00, 0x00, 0x00,
  90          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00
  91          };
  92          
  93          
  94          unsigned int SWAP(unsigned int temp)                                //高低字节交换
  95          {
  96   1       unsigned char i;
  97   1       i=((unsigned char *)&temp)[0];
  98   1       ((unsigned char *)&temp)[0]=((unsigned char *)&temp)[1];
  99   1       ((unsigned char *)&temp)[1]=i;
 100   1       return temp;
 101   1      }
 102          
 103          unsigned char LSB(unsigned int temp)                                //取一个整数的低字节
 104          {
 105   1       return (unsigned char)temp;
 106   1      }
 107          
 108          unsigned char MSB(unsigned int temp)                                //取一个整数的高字节
 109          {
 110   1       return (unsigned char)(temp>>8);
 111   1      }
 112          
 113          code unsigned char LANGUAGE_ID[4]={0x04,0x03,0x09,0x04};            //字符串描述符所用的语言种类        
 114          
 115          code unsigned char device_serial_number[18]=                         //设备序列号
 116           {0x12,0x03,0x32,0x00,0x30,0x00,0x37,0x00,0x31,0x00,0x30,0x00,0x39,0x00,0x38,0x00,0x32,0x00};
 117          
C51 COMPILER V7.06   USB                                                                   03/02/2006 23:51:19 PAGE 3   

 118          code DEVICE_DESCRIPTOR_STRUCT device_descriptor=     //设备描述符
 119          {
 120           sizeof(DEVICE_DESCRIPTOR_STRUCT),                   //设备描述符的字节数大小
 121           DEVICE_DESCRIPTOR,                                  //设备描述符类型编号
 122           0x1001,                                             //USB版本号
 123           0x00,                                               //USB分配的设备类代码
 124           0x00,                                               //USB分配的子类代码
 125           0x00,                                               //USB分配的设备协议代码
 126           0x10,                                               //端点0的最大包大小
 127           0x7104,                                             //厂商编号
 128           0xf0ff,                                             //产品编号
 129           0x0100,                                             //设备出厂编号
 130           0x00,                                               //设备厂商字符串的索引
 131           0x00,                                               //描述产品字符串的索引
 132           0x02,                                               //描述设备序列号字符串的索引
 133           0x01                                                //可能的配置数量
 134          };
 135          
 136          typedef struct _CON_INT_ENDP_DESCRIPTOR_STRUCT
 137          {
 138           CONFIGURATION_DESCRIPTOR_STRUCT configuration_descriptor;
 139           INTERFACE_DESCRIPTOR_STRUCT  interface_descritor;
 140           ENDPOINT_DESCRIPTOR_STRUCT  endpoint_descriptor[ENDPOINT_NUMBER];
 141          }CON_INT_ENDP_DESCRIPTOR_STRUCT;
 142          
 143          code CON_INT_ENDP_DESCRIPTOR_STRUCT con_int_endp_descriptor=
 144          {
 145          //configuration_descriptor                                          //配置描述符
 146          {
 147           sizeof(CONFIGURATION_DESCRIPTOR_STRUCT),                           //配置描述符的字节数大小
 148           CONFIGURATION_DESCRIPTOR,                                          //配置描述符类型编号
 149           (sizeof(CONFIGURATION_DESCRIPTOR_STRUCT)+sizeof(INTERFACE_DESCRIPTOR_STRUCT)+sizeof(ENDPOINT_DESCRIPTOR_S
             -TRUCT)*ENDPOINT_NUMBER)*256+
 150           (sizeof(CONFIGURATION_DESCRIPTOR_STRUCT)+sizeof(INTERFACE_DESCRIPTOR_STRUCT)+sizeof(ENDPOINT_DESCRIPTOR_S
             -TRUCT)*ENDPOINT_NUMBER)/256,
 151           0x01,                                                            //只包含一个接口
 152           0x01,                                                            //该配置的编号
 153           0x00,                                                            //iConfiguration字段
 154           0x80,                                                            //采用总线供电,不支持远程唤醒
 155           0xC8                                                            //从总线获取最大电流400mA
 156          },
 157          //interface_descritor                                             //接口描述符
 158          {
 159           sizeof(INTERFACE_DESCRIPTOR_STRUCT),                             //接口描述符的字节数大小
 160           INTERFACE_DESCRIPTOR,                                            //接口描述符类型编号
 161           0x00,                                                            //接口编号为0
 162           0x00,                                                            //该接口描述符的编号
 163           ENDPOINT_NUMBER,                                                 //非0端点数量为2,只使用端点主端点输入和
             -输出
 164           0x08,                                                            //USB大容量存储设备
 165           0x06,                                                            //使用的子类,为简化块命令
 166           0x50,                                                            //使用的协议,这里使用单批量传输协议
 167           0x00                                                             //接口描述符字符串索引
 168          },
 169          
 170          //endpoint_descriptor[]
 171          {
 172           {                                                                  //主端点输入描述
 173            sizeof(ENDPOINT_DESCRIPTOR_STRUCT),                             //端点描述符的字节数大小
 174            ENDPOINT_DESCRIPTOR,                                            //端点描述符类型编号
 175            MAIN_POINT_IN,                                                  //端点号,主输入端点
 176            ENDPOINT_TYPE_BULK,                                             //使用的传输类型,批量传输
C51 COMPILER V7.06   USB                                                                   03/02/2006 23:51:19 PAGE 4   

 177            0x4000,                                                         //该端点支持的最大包尺寸,64字节
 178            0x00                                                            //中断扫描时间,对批量传输无效
 179           },
 180            
 181           {                                                                  //主端点输出描述
 182            sizeof(ENDPOINT_DESCRIPTOR_STRUCT),                             //端点描述符的字节数大小
 183            ENDPOINT_DESCRIPTOR,                                            //端点描述符类型编号
 184            MAIN_POINT_OUT,                                                 //端点号,主输出端点
 185            ENDPOINT_TYPE_BULK,                                             //使用的传输类型,批量传输
 186            0x4000,                                                         //该端点支持的最大包尺寸,64字节
 187            0x00                                                            //中断扫描时间,对批量传输无效
 188           }
 189          }
 190          };
 191          
 192          /*union                                          //程序标志位
 193          {
 194          unsigned int Register;
 195          struct 
 196          {
 197           unsigned char bus_reset        :1;
 198           unsigned char suspend          :1;
 199           unsigned char remote_wakeup    :1;
 200           unsigned char int_isr          :1;
 201           unsigned char not_end          :1;
 202           unsigned char usb_idle         :1;
 203           unsigned char usb_busy         :1;
 204           unsigned char setup_packet_in  :1;
 205           unsigned char setup_packet_out :1;
 206           unsigned char set_addr         :1;
 207           unsigned char usb_endp0_in     :1;
 208           unsigned char usb_endp2_in     :1;
 209           unsigned char usb_endp2_out    :1;
 210          }flags;
 211          }usb_flags;
 212          */
 213          
 214          unsigned int bdata usb_flags_Register;
 215          //unsigned bdata usb_flags_Register2;
 216          
 217          sbit USB__bus_reset        = usb_flags_Register^0;
 218          sbit USB_suspend          = usb_flags_Register^1;
 219          sbit USB_remote_wakeup    = usb_flags_Register^2;
 220          sbit USB_int_isr          = usb_flags_Register^3;
 221          sbit USB_not_end          = usb_flags_Register^4;
 222          sbit USB_usb_idle         = usb_flags_Register^5;
 223          sbit USB_usb_busy         = usb_flags_Register^6;
 224          sbit USB_setup_packet_in  = usb_flags_Register^7;
 225          sbit USB_setup_packet_out = usb_flags_Register^8;
 226          sbit USB_set_addr         = usb_flags_Register^9;
 227          sbit USB_usb_endp0_in     = usb_flags_Register^10;
 228          sbit USB_usb_endp2_in     = usb_flags_Register^11;
 229          sbit USB_usb_endp2_out    = usb_flags_Register^12;
 230          
 231          /*union                                         //中断寄存器
 232          {
 233           unsigned char Register[2];
 234           struct 
 235            {
 236             unsigned char control_out_port  :1;
 237             unsigned char control_in_port   :1;
 238             unsigned char port_out_1        :1;
C51 COMPILER V7.06   USB                                                                   03/02/2006 23:51:19 PAGE 5   

 239             unsigned char port_in_1         :1;
 240             unsigned char main_out_port     :1;
 241             unsigned char main_in_port      :1;
 242             unsigned char bus_reset         :1;
 243             unsigned char suspend_change    :1;
 244             unsigned char DMA_EOT           :1;
 245             unsigned char not_use           :7;
 246            }Interrupt;
 247          }Interrupt_Register;
 248          */
 249          
 250          unsigned char bdata Interrupt_Register[2];
 251          
 252          sbit Interrupt_control_out_port   =  Interrupt_Register[0]^0;
 253          sbit Interrupt_control_in_port    =  Interrupt_Register[0]^1;
 254          sbit Interrupt_port_out_1         =  Interrupt_Register[0]^2;
 255          sbit Interrupt_port_in_1          =  Interrupt_Register[0]^3;
 256          sbit Interrupt_main_out_port      =  Interrupt_Register[0]^4;
 257          sbit Interrupt_main_in_port       =  Interrupt_Register[0]^5;
 258          sbit Interrupt_bus_reset          =  Interrupt_Register[0]^6;
 259          sbit Interrupt_suspend_change     =  Interrupt_Register[0]^7;
 260          sbit Interrupt_DMA_EOT            =  Interrupt_Register[1]^0;
 261          
 262          
 263          /*union                                    //端点最后处理状态
 264          {
 265           unsigned char Register;
 266           struct
 267           {
 268            unsigned char successful          :1;
 269            unsigned char error_code          :4;
 270            unsigned char setup_packet        :1;
 271            unsigned char data_1              :1;
 272            unsigned char prestatus_not_read  :1;
 273           }Status;
 274          }Last_Status;*/
 275          
 276          unsigned char bdata Last_Status_Register;
 277          
 278          sbit Last_Status_successful          =   Last_Status_Register^0;
 279          sbit Last_Status_error_code0         =   Last_Status_Register^1;
 280          sbit Last_Status_error_code1         =   Last_Status_Register^2;
 281          sbit Last_Status_error_code2         =   Last_Status_Register^3;
 282          sbit Last_Status_error_code3         =   Last_Status_Register^4;
 283          sbit Last_Status_setup_packet        =   Last_Status_Register^5;
 284          sbit Last_Status_data_1              =   Last_Status_Register^6;
 285          sbit Last_Status_prestatus_not_read  =   Last_Status_Register^7;
 286          
 287          //*****************   延时x毫秒函数 ***********
 288          void delay(unsigned int x)                
 289          {
 290   1       unsigned int i;
 291   1       unsigned int j;
 292   1       for(i=0;i<x;i++)
 293   1       for(j=0;j<230;j++);
 294   1      }
 295          //********************************************
 296          
 297          
 298          void write_usb_command(unsigned char usb_command)       //写USB命令
 299          {
 300   1       USB_A0=USB_COMMAND_ADD;
C51 COMPILER V7.06   USB                                                                   03/02/2006 23:51:19 PAGE 6   

 301   1       USB_DATA=usb_command;
 302   1       USB_WR=0;
 303   1       USB_WR=1;
 304   1       USB_DATA=0xFF;
 305   1      }
 306          
 307          void write_a_usb_data(unsigned char usb_data)        //写一字节USB数据
 308          {
 309   1       USB_A0=USB_DATA_ADD;
 310   1       USB_DATA=usb_data;
 311   1       USB_WR=0;
 312   1       USB_WR=1;
 313   1       USB_DATA=0XFF;
 314   1      }

⌨️ 快捷键说明

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