?? fat32_buffer.c
字號:
#include "fat_include.h"
//-----------------------------------------------------------------------------
// FAT Buffer Code
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// FAT Buffer structure
//-----------------------------------------------------------------------------
FATBuffer_t 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);
s3c44b0x_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;
s3c44b0x_Printf("\r\nDEBUG: FAT Buffer DUMP");
for (i = 0; i <bufferlength; i++)
{
s3c44b0x_Printf("\r\nBuffer Entry %d",i);
s3c44b0x_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;
// 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 + -