/******************************************************************
本程序只供学习使用,未经作者许可,不得用于其它任何用途
欢迎访问我的USB专区:http://usb.cepark.com/index.php
欢迎访问我的blog: http:computer00.cepark.com
USB Disk.C file
作者:Computer-lov
建立日期: 2004.09.21
修改日期: 2007.12.23
版本:V1.1
版权所有,盗版必究。
Copyright(C) Computer-lov 2007-2017
All rights reserved
*******************************************************************/
#include "AT89x52.H"
#include "USB.H"
#include "PDIUSBD12.H"
#include "MASS_STORAGE.H"
#include "My_type.h"
#include "UART.h"
#define USB_COMMAND_ADD 1
#define USB_DATA_ADD 0
#define USB_DATA P0
#define USB_A0 P3_5
#define USB_WR P3_6
#define USB_RD P3_7
#define USB_INT P3_2
#define ENDPOINT_NUMBER 2
#define MAX_CONTROL_DATA_SIZE 16
#define MAX_LUN 0
#define MAX_BULK_DATA_SIZE 64
CONTROL_DATA_BUFF Control_Data;
unsigned char idata buffer[64];
//高低字节交换
#define SWAP16(x) ((((uint16)(x))<<8)|(((uint16)(x))>>8))
//取一个整数的低字节
#define LSB(x) ((uint8)(x))
//取一个整数的高字节
#define MSB(x) ((uint8)(((uint16)(x))>>8))
idata CBW cbw;
idata CSW csw;
#define debug
//#define debug1
union
{
unsigned char Register;
struct
{
unsigned char Command :1;
unsigned char Data :1;
}Status;
}SCSI;
unsigned char *pData;
code unsigned char DISK_INF[36]= //磁盘信息
{
0x00,
0x00,
0x02,
0x02,
0x1F,
0x00,0x00,0x00,
0xB5,0xE7,0XC4,0xD4,0xC8,0xA6,0xC8,0xA6, //电脑圈圈
0xD7,0xD4,0xBC,0xBA,0xD7,0xF6,0xB5,0xC4,0xBC,0xD9,0x55,0xC5,0xCC,0x00,0x00,0x00, //自己做的假U盘
0x31,0x2E,0x30,0x31
};
code unsigned char DISK_CAPACITY[8]= //磁盘容量
{
0x00,0x01,0xff,0x00,
0x00,0x00,0x02,0x00
};
code unsigned char SENSE[0x12]= //模式探测返回数据
{
0x70, 0x00, 0x05, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00
};
code unsigned char DBR[512]= //DOS引导记录
{
0xeb, 0x3e, 0x90, //跳转指令
0x4d, 0x53, 0x44, 0x4f, 0x53, 0x35, 0x2e, 0x30, //文件系统版本信息
0x00, 0x02, //扇区字节数
0x20, //每簇扇区数
0x01, 0x00, //保留扇区数
0x02, //该分区的FAT副本数
0xF0, 0x01, //根目录项数
0x00, 0x00, //小扇区数
0xf8, //媒体描述符
0x10, 0x00, //每FAT扇区数
0x20, 0x00, //每道扇区数
0x40, 0x00, //磁头数
0x00, 0x00, 0x00, 0x00, //隐藏扇区数
0x40, 0xff, 0x01, 0x00, //大扇区数
0x80, //磁盘驱动器参数,80表示硬盘
0x00, //保留
0x29, //扩展引导标记,0x29表示后三个区可用
0x88, 0x09, 0x71, 0x20, //标卷序列号
0xC8, 0xA6, 0xC8, 0xA6, 0xB5, 0xC4, 0xBC, 0xD9, 0x55, 0xC5, 0xCC, //磁盘标卷:圈圈的假U盘
0x46, 0x41, 0x54, 0x31, 0x36, 0x20, 0x20, 0x20, //文件系统类型信息
0xf1, 0x7d,
0xfa, 0x33, 0xc9, 0x8e, 0xd1, 0xbc, 0xfc, 0x7b, 0x16, 0x07, 0xbd, 0x78, 0x00, 0xc5, 0x76, 0x00,
0x1e, 0x56, 0x16, 0x55, 0xbf, 0x22, 0x05, 0x89, 0x7e, 0x00, 0x89, 0x4e, 0x02, 0xb1, 0x0b, 0xfc,
0xf3, 0xa4, 0x06, 0x1f, 0xbd, 0x00, 0x7c, 0xc6, 0x45, 0xfe, 0x0f, 0x8b, 0x46, 0x18, 0x88, 0x45,
0xf9, 0xfb, 0x38, 0x66, 0x24, 0x7c, 0x04, 0xcd, 0x13, 0x72, 0x3c, 0x8a, 0x46, 0x10, 0x98, 0xf7,
0x66, 0x16, 0x03, 0x46, 0x1c, 0x13, 0x56, 0x1e, 0x03, 0x46, 0x0e, 0x13, 0xd1, 0x50, 0x52, 0x89,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 0x70, 0x00, 0x50, 0x52, 0x51, 0x91, 0x92,
0x33, 0xd2, 0xf7, 0x76, 0x18, 0x91, 0xf7, 0x76, 0x18, 0x42, 0x87, 0xca, 0xf7, 0x76, 0x1a, 0x8a,
0xf2, 0x8a, 0x56, 0x24, 0x8a, 0xe8, 0xd0, 0xcc, 0xd0, 0xcc, 0x0a, 0xcc, 0xb8, 0x01, 0x02, 0xcd,
0x13, 0x59, 0x5a, 0x58, 0x72, 0x09, 0x40, 0x75, 0x01, 0x42, 0x03, 0x5e, 0x0b, 0xe2, 0xcc, 0xc3,
0x03, 0x18, 0x01, 0x27, 0x0d, 0x0a, 0x49, 0x6e, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x20, 0x73, 0x79,
0x73, 0x74, 0x65, 0x6d, 0x20, 0x64, 0x69, 0x73, 0x6b, 0xff, 0x0d, 0x0a, 0x44, 0x69, 0x73, 0x6b,
0x20, 0x49, 0x2f, 0x4f, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0xff, 0x0d, 0x0a, 0x52, 0x65, 0x70,
0x6c, 0x61, 0x63, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x64, 0x69, 0x73, 0x6b, 0x2c, 0x20, 0x61,
0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x61, 0x6e,
0x79, 0x20, 0x6b, 0x65, 0x79, 0x0d, 0x0a, 0x00, 0x49, 0x4f, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x53, 0x59, 0x53, 0x4d, 0x53, 0x44, 0x4f, 0x53, 0x20, 0x20, 0x20, 0x53, 0x59, 0x53, 0x80, 0x01,
0x00, 0x57, 0x49, 0x4e, 0x42, 0x4f, 0x4f, 0x54, 0x20, 0x53, 0x59, 0x53, 0x00, 0x00, 0x55, 0xaa,
};
code unsigned char FAT[64]= //模拟的文件分配表
{
0xF8, 0xFF, 0xFF, 0xFF, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ,0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
code unsigned char ZERO[64]= //填充0
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ,0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
code unsigned char FILE_DATA[]=
{
"这是一个测试程序,用来测试