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

📄 main.c

📁 用89C51SND1C实现的USB存储器的代码,包括USB通信和FLASH的读写操作.
💻 C
字号:
/****************************************************************/
/*					UP-MP3DIY100								*/
/*		Beijing Universal Pioneering Technology Co., LTD		*/
/*		CopyRighted,All right Reserved	www.up-tech.com			*/
/*						July,2003								*/
/*				010-62387873  62381842							*/
/****************************************************************/

#include <REGSND1.H>
#include "uart.h"
#include "atmelusbci.h"
#include "main.h"
#include "bulk_only.h"

extern unsigned char bulk_state;
extern unsigned char Bulk_Out_Buf[512];

code unsigned char Device_Descriptor[18]={
								    	0x12,			//0x12
										 0x01,              //DEVICE descriptor
										 0x10,0x01,         //spec rev level (BCD) 1.0
										 0x0,              //device class
										 0x0,              //device subclass
										 0x0,              //device protocol
										 0x20,              //max packet size
										 0x05,0x82,         //National's vendor ID
										 0x00,0x11,         //National's product ID  
										 0x00,0x00,         //National's revision ID  
										 0,                 //index of manuf. string   
										 0,                 //index of prod.  string  
										 0,                 //index of ser. # string   
										 0x01               //number of configs. 	
									};

code unsigned char Configuration_Descriptor_All[32]={

									    9,//Size of Descriptor in Bytes
									    2,//Configuration Descriptor (0x02)
									    0x20,//Total length in bytes of data returned  LSB
										0x00,//MSB
										1,//Number of Interfaces
										1,//Value to use as an argument to select this configuration
									    0,//Index of String Descriptor describing this configuration
										0x80,
										0xfa,//Maximum Power Consumption in 2mA units 
									
									    9,
									    4,
									    0,//the index of the interface descriptor Number of Interface
									    0,//Value used to select alternative setting
										2,//EndPoint Number Used in this Descriptor
										8,//Class Code (Assigned by USB Org)
										6,	//interface subclass1=RBC,2=SFF,3=QIC,4=UFI,5=SFF,6=SCSI
										0x50,//bulk 0nly Transport
										0,//Index of String Descriptor Describing this interface

										 //Bulk-in Endpoint
										 0x07,              //length of this desc.   
										 0x05,              //ENDPOINT descriptor TYPE
										 0x81,              //address (IN) Endpoint 4 84
										 0x02,              //attributes  (BULK)    
										 0x40,0x00,         //max packet size (64)
										 0x0,              //Does not apply to Bulk endpoints
							
										 //Bulk-out Endpoint
										 0x07,              //length of this desc.   
										 0x05,              //ENDPOINT descriptor TYPE
										 0x02,              //address (OUT) Endpoint 5 05
										 0x02,              //attributes  (BULK)
										 0x40,0x00,         //max packet size (64)
										 0x0             //Does not apply to Bulk endpoints
										};

unsigned char ReadEp(unsigned char EpNum,unsigned char *Data)
{
	unsigned char i=0,nLength;
	UEPNUM=EpNum;

	nLength=UBYCTX;
	while(nLength--) {Data[i++]=UEPDATX;/*printuf("%x ",Data[i]);*/}

	UEPSTAX&=~(RXOUTB0|RXOUTB1|RXSETUP);
	return(i);
}

void WriteEp(unsigned char EpNum,unsigned char nLength,unsigned char *Data)
{
	unsigned char i=0;
	UEPNUM=EpNum;
	UEPSTAX|=DIR;//Set for status of a Control In transaction
	while(nLength--) UEPDATX=Data[i++];
	UEPSTAX|=TXRDY;//Sending the data,this action will switch between bank0 and bank1
	while(!(UEPSTAX&TXCMP)) ;
	UEPSTAX&=(~(TXCMP));//clear TXCMP
}


void Get_Status()
{
	printu("Get_Status\n");
}
void Clear_Feature()
{
	printu("Clear_Feature\n");
}
void Set_Feature()
{
	printu("Set_Feature\n");
}
void Set_Address(unsigned char EpNum)
{
	printu("Set_Address\n");
//	unsigned char Test[]={0};
	WriteEp(0,0,0);//在Status阶段过后才能改变设备地址

	USBADDR|=EpNum;
	USBADDR|=FEN;
	USBCON|=FADDEN;

//	printuf("\nSet_Address:%x\n",USBADDR);
}
void Get_Descriptor(unsigned char DesType,unsigned char nLength)
{
	printu("Get_Descriptor\n");
	if(DesType==0x01) WriteEp(0,18,Device_Descriptor);
	if((DesType==0x02)&&(nLength==0x09)) WriteEp(0,9,Configuration_Descriptor_All);
	if((DesType==0x02)&&(nLength==0xff)) {WriteEp(0,32,Configuration_Descriptor_All);WriteEp(0,2,&Device_Descriptor[4]);}
	if((DesType==0x02)&&(nLength==0x20)) {WriteEp(0,32,Configuration_Descriptor_All);}

//		printu("Get_Descriptor\n");
}
void Get_Configuration()
{
	printu("Get_Configuration\n");
}
void Set_Configuration(unsigned char wValue)
{

	printu("Set_Configuration\n");
	if(wValue == 0) {
	/* put device in unconfigured state */
		 /* Disable all endpoints but EPP0. *///init_unconfig

 		UEPNUM=0x00;//Ep0 as control Endpoint
		UEPCONX=0x80;
	
		UEPNUM=0x01;//Ep1 as Bulk-in Endpoint
		UEPCONX=0x86;
	
		UEPNUM=0x02;//Ep2 as Bulk-out Endpoint
		UEPCONX=0x82;

//		WriteEp(0,&Device_Descriptor[4],2);
		USBCON&=(~CONFG);
		WriteEp(0,0,0);//状态传送阶段之前完成指定操作

	}
	else if(wValue == 1) {
	/* Configure device */
		/* Enable generic/iso endpoints. init_config*/

		UEPNUM=0x00;//Ep0 as control Endpoint
		UEPCONX=0x80;
	
		UEPNUM=0x01;//Ep1 as Bulk-in Endpoint
		UEPCONX=0x86;
	
		UEPNUM=0x02;//Ep2 as Bulk-out Endpoint
		UEPCONX=0x82;


//		WriteEp(0,&Device_Descriptor[4],2);
		USBCON|=CONFG;
		WriteEp(0,0,0);//状态传送阶段之前完成指定操作
//		printuf("After Config,USBADDR:%x  ",USBADDR);
//		printuf("USBCON:%x \n",USBCON);

	}
//	printu("Set_Configuration ");
}
void Get_Interface()
{
	printu("Get_Interface\n");
}
void Set_Interface()
{
	WriteEp(0,0,0);
	printu("Set_Interface\n");
}
void Reserved()
{}

void Ep0Int()
{
	unsigned char i,j;
	unsigned char ControlData[32]={0,};
//	printu("\nEp0 Interrrupted!");
	i=ReadEp(0,ControlData);//读端点0数据
    for(j=0;j<i;j++) printuf("%x ",ControlData[j]);
	j=ControlData[0]&0x60;	//判断是否是USB标准请求
	if((j==0)&&i){			//如果端点0数据长度非零且为标准请求
		switch(ControlData[1]){	//分支处理
				case get_status			:	Get_Status();break;
				case clear_feature 		:	Clear_Feature();break;
	
				case set_feature		:	Set_Feature();break;
	
				case set_address		:	Set_Address(ControlData[2]);break;
				case get_descriptor		:	Get_Descriptor(ControlData[3],ControlData[6]);break;
	
				case get_configuration	:	Get_Configuration();break;
				case set_configuration	:	Set_Configuration(ControlData[2]);break;
				case get_interface		:	Get_Interface();break;
				case set_interface		:	Set_Interface();break;
				default					:	Reserved();break;
			}
	}
	else if(ControlData[0]==0xa1){//非标准USB请求
			WriteEp(0,0,0);
		}
}

void main_txdone()
{
	printu("Bulk_In Ep Intterupt\n");

}
void main_rxdone()
{
	unsigned char i,j;
	printu("Bulk_Out Ep Intterupt\n");
	i=ReadEp(2,Bulk_Out_Buf);
    for(j=0;j<i;j++) printuf("%x ",Bulk_Out_Buf[j]);
	if((bulk_state != BULK_DATA_RECIEVE) &&(Bulk_Out_Buf[0] == 0x55)) InterpretCBW();
	else RecieveData();	
}
void main()
{
	printu("Test Ep0 Intterupt\n");
	AtmelUSBInit(0,2,0);
	EpEnable();
	while(1){
		if(UEPINT&EP0INT) Ep0Int();
		if(UEPINT&EP1INT) main_txdone();
		if(UEPINT&EP2INT) main_rxdone();
		UEPINT=0;
	}
}

⌨️ 快捷键说明

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