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

📄 fat32_buffer.c

📁 一个FAT32文件系统的源代码,可用于嵌入式系统,并能够直接在linux下直接验证.
💻 C
字号:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//					    FAT32 File IO Library Linux Test Version
//									  V1.0L
// 	  								Rob Riglar
//								Copyright 2003,2004 
//
//   					  Email: admin@robs-projects.com
//
//			   Compiled and tested on Redhat 'Shrike' with GNU gcc
//-----------------------------------------------------------------------------
//
// This file is part of FAT32 File IO Library.
//
// FAT32 File IO Library is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// FAT32 File IO Library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FAT32 File IO Library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../IDE/IDE_Access.h"
#include "FAT32_Base.h"
#include "FAT32_Buffer.h"

//-----------------------------------------------------------------------------
//								FAT Buffer Code
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// FAT Buffer structure
//-----------------------------------------------------------------------------
struct 
{
	int first;						// The head of the circular buffer
	int last;						// The tail of buffer
	UI32 buffer[bufferlength+1];	// The data storing element
	int count;						// The data counter
	int clustersused;				// Counts how many times a cluster has been 
									// removed from the buffer
	UI32 lastcluster;				// Last cluster read
	byte EOFdetected;
} FATBuffer;

//-----------------------------------------------------------------------------
// ReFillFATBuffer
//				  Reads the last cluster in the FAT buffer and then traverses
//				  from there adding new clusters to the buffer until full or
//				  until the STA013 Decoder requires attention.
//-----------------------------------------------------------------------------
void FATBuffer_ReFillFATBuffer(void)
{
	UI32 cluster=0;
	int bufferstate=1;
	int fillcount = 0;
	
	// Record sector in sector buffer (containing mp3 data in play mode)

	// Find cluster on top of the list
	cluster = FATBuffer.buffer[FATBuffer.last-1];

	// Fill until full or STA013 is requiring data
	do
	{
	fillcount++;
		// Find the next cluster in the chain
		cluster = FAT32_FindNextCluster(cluster);

		// If not last cluster add to buffer
		bufferstate = FATBuffer_AddtoTail(cluster);
		if (cluster==0xffffffff) 
		{
		   FATBuffer.EOFdetected = 1;
		   bufferstate = 0;
		}
		
		//debugcounter++;
	}
	while ((bufferstate) && (fillcount<4));

	//printf("\r\nMP3_DREQ was %d", MP3_DREQ_STATE);
	printf("\r\n%d", FATBuffer.count);

	// Rebuffer sector origonally in sector buffer for quick access by play routine
}

//-----------------------------------------------------------------------------
// FillFATBuffer
//			    Fill the FAT buffer with cluster chains until
//			    it is full.
//-----------------------------------------------------------------------------
void FATBuffer_FillFATBuffer(UI32 cluster)
{
	 int bufferstate=1;

	 // Setup buffer initials
	 FATBuffer_BufferInit();
	 
	 // Add initial item to buffer
	 FATBuffer_AddtoTail(cluster);
 
	 // Fill buffer state
	 do 
	 {
		 // Find the next cluster in the chain
		 cluster = FAT32_FindNextCluster(cluster);

		 // If not last cluster add to buffer
		 bufferstate = FATBuffer_AddtoTail(cluster);
		 if (cluster==0xffffffff) bufferstate = 0;
	 }
	 while (bufferstate);
}

//-----------------------------------------------------------------------------
// DEBUG_PrintFATBuffer: Dump the contents of the FAT Buffer to console
//-----------------------------------------------------------------------------
void DEBUG_PrintFATBuffer(void)
{
	int i;

	printf("\r\nDEBUG: FAT Buffer DUMP");

	for (i = 0; i <bufferlength; i++)
		{
 		printf("\r\nBuffer Entry %d",i);
		printf(" is |0x%lx|",FATBuffer.buffer[i]);
		}
}

//-----------------------------------------------------------------------------
// Init Buffer - Set all configurables to 0 for buffer initialisation
//-----------------------------------------------------------------------------
void FATBuffer_BufferInit(void)
{
   	 FATBuffer.first = 0;
  	 FATBuffer.last = 0;
	 FATBuffer.count=0;
	 FATBuffer.clustersused = 0;
	 FATBuffer.EOFdetected = 0;
}

//-----------------------------------------------------------------------------
// AddtoTail
//			Function to add a data element to the end of the circular queue.
//			Returns 0 if buffer is full else returns 1 for data added.
//-----------------------------------------------------------------------------
int FATBuffer_AddtoTail(UI32 data)
{

	// Check if buffer full
	if (FATBuffer.count==bufferlength) return 0;

	// Check if buffer tail is overflowing, if so put back to 0
	if (FATBuffer.last>=bufferlength) FATBuffer.last = 0;

	// Add data to buffer
	FATBuffer.buffer[FATBuffer.last++] = data;

	// Increment Counter
	FATBuffer.count++; 

	// Increment clusters taken from stack
	FATBuffer.clustersused++; 
	
	// Return Success
	return 1;
}


//-----------------------------------------------------------------------------
// RemovefromHead
//				 Remove and return 1 data entry from head of the buffer.
//				 A check should be made to see if buffer is empty before 
//			     reading.
//-----------------------------------------------------------------------------
UI32 FATBuffer_RemovefromHead(void)
{
	UI32 data = 0;

	// Check if buffer empty
	if (FATBuffer.count==0) return 0;

	// Check if buffer head is overflowing, if so put back to 0
	if (FATBuffer.first>=bufferlength) FATBuffer.first = 0;

	// Remove data from buffer
	data = FATBuffer.buffer[FATBuffer.first++];

	// Increment Counter
	FATBuffer.count--; 
	return data;
}

//-----------------------------------------------------------------------------
// FAT32_SectorReaderBUFFERED:
//							  Access a sector using offset and startcluster
//							  using a FATBuffer. Firstuse is set to 1 when
//							  first sector read occurs (initialisation purposes)
//-----------------------------------------------------------------------------
int FATBuffer_SectorReaderBUFFERED(UI32 Startcluster, UI32 offset, int firstuse)
{
 	  UI32 SectortoRead = 0;
	  UI32 ClustertoRead = 0;
	  UI32 Cluster = 0;
	  int i;
	  
	  // Initialise, doesnt work if first read isnt offset 0	  
	  if (firstuse)
	  {
	   	 FATBuffer_FillFATBuffer(Startcluster);
         // Set start of cluster chain to initial value
		 FATBuffer.lastcluster = Startcluster;
		 FATBuffer.clustersused = 0;
	  }
	  else if ((FATBuffer.count<25) && (!FATBuffer.EOFdetected))
	  {
  	   if (FATBuffer.lastcluster==0xffffffff) return 1;
	   FATBuffer_ReFillFATBuffer();
	  }
	  
	  // Find parameters (cluster and secotr)
	  ClustertoRead = offset / FAT32.SectorsPerCluster;	  
	  SectortoRead = offset - (ClustertoRead*FAT32.SectorsPerCluster);
 
 	  // Check if a new sector needs to be retrieved from the buffer
	  // or if there are still sectors to be read within cluster
 	  if ((ClustertoRead!=FATBuffer.clustersused)&&(SectortoRead==0))
	  	 		Cluster = FATBuffer_RemovefromHead();
	  else
	  	   	  	Cluster = FATBuffer.lastcluster;
	  
	  // If cluster to read is the last in the chain, stop read
	  if (Cluster==0xffffffff) return 1;
	  
	  // Else read sector and return 0 
	  IDE_BufferSector(FAT32_LBAofCluster(Cluster)+SectortoRead);
	  
	  // Set the last cluster read to the one just used
	  FATBuffer.lastcluster = Cluster;
	  
	  return 0;
}

⌨️ 快捷键说明

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