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

📄 test_new_001.c

📁 51环境下的FAT及FAT32文件系统源码
💻 C
字号:
#include "common.h"
#include "usb.h"
#include "tpbulk.h"
#include "fat.h"

XXGFLAGS bdata bXXGFlags;             	// status flags
UINT8 xdata pUSERBUF[64];       		// user buffer
UINT8 xdata DBUF[SECT_LEN] _at_ 0x0;  	// 512B data buffer, allocated at 0x0
UINT16 xdata FAT[SECT_LEN/2]; 		  	// FAT buffer

//extern FILE_INFO xdata ThisFile;          // file information
extern SYS_INFO_BLOCK DeviceInfo; 
extern XXGPKG usbstack;                   // USB packet variables       
extern TPB_STRUC data TPBulk_Block;

void main(void)
{
	unsigned char i, k;
    UINT16   j;
	pDIR_INFO pDIR;

	UINT32 file_len;
	UINT32 dSectorNum;
	UINT32 currCluster;
	UINT8  ext_match;

	bXXGFlags.SLAVE_ONLINE = FALSE;
	bXXGFlags.SLAVE_FOUND = FALSE;
	bXXGFlags.SLAVE_REMOVED=FALSE;
	
	bXXGFlags.SLAVE_ENUMERATED = FALSE;
	bXXGFlags.SLAVE_IS_ATTACHED = FALSE;
	bXXGFlags.bUartInDone=FALSE;
	bXXGFlags.bToggle=FALSE;
	bXXGFlags.bIN_ISR=FALSE;
  
  	P0= 0x0;
  	for(i=0;i<64;i++)
  	{	
		DBUF[i] = 0;
  	}

    usbInit();

	usbReset();                   // bus reset
 //while(TRUE)        // the good'ol infinite loop
 //{ 
   if(!bXXGFlags.SLAVE_ENUMERATED) 
    {
//      DISABLE_INTERRUPTS; //EA=0
	  
      if(enumUsbDev(1))
      {
        bXXGFlags.SLAVE_ENUMERATED = TRUE;
		P0 = 0x1;
      }
      else 
      {
        bXXGFlags.SLAVE_ENUMERATED = FALSE;
        bXXGFlags.SLAVE_IS_ATTACHED = FALSE;
		P0 = 0x2;
      }
       
    }

   if(bXXGFlags.SLAVE_ENUMERATED)
    {     //bXXGFlags.bMassDevice is valued in EnumUsbDev();

		bXXGFlags.bMassDevice=FALSE; //can only be set by enumeration
		if(EnumMassDev())
		{
        	bXXGFlags.SLAVE_IS_ATTACHED = TRUE;
			P0 = 0x3;
		}
		else
		{
			bXXGFlags.SLAVE_IS_ATTACHED = FALSE;
			P0 = 0x4;

		}
	}

	// let try to open first file
	//  we already have the boot record, now we need
	// the following things:
	// start of FAT
	// start of data
	// let's read the FAT first
	if(!RBC_ReadOneSec(DeviceInfo.FatStartSector, FAT))
	{
		P0 = 0x7;
		DelayMs(1000);
		P0 = 0x0;
	};

    // search for the first MP3 file
	pDIR = (pDIR_INFO) DBUF;
	dSectorNum = DeviceInfo.RootStartSector;
	RBC_ReadOneSec(dSectorNum, DBUF);
	
	file_len = 0;
    i = 0;
/*
    P0 = 0x0; 

    for ( j=0; j<DeviceInfo.BPB_RootEntCnt; j++ )
    {
			
		if((pDIR->name[0] != 0xe5) && (pDIR->attribute & 0x20))// if first char of file name is 0xe5 = erased
		{
			if((pDIR->extension[0] == 0x4d) && (pDIR->extension[1] == 0x50) &&
				(pDIR->extension[2] == 0x33))
			{
			    P0 = 0x1;

				file_len = SwapINT32(pDIR->length);
				// Enable MP3 decoder & clear work memory
				Mp3Ctrl = 0x31;
				// now lets get the whole file
				// some cheating stuff:
				//dSectorNum = DeviceInfo.FirstDataSector;
				//fSectorNum = DeviceInfo.FatStartSector;
				currCluster = WordSwap ( pDIR->startCluster );
		
				while (file_len)
				{	
					dSectorNum = FirstSectorofCluster ( currCluster );
					for ( k=0; k=DeviceInfo.BPB_SecPerClus; k++ )
					{
						if(Alarm & MP3_REQ)
						{	
							bXXGFlags.bIN_ISR=TRUE;
							if(file_len < 512 || currCluster > 0xFFF8)       // last sector
							{	
								RBC_ReadOneSec(dSectorNum, DBUF);
								file_len = 0;
								break;
							}
							else
							{ 
								if(!RBC_ReadOneSec(dSectorNum, DBUF))
								{
									break;
								}
								file_len -= 512;
							}
							// ***important: if using polling, make sure to clear Alarm
							// only after sector is transfered.
							Alarm = MP3_REQ;
							bXXGFlags.bIN_ISR=FALSE;

							dSectorNum++;
						}
					}
					currCluster = GetNextClusterNum ( currCluster );
				}			
            }
		}
	
		DelayMs(1000);
		Mp3Ctrl = 0x0;

		pDIR++;
		i++;
		if (i == 16)
		{
			dSectorNum++;
			pDIR = (pDIR_INFO) DBUF;
			RBC_ReadOneSec(dSectorNum, DBUF);
			i=0; 
		}
	

    }

/*/
/*/
	while (!ext_match)
	{

		if((pDIR->name[0] != 0xe5) && (pDIR->attribute & 0x20))// if first char of file name is 0xe5 = erased
		{
			if((pDIR->extension[0] == 0x4d) && (pDIR->extension[1] == 0x50) &&
				(pDIR->extension[2] == 0x33))
			{
				ext_match = 0x1;
				file_len = SwapINT32(pDIR->length);
			}
		}

		pDIR++;
		i++;
		if (i == 16)
		{
			dSectorNum++;
			pDIR = (pDIR_INFO) DBUF;
			RBC_ReadOneSec(dSectorNum, DBUF);
			i=0;
		}
	}

	// Enable MP3 decoder & clear work memory
	Mp3Ctrl = 0x31;
	// now lets get the whole file
	// some cheating stuff:
	dSectorNum = DeviceInfo.FirstDataSector;

	while (file_len)
	{	
		if(Alarm & MP3_REQ)
		{	
			bXXGFlags.bIN_ISR=TRUE;
			if(file_len < 512)       // last sector
			{	
				RBC_ReadOneSec(dSectorNum, DBUF);
				file_len = 0;
				break;
			}
			else
			{
				if(!RBC_ReadOneSec(dSectorNum, DBUF))
				{
					Scratch = (UINT8) dSectorNum;
					break;
				}
				file_len -= 512;
				dSectorNum++;                      // point to next sector
			}
			// ***important: if using polling, make sure to clear Alarm
			// only after sector is transfered.
			Alarm = MP3_REQ;
			bXXGFlags.bIN_ISR=FALSE;
		}
	}
/*/

	while (TRUE)
	{
		DelayMs(1000);
		P0 = bXXGFlags.SLAVE_IS_ATTACHED? 0x5 : 0x6;
		DelayMs(1000);
		P0 = 0x0;
	}
}
	

⌨️ 快捷键说明

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