/****************finding_BootSector*************************/
void finding_BootSector(unsigned char *buffer)
{
unsigned long index = 0;
while(1)
{
sd_read_sector(index,buffer);
if((buffer[0] == 0xEB && buffer[2] == 0x90) || (buffer[0] == 0xE9))
{
BootSectorAddr = index;
break;
}
index++;
}
}
/**********************************************************/
/**************************read_fat_info********************/
void read_fat_info(unsigned char *buffer)
{
finding_BootSector(buffer);
sd_read_sector(BootSectorAddr, buffer);
struct fat_info *info = (struct fat_info*) &buffer[11];
BytesPerSector = (info->bytesPerSector);
SectorPerCluster = (info->sectorPerCluster);
ReservedSector = (info->reservedSectorCount);
RootDirAddr = (BootSectorAddr + (info->reservedSectorCount) + (info->numberofFATs*info->FATsize_F32));
}
/*********************************************************/
/**********************FirstClusterSector********************/
unsigned long FirstClusterSector(unsigned long cluster)
{
return (RootDirAddr + ((cluster-2)*SectorPerCluster));
}
/********************************************************/
/**************************FileList************************/
unsigned long NextCluster(unsigned long cluster, unsigned char *buffer)
{
unsigned long fat_in_sec;
unsigned long sector_in_fat;
unsigned long offset_in_fat;
struct next {unsigned long nextcluster;};
/*******************fat_in_sector*********************/
fat_in_sec = cluster*4;
/********************sector_in_fat********************/
if(fat_in_sec % BytesPerSector)
{
sector_in_fat = (fat_in_sec / BytesPerSector);
sector_in_fat++;
}
else
{
sector_in_fat = fat_in_sec / BytesPerSector;
}
/************************offset_inf_fat***************/
offset_in_fat = fat_in_sec - (BytesPerSector*(sector_in_fat-1));
sd_read_sector(((BootSectorAddr + ReservedSector + sector_in_fat) - 1),buffer);
struct next *ptr = (struct next*)&buffer[offset_in_fat];
if((ptr->nextcluster) >= 0x0FFFFFF8) return 1;
return (ptr->nextcluster);
}
/******************************************************/
/************************FindFile**************************/
unsigned long FindFileNr(unsigned int FileNr, unsigned char *buffer)
{
unsigned long index, index2;
unsigned long FirstCluster;
unsigned int index3=0;
NextClus=RootDirCluster;
while(1)
{
for(index2=0; index2
index3++;
if(index3 == FileNr)
{
FirstCluster |=(dir->firstClusterHI);
FirstCluster<<=16; FirstCluster |=(dir->firstClusterLO);
return FirstCluster;
}
}
}
NextClus=(NextCluster(NextClus,buffer));
if(NextClus == 1) return 0;
RootDirAddr = (FirstClusterSector(NextClus));
}
read_fat_info(buffer);
}
/******************************************************/
/**************************ReadFile*********************/
void ReadFile(unsigned int FileNr, unsigned char *buffer)
{
unsigned long NextClus;
unsigned int index;
unsigned char index1;
unsigned long FirstSector;
NextClus = FindFileNr(FileNr, buffer);
while(1)
{
FirstSector = FirstClusterSector(NextClus);
for(index1=0; index1<SectorPerCluster; index1++)
{
sd_read_sector((FirstSector+index1),buffer);
for(index=0;index<BytesPerSector;index++)
{
USART_Transmit(buffer[index]);
}
}
NextClus=NextCluster(NextClus,buffer);
}
}
/*******************************************************/
Brak komentarzy:
Prześlij komentarz