Rev 362 | Rev 368 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 362 | Rev 363 | ||
---|---|---|---|
Line 714... | Line 714... | ||
714 | { |
714 | { |
715 | file->SectorInCache = file->FirstSectorOfCurrCluster + i; |
715 | file->SectorInCache = file->FirstSectorOfCurrCluster + i; |
716 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
716 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
717 | { |
717 | { |
718 | Fat16_Deinit(); |
718 | Fat16_Deinit(); |
719 | retvalue = 0; |
- | |
720 | return(retvalue); |
719 | return(0); |
721 | } |
720 | } |
722 | } |
721 | } |
723 | return(retvalue); |
722 | return(retvalue); |
724 | } |
723 | } |
Line 754... | Line 753... | ||
754 | { |
753 | { |
755 | file->SectorInCache = sector; // update sector stored in buffer |
754 | file->SectorInCache = sector; // update sector stored in buffer |
756 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector from sd-card |
755 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector from sd-card |
757 | { |
756 | { |
758 | Fat16_Deinit(); |
757 | Fat16_Deinit(); |
759 | return (cluster); |
758 | return (CLUSTER_UNDEFINED); |
760 | } |
759 | } |
761 | } |
760 | } |
762 | // read the next cluster from cache |
761 | // read the next cluster from cache |
763 | fat = (Fat16Entry_t *)(&(file->Cache[byte])); |
762 | fat = (Fat16Entry_t *)(&(file->Cache[byte])); |
764 | cluster = fat->NextCluster; |
763 | cluster = fat->NextCluster; |
Line 803... | Line 802... | ||
803 | curr_sector = Partition.FirstFatSector + fat_sector; // calculate sector to read |
802 | curr_sector = Partition.FirstFatSector + fat_sector; // calculate sector to read |
804 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
803 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
805 | if( SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector of fat from sd-card. |
804 | if( SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector of fat from sd-card. |
806 | { |
805 | { |
807 | Fat16_Deinit(); |
806 | Fat16_Deinit(); |
808 | return(free_cluster); |
807 | return(CLUSTER_UNDEFINED); |
809 | } |
808 | } |
Line 810... | Line 809... | ||
810 | 809 | ||
Line 811... | Line 810... | ||
811 | fat = (Fat16Entry_t *)file->Cache; // set fat pointer to file cache |
810 | fat = (Fat16Entry_t *)file->Cache; // set fat pointer to file cache |
Line 816... | Line 815... | ||
816 | { |
815 | { |
817 | fat[fat_entry].NextCluster = FAT16_CLUSTER_LAST_MAX; // mark this fat-entry as used |
816 | fat[fat_entry].NextCluster = FAT16_CLUSTER_LAST_MAX; // mark this fat-entry as used |
818 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // and save the sector at the sd-card. |
817 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // and save the sector at the sd-card. |
819 | { |
818 | { |
820 | Fat16_Deinit(); |
819 | Fat16_Deinit(); |
821 | return(free_cluster); |
820 | return(CLUSTER_UNDEFINED); |
822 | } |
821 | } |
823 | free_cluster = (u16)(fat_sector * FAT16_ENTRIES_PER_SECTOR + (u32)fat_entry); |
822 | free_cluster = (u16)(fat_sector * FAT16_ENTRIES_PER_SECTOR + (u32)fat_entry); |
824 | fat_entry = FAT16_ENTRIES_PER_SECTOR; // terminate the search for a free cluster in this sector. |
823 | fat_entry = FAT16_ENTRIES_PER_SECTOR; // terminate the search for a free cluster in this sector. |
825 | } |
824 | } |
826 | } |
825 | } |
Line 1093... | Line 1092... | ||
1093 | // check if the directory entry of current file is existent and has the dir-flag set |
1092 | // check if the directory entry of current file is existent and has the dir-flag set |
1094 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1093 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1095 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1094 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1096 | { |
1095 | { |
1097 | Fat16_Deinit(); |
1096 | Fat16_Deinit(); |
1098 | return(direntry_exist); |
1097 | return(0); |
1099 | } |
1098 | } |
1100 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1099 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1101 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
1100 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
1102 | { |
1101 | { |
1103 | case SLOT_EMPTY: |
1102 | case SLOT_EMPTY: |
Line 1129... | Line 1128... | ||
1129 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
1128 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
1130 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
1129 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
1131 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector |
1130 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector |
1132 | { |
1131 | { |
1133 | Fat16_Deinit(); |
1132 | Fat16_Deinit(); |
1134 | return(direntry_exist); |
1133 | return(0); |
1135 | } |
1134 | } |
1136 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1135 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1137 | // search all directory entries within that sector |
1136 | // search all directory entries within that sector |
1138 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
1137 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
1139 | { // check for existing dir entry |
1138 | { // check for existing dir entry |
Line 1226... | Line 1225... | ||
1226 | // check if the directory entry of current file is existent and has the dir-flag set |
1225 | // check if the directory entry of current file is existent and has the dir-flag set |
1227 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1226 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1228 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1227 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1229 | { |
1228 | { |
1230 | Fat16_Deinit(); |
1229 | Fat16_Deinit(); |
1231 | return(retvalue); |
1230 | return(0); |
1232 | } |
1231 | } |
1233 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1232 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1234 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
1233 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
1235 | { |
1234 | { |
1236 | case SLOT_EMPTY: |
1235 | case SLOT_EMPTY: |
Line 1268... | Line 1267... | ||
1268 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
1267 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
1269 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
1268 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
1270 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1269 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1271 | { |
1270 | { |
1272 | Fat16_Deinit(); |
1271 | Fat16_Deinit(); |
1273 | return(retvalue); |
1272 | return(0); |
1274 | } |
1273 | } |
Line 1275... | Line 1274... | ||
1275 | 1274 | ||
1276 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1275 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1277 | // search all directory entries of a sector |
1276 | // search all directory entries of a sector |
Line 1294... | Line 1293... | ||
1294 | dir[dir_entry].StartCluster = subdircluster; // copy the location of the first datacluster to the directoryentry. |
1293 | dir[dir_entry].StartCluster = subdircluster; // copy the location of the first datacluster to the directoryentry. |
1295 | dir[dir_entry].Size = 0; // the new createted file has no content yet. |
1294 | dir[dir_entry].Size = 0; // the new createted file has no content yet. |
1296 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // write back to card |
1295 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // write back to card |
1297 | { |
1296 | { |
1298 | Fat16_Deinit(); |
1297 | Fat16_Deinit(); |
1299 | return(retvalue); |
1298 | return(0); |
1300 | } |
1299 | } |
1301 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(subdircluster); // Calculate absolute sectorposition of first datacluster. |
1300 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(subdircluster); // Calculate absolute sectorposition of first datacluster. |
1302 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; // Start reading the file with the first sector of the first datacluster. |
1301 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; // Start reading the file with the first sector of the first datacluster. |
1303 | file->SectorOfCurrCluster = 0; // reset sector of cureen cluster |
1302 | file->SectorOfCurrCluster = 0; // reset sector of cureen cluster |
1304 | file->ByteOfCurrSector = 0; // reset the byte location within the current sector |
1303 | file->ByteOfCurrSector = 0; // reset the byte location within the current sector |
Line 1312... | Line 1311... | ||
1312 | ClearCurrCluster(file); // fill cluster with zeros |
1311 | ClearCurrCluster(file); // fill cluster with zeros |
1313 | file->SectorInCache = file->FirstSectorOfFirstCluster; |
1312 | file->SectorInCache = file->FirstSectorOfFirstCluster; |
1314 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1313 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1315 | { |
1314 | { |
1316 | Fat16_Deinit(); |
1315 | Fat16_Deinit(); |
1317 | return(retvalue); |
1316 | return(0); |
1318 | } |
1317 | } |
1319 | dir = (DirEntry_t *)file->Cache; |
1318 | dir = (DirEntry_t *)file->Cache; |
1320 | // create direntry "." to current dir |
1319 | // create direntry "." to current dir |
1321 | dir[0].Name[0] = 0x2E; |
1320 | dir[0].Name[0] = 0x2E; |
1322 | for(i = 1; i < 11; i++) dir[0].Name[i] = ' '; |
1321 | for(i = 1; i < 11; i++) dir[0].Name[i] = ' '; |
Line 1331... | Line 1330... | ||
1331 | dir[1].StartCluster = dircluster; |
1330 | dir[1].StartCluster = dircluster; |
1332 | dir[1].Size = 0; |
1331 | dir[1].Size = 0; |
1333 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// read in the sector. |
1332 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// read in the sector. |
1334 | { |
1333 | { |
1335 | Fat16_Deinit(); |
1334 | Fat16_Deinit(); |
1336 | return(retvalue); |
1335 | return(0); |
1337 | } |
1336 | } |
1338 | } |
1337 | } |
1339 | retvalue = 1; |
1338 | retvalue = 1; |
1340 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
1339 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
1341 | } |
1340 | } |
Line 1720... | Line 1719... | ||
1720 | { |
1719 | { |
1721 | file->SectorInCache = curr_sector; |
1720 | file->SectorInCache = curr_sector; |
1722 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache,file->Cache)) |
1721 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache,file->Cache)) |
1723 | { |
1722 | { |
1724 | Fat16_Deinit(); |
1723 | Fat16_Deinit(); |
1725 | return(c); |
1724 | return(EOF); |
1726 | } |
1725 | } |
1727 | } |
1726 | } |
1728 | c = (s16) file->Cache[file->ByteOfCurrSector]; |
1727 | c = (s16) file->Cache[file->ByteOfCurrSector]; |
1729 | file->Position++; // increment file position |
1728 | file->Position++; // increment file position |
1730 | file->ByteOfCurrSector++; // goto next byte in sector |
1729 | file->ByteOfCurrSector++; // goto next byte in sector |
Line 2051... | Line 2050... | ||
2051 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
2050 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
2052 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
2051 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
2053 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector |
2052 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector |
2054 | { |
2053 | { |
2055 | Fat16_Deinit(); |
2054 | Fat16_Deinit(); |
2056 | return(pVolumeLabel); |
2055 | return(NULL); |
2057 | } |
2056 | } |
2058 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
2057 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
2059 | // search all directory entries within that sector |
2058 | // search all directory entries within that sector |
2060 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
2059 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
2061 | { // check for existing dir entry |
2060 | { // check for existing dir entry |
Line 2139... | Line 2138... | ||
2139 | max_dir_sector = Partition.SectorsPerCluster; // limit max secters before next cluster |
2138 | max_dir_sector = Partition.SectorsPerCluster; // limit max secters before next cluster |
2140 | } |
2139 | } |
Line 2141... | Line 2140... | ||
2141 | 2140 | ||
2142 | do |
2141 | do |
2143 | { // search the next 16 rootentries in this sector of the roordirectory. |
2142 | { // search the next 16 rootentries in this sector of the roordirectory. |
- | 2143 | if(SD_SUCCESS != SDC_GetSector(((u32) file.FirstSectorOfCurrCluster + (u32)file.SectorOfCurrCluster), file.Cache)); // Read the Rootdirectory. |
|
- | 2144 | { |
|
- | 2145 | Fat16_Deinit(); |
|
- | 2146 | return(0); |
|
- | 2147 | } |
|
2144 | SDC_GetSector(((u32) file.FirstSectorOfCurrCluster + (u32)file.SectorOfCurrCluster), file.Cache); // Read the Rootdirectory. |
2148 | |
Line 2145... | Line 2149... | ||
2145 | DirectoryEntry = (DirEntry_t *)file.Cache; |
2149 | DirectoryEntry = (DirEntry_t *)file.Cache; |
2146 | 2150 | ||
2147 | while((!retvalue)&&(index<16) && (SD_WatchDog)) |
2151 | while((!retvalue)&&(index<16) && (SD_WatchDog)) |