Subversion Repositories NaviCtrl

Rev

Rev 360 | Rev 363 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 360 Rev 362
Line 58... Line 58...
58
#include "91x_lib.h"
58
#include "91x_lib.h"
59
#include "timer1.h"
59
#include "timer1.h"
60
#include "fat16.h"
60
#include "fat16.h"
61
#include "sdc.h"
61
#include "sdc.h"
62
#include "uart1.h"
62
#include "uart1.h"
-
 
63
#include "main.h"
Line 63... Line 64...
63
 
64
 
64
//________________________________________________________________________________________________________________________________________
65
//________________________________________________________________________________________________________________________________________
65
// Module name:                 fat16.c
66
// Module name:                 fat16.c
66
// Compiler used:               avr-gcc 3.4.5
67
// Compiler used:               avr-gcc 3.4.5
Line 437... Line 438...
437
 
438
 
438
        // search subpath from beginning of filepath
439
        // search subpath from beginning of filepath
439
        subpath = NULL;
440
        subpath = NULL;
440
        readpointer     = 0;
441
        readpointer     = 0;
441
        if(filepath[0] == '/') readpointer = 1; // ignore first '/'
442
        if(filepath[0] == '/') readpointer = 1; // ignore first '/'
442
        while(subpath == NULL)  // search the filepath until a subpath was found.
443
        while(subpath == NULL && (SD_WatchDog)) // search the filepath until a subpath was found.
443
        {
444
        {
444
                if(((filepath[readpointer] == 0) || (filepath[readpointer] == '/')))    // if '/' found or end of filepath reached
445
                if(((filepath[readpointer] == 0) || (filepath[readpointer] == '/')))    // if '/' found or end of filepath reached
445
                {
446
                {
446
                        subpath = (s8*)&filepath[readpointer];                          // store the position of the first "/" found after the beginning of the filenpath
447
                        subpath = (s8*)&filepath[readpointer];                          // store the position of the first "/" found after the beginning of the filenpath
Line 475... Line 476...
475
 
476
 
476
        writepointer = 0;
477
        writepointer = 0;
477
        // start seperating the dirname from the filepath.
478
        // start seperating the dirname from the filepath.
478
        readpointer = 0;
479
        readpointer = 0;
479
        if(filepath[0] == '/') readpointer = 1; // ignore first '/'
480
        if(filepath[0] == '/') readpointer = 1; // ignore first '/'
480
        while( &filepath[readpointer] < subpath)
481
        while( &filepath[readpointer] < subpath && (SD_WatchDog))
481
        {
482
        {
482
                if(writepointer >= 11) return(NULL);            // dirname to long
483
                if(writepointer >= 11) return(NULL);            // dirname to long
483
                if(filepath[readpointer] == '.')                        // seperating dirname and extension.
484
                if(filepath[readpointer] == '.')                        // seperating dirname and extension.
484
                {
485
                {
Line 823... Line 824...
823
                                fat_entry = FAT16_ENTRIES_PER_SECTOR;                                   // terminate the search for a free cluster in this sector.
824
                                fat_entry = FAT16_ENTRIES_PER_SECTOR;                                   // terminate the search for a free cluster in this sector.
824
                        }
825
                        }
825
                }
826
                }
826
                fat_sector++;                                                                                                   // continue the search in next fat sector
827
                fat_sector++;                                                                                                   // continue the search in next fat sector
827
        // repeat until the end of the fat is  reached and no free cluster has been found so far
828
        // repeat until the end of the fat is  reached and no free cluster has been found so far
828
        }while((fat_sector < Partition.SectorsPerFat) && (!free_cluster));
829
        }while((fat_sector < Partition.SectorsPerFat) && (!free_cluster) && (SD_WatchDog));
829
        return(free_cluster);
830
        return(free_cluster);
830
}
831
}
Line 831... Line 832...
831
 
832
 
Line 865... Line 866...
865
                file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster;
866
                file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster;
866
                file->SectorOfCurrCluster       = 0;
867
                file->SectorOfCurrCluster       = 0;
867
                file->ByteOfCurrSector          = 0;
868
                file->ByteOfCurrSector          = 0;
868
                file->Position                          = 0;
869
                file->Position                          = 0;
869
                if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return(retvalue);
870
                if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return(retvalue);
870
                while(file->Position < fposition)       // repeat until the current position is less than target
871
                while(file->Position < fposition && (SD_WatchDog))      // repeat until the current position is less than target
871
                {
872
                {
872
                        file->Position++;                               // increment file position
873
                        file->Position++;                               // increment file position
873
                        file->ByteOfCurrSector++;               // next byte in current sector
874
                        file->ByteOfCurrSector++;               // next byte in current sector
874
                        if(file->ByteOfCurrSector >= BYTES_PER_SECTOR)
875
                        if(file->ByteOfCurrSector >= BYTES_PER_SECTOR)
875
                        {
876
                        {
Line 955... Line 956...
955
                                Fat16_Deinit();
956
                                Fat16_Deinit();
956
                                return(0);
957
                                return(0);
957
                        }
958
                        }
958
                }
959
                }
959
        }
960
        }
960
        while(repeat);
961
        while(repeat && (SD_WatchDog));
Line 961... Line 962...
961
 
962
 
962
        return 1;
963
        return 1;
Line 1162... Line 1163...
1162
                                                dir_entry = DIRENTRIES_PER_SECTOR;      // stop for-loop
1163
                                                dir_entry = DIRENTRIES_PER_SECTOR;      // stop for-loop
1163
                                } // end of first byte of name check
1164
                                } // end of first byte of name check
1164
                        }
1165
                        }
1165
                        dir_sector++; // search next sector
1166
                        dir_sector++; // search next sector
1166
                // stop if we reached the end of the cluster or the end of the root dir
1167
                // stop if we reached the end of the cluster or the end of the root dir
1167
                }while((dir_sector < max_dir_sector) && (!direntry_exist));
1168
                }while((dir_sector < max_dir_sector) && (!direntry_exist) && (SD_WatchDog));
Line 1168... Line 1169...
1168
 
1169
 
1169
                // if we are seaching in the data area and the file not found in this cluster so take next cluster.
1170
                // if we are seaching in the data area and the file not found in this cluster so take next cluster.
1170
                if(!direntry_exist && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster))
1171
                if(!direntry_exist && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster))
1171
                {
1172
                {
1172
                        end_of_directory_not_reached = GetNextCluster(file);  // updates File->FirstSectorOfCurrCluster
1173
                        end_of_directory_not_reached = GetNextCluster(file);  // updates File->FirstSectorOfCurrCluster
1173
                }
1174
                }
1174
        }while((end_of_directory_not_reached) && (!direntry_exist)); // repeat until a next cluster exist an no
1175
        }while((end_of_directory_not_reached) && (!direntry_exist) && (SD_WatchDog)); // repeat until a next cluster exist an no
1175
        return(direntry_exist);
1176
        return(direntry_exist);
Line 1176... Line 1177...
1176
}
1177
}
Line 1339... Line 1340...
1339
                                        dir_entry = DIRENTRIES_PER_SECTOR;      // stop for-loop
1340
                                        dir_entry = DIRENTRIES_PER_SECTOR;      // stop for-loop
1340
                                }
1341
                                }
1341
                        }
1342
                        }
1342
                        dir_sector++; // search next sector
1343
                        dir_sector++; // search next sector
1343
                // stop if we reached the end of the cluster or the end of the root dir
1344
                // stop if we reached the end of the cluster or the end of the root dir
1344
                }while((dir_sector < max_dir_sector) && (!retvalue));
1345
                }while((dir_sector < max_dir_sector) && (!retvalue) && (SD_WatchDog));
Line 1345... Line 1346...
1345
 
1346
 
1346
                // if we are seaching in the data area and the file not found in this cluster so take next cluster.
1347
                // if we are seaching in the data area and the file not found in this cluster so take next cluster.
1347
                if(!retvalue && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster))
1348
                if(!retvalue && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster))
1348
                {
1349
                {
1349
                        end_of_directory_not_reached = GetNextCluster(file);  // updates File->FirstSectorOfCurrCluster
1350
                        end_of_directory_not_reached = GetNextCluster(file);  // updates File->FirstSectorOfCurrCluster
1350
                }
1351
                }
1351
        }while((end_of_directory_not_reached) && (!retvalue));
1352
        }while((end_of_directory_not_reached) && (!retvalue) && (SD_WatchDog));
1352
        // Perhaps we are at the end of the last cluster of a directory file and have no free direntry found.
1353
        // Perhaps we are at the end of the last cluster of a directory file and have no free direntry found.
1353
        // Then we would need to add a cluster to that file and create the new direntry there.
1354
        // Then we would need to add a cluster to that file and create the new direntry there.
1354
        // This code is not implemented yet, because its occurs only if more that 32*32=1024 direntries are
1355
        // This code is not implemented yet, because its occurs only if more that 32*32=1024 direntries are
Line 1387... Line 1388...
1387
                if(Partition.CurrentWorkingDirectory == Partition.FirstRootDirSector) file->DirectorySector = 0;
1388
                if(Partition.CurrentWorkingDirectory == Partition.FirstRootDirSector) file->DirectorySector = 0;
1388
                /* otherwise we are working in an subdirectory */
1389
                /* otherwise we are working in an subdirectory */
1389
                else file->DirectorySector = Partition.CurrentWorkingDirectory;
1390
                else file->DirectorySector = Partition.CurrentWorkingDirectory;
1390
        }
1391
        }
1391
        // as long as the file was not found and the remaining path is not empty
1392
        // as long as the file was not found and the remaining path is not empty
1392
        while((*path != 0) && !file_exist)
1393
        while((*path != 0) && !file_exist && (SD_WatchDog))
1393
        {       // separate dirname and subpath from filepath string
1394
        {       // separate dirname and subpath from filepath string
1394
                subpath = SeperateDirName(path, dirname);
1395
                subpath = SeperateDirName(path, dirname);
1395
                if(subpath != NULL)
1396
                if(subpath != NULL)
1396
                {
1397
                {
1397
                        if(*subpath == 0)
1398
                        if(*subpath == 0)
Line 1449... Line 1450...
1449
        // trace along the filepath
1450
        // trace along the filepath
1450
        path = (s8*)filename;                                                                   // start a the beginning of the filename string
1451
        path = (s8*)filename;                                                                   // start a the beginning of the filename string
1451
        file->DirectorySector = 0;                                                              // start at RootDirectory with file search
1452
        file->DirectorySector = 0;                                                              // start at RootDirectory with file search
1452
        file->DirectoryIndex = 0;
1453
        file->DirectoryIndex = 0;
1453
        // as long as the file was not created and the remaining file path is not empty
1454
        // as long as the file was not created and the remaining file path is not empty
1454
        while((*path != 0) && !file_created)
1455
        while((*path != 0) && !file_created && (SD_WatchDog))
1455
        {   // separate dirname and subpath from filepath string
1456
        {   // separate dirname and subpath from filepath string
1456
                subpath = SeperateDirName(path, dirname);
1457
                subpath = SeperateDirName(path, dirname);
1457
                if(subpath != NULL)
1458
                if(subpath != NULL)
1458
                {
1459
                {
1459
                        if(*subpath == 0)
1460
                        if(*subpath == 0)
Line 1522... Line 1523...
1522
        file->Attribute                                 = 0;            // the attribute of the file opened.
1523
        file->Attribute                                 = 0;            // the attribute of the file opened.
Line 1523... Line 1524...
1523
 
1524
 
1524
        // bring the path into the correct syntax 
1525
        // bring the path into the correct syntax 
1525
        cptr = filename;
1526
        cptr = filename;
1526
        // search the whole string 
1527
        // search the whole string 
1527
        while(*cptr != 0)
1528
        while(*cptr != 0 && (SD_WatchDog))
1528
        {
1529
        {
1529
                // replace all '\' by '/'
1530
                // replace all '\' by '/'
1530
                if(*cptr == '\\') *cptr = '/';
1531
                if(*cptr == '\\') *cptr = '/';
1531
                cptr++;
1532
                cptr++;
Line 1846... Line 1847...
1846
 
1847
 
Line 1847... Line 1848...
1847
        if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0);
1848
        if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0);
Line 1848... Line 1849...
1848
 
1849
 
1849
        pbuff = (u8 *) buffer;                                                                                  // cast the void pointer to an u8 *
1850
        pbuff = (u8 *) buffer;                                                                                  // cast the void pointer to an u8 *
1850
 
1851
 
1851
        while((object_cnt < count) && success)
1852
        while((object_cnt < count) && success && (SD_WatchDog))
1852
        {
1853
        {
1853
                object_size = size;
1854
                object_size = size;
1854
                while((size > 0) && success)
1855
                while((size > 0) && success && (SD_WatchDog))
1855
                {
1856
                {
1856
                        c = fgetc_(file);
1857
                        c = fgetc_(file);
Line 1889... Line 1890...
1889
 
1890
 
1890
        if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0);
1891
        if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0);
1891
        if(file->Mode == 'r') return (0); // opened read only
1892
        if(file->Mode == 'r') return (0); // opened read only
Line 1892... Line 1893...
1892
        pbuff = (u8 *) buffer;                                                                                                          // cast the void pointer to an u8 *
1893
        pbuff = (u8 *) buffer;                                                                                                          // cast the void pointer to an u8 *
1893
 
1894
 
1894
        while((object_cnt < count) && success)
1895
        while((object_cnt < count) && success && (SD_WatchDog))
1895
        {
1896
        {
1896
                object_size = size;
1897
                object_size = size;
1897
                while((size > 0) && success)
1898
                while((size > 0) && success && (SD_WatchDog))
1898
                {
1899
                {
1899
                        c = fputc_(*pbuff, file);                                                                               // write a byte from the buffer to the opened file.
1900
                        c = fputc_(*pbuff, file);                                                                               // write a byte from the buffer to the opened file.
1900
                        if(c != EOF)
1901
                        if(c != EOF)
Line 1926... Line 1927...
1926
        u8 i=0;
1927
        u8 i=0;
1927
        s16 c = 0;
1928
        s16 c = 0;
Line 1928... Line 1929...
1928
 
1929
 
1929
        if((!Partition.IsValid) || (file == NULL) || (string == NULL)) return(EOF);
1930
        if((!Partition.IsValid) || (file == NULL) || (string == NULL)) return(EOF);
1930
        if(file->Mode == 'r') return(EOF);
1931
        if(file->Mode == 'r') return(EOF);
1931
        while((string[i] != 0)&& (c != EOF))
1932
        while((string[i] != 0)&& (c != EOF) && (SD_WatchDog))
1932
        {
1933
        {
1933
                c = fputc_(string[i], file);
1934
                c = fputc_(string[i], file);
1934
                i++;
1935
                i++;
1935
        }
1936
        }
Line 1949... Line 1950...
1949
        s16 c = 0, bytecount;
1950
        s16 c = 0, bytecount;
Line 1950... Line 1951...
1950
 
1951
 
1951
        if((!Partition.IsValid) || (file == NULL) || (string == NULL) || (length < 1)) return (0);
1952
        if((!Partition.IsValid) || (file == NULL) || (string == NULL) || (length < 1)) return (0);
1952
        bytecount = length;
1953
        bytecount = length;
1953
        pbuff = string;                                                         // set write pointer to start of string
1954
        pbuff = string;                                                         // set write pointer to start of string
1954
        while(bytecount > 1)                                            // read the length-1 characters from the file to the string.
1955
        while(bytecount > 1 && (SD_WatchDog))                                           // read the length-1 characters from the file to the string.
1955
        {
1956
        {
1956
                c = fgetc_(file);                                               // read a character from the opened file.
1957
                c = fgetc_(file);                                               // read a character from the opened file.
1957
                switch(c)
1958
                switch(c)
1958
                {
1959
                {
Line 2082... Line 2083...
2082
                                        pVolumeLabel =  Partition.VolumeLabel;
2083
                                        pVolumeLabel =  Partition.VolumeLabel;
2083
                        } // end of first byte of name check
2084
                        } // end of first byte of name check
2084
                }
2085
                }
2085
                dir_sector++; // search next sector
2086
                dir_sector++; // search next sector
2086
        // stop if we reached the end of the cluster or the end of the root dir
2087
        // stop if we reached the end of the cluster or the end of the root dir
2087
        }while((dir_sector < max_dir_sector) && (!pVolumeLabel));
2088
        }while((dir_sector < max_dir_sector) && (!pVolumeLabel) && (SD_WatchDog));
Line 2088... Line 2089...
2088
 
2089
 
2089
        UnlockFilePointer(file);
2090
        UnlockFilePointer(file);
2090
        return(pVolumeLabel);
2091
        return(pVolumeLabel);
Line 2141... Line 2142...
2141
        do
2142
        do
2142
        {                                                                                                                                                               // search the next 16 rootentries in this sector of the roordirectory.          
2143
        {                                                                                                                                                               // search the next 16 rootentries in this sector of the roordirectory.          
2143
                SDC_GetSector(((u32) file.FirstSectorOfCurrCluster + (u32)file.SectorOfCurrCluster), file.Cache);                                               // Read the Rootdirectory.
2144
                SDC_GetSector(((u32) file.FirstSectorOfCurrCluster + (u32)file.SectorOfCurrCluster), file.Cache);                                               // Read the Rootdirectory.
2144
                DirectoryEntry = (DirEntry_t *)file.Cache;
2145
                DirectoryEntry = (DirEntry_t *)file.Cache;
Line 2145... Line 2146...
2145
 
2146
 
2146
                while((!retvalue)&&(index<16))
2147
                while((!retvalue)&&(index<16) && (SD_WatchDog))
2147
                {
2148
                {
2148
                        i=0;                   
2149
                        i=0;                   
2149
                        if((u8) DirectoryEntry[index].Name[0] != 0xe5)                                                          // ignore deleted items.
2150
                        if((u8) DirectoryEntry[index].Name[0] != 0xe5)                                                          // ignore deleted items.
2150
                        {
2151
                        {
Line 2205... Line 2206...
2205
                        {
2206
                        {
2206
                                end_of_directory_not_reached = GetNextCluster(&file);
2207
                                end_of_directory_not_reached = GetNextCluster(&file);
2207
                        }
2208
                        }
2208
                }
2209
                }
2209
        }
2210
        }
2210
        while((end_of_directory_not_reached) && (!retvalue));
2211
        while((end_of_directory_not_reached) && (!retvalue)  && (SD_WatchDog));
Line 2211... Line 2212...
2211
 
2212
 
2212
        return(retvalue);      
2213
        return(retvalue);      
Line 2312... Line 2313...
2312
u8 GetDirCount(u8 *dirpath)
2313
u8 GetDirCount(u8 *dirpath)
2313
{
2314
{
2314
   u8 i=0;
2315
   u8 i=0;
2315
   u8 cnt=0;
2316
   u8 cnt=0;
Line 2316... Line 2317...
2316
 
2317
 
2317
   while(dirpath[i] != 0)
2318
   while(dirpath[i] != 0  && (SD_WatchDog))
2318
   {
2319
   {
2319
      if(dirpath[i]=='/')
2320
      if(dirpath[i]=='/')
2320
      {  
2321
      {  
2321
         if(dirpath[i+1]!=0) cnt++;                                            // ignore last'/'
2322
         if(dirpath[i+1]!=0) cnt++;                                            // ignore last'/'
Line 2342... Line 2343...
2342
   u8 *retvalue = NULL;
2343
   u8 *retvalue = NULL;
Line 2343... Line 2344...
2343
       
2344
       
2344
   /* if the first character of the path is an '/' we go to the next character */
2345
   /* if the first character of the path is an '/' we go to the next character */
2345
   if(*cptr == '/') cptr++;
2346
   if(*cptr == '/') cptr++;
2346
   /* search end of path or subdirectory*/
2347
   /* search end of path or subdirectory*/
2347
   while((*cptr != 0) && (*cptr != '/'))
2348
   while((*cptr != 0) && (*cptr != '/') && (SD_WatchDog))
2348
   {
2349
   {
2349
      *dptr = *cptr;
2350
      *dptr = *cptr;
2350
       dptr++;
2351
       dptr++;
2351
       cptr++;
2352
       cptr++;
Line 2414... Line 2415...
2414
void RemoveLastDirFromPath(void)
2415
void RemoveLastDirFromPath(void)
2415
{
2416
{
2416
        /* a pointer to the beginning of the absolute path to the cwd */
2417
        /* a pointer to the beginning of the absolute path to the cwd */
2417
        s8 * cptr = Partition.PathToCwd;
2418
        s8 * cptr = Partition.PathToCwd;
2418
        /* lets find the end of the path to the cwd */
2419
        /* lets find the end of the path to the cwd */
2419
        while(*cptr != 0) cptr++;
2420
        while(*cptr != 0  && (SD_WatchDog)) cptr++;
2420
        /* if the path is terminated with an '/' */
2421
        /* if the path is terminated with an '/' */
2421
        if((*(cptr-1)) == '/') *(cptr-1)=0;    
2422
        if((*(cptr-1)) == '/') *(cptr-1)=0;    
2422
        /* now lets find the beginning of the last directorientry */
2423
        /* now lets find the beginning of the last directorientry */
2423
        while((*cptr != '/') && cptr > Partition.PathToCwd) cptr--;
2424
        while((*cptr != '/'  && (SD_WatchDog)) && cptr > Partition.PathToCwd) cptr--;
2424
        /* is there one subdirectory left within the path? */
2425
        /* is there one subdirectory left within the path? */
2425
        if(cptr > Partition.PathToCwd)
2426
        if(cptr > Partition.PathToCwd)
2426
        {
2427
        {
2427
                /* we delete the direntry by terminating the path with 0 */
2428
                /* we delete the direntry by terminating the path with 0 */
2428
                *cptr = 0;
2429
                *cptr = 0;
Line 2467... Line 2468...
2467
        s8     *cptr;
2468
        s8     *cptr;
Line 2468... Line 2469...
2468
 
2469
 
2469
        /* bring the path into the correct syntax */
2470
        /* bring the path into the correct syntax */
2470
        cptr = path;
2471
        cptr = path;
2471
        /* search the whole string */
2472
        /* search the whole string */
2472
        while(*cptr != 0)
2473
        while(*cptr != 0  && (SD_WatchDog))
2473
        {
2474
        {
2474
                if(*cptr == '\\') *cptr = '/';
2475
                if(*cptr == '\\') *cptr = '/';
2475
                cptr++;
2476
                cptr++;
2476
        }
2477
        }
Line 2516... Line 2517...
2516
                        /* we found the folder specified by the foldername */
2517
                        /* we found the folder specified by the foldername */
2517
                        retvalue = 1;
2518
                        retvalue = 1;
2518
                }
2519
                }
2519
        }
2520
        }
2520
        /* do this until all subdirectories have been found or a subdirectory is missing */
2521
        /* do this until all subdirectories have been found or a subdirectory is missing */
2521
        while(dircount && retvalue);
2522
        while(dircount && retvalue  && (SD_WatchDog));
Line 2522... Line 2523...
2522
 
2523
 
2523
        /* if we could not change to the specified directory we restore the actual path */
2524
        /* if we could not change to the specified directory we restore the actual path */
2524
        if(!retvalue)
2525
        if(!retvalue)
2525
        {
2526
        {