AVR单片机系统开发经典实例部分源程序

源代码在线查看: ch375.i

软件大小: 1054 K
上传用户: dongjunxi126
关键词: AVR 单片机 系统开发
下载地址: 免注册下载 普通下载 VIP

相关代码

				#line 1 "C:\AVR\OK\ch375.c"
				#line 1 "C:\AVR\OK/ch375.h"
				
				
				unsigned int WordSwap(unsigned int input);
				unsigned int LSwapINT16(unsigned short dData1,unsigned short dData2);
				unsigned long LSwapINT32(unsigned long dData1,unsigned long dData2,unsigned long dData3,unsigned long dData4);
				unsigned char ch375_init( unsigned char mode);
				unsigned char set_usb_mode( unsigned char mode );
				unsigned char get_ver(void);
				void usb_send_data(unsigned char *pbuff,unsigned int len);
				void m_nop(unsigned char m);
				unsigned char EnumMassDev(void);
				unsigned char InitDisk(void);
				unsigned char  ReadSector(unsigned long iLbaStart, unsigned char iSectorCount,unsigned char *oDataBuffer);
				unsigned char  WriteSector(unsigned long iLbaStart, unsigned char iSectorCount,unsigned char *oDataBuffer);
				extern unsigned char diskbuff[512*2];
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				#line 2 "C:\AVR\OK\ch375.c"
				
				unsigned char usb_rec_buff[64];
				unsigned char usb_rec_buflen;
				unsigned char diskbuff[512*2];
				
				
				void m_nop(unsigned char m)
				{
				  while(m--)
				  _NOP();
				  _NOP();
				}
				
				unsigned char set_usb_mode( unsigned char mode )
				{
								   unsigned char i;
								   unsigned char RD_Data;
				
				(*(volatile unsigned char *)0xa001)= 0x15;
								   nop();
								   nop();
								   nop();
								   nop();
				(*(volatile unsigned char *)0xa000)=mode;
				                   nop();
								   for( i=100; i!=0; i-- )
								   		{
										RD_Data=(*(volatile unsigned char *)0xa000);
										if( RD_Data == 0x51 )
										return 1;
										}
										return 0;
				}
				
				unsigned char ch375_init(unsigned char mode)
				{
					 unsigned char i, k;
					 unsigned char RD_Data;
					 EIMSK|=0x10;
				
					for( k=100; k!=0; k-- )
					   {
				(*(volatile unsigned char *)0xa001)= 0x06 ;
						nop();
						nop();
						nop();
						nop();
						nop();
				
				(*(volatile unsigned char *)0xa000)=0x0f;
				
						 nop();
						i=(*(volatile unsigned char *)0xa000);
					    if ( 0xf0!= i)
					    {
						    for ( i=5; i!=0; i-- )
				(*(volatile unsigned char *)0xa001)= 0x05;
							 delay_ms(50);
					    }
						else
				    	  break;
					   }
					  if( k==0 )
					   return 0;
				
				
				
				
				
				
				
					return set_usb_mode(mode);
				}
				
				unsigned char get_ver(void)
				{  unsigned char ver;
				(*(volatile unsigned char *)0xa001)=0x01;
					ver=(*(volatile unsigned char *)0xa000);
				    return ver;
				}
				
				
				
				void _usb_send(unsigned char *pbuff,unsigned char len)
				{
				     unsigned char i,status;
				(*(volatile unsigned char *)0xa001)= 0x2B;
					 nop();
					 nop();
					 nop();
				(*(volatile unsigned char *)0xa000)=len;
				
					 nop();
				     for(i=0;i					 {
				(*(volatile unsigned char *)0xa000)=pbuff[i];
						nop();
					 }
					while(status!= 0x0A)
					{
				(*(volatile unsigned char *)0xa001)= 0x22;
					nop();
					nop();
					nop();
				    status=(*(volatile unsigned char *)0xa000);
					nop();
					nop();
					nop();
					}
				}
				void usb_send_data(unsigned char *pbuff,unsigned int len)
				{
				     unsigned char i,status,r,n;
				     EIMSK&=~0x10;
					  r=len/64;
					  n=len%64;
					  for(i=0;i					  _usb_send((pbuff+i*64),64);
					  _usb_send((pbuff+i*64),n);
				
				(*(volatile unsigned char *)0xa001)= 0x23;
					 nop();
				     EIMSK|=0x10;
				
				}
				
				
				
				
				unsigned char waitint(void)
				{
				  while(PINE&0x10);
				(*(volatile unsigned char *)0xa001)= 0x22;
				  m_nop(10);
				  return(*(volatile unsigned char *)0xa000);
				
				}
				unsigned char InitDisk(void)
				{
				   unsigned char status;
				(*(volatile unsigned char *)0xa001)= 0x22;
				   m_nop(10);
				   status=(*(volatile unsigned char *)0xa000);
				   if (status== 0x16) return status;
				(*(volatile unsigned char *)0xa001)= 0x51;
				   status=waitint();
				   if (status!= 0x14) return status;
				(*(volatile unsigned char *)0xa001)= 0x53;
				   status=waitint();
				   if (status!= 0x14)
				   {
				      delay_ms(250);
				(*(volatile unsigned char *)0xa001)= 0x53;
				      status=waitint();
				   }
				    if (status!= 0x14) return status;
					return 1;
				}
				
				unsigned char  ReadSector(unsigned long iLbaStart, unsigned char iSectorCount,unsigned char *oDataBuffer)
				{
				  unsigned int  mBlockCount;
				  unsigned char  c;
				(*(volatile unsigned char *)0xa001)= 0x54;
				  m_nop(10);
				(*(volatile unsigned char *)0xa000)=(unsigned char)iLbaStart;
				  m_nop(2);
				(*(volatile unsigned char *)0xa000)=(unsigned char)(iLbaStart >> 8);
				  m_nop(2);
				(*(volatile unsigned char *)0xa000)=(unsigned char)(iLbaStart>> 16);
				  m_nop(2);
				(*(volatile unsigned char *)0xa000)=(unsigned char)(iLbaStart>> 24);
				  m_nop(2);
				(*(volatile unsigned char *)0xa000)=iSectorCount ;
				  for ( mBlockCount = iSectorCount * 8; mBlockCount != 0; mBlockCount -- )
				  {
				       c = waitint();
				     if ( c == 0x1D )
				    	 {
				(*(volatile unsigned char *)0xa001)= 0x28;
					     m_nop(10);
				         c =(*(volatile unsigned char *)0xa000);
					     m_nop(2);
				         while ( c -- )
				     	  {
					     *oDataBuffer++ =(*(volatile unsigned char *)0xa000);
					      m_nop(2);
					      }
				(*(volatile unsigned char *)0xa001)= 0x55;
				       }
				    else break;
				  }
				  if ( mBlockCount == 0 )
				  {
				    c = waitint();
				    if ( c== 0x14 ) return( 1 );
				  }
				  return( c );
				}
				
				unsigned char  WriteSector(unsigned long iLbaStart, unsigned char iSectorCount,unsigned char *oDataBuffer)
				{
				  unsigned int  mBlockCount;
				  unsigned char  c;
				(*(volatile unsigned char *)0xa001)= 0x56;
				  m_nop(10);
				(*(volatile unsigned char *)0xa000)=(unsigned char)iLbaStart;
				  m_nop(2);
				(*(volatile unsigned char *)0xa000)=(unsigned char)(iLbaStart >> 8);
				  m_nop(2);
				(*(volatile unsigned char *)0xa000)=(unsigned char)(iLbaStart>> 16);
				  m_nop(2);
				(*(volatile unsigned char *)0xa000)=(unsigned char)(iLbaStart>> 24);
				  m_nop(2);
				(*(volatile unsigned char *)0xa000) =iSectorCount;
				  for ( mBlockCount = iSectorCount * 8; mBlockCount != 0; mBlockCount -- )
				  {
				       c = waitint();
				     if ( c == 0x1E )
				    	 {
				(*(volatile unsigned char *)0xa001)= 0x2B;
					     m_nop(10);
						 c=64;
				(*(volatile unsigned char *)0xa000) =c;
					     m_nop(2);
				         while ( c -- )
				     	  {
				(*(volatile unsigned char *)0xa000)= *oDataBuffer++ ;
					      m_nop(2);
					      }
				(*(volatile unsigned char *)0xa001)= 0x57;
				       }
				    else break;
				  }
				  if ( mBlockCount == 0 )
				  {
				    c = waitint();
				    if ( c== 0x14 ) return( 1 );
				  }
				  return( c );
				}
				
				unsigned int WordSwap(unsigned int input)
				{
					return(((input&0x00FF)8));
				}
				
				
				
				unsigned int LSwapINT16(unsigned short dData1,unsigned short dData2)
				{
				    unsigned int  dData;
				    dData = ((dData2					return dData;
				}
				
				unsigned long LSwapINT32(unsigned long dData1,unsigned long dData2,unsigned long dData3,unsigned long dData4)
				{
				    unsigned long  dData;
				    dData = ((dData4					return dData;
				}
				unsigned char EnumMassDev(void)
				{   unsigned char c;
					unsigned int ReservedSectorsNum;
				
				
				#line 280 "C:\AVR\OK\ch375.c"
				
				
					DeviceInfo.BPB_BytesPerSec=512;
				
				
				#line 290 "C:\AVR\OK\ch375.c"
				
					c=ReadSector(0x0,1,diskbuff);
					if(c!=1) return c;
				
					if(diskbuff[0]==0xeb||diskbuff[0]==0xe9)
						{
						DeviceInfo.StartSector=0;
						}
					else
						{
						DeviceInfo.StartSector=LSwapINT32(diskbuff[454],diskbuff[455],diskbuff[456],diskbuff[457]);
						c=ReadSector(DeviceInfo.StartSector,1,diskbuff);
					    if(c!=1) return c;
						}
				
				
				
					DeviceInfo.BPB_BytesPerSec=LSwapINT16(diskbuff[11],diskbuff[12]);
					DeviceInfo.BPB_SecPerClus=diskbuff[13];
					ReservedSectorsNum=LSwapINT16(diskbuff[14],diskbuff[15]);
					DeviceInfo.BPB_NumFATs=diskbuff[16];
				
					if(diskbuff[82]=='F'&&diskbuff[83]=='A'&&diskbuff[84]=='T'&&diskbuff[85]=='3'&&diskbuff[86]=='2')
						{
						DeviceInfo.BPB_TotSec32=LSwapINT32(diskbuff[32],diskbuff[33],diskbuff[34],diskbuff[35]);
						DeviceInfo.BPB_FATSz32=LSwapINT32(diskbuff[36],diskbuff[37],diskbuff[38],diskbuff[39]);
						DeviceInfo.RootStartCluster=LSwapINT32(diskbuff[44],diskbuff[45],diskbuff[46],diskbuff[47]);
						DeviceInfo.FatStartSector=DeviceInfo.StartSector+ReservedSectorsNum;
						DeviceInfo.FirstDataSector=DeviceInfo.FatStartSector+DeviceInfo.BPB_NumFATs*DeviceInfo.BPB_FATSz32;
						DeviceInfo.TotCluster=(DeviceInfo.BPB_TotSec32-DeviceInfo.FirstDataSector+1)/DeviceInfo.BPB_SecPerClus+1;
				
						DeviceInfo.FAT=1;
						}
					else
						{
						DeviceInfo.BPB_RootEntCnt=LSwapINT16(diskbuff[17],diskbuff[18]);
						DeviceInfo.BPB_RootEntCnt=(DeviceInfo.BPB_RootEntCnt)*32/DeviceInfo.BPB_BytesPerSec;
						DeviceInfo.BPB_TotSec16=LSwapINT16(diskbuff[19],diskbuff[20]);
						DeviceInfo.BPB_FATSz16=LSwapINT16(diskbuff[22],diskbuff[23]);
						DeviceInfo.FatStartSector=DeviceInfo.StartSector+ReservedSectorsNum;
						DeviceInfo.RootStartSector=DeviceInfo.StartSector+DeviceInfo.BPB_NumFATs*DeviceInfo.BPB_FATSz16+ReservedSectorsNum;
						DeviceInfo.FirstDataSector=DeviceInfo.FatStartSector+DeviceInfo.BPB_NumFATs*DeviceInfo.BPB_FATSz16+DeviceInfo.BPB_RootEntCnt;
						DeviceInfo.FAT=0;
						}
				
				
				
					return 1;
				}
				
				
				#pragma interrupt_handler usb_int:iv_INT4
				void usb_int(void)
				{
				    unsigned char status,len,i;
				(*(volatile unsigned char *)0xa001)= 0x22;
					 nop();
				     nop();
					 nop();
					 nop();
					 nop();
					 nop();
				    status=(*(volatile unsigned char *)0xa000);
				
				    switch(status)
					{case 0x02 :
				(*(volatile unsigned char *)0xa001)= 0x28;
						  nop();
						  nop();
						  nop();
						  nop();
						  nop();
						  nop();
						  usb_rec_buflen=(*(volatile unsigned char *)0xa000);
						  nop();
				
				
						  if (usb_rec_buflen!=0)
						  {
						     for(i=0;i							 usb_rec_buff[i]=(*(volatile unsigned char *)0xa000);
							 nop();
				
						  }
						  break;
				
				
				
				    case 0x15 :
					     EIMSK&=~0x10;
						 delay_ms(250);
				
				
						 return;
				     default :
				(*(volatile unsigned char *)0xa001)= 0x23;
						  nop();
						  nop();
						  nop();
				
					}
				
				}
							

相关资源