Subversion Repositories NaviCtrl

Rev

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

Rev 380 Rev 383
Line 60... Line 60...
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"
63
#include "main.h"
Line -... Line 64...
-
 
64
 
64
 
65
 
65
//________________________________________________________________________________________________________________________________________
66
//________________________________________________________________________________________________________________________________________
66
// Module name:                 fat16.c
67
// Module name:                 fat16.c
67
// Compiler used:               avr-gcc 3.4.5
68
// Compiler used:               avr-gcc 3.4.5
68
// Last Modifikation:   20.03.2010
69
// Last Modifikation:   20.03.2010
Line 981... Line 982...
981
u16 AppendCluster(File_t *file)
982
u16 AppendCluster(File_t *file)
982
{
983
{
983
        u16 last_cluster, new_cluster = CLUSTER_UNDEFINED;
984
        u16 last_cluster, new_cluster = CLUSTER_UNDEFINED;
984
        u32 fat_byte_offset, sector, byte;
985
        u32 fat_byte_offset, sector, byte;
985
        Fat16Entry_t * fat;
986
        Fat16Entry_t * fat;
986
//      s8 text[64];
-
 
Line 987... Line 987...
987
 
987
 
Line 988... Line 988...
988
        if((!Partition.IsValid) || (file == NULL)) return(new_cluster);
988
        if((!Partition.IsValid) || (file == NULL)) return(new_cluster);
989
 
989
 
990
        new_cluster = FindNextFreeCluster(file);        // the next free cluster found on the disk.
990
        new_cluster = FindNextFreeCluster(file);        // the next free cluster found on the disk.
991
        if(new_cluster != CLUSTER_UNDEFINED)
-
 
992
        {       // A free cluster was found and can be added to the end of the file.            
-
 
993
                // is there at least one cluster appended to the file?
-
 
994
                if(file->FirstSectorOfLastCluster == CLUSTER_UNDEFINED)
991
        if(new_cluster != CLUSTER_UNDEFINED)
995
                {
-
 
996
                        fseek_(file, 0, SEEK_END);                                                                                                      // jump to the end of the file
-
 
997
                        // remember the first sector of the last cluster
992
        {       // A free cluster was found and can be added to the end of the file.
998
                        file->FirstSectorOfLastCluster = file->FirstSectorOfCurrCluster;
-
 
999
                        last_cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster);            // determine current file cluster
-
 
1000
                }
-
 
1001
                else
-
 
1002
                {
-
 
1003
                        last_cluster = SectorToFat16Cluster(file->FirstSectorOfLastCluster);            // determine current file cluster
-
 
1004
                }
993
                fseek_(file, 0, SEEK_END);                                                                                                      // jump to the end of the file
1005
 
994
                last_cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster);            // determine current file cluster
1006
                if(last_cluster != CLUSTER_UNDEFINED)
995
                if(last_cluster != CLUSTER_UNDEFINED)
1007
                {
996
                {
1008
                        // update FAT entry of last cluster
997
                        // update FAT entry of last cluster
Line 1024... Line 1013...
1024
                        if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))               // save the modified sector to the FAT.
1013
                        if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))               // save the modified sector to the FAT.
1025
                        {
1014
                        {
1026
                                Fat16_Deinit();
1015
                                Fat16_Deinit();
1027
                                return(0);
1016
                                return(0);
1028
                        }
1017
                        }
1029
                        // now the new cluster appended to the fat is the last cluster
-
 
1030
                        file->FirstSectorOfLastCluster = Fat16ClusterToSector(new_cluster);
-
 
1031
                }
1018
                }
1032
                else // last cluster of the file is undefined
1019
                else // last cluster of the file is undefined
1033
                {   // then the new cluster must be the first one of the file
1020
                {   // then the new cluster must be the first one of the file
1034
                    // and its cluster number must be set in the direntry
1021
                    // and its cluster number must be set in the direntry
1035
                        DirEntry_t * dir;
1022
                        DirEntry_t * dir;
Line 1054... Line 1041...
1054
                                Fat16_Deinit();
1041
                                Fat16_Deinit();
1055
                                return(CLUSTER_UNDEFINED);
1042
                                return(CLUSTER_UNDEFINED);
1056
                        }
1043
                        }
1057
                        // update file info     
1044
                        // update file info     
1058
                        file->FirstSectorOfFirstCluster = Fat16ClusterToSector(new_cluster);
1045
                        file->FirstSectorOfFirstCluster = Fat16ClusterToSector(new_cluster);
1059
                        file->FirstSectorOfLastCluster = file->FirstSectorOfFirstCluster;
-
 
1060
                        file->Size = 0;
1046
                        file->Size = 0;
1061
                        file->Position = 0;
1047
                        file->Position = 0;
1062
                }
1048
                }
1063
                // update file pointes
1049
                // update file pointes
1064
                file->FirstSectorOfCurrCluster = Fat16ClusterToSector(new_cluster);
1050
                file->FirstSectorOfCurrCluster = Fat16ClusterToSector(new_cluster);
Line 1816... Line 1802...
1816
                        if(file->Size == file->Position) file->Size++;          // a character has been written to the file so the size is incremented only when the character has been added at the end of the file.
1802
                        if(file->Size == file->Position) file->Size++;          // a character has been written to the file so the size is incremented only when the character has been added at the end of the file.
1817
                        file->Position++;                                                                       // the actual positon within the file.
1803
                        file->Position++;                                                                       // the actual positon within the file.
1818
                        file->ByteOfCurrSector++;                                                       // goto next byte in sector
1804
                        file->ByteOfCurrSector++;                                                       // goto next byte in sector
1819
                        if(file->ByteOfCurrSector >= BYTES_PER_SECTOR)          // if the end of this sector is reached yet
1805
                        if(file->ByteOfCurrSector >= BYTES_PER_SECTOR)          // if the end of this sector is reached yet
1820
                        {       // save the sector to the sd-card
1806
                        {       // save the sector to the sd-card
-
 
1807
       
1821
                                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))
1808
                                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))
1822
                                {
1809
                                {
1823
                                        Fat16_Deinit();
1810
                                        Fat16_Deinit();
1824
                                        return(EOF);
1811
                                        return(EOF);
1825
                                }
1812
                                }