Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 430 → Rev 431

/trunk/fat16.c
1091,59 → 1091,66
u8 direntry_exist = 0;
DirEntry_t * dir;
 
// if incomming pointers are useless return immediatly
// incomming pointers are useless?
if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return(direntry_exist);
 
// dir entries can be searched only in filesclusters that have
// a corresponding dir entry with a dir-flag set in its attribute
// or direct within the root directory area
file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED;
// no current directory exist therefore assume searching in the root
if(file->DirectorySector == SECTOR_UNDEFINED)
{
max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector;
// check sector range to be searched
 
// no search area defined?
if(file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED)
{
return(direntry_exist);
}
// within the root directory area we can read sectors sequentially until the end of this area
else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector))
//is search area the root?
else if(Partition.FirstRootDirSector == file->FirstSectorOfFirstCluster)
{
max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector;
max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; // limit to size of root
file->DirectorySector = SECTOR_UNDEFINED; // if we have to search within the root, there is no corresponding directory entry available
}
// within the data clusters we can read sectors sequentially only within the cluster
else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector))
// is search area within the data cluster range?
else if((Partition.FirstDataSector <= file->FirstSectorOfFirstCluster) && (file->FirstSectorOfFirstCluster <= Partition.LastDataSector))
{
max_dir_sector = Partition.SectorsPerCluster; // limit max sectors before next cluster
}
else return (direntry_exist); // bad sector range for directory sector of the file
// if search area is not defined yet, i.e. not in the root directory area
if(file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED)
{
// check if the directory entry of current file is existent and has the dir-flag set
file->SectorInCache = file->DirectorySector; // update the sector number of file cache.
if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
/*
//there must be a corresponding directory entry
if(file->DirectorySector == SECTOR_UNDEFINED)
{
Fat16_Deinit();
return(0);
return (direntry_exist);
}
dir = (DirEntry_t *)file->Cache; // set pointer to directory
switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist
else // check directory entry
{
case SLOT_EMPTY:
case SLOT_DELETED:
// the directrory pointer of this file points to a deleted or not existen directory
// therefore no file or subdirectory exist here
return (direntry_exist);
break;
default: // and is a real directory
if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY)
{ // current file is not a directory therefore no file or subdirectory can be created here
return (direntry_exist);
}
break;
}
file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[file->DirectoryIndex].StartCluster);
file->SectorInCache = file->DirectorySector; // update the sector number of file cache.
if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
{
Fat16_Deinit();
return (direntry_exist);
}
dir = (DirEntry_t *)file->Cache; // set pointer to directory
switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist
{
case SLOT_EMPTY:
case SLOT_DELETED:
// the directrory pointer of this file points to a deleted or not existen directory
// therefore no file or subdirectory exist here
return(direntry_exist);
break;
default: // and is a real directory
if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY)
{ // current file is not a directory therefore no file or subdirectory can be searched here
return(direntry_exist);
}
break;
}
// check that search area matches directory entry
if(file->FirstSectorOfFirstCluster != Fat16ClusterToSector(dir[file->DirectoryIndex].StartCluster))
{
return (direntry_exist);
}
} */
}
else return (direntry_exist); // bad sector range for search area
// update current file data area position to start of first cluster
file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster;
file->SectorOfCurrCluster = 0;
1159,7 → 1166,7
if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector
{
Fat16_Deinit();
return(0);
return(direntry_exist);
}
dir = (DirEntry_t *)file->Cache; // set pointer to directory
// search all directory entries within that sector
1200,7 → 1207,7
{
end_of_directory_not_reached = GetNextCluster(file); // updates File->FirstSectorOfCurrCluster
}
}while((end_of_directory_not_reached) && (!direntry_exist)); // repeat until a next cluster exist an no
}while((end_of_directory_not_reached) && (!direntry_exist)); // repeat until a next cluster exist an no match has been found
return(direntry_exist);
}
 
1223,63 → 1230,65
u8 retvalue = 0;
DirEntry_t *dir;
 
if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return (retvalue);
// It is not checked here that the dir entry that should be created is already existent!
// incomming pointers are useless?
if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return(retvalue);
 
// Dir entries can be created only in file-clusters that have
// the dir-flag set in its attribute or within the root directory
// check sector range to be searched
 
file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED;
// no current directory exist therefore assume creating in the root
if(file->DirectorySector == SECTOR_UNDEFINED)
{
max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
dircluster = 0;
file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector;
// no creation area defined?
if(file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED)
{
return(retvalue);
}
// within the root directory area we can read sectors sequentially until the end of this area
else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector))
//is creation area the root?
else if(Partition.FirstRootDirSector == file->FirstSectorOfFirstCluster)
{
max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
dircluster = 0;
file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector;
max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; // limit to size of root
file->DirectorySector = SECTOR_UNDEFINED; // if we have to search within the root, there is no corresponding directory entry available
}
// within the data clusters we can read sectors sequentially only within the cluster
else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector))
// is creation area within the data cluster range?
else if((Partition.FirstDataSector <= file->FirstSectorOfFirstCluster) && (file->FirstSectorOfFirstCluster <= Partition.LastDataSector))
{
max_dir_sector = Partition.SectorsPerCluster;
file->FirstSectorOfFirstCluster = Partition.CurrentWorkingDirectory;
}
else return (retvalue); // bad sector range for directory sector of the file
// if search area is not defined yet
if(file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED)
{
// check if the directory entry of current file is existent and has the dir-flag set
file->SectorInCache = file->DirectorySector; // update the sector number of file cache.
if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
max_dir_sector = Partition.SectorsPerCluster; // limit max sectors before next cluster
// there must be a corresponding directory entry
if(file->DirectorySector == SECTOR_UNDEFINED)
{
Fat16_Deinit();
return(0);
return (retvalue);
}
dir = (DirEntry_t *)file->Cache; // set pointer to directory
switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist
else // check directory entry
{
case SLOT_EMPTY:
case SLOT_DELETED:
return (retvalue);
break;
default: // and is a real directory
if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY)
{ // current file is not a directory therefore no file or subdirectory can be created here
return (retvalue);
}
break;
file->SectorInCache = file->DirectorySector; // update the sector number of file cache.
if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read in the sector.
{
Fat16_Deinit();
return (retvalue);
}
dir = (DirEntry_t *)file->Cache; // set pointer to directory
switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist
{
case SLOT_EMPTY:
case SLOT_DELETED:
// the directrory pointer of this file points to a deleted or not existen directory
// therefore no file or subdirectory exist here
return(retvalue);
break;
default: // and is a real directory
if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY)
{ // current file is not a directory therefore no file or subdirectory can be created here
return(retvalue);
}
break;
}
// check that search area matches directory entry
if(file->FirstSectorOfFirstCluster != Fat16ClusterToSector(dir[file->DirectoryIndex].StartCluster))
{
return (retvalue);
}
}
dircluster = dir[file->DirectoryIndex].StartCluster;
file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dircluster);
}
else return(retvalue); // bad sector range for search area
 
// if the new direntry is a subdirectory
// if the new directory entry is a subdirectory
if((attrib & ATTR_SUBDIRECTORY) == ATTR_SUBDIRECTORY)
{ // get a free cluster for its content
subdircluster = FindNextFreeCluster(file); // get the next free cluster on the disk and mark it as used.
1301,7 → 1310,7
if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
{
Fat16_Deinit();
return(0);
return(retvalue);
}
 
dir = (DirEntry_t *)file->Cache; // set pointer to directory
1327,7 → 1336,7
if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // write back to card
{
Fat16_Deinit();
return(0);
return(retvalue);
}
file->FirstSectorOfFirstCluster = Fat16ClusterToSector(subdircluster); // Calculate absolute sectorposition of first datacluster.
file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; // Start reading the file with the first sector of the first datacluster.
1345,7 → 1354,7
if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
{
Fat16_Deinit();
return(0);
return(retvalue);
}
dir = (DirEntry_t *)file->Cache;
// create direntry "." to current dir
1364,7 → 1373,7
if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// read in the sector.
{
Fat16_Deinit();
return(0);
return(retvalue);
}
}
retvalue = 1;
1402,17 → 1411,20
u8 retval = 0;
 
if((!Partition.IsValid) || (file == NULL)) return (retval);
 
// try to read the sector containing the directoryentry of the file to be deleted
if(SD_SUCCESS != SDC_GetSector((u32) file->DirectorySector,file->Cache)) return(retval);
// get access to the elements of the directoryentry
DirectoryEntry = (DirEntry_t *)file->Cache;
// delete the directoryentry
DirectoryEntry[file->DirectoryIndex].Name[0] = SLOT_DELETED;
DirectoryEntry[file->DirectoryIndex].Attribute = 0;
DirectoryEntry[file->DirectoryIndex].Size = 0;
// the file has been deleted from the directory, save the modified sector back to the filesystem
if(SD_SUCCESS == SDC_PutSector((u32) file->DirectorySector,file->Cache)) retval = 1;
// valid directory sector?
if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.LastDataSector))
{
// try to read the sector containing the directory entry of the file to be deleted
if(SD_SUCCESS != SDC_GetSector((u32) file->DirectorySector,file->Cache)) return(retval);
// get access to the elements of the directoryentry
DirectoryEntry = (DirEntry_t *)file->Cache;
// delete the directoryentry
DirectoryEntry[file->DirectoryIndex].Name[0] = SLOT_DELETED;
DirectoryEntry[file->DirectoryIndex].Attribute = 0;
DirectoryEntry[file->DirectoryIndex].Size = 0;
// the file has been deleted from the directory, save the modified sector back to the filesystem
if(SD_SUCCESS == SDC_PutSector((u32) file->DirectorySector,file->Cache)) retval = 1;
}
return(retval);
}
 
1436,14 → 1448,14
 
// trace along the filepath
path = (s8*)filename; // start at the beginning of the filename string
file->DirectoryIndex = 0;
file->DirectoryIndex = 0; // start search at first dir entry of cluster
if(path[0] == '/') // if a path begins with a '/' the search starts at RootDirectory with file search
{
file->DirectorySector = Partition.FirstRootDirSector; // start at RootDirectory with file search
file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; // start at RootDirectory with file search
}
else // if a path begins not with a '/' the search starts relative to the CWD
{
file->DirectorySector = Partition.CurrentWorkingDirectory;
file->FirstSectorOfFirstCluster = Partition.CurrentWorkingDirectory;
}
// as long as the file was not found and the remaining path is not empty
while((*path != 0) && !file_exist)
1463,7 → 1475,7
}
if(!DirectoryEntryExist(dirname, af, am, file))
{
return (file_exist); // subdirectory does not exist
return(file_exist); // subdirectory does not exist
}
else
{
1475,12 → 1487,12
}
else // error seperating the subpath
{
return file_exist; // bad subdir format
return(file_exist); // bad subdir format
}
path = subpath;
subpath = 0;
}
return (file_exist);
return(file_exist);
}
 
 
1499,6 → 1511,8
s8 *subpath = 0;
u8 af, am, file_created = 0;
s8 dirname[12];
s8 txt[60];
u8 i=0;
 
// if incomming pointers are useless return immediatly
if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return(0);
1508,11 → 1522,11
 
if(path[0] == '/') // if a path begins with a '/' the search starts at RootDirectory with file search
{
file->DirectorySector = Partition.FirstRootDirSector; // start at RootDirectory with file search
file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; // start at RootDirectory with file search
}
else // if a path begins not with a '/' the search starts relative to the CWD
{
file->DirectorySector = Partition.CurrentWorkingDirectory;
file->FirstSectorOfFirstCluster = Partition.CurrentWorkingDirectory;
}
file->DirectoryIndex = 0;
 
1541,15 → 1555,18
}
else if(*subpath == 0) file_created = 1; // last element of path chain was created
}
sprintf(txt+i, "s:%06d",file->FirstSectorOfFirstCluster);
i+=8;
}
else // error seperating the subpath
{
return file_created; // bad subdir format
return(file_created); // bad subdir format
}
path = subpath;
subpath = NULL;
}
return (file_created);
Debug(txt);
return(file_created);
}
 
 
2163,6 → 2180,20
if(filepath == NULL) return(0);
// correct filepath formatting
Slashing_Path(filepath);
file.FirstSectorOfFirstCluster = SECTOR_UNDEFINED; // First sector of the first cluster of the file.
file.FirstSectorOfCurrCluster = SECTOR_UNDEFINED; // First sector of the cluster which is edited at the moment.
file.FirstSectorOfLastCluster = SECTOR_UNDEFINED; // First sector of the last cluster of the file
file.SectorOfCurrCluster = SECTOR_UNDEFINED; // The sector within the current cluster.
file.ByteOfCurrSector = 0; // The byte location within the current sector.
file.Mode = 0; // Mode of fileoperation (read,write)
file.Size = 0; // The size of the opend file in bytes.
file.Position = 0; // Pointer to a character within the file 0 < fileposition < filesize
file.DirectorySector = SECTOR_UNDEFINED; // the sectorposition where the directoryentry has been made.
file.DirectoryIndex = 0; // The index to the directoryentry within the specified sector.
file.Attribute = 0; // The attribute of the file opened.
file.SectorInCache = SECTOR_UNDEFINED; // The last sector read, which is still in the sector cache.
file.State = FSTATE_USED; // mark it as used
// if file is existent
if(FileExist(filepath, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, &file))
{
2685,6 → 2716,7
// we have modified the result of the findfirst_ operation therefore we have to redo findfirst_ to get detailed infos about the directory entry to be deleted
if(findfirst_(dn, 0xff, &fe))
{
Debug("found");
// try to clear the allocated clusters within the fat
if(DeleteClusterChain(fe.fp.FirstSectorOfFirstCluster))
{
2719,6 → 2751,19
if(dirpath == NULL) return(0);
// correct filepath formatting
Slashing_Path(dirpath);
file.FirstSectorOfFirstCluster = SECTOR_UNDEFINED; // First sector of the first cluster of the file.
file.FirstSectorOfCurrCluster = SECTOR_UNDEFINED; // First sector of the cluster which is edited at the moment.
file.FirstSectorOfLastCluster = SECTOR_UNDEFINED; // First sector of the last cluster of the file
file.SectorOfCurrCluster = SECTOR_UNDEFINED; // The sector within the current cluster.
file.ByteOfCurrSector = 0; // The byte location within the current sector.
file.Mode = 0; // Mode of fileoperation (read,write)
file.Size = 0; // The size of the opend file in bytes.
file.Position = 0; // Pointer to a character within the file 0 < fileposition < filesize
file.DirectorySector = SECTOR_UNDEFINED; // the sectorposition where the directoryentry has been made.
file.DirectoryIndex = 0; // The index to the directoryentry within the specified sector.
file.Attribute = 0; // The attribute of the file opened.
file.SectorInCache = SECTOR_UNDEFINED; // The last sector read, which is still in the sector cache.
file.State = FSTATE_USED; // mark it as used
// if directory is not existent
if(!FileExist(dirpath, ATTR_SUBDIRECTORY, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, &file))
{
/trunk/main.c
458,12 → 458,12
//----------------------------------------------------------------------------------------------------
int main(void)
{
/*
static u32 ftimer =0;
static u8 fstate = 0;
static File_t* f = NULL;
*/
/* Configure the system clocks */
SCU_Config();
/* init VIC (Vectored Interrupt Controller) */
543,27 → 543,32
LED_RED_OFF;
for (;;) // the endless main loop
{
Polling();
Polling();
PollingTimeout = 15;
// ---------------- Logging ---------------------------------------
// ---------------- Logging ---------------------------------------
if(SD_WatchDog)
{
SD_WatchDog = 30000;
if(SDCardInfo.Valid == 1) Logging_Update(); // could be block some time for at max. 2 seconds, therefore move time critical part of the mainloop into the ISR of timer 1
else if(FC.StatusFlags & FC_STATUS_START) SD_LoggingError = 100;
if(!SD_WatchDog) UART1_PutString("\n\rSD-Watchdog - Logging aborted\n\r");
}
/*
{
SD_WatchDog = 30000;
if(SDCardInfo.Valid == 1) Logging_Update(); // could be block some time for at max. 2 seconds, therefore move time critical part of the mainloop into the ISR of timer 1
else if(FC.StatusFlags & FC_STATUS_START) SD_LoggingError = 100;
if(!SD_WatchDog) UART1_PutString("\n\rSD-Watchdog - Logging aborted\n\r");
}
if(CheckDelay(ftimer))
{
 
s8* filename = "test.txt";
static s8 filename[35];
static u8 i = 0;
s8 dbgmsg[40];
 
ftimer = SetDelay(100);
if(FC.Poti[3]>100 && fstate == 0)
{
fstate = 1;
sprintf(filename, "/toast/toasta/toast%02i.txt",i++);
}
else if(FC.Poti[3]<100 && fstate == 2)
{
573,8 → 578,8
switch(fstate)
{
case 1:
sprintf(text,"\r\nStart writing file: %s", filename);
UART1_PutString(text);
sprintf(dbgmsg,"\r\nStart writing file: %s", filename);
Debug(dbgmsg);
f = fopen_(filename, 'a');
if(f== NULL) Fat16_Init();
fstate = 2;
585,8 → 590,8
break;
case 3:
sprintf(text,"\r\nClosing file: %s", filename);
UART1_PutString(text);
sprintf(dbgmsg,"\r\nClosing file: %s", filename);
Debug(dbgmsg);
fclose_(f);
fstate = 0;
break;
595,8 → 600,7
break;
}
}
*/
}
}
 
// DebugOut.Analog[]
/trunk/main.h
9,7 → 9,7
#endif
 
//-----------------------
//#define DEBUG 0
#define DEBUG 0
//-----------------------
 
#define VERSION_MAJOR 0