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

📄 test2.c

📁 51环境下的FAT及FAT32文件系统源码
💻 C
字号:
#include "common.h"
#include "usb.h"
#include "tpbulk.h"
#include "fat.h"
#include "fat32.h"
#define FAT32_ACCESS
void playMp3();	
void usbDebug(unsigned char nBytes, unsigned char *pBytes);

XXGFLAGS bdata bXXGFlags;             	// status flags
UINT8 xdata pUSERBUF[64];       		// user buffer
UINT8 xdata DBUF[SECT_LEN] _at_ 0x0;  	// 512B data buffer, allocated at 0x0
UINT8 xdata RDIRBUF[SECT_LEN];          // 512 byte buffer for root directory
DIR_IDX dirIdx;
pDIR_INFO pDIR;

//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)
{
    UINT8 i;
	UINT16 j;
	UINT32 nSector;

	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;
  
  	P1= 0x0; 
  	for(i=0;i<64;i++)
  	{	
		DBUF[i] = 0;
  	}

    usbInit();
					 
	usbReset();                   // bus reset
 
    if(!bXXGFlags.SLAVE_ENUMERATED) 
    {
      DISABLE_INTERRUPTS; //EA=0
	  
      if(enumUsbDev(1))
      {
        bXXGFlags.SLAVE_ENUMERATED = TRUE;
      }
      else 
      {
        bXXGFlags.SLAVE_ENUMERATED = FALSE;
        bXXGFlags.SLAVE_IS_ATTACHED = FALSE;
      }    
    }

    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;
		} 
		else
		{
			bXXGFlags.SLAVE_IS_ATTACHED = FALSE;
		}
	}
	
	//Start fat16 file system
	if (DeviceInfo.bFatType == 2)
	{
		dirIdx.sector = 0;
		dirIdx.offset = 0;

		RBC_ReadOneSec(DeviceInfo.RootStartSector, RDIRBUF);		
		pDIR = (pDIR_INFO) RDIRBUF;          // 16 Byte file information

		usbDebug(4, &DeviceInfo.RootStartSector);
		usbDebug(2, pDIR);
		//
		for (j=0;j<DeviceInfo.BPB_RootEntCnt;j++)   // loop for number of root dir entries
		{
	
			usbDebug(2, &j);
			usbDebug(8, pDIR->name);

			if ((pDIR->name[0] != 0xe5) && (pDIR->name[0] != 0x0) && (pDIR->attribute & 0x20))// if first char of file name is 0xe5 = erased
			{	
		    	// check extension = mp3, if OK, play song
				usbDebug(3, pDIR->extension);
				if((pDIR->extension[0] == 0x4d) && (pDIR->extension[1] == 0x50) &&
					(pDIR->extension[2] == 0x33))
				{
					playMp3();
				}
			}

			if (dirIdx.offset == 15) 
			{
				dirIdx.sector++;
				nSector = DeviceInfo.RootStartSector + (UINT32) dirIdx.sector;
				usbDebug(4, &nSector); 

				RBC_ReadOneSec(nSector, RDIRBUF);

				dirIdx.offset = 0;
				pDIR = (pDIR_INFO) RDIRBUF;
			}
			else 
			{
				dirIdx.offset++;
				usbDebug(1, &dirIdx.offset);
				pDIR++;
			}
		
		}
	}
	
}

void playMp3()
{
	UINT8    k;
	UINT16   currCluster;
    UINT16   nextCluster;
	
	UINT32 file_len;
	UINT32 dSectorNum;
	UINT8 usTemp;

	file_len = SwapINT32( pDIR->length );
	currCluster = WordSwap( pDIR->startCluster );
    usTemp = Mp3Ctrl;

	Playmode = 0x10;
    Playmode = 0x00;
	
    DelayUs ( 100 );
	// Enable MP3 decoder & clear work memory
	Mp3Ctrl = 0x31;
		
	while (file_len)
	{	
		dSectorNum  = FirstSectorofCluster ( currCluster );
		nextCluster = GetNextClusterNum ( currCluster );
                
		for ( k=0; k<DeviceInfo.BPB_SecPerClus;  )
		{
			if(Alarm & MP3_REQ)
			{	
				bXXGFlags.bIN_ISR=TRUE;
				if(file_len < (51200) )       // last sector
				{	
//					RBC_ReadOneSec(dSectorNum, DBUF);
					file_len = 0;
					break;
				}
				else
				{ 
					if(!RBC_ReadOneSec(dSectorNum, DBUF))
					{
						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;
				k++;
			}
		}
		currCluster = nextCluster;
	}
					
	DelayMs(500);
	Mp3Ctrl = usTemp;

}

void usbDebug(unsigned char nBytes, unsigned char *pBytes)
{
	unsigned char i;

	// send out 8 byte data on the USB
	BufSel = MCU2USB;                      //src=CPU,dest=USB
	TxPktlen = nBytes; 
	BufRst = 0x01;                    // reset write pointer
	BufRst = 0x00; 

	for (i=0;i<nBytes;i++)
	{
    	BufAddr = *pBytes;
		pBytes++;
	}
	BufRst = 0x03;                    // flip buffer state for bus read
	BufRst = 0x00; 
	TxAddr = 0x5;
	TxCtrl = WRITE0;
	DelayMs(1);
	TxAddr = 0x1;
}

⌨️ 快捷键说明

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