Subversion Repositories Projects

Rev

Rev 231 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 231 Rev 297
1
#include <stdio.h>
1
#include <string.h>
-
 
2
#include "printf_P.h"
2
#include <avr/io.h>
3
#include "timer0.h"
3
#include "fat16.h"
4
#include "fat16.h"
4
#include "sdc.h"
5
#include "sdc.h"
5
#include "printf_P.h"
6
#include "uart1.h"
86
 
253
        File_t * File = 0;
-
 
254
        for(i = 0; i < FILE_MAX_OPEN; i++)
-
 
255
        {
-
 
256
                if(FilePointer[i].State == FSTATE_UNUSED)               // found an unused one
87
        for(cnt=0;cnt<__MAX_FILES_USED;cnt++)
257
                {
-
 
258
                        File = &FilePointer[i];                                         // set pointer to that entry
-
 
259
                        FilePointer[i].State = FSTATE_USED;                     // mark it as used
88
        {
260
                        break;
-
 
261
                }
-
 
262
        }
89
                FilePointer[cnt].state = _UNUSED;                                                               // declare the filepointers as unused.
-
 
90
        }
-
 
-
 
263
        return(File);
91
 
264
}
92
        file = &FilePointer[0];
265
 
93
 
-
 
94
        while((SDC_Init() != 0) && (cnt++<200));                                        // initialise the sdcard.
266
/****************************************************************************************************************************************/
95
        if(cnt <200)                                                                                            //sdcard initialised succesfully
267
/*      Function:               UnlockFilePointer(file_t *);                                                                                                                                                                            */
96
        {
268
/*                                                                                                                                                                                                                                                                              */
-
 
269
/*      Description:    This function trys to unlock a file pointer.                                                                                                                                            */
97
                SDC_GetSector((u32)_MBR_SECTOR,file->buffer);                   // Read the MasterBootRecord from mmc.
270
/*                                                                                                                                                                                                                                                                              */
-
 
271
/*      Returnvalue:    Returns 1 if file pointer was freed else 0.                                                                                                                                                     */
-
 
272
/****************************************************************************************************************************************/
98
                MBR = (struct MBR_Entry *) file->buffer;
273
uint8_t UnlockFilePointer(File_t * file)
99
                FirstPartitionSector = MBR->PartitionEntry1.NoSectorsBeforePartition;
274
{
100
                if((MBR->PartitionEntry1.Type == _FAT16_32_MB_BIOS_Extension) ||
275
        uint8_t cnt;
101
                   (MBR->PartitionEntry1.Type == _FAT16_ST_32_MB) ||
276
        if(file == NULL) return(0);
102
                   (MBR->PartitionEntry1.Type == _FAT16_LT_32_MB))
277
        for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++)
-
 
278
        {
103
                {
279
                if(&FilePointer[cnt] == file)                                           // filepointer to be freed found?
-
 
280
                {
104
                        SDC_GetSector(FirstPartitionSector,file->buffer);               // Read the volume bootrecord from mmc.
281
                        file->State = FSTATE_UNUSED;
105
 
282
                        file->FirstSectorOfFirstCluster = 0;                    // Sectorpointer to the first sector of the first datacluster of the file.
106
                        VBR = (struct VBR_Entry *) file->buffer;                                        // Enter the VBR using the structure VBR_Entry.
283
                        file->FirstSectorOfCurrCluster  = 0;
107
                        SectorsPerCluster       = VBR->SectorsPerCluster;                       // Number of sectors per cluster. Depends on the memorysize of the sd-card.
284
                        file->SectorOfCurrCluster               = 0;                    // Pointer to the cluster which is edited at the moment.
108
                        FatCopies                       = VBR->NoFATCopies;                                     // Number of fatcopies.
285
                        file->SectorOfCurrCluster               = 0;                    // The sector which is edited at the moment (cluster_pointer + sector_index).
109
                        PossibleRootEntries = VBR->MaxRootEntries;                              // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries).
-
 
110
                        SectorsPerFat           = VBR->SectorsPerFAT;                           // The number of sectors per FAT.
286
                        file->ByteOfCurrSector                  = 0;                    // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index).
111
                        ReservedSectors         = VBR->ReservedSectors;                         // calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster.
287
                        file->Mode                                              = 0;                    // mode of fileoperation (read,write)
112
 
288
                        file->Size                                              = 0;                    // the size of the opend file in bytes.
113
                        FileAllocationTable     =   (u32)(FirstPartitionSector + (u32)ReservedSectors);                                                 // Calculate the position of the FileAllocationTable.
289
                        file->Position                                  = 0;                    // pointer to a character within the file 0 < fileposition < filesize
-
 
290
                        file->SectorInCache                     = 0;                    // the last sector read, wich is still in the sectorbuffer.
-
 
291
                        file->DirectorySector                   = 0;                    // the sectorposition where the directoryentry has been made.
114
                        RootDirectory           =   (u32)((u32)FileAllocationTable + (u32)((u32)SectorsPerFat*(u32)FatCopies)); // Calculate the position of the Rootdirectory.
292
                        file->DirectoryIndex                    = 0;                    // the index to the directoryentry within the specified sector.
115
                        FirstDataCluster        =   (u32)((u32)RootDirectory + ((u32)(PossibleRootEntries>>4)));                                        // Calculate the position of the first datacluster.
293
                        file->Attribute                                 = 0;                    // the attribute of the file opened.
116
                        CWD                                     =       RootDirectory;                                                                                                                                  // The actual directory is the rootdirectory.
294
                        file = NULL;
117
                        retvalue = 1;
295
                        return(1);
118
                }
296
                }
119
        }
297
        }
-
 
298
        return(0);
120
        return(retvalue);
299
}
121
}
-
 
122
 
300
 
-
 
301
/****************************************************************************************************************************************/
123
//________________________________________________________________________________________________________________________________________
302
/*      Function:               SeperateDirName(int8_t*, int8_t*);                                                                                                                                                                              */
-
 
303
/*                                                                                                                                                                                                                                                                              */
-
 
304
/*      Description:    This function seperates the first dirname from filepath and brings them                                                                                         */
-
 
305
/*                                      into the needed format ('test.txt' -> 'TEST    TXT')                                                                                                                            */
-
 
306
/*                                      The subpath is the pointer to the remaining substring if the filepath                                                                                           */
124
// Funtion:     File * fopen_(u8*, u8 *);
307
/*                                                                                                                                                                                                                                                                              */
-
 
308
/*      Returnvalue:    Return NULL on error or pointer to subpath                                                                                                                                                                                                      */
125
//
309
/****************************************************************************************************************************************/
126
// Description: This function looks for the file in the directory specified by the variable "CWD" of the drive. If the file is found the number of the
310
int8_t* SeperateDirName(const int8_t *filepath, int8_t *dirname)
127
//                              corrosponding datacluster is returned to main. Only modes 'r' (reading) and 'a' append are implemented yet.
-
 
128
//
311
{
129
//      Return:         0 = faild to open speicified file
312
        int8_t* subpath = NULL;
-
 
313
        uint8_t readpointer     = 0;
-
 
314
        uint8_t writepointer = 0;
-
 
315
 
-
 
316
        // search subpath from beginning of filepath
130
//                              1 = file opened
317
        subpath = NULL;
-
 
318
        readpointer     = 0;
-
 
319
        if(filepath[0] == '/') readpointer = 1; // ignore first '/'
-
 
320
        while(subpath == NULL)  // search the filepath until a subpath was found.
-
 
321
        {
131
//________________________________________________________________________________________________________________________________________
322
                if(((filepath[readpointer] == 0) || (filepath[readpointer] == '/')))    // if '/' found or end of filepath reached
132
 
323
                {
133
File * fopen_(s8 *fname, s8 mode)
-
 
134
{
324
                        subpath = (int8_t*)&filepath[readpointer];                              // store the position of the first "/" found after the beginning of the filenpath
-
 
325
                }
-
 
326
                readpointer++;
-
 
327
        }
-
 
328
 
-
 
329
        // clear dirname with spaces
-
 
330
        dirname[11] = 0; // terminate dirname
135
        File *file;
331
        for(writepointer = 0; writepointer < 11; writepointer++) dirname[writepointer] = ' ';
-
 
332
        writepointer = 0;
136
 
333
        // start seperating the dirname from the filepath.
137
        file = ReserveFilePointer();                                                                                                    // reserve a filepointer.
334
        readpointer = 0;
138
 
-
 
139
        if(file != NULL)                                                                                                                                // A free filepointer was found.
335
        if(filepath[0] == '/') readpointer = 1; // ignore first '/'
140
        {
336
        while( &filepath[readpointer] < subpath)
141
                file->mode      = mode;                                                                                                                 // mode of fileoperation (read,write)
337
        {
142
 
338
                if(writepointer >= 11) return(NULL);            // dirname to long
143
                if(SeekFileInDirectory(fname, file))                                                                            // if file was found
339
                if(filepath[readpointer] == '.')                        // seperating dirname and extension.
144
                {
-
 
145
                        if(mode == 'a')                                                                                                                 // open existing file for writing (append data at the end of the file)
340
                {
146
                        {
-
 
147
                                fseek_(file, 0, SEEK_END);                                                                                      // fseek points to the end of the file
-
 
148
                        }
-
 
149
                        else if(mode == 'r')
-
 
150
                        {
-
 
151
                                return(file);
-
 
152
                        }
-
 
153
                        else
341
                        if(writepointer <= 8)
154
                        {
-
 
-
 
342
                        {
155
                                fclose_(file);
343
                                readpointer++;                                          // next character in filename
156
                                file = NULL;
344
                                writepointer = 8;                                       // jump to start of extension
157
                        }
345
                        }
158
 
346
                        else return(NULL);                                              // dirbasename to long
159
                }
347
                }
160
                else
348
                else
161
                {
349
                {
162
                        if((mode == 'a') || (mode == 'w'))                                                                              // specified file doesn't exist so create new file for writing data.
350
                        if((0x60 < filepath[readpointer]) && (filepath[readpointer] < 0x7B))
163
                        {
351
                        {
164
                                if(CreateFileInDirectory(fname,file))                                                           // Could an entry for the new file in the rootdirectory be created?
352
                                dirname[writepointer] = (filepath[readpointer] - 0x20);                                 // all characters must be upper case.
165
                                {
-
 
166
                                        return(file);
-
 
167
                                }
353
                        }
168
                                else
354
                        else
169
                                {
355
                        {
170
                                        fclose_(file);
356
                                dirname[writepointer] = filepath[readpointer];
171
                                        file = NULL;
-
 
172
                                }
-
 
173
                        }
357
                        }
-
 
358
                        readpointer++;
-
 
359
                        writepointer++;
174
                }
360
                }
175
        }
361
        }
176
        return(file);
362
        return(subpath);
177
}
363
}
178
 
364
 
179
 
-
 
180
 
365
 
-
 
366
/**************************************************************************************************************************************+*/
181
//________________________________________________________________________________________________________________________________________
367
/*      Function:       Fat16ClusterToSector( uint16_t cluster);                                                                                                                                                                                */
182
// Funtion:     fflush_(File *file);
-
 
183
//
368
/*                                                                                                                                                                                                                                                                              */
184
// Description: This function writes the data already in the buffer but not yet written to the file.
-
 
-
 
369
/*      Description:    This function converts a cluster number given by the fat to the corresponding                                                                           */
-
 
370
/*                                      sector that points to the start of the data area that is represented by the cluster number.                                                     */
-
 
371
/*                                                                                                                                                                                                                                                                              */
185
//
372
/*      Returnvalue: The sector number with the data area of the given cluster                                                                                                                          */
186
//________________________________________________________________________________________________________________________________________
-
 
187
 
373
/****************************************************************************************************************************************/
188
s16     fflush_(File *file)
374
uint32_t        Fat16ClusterToSector(uint16_t cluster)
189
{
-
 
190
        u16 time=0;
-
 
191
        u16 date=0;
-
 
192
 
-
 
193
 
375
{
194
        if(file && ((file->mode =='a') || (file->mode =='w')))
-
 
195
        {
376
        if(!Partition.IsValid) return 0;
196
                if(file->byte_index > 0)                                                                                                                        // has data been added to the file?
-
 
197
                {
377
        if (cluster < 2) cluster = 2; // the 0. and 1. cluster in the fat are used for the media descriptor
198
                        SDC_PutSector((u32)(file->cluster_pointer + file->sector_index),file->buffer);// save the data still in the buffer
-
 
199
                }
-
 
200
                SDC_GetSector((u32)file->directory_sector,file->buffer);                                                        // read the directoryentry for this file.
-
 
201
                DirectoryEntry = (struct DirEntry *)file->buffer;
-
 
202
                DirectoryEntry[file->directory_index].size = (u32) file->filesize;
-
 
203
                DirectoryEntry[file->directory_index].time = (u16) time;
-
 
204
                DirectoryEntry[file->directory_index].date = (u16) date;
-
 
205
                SDC_PutSector((u32)file->directory_sector,file->buffer);
-
 
206
        }
-
 
207
        return(0);
378
        return ( (cluster - 2) * Partition.SectorsPerCluster) + Partition.FirstDataSector; // the first data sector     is represented by the 2nd cluster
208
}
379
}
-
 
380
 
209
 
381
/****************************************************************************************************************************************/
210
//________________________________________________________________________________________________________________________________________
-
 
211
// Funtion:     fclose_(File *file);
382
/*      Function:       SectorToFat16Cluster( uint32_t sector);                                                                                                                                                                         */
-
 
383
/*                                                                                                                                                                                                                                                                              */
212
//
384
/*      Description:    This function converts a given sector number given to the corresponding                                                                                         */
-
 
385
/*                                      cluster number in the fat that represents this data area.                                                                                                                       */
213
// Description: This function closes the open file by writing the remaining data from the buffer to the device and entering the filesize
386
/*                                                                                                                                                                                                                                                                              */
214
//                              in the directory entry.
-
 
215
//________________________________________________________________________________________________________________________________________
387
/*      Returnvalue: The cluster number representing the data area of the sector.                                                                                                                       */
216
 
388
/****************************************************************************************************************************************/
217
void fclose_(File *file)
389
uint16_t        SectorToFat16Cluster(uint32_t sector)
218
{
-
 
219
        if(file != NULL)
-
 
220
        {
-
 
221
                fflush_(file);                                                                                                                                          // save buffered data to the disk
390
{
222
        }
391
        if(!Partition.IsValid) return 0;
223
        FreeFilePointer(file);                                                                                                                                          // and free the filepointer.
-
 
224
}
-
 
225
 
-
 
226
//________________________________________________________________________________________________________________________________________
-
 
227
// Funtion:     u32 fread_(void *buffer, s32 size, s32 count, File *file);
-
 
228
//
-
 
229
// Description: This function reads count objects of the specified size from the actual position of the file to the specified buffer.
-
 
230
//
-
 
-
 
392
        return ((uint16_t)((sector - Partition.FirstDataSector) / Partition.SectorsPerCluster) + 2);
-
 
393
}
-
 
394
 
-
 
395
 
-
 
396
/****************************************************************************************************************************************/
231
// Returnvalue: The function returns the number of objects (not bytes) read from the file.
397
/*      Function:       Fat16_Deinit(void);                                                                                                                                                                                                             */
-
 
398
/*                                                                                                                                                                                                                                                                              */
-
 
399
/*      Description:    This function uninitializes the fat 16 api                                                                                                                                                      */
232
//
400
/*                                                                                                                                                                                                                                                                              */
233
//________________________________________________________________________________________________________________________________________
401
/*      Returnvalue: The function returns "0" on success                                                                                                                                                                        */
234
 
-
 
235
u32 fread_(void *buffer, u32 size, u32 count, File *file)
402
/****************************************************************************************************************************************/
236
{
-
 
237
        u32 object_cnt  = 0;                                                                                                            // count the number of objects read from the file.
-
 
238
        u32 object_size         = 0;                                                                                                            // count the number of bytes read from the actual object.
403
uint8_t Fat16_Deinit(void)
239
        u8 *buff_pnt    = 0;                                                                                                            // a pointer to the actual bufferposition.
-
 
240
        u8 success      = 1;                                                                                                            // no error occured during read operation to the file.
404
{
241
 
405
        int16_t returnvalue = 0;
242
        buff_pnt = (u8 *) buffer;                                                                                                       // cast the void pointer to an u8 *
-
 
243
 
406
        uint8_t cnt;
244
        while((object_cnt < count) && success)
407
        // declare the filepointers as unused.
245
        {
408
        for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++)
246
                object_size = size;
-
 
247
                while((size > 0) && success)
-
 
248
                {
409
        {
249
                        *buff_pnt = (u8) fgetchar_(file);                                                                       // read a byte from the buffer to the opened file.
-
 
250
                        buff_pnt++;
-
 
-
 
410
                if(FilePointer[cnt].State == FSTATE_USED)
-
 
411
                {
251
                        size--;
412
                        returnvalue += fclose_(&FilePointer[cnt]); // try to close open file pointers
-
 
413
                }
252
                }
414
 
253
                if(success) object_cnt++;
415
        }
254
        }
416
        SDC_Deinit();                   // uninitialize interface to sd-card
255
 
-
 
-
 
417
        Partition.IsValid = 0;  // mark data in partition structure as invalid
256
        return(object_cnt);                                                                                                                                     // return the number of objects succesfully read from the file
418
        return(returnvalue);
257
}
-
 
-
 
419
}
-
 
420
 
258
 
421
/****************************************************************************************************************************************/
259
//________________________________________________________________________________________________________________________________________
-
 
260
// Funtion:     u32 fwrite_(void *buffer, s32 size, s32 count, File *file);
422
/*      Function:               Fat16_Init(void);                                                                                                                                                                                                       */
261
//
-
 
262
// Description: This function writes count objects of the specified size from the buffer to the actual positon within the file.
423
/*                                                                                                                                                                                                                                                                          */
263
//
424
/*      Description:    This function reads the Masterbootrecord and finds the position of the Volumebootrecord, the FAT and the Rootdir    */
264
// Returnvalue: The function returns the number of objects (not bytes) written to the file.
425
/*                                      and stores the information in global variables.                                                                                                                                     */
265
//
426
/*                                                                                                                                                                                                                                                                          */
-
 
427
/*      Returnvalue:    The function returns "0" if the filesystem is initialized.                                                                                                                      */
266
//________________________________________________________________________________________________________________________________________
428
/****************************************************************************************************************************************/
-
 
429
uint8_t Fat16_Init(void)
267
 
430
{
268
u32 fwrite_(void *buffer, u32 size, u32 count, File *file)
431
    uint8_t     cnt     = 0;
-
 
432
        uint32_t        partitionfirstsector;
269
{
433
        VBR_Entry_t *VBR;
-
 
434
        MBR_Entry_t *MBR;
270
        u32 object_cnt  = 0;                                                                                                            // count the number of objects written to the file.
435
        File_t *file;
271
        u32 object_size         = 0;                                                                                                            // count the number of bytes written from the actual object.
-
 
272
        u8 *buff_pnt    = 0;                                                                                                            // a pointer to the actual bufferposition.
436
        uint8_t result = 0;
273
        u8 success      = 1;                                                                                                            // no error occured during write operation to the file.
-
 
274
 
-
 
275
        buff_pnt = (u8 *) buffer;                                                                                                       // cast the void pointer to an u8 *
-
 
276
 
-
 
277
        while((object_cnt < count) && success)
-
 
278
        {
-
 
279
                object_size = size;
437
 
-
 
438
        printf("\r\n FAT16 init...");
-
 
439
        Partition.IsValid = 0;
280
                while((size > 0) && success)
-
 
281
                {
-
 
282
                        success = fputchar_(file, *buff_pnt);                                                                                   // write a byte from the buffer to the opened file.
-
 
283
                        buff_pnt++;
-
 
284
                        size--;
440
 
285
                }
-
 
286
                if(success) object_cnt++;
-
 
287
        }
-
 
288
 
-
 
289
        return(object_cnt);                                                                                                                                     // return the number of objects succesfully written to the file
-
 
290
}                                                                                                                                                                                       // (!!!!! objects and not bytes !!!!)
-
 
291
 
441
        // declare the filepointers as unused.
292
//________________________________________________________________________________________________________________________________________
442
        for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++)
293
// Funtion:     s16 fseek_(File *, s32, s16)
443
        {
294
//
444
                FilePointer[cnt].State = FSTATE_UNUSED;
295
// Description: This function sets the pointer of the stream relative to the position
445
        }
-
 
446
        // set current file pinter to first position in list
296
//                              specified by origin (SEEK_SET, SEEK_CUR, SEEK_END).
447
        file = &FilePointer[0];
297
//
448
 
298
//________________________________________________________________________________________________________________________________________
449
        // try to initialise the sd-card.
-
 
450
        if(SD_SUCCESS != SDC_Init())
299
 
451
        {
-
 
452
                printf("SD-Card could not be initialized.");
300
s16 fseek_(File *file, s32 offset, s16 origin)
453
                result = 1;
-
 
454
                goto end;
301
{
455
        }
302
        s32                     fposition       = 0;
456
 
-
 
457
        // SD-Card is initialized successfully
303
        s16                     retvalue        = 1;
458
        if(SD_SUCCESS != SDC_GetSector((uint32_t)MBR_SECTOR,file->Cache))       // Read the MasterBootRecord
-
 
459
        {
-
 
460
                printf("Error reading the MBR.");
-
 
461
                result = 2;
-
 
462
                goto end;
-
 
463
        }
304
        u32     temp            = 0;
464
        MBR = (MBR_Entry_t *)file->Cache;                                               // Enter the MBR using the structure MBR_Entry_t.
-
 
465
        if((MBR->PartitionEntry1.Type == PART_TYPE_FAT16_ST_32_MB) ||
-
 
466
           (MBR->PartitionEntry1.Type == PART_TYPE_FAT16_LT_32_MB) ||
-
 
467
           (MBR->PartitionEntry1.Type == PART_TYPE_FAT16LBA))
305
 
468
        {
306
//......................................................
-
 
307
        if(origin == SEEK_SET)                                                                                          // Fileposition relative to the beginning of the file.
469
                // get sector offset 1st partition
308
        {
470
                partitionfirstsector = MBR->PartitionEntry1.NoSectorsBeforePartition;
309
                fposition = 0;
471
                // Start of Partition is the Volume Boot Sector
310
        }
472
                if(SD_SUCCESS != SDC_GetSector(partitionfirstsector,file->Cache)) // Read the volume boot record
311
//......................................................
-
 
312
        else if(origin == SEEK_END)                                                                                     // Fileposition relative to the end of the file.
-
 
313
        {
473
                {
-
 
474
                        printf("Error reading the VBR.");
314
                fposition  = (s32) file->filesize;
475
                        result = 3;
315
        }
476
                        goto end;
316
//......................................................
477
                }
317
        else if(origin == SEEK_CUR)                                                                                     // Fileposition relative to the current position of the file.
478
        }
-
 
479
        else  // maybe the medium has no partition assuming sector 0 is the vbr
-
 
480
        {
318
        {
481
                partitionfirstsector = 0;
-
 
482
        }
319
                fposition = file->fileposition;
483
 
320
        }
484
        VBR = (VBR_Entry_t *) file->Cache;                                              // Enter the VBR using the structure VBR_Entry_t.
321
 
-
 
322
        fposition += offset;
485
        if(VBR->BytesPerSector != BYTES_PER_SECTOR)
323
 
486
        {
324
        if((fposition >= 0) && (fposition <= (s32)file->filesize))              // is the pointer still within the file?
-
 
325
        {
487
                printf("VBR: Sector size not supported.");
326
                retvalue                                = 0;
-
 
327
                file->sector_index              = 0;
-
 
328
                file->byte_index                = 0;
-
 
329
                file->fileposition              = 0;
488
                result = 4;
330
                file->cluster_pointer   = file->start_cluster;
489
                goto end;
331
 
490
        }
332
                while(file->fileposition < fposition)
-
 
333
                {
491
        Partition.SectorsPerCluster             = VBR->SectorsPerCluster;                       // Number of sectors per cluster. Depends on the memorysize of the sd-card.
334
                        file->fileposition++;
-
 
335
                        if(file->byte_index < 511)
492
        Partition.FatCopies                     = VBR->NoFATCopies;                                     // Number of fatcopies.
336
                        {
493
        Partition.MaxRootEntries                = VBR->MaxRootEntries;                          // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries).
337
                                file->byte_index++;
494
        Partition.SectorsPerFat                 = VBR->SectorsPerFAT;                           // The number of sectors per FAT.
338
                        }
-
 
339
                        else
-
 
340
                        {
-
 
341
                                file->byte_index=0;                                                                             // reading at the beginning of new sector.
495
 
342
                                file->sector_index++;                                                                   // continue reading in next sector
496
        /* Calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. */
343
                                if(file->sector_index >= SectorsPerCluster)                     // When end of cluster is reached, the next datacluster has to be searched in the FAT.
497
        // Calculate the position of the FileAllocationTable:
344
                                {
-
 
-
 
498
        // Start + # of Reserved Sectors
345
                                        if(file->fileposition < fposition)
499
        Partition.FirstFatSector        =   (uint32_t)(partitionfirstsector + (uint32_t)(VBR->ReservedSectors));
346
                                        {
500
        // Calculate the position of the Rootdirectory:
-
 
501
        // Start + # of Reserved Sectors + (# of Sectors Per FAT * # of FAT Copies)
-
 
502
        Partition.FirstRootDirSector    =   Partition.FirstFatSector + (uint32_t)((uint32_t)Partition.SectorsPerFat*(uint32_t)Partition.FatCopies);
-
 
503
        // Calculate the position of the first datacluster:
-
 
504
        // Start + # of Reserved + (# of Sectors Per FAT * # of FAT Copies) + ((Maximum Root Directory Entries * 32) / Bytes per Sector)
-
 
505
        Partition.FirstDataSector       =   Partition.FirstRootDirSector + (uint32_t)(Partition.MaxRootEntries>>4);  // assuming 512 Byte Per Sector
-
 
506
        // Calculate the last data sector
-
 
507
        if(VBR->NoSectors == 0)
-
 
508
        {
-
 
509
                printf("VBR: Bad number of sectors.");
-
 
510
                result = 5;
-
 
511
                goto end;
-
 
512
        }
-
 
513
        Partition.LastDataSector = Partition.FirstDataSector + VBR->NoSectors - 1;
347
                                                file->sector_index = 0;                                                 // start reading new cluster at first sector of the cluster.
514
        // check for FAT16 in VBR of first partition
348
                                                GetNextCluster(file);                                                   // Sets the clusterpointer of the file to the next datacluster.
515
        if(!((VBR->FATName[0]=='F') && (VBR->FATName[1]=='A') && (VBR->FATName[2]=='T') && (VBR->FATName[3]=='1')&&(VBR->FATName[4]=='6')))
349
                                        }
-
 
350
                                }
-
 
351
                        }
-
 
352
                }
-
 
353
                if(file->byte_index)
-
 
354
                {
-
 
355
                        temp = (u32)((u32)file->cluster_pointer + (u32)file->sector_index);
-
 
-
 
516
        {
-
 
517
                printf("VBR: Partition ist not FAT16 type.");
-
 
518
                result = 6;
-
 
519
                goto end;
-
 
520
        }
-
 
521
        Partition.IsValid = 1; // mark data in partition structure as valid
-
 
522
        result = 0;
-
 
523
        end:
356
                        SDC_GetSector((u32)temp,file->buffer);          // FileBuffer will be written at once at the end of the cluster and has to be updated.
524
        if(result != 0) Fat16_Deinit();
357
                }
525
        else printf(" ...ok");
358
        }
526
        return(result);
359
        return(retvalue);
527
}
360
}
-
 
361
 
-
 
362
//________________________________________________________________________________________________________________________________________
-
 
363
// Funtion:     fgetchar_(File *file);
528
 
-
 
529
 
-
 
530
 
-
 
531
/****************************************************************************************************************************************/
364
//
532
/* Function:    ClearCurrCluster(File_t*);                                                                                                                                                                                      */
-
 
533
/*                                                                                                                                                                                                                                                                              */
365
// Description: This function reads and returns one character from the specified file.
534
/* Description: This function fills the current cluster with 0.                                                                                                                                                 */
366
//
535
/*                                                                                                                                                                                                                                                                              */
367
// Returnvalue: The function returns the character read from the specified memorylocation as u8 casted to s16 or EOF.
536
/* Returnvalue: The function returns 1 on success else 0.                                                                                                                                                               */
368
//________________________________________________________________________________________________________________________________________
537
/****************************************************************************************************************************************/
-
 
538
uint8_t ClearCurrCluster(File_t * file)
369
 
539
{
370
s16 fgetchar_(File *file)
-
 
-
 
540
        uint8_t retvalue = 1;
-
 
541
        uint32_t i;
-
 
542
 
-
 
543
        if((!Partition.IsValid) || (file == NULL)) return(0);
-
 
544
 
-
 
545
        for(i = 0; i < BYTES_PER_SECTOR; i++) file->Cache[i] = 0; // clear file cache
-
 
546
        for(i = 0; i < Partition.SectorsPerCluster; i++)
-
 
547
        {
-
 
548
                file->SectorInCache = file->FirstSectorOfCurrCluster + i;
-
 
549
                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))
-
 
550
                {
-
 
551
                        Fat16_Deinit();
-
 
552
                        retvalue = 0;
-
 
553
                }
-
 
554
        }
-
 
555
        return(retvalue);
-
 
556
}
-
 
557
 
-
 
558
/*****************************************************************************************************************************************/
-
 
559
/* Function:    GetNextCluster(File_t* );                                                                                                                                                                                        */
-
 
560
/*                                                                                                                                                                                                                                                                               */
-
 
561
/* Description: This function finds the next datacluster of the file specified with File *File.                                                                                  */
-
 
562
/*                                                                                                                                                                                                                                                                               */
-
 
563
/* Returnvalue: The function returns the next cluster or 0 if the last cluster has already reached.                                                                                                      */
-
 
564
/*****************************************************************************************************************************************/
-
 
565
uint16_t GetNextCluster(File_t * file)
-
 
566
{
-
 
567
        uint16_t cluster = 0;
-
 
568
        uint32_t fat_byte_offset, sector, byte;
371
{
569
        Fat16Entry_t * fat;
-
 
570
 
-
 
571
        if((!Partition.IsValid) || (file == NULL)) return(cluster);
-
 
572
        // if sector is within the data area
-
 
573
        if((Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)&& (file->FirstSectorOfCurrCluster <= Partition.LastDataSector))
-
 
574
        {
-
 
575
                // determine current file cluster
-
 
576
                cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster);
-
 
577
                // calculate byte offset in the fat for corresponding entry
-
 
578
                fat_byte_offset = ((uint32_t)cluster)<<1; // two FAT bytes (16 bits) for every cluster
-
 
579
                // calculate the sector that contains the current cluster within the fat
-
 
580
                sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR);
372
        s16 c = EOF;
581
                // calculate byte offset of the current cluster within that fat sector
373
        u32 temp1;
582
                byte = fat_byte_offset % BYTES_PER_SECTOR;
374
 
583
                // read this sector to the file cache
375
        if(file->filesize > 0)                                                                                                          // wen the end of the file is not reached, get the next character.
584
                if(file->SectorInCache != sector)
376
        {
585
                {
377
                temp1  = (u32)file->cluster_pointer;                                            // calculate the adress of the next character to be read.
586
                        file->SectorInCache = sector;                                           // update sector stored in buffer
378
                temp1 += (u32)file->sector_index;
587
                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))       // read sector from sd-card
379
 
-
 
380
                if(file->sector_in_buffer != temp1)                                                                     // Has the content of the buffer been modified and has to be updated?
-
 
381
                {
588
                        {
382
                        SDC_GetSector((u32)temp1,file->buffer);                                 // Read the calculated cluster.
-
 
383
                        file->sector_in_buffer = (u32)temp1;
-
 
384
                }
589
                                Fat16_Deinit();
385
                c = (s16) file->buffer[file->byte_index];
590
                                return (cluster);
386
                file->filesize--;                                                                                                               // decrement the number of characters available.
591
                        }
387
 
592
                }
388
                if(file->byte_index < 511)                                                                                              // continue reading from this sector until the end of the sector is reached.
-
 
389
                {
-
 
390
                        file->byte_index++;
-
 
391
                }
-
 
392
                else                                                                                                                                    // has the end of an sector been reached->
-
 
393
                {
-
 
394
                        file->byte_index=0;                                                                                                     // continue reading at the beginning -
-
 
-
 
593
                // read the next cluster from cache
-
 
594
                fat = (Fat16Entry_t *)(&(file->Cache[byte]));
-
 
595
                cluster = fat->NextCluster;
-
 
596
                // if last cluster fat entry
-
 
597
                if(FAT16_CLUSTER_LAST_MIN <= cluster)
-
 
598
                {
-
 
599
                         cluster = 0;
395
                        file->sector_index++;                                                                                           // of new sector.
600
                }
396
                        if(file->sector_index >= SectorsPerCluster)                                             // When the end of an cluster is reached, the next datacluster has to be searched in the FAT.
601
                else
-
 
602
                {
397
                        {
603
                        file->FirstSectorOfCurrCluster = Fat16ClusterToSector(cluster);
-
 
604
                        file->SectorOfCurrCluster = 0;
-
 
605
                        file->ByteOfCurrSector = 0;
398
                                file->sector_index = 0;                                                                                 // start reading new cluster at first sector of the cluster.
606
                }
399
                                GetNextCluster(file);                                                                                   // Sets the clusterpointer of the file to the next datacluster.
-
 
400
                        }
-
 
401
                }
607
        }
402
        }
-
 
403
        return(c);
-
 
404
}
-
 
405
 
-
 
406
//________________________________________________________________________________________________________________________________________
608
        return(cluster);
407
// Funtion:     fputchar_(File *file, s8 *);
609
}
408
//
-
 
409
// Description: This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries.
-
 
410
//
-
 
411
// Returnvalue: The function returns a value of 0 if the data could not be written.
610
 
412
//________________________________________________________________________________________________________________________________________
-
 
413
 
611
 
414
u8 fputchar_(File *file,s8 c)
612
/****************************************************************************************************************************************/
415
{
-
 
416
        u32 ul_temp  = 0;
-
 
417
        u8 retvalue = 1;
-
 
418
 
613
/* Function:    FindNextFreeCluster(File_t *);                                                                                                                                                                          */
419
    if(file->sector_index >= SectorsPerCluster)                                                                         // if end of the cluster is reached, find next free cluster
-
 
420
        {
614
/*                                                                                                                                                                                                                                                                              */
421
                file->sector_index = 0;
615
/* Description: This function looks in the fat to find the next free cluster                                                                                                                    */
422
                if(!AppendCluster(file)) retvalue = 0;                                                                                  // append a new and free cluster at the end of the file.
616
/*                                                                                                                                                                                                                                                                              */
423
        }
617
/* Returnvalue: The function returns the cluster number of the next free cluster found within the fat.                                                                  */
424
 
618
/****************************************************************************************************************************************/
425
        file->buffer[file->byte_index] = c;                                                                                             // write databyte into the buffer. The byte will be written to the device at once
-
 
426
        if(file->filesize == file->fileposition) file->filesize++;                                                      // a character has been written to the file so the size is inkremented but only when the character has been added at the end of the file.
-
 
427
        file->fileposition++;                                                                                                                           // the actual positon within the file.
-
 
428
                                                                                                                                                                                // if the buffer contains the complete sectordata.
-
 
429
        if(file->byte_index < 511)                                                                                                                      // if the end of this sector is not reached yet
-
 
430
        {
-
 
431
                file->byte_index++;                                                                                                                             // the next byte will be written to the next byteposition in this sector.
-
 
432
        }
-
 
433
        else                                                                                                                                                            // otherwise the data in the sectorbuffer will be written to the device and the next sector will be selected.
-
 
434
        {
-
 
435
                ul_temp  = (u32)file->cluster_pointer;
-
 
436
                ul_temp += (u32)file->sector_index;
-
 
437
 
-
 
438
                SDC_PutSector((u32)ul_temp,file->buffer);
-
 
439
                file->byte_index=0;                                                                                                                             // and the next byte will be written at the beginning of this new sector.
-
 
440
                file->sector_index++;
-
 
441
                if(file->sector_index >= SectorsPerCluster)                                                                     // if end of the cluster is reached, find next free cluster
-
 
442
                {
-
 
443
                        file->sector_index = 0;
-
 
444
                        if(!AppendCluster(file)) retvalue = 0;                                                                          // append a new and free cluster at the end of the file.
-
 
445
                }
-
 
446
        }
-
 
447
        return(retvalue);
-
 
448
}
619
uint16_t FindNextFreeCluster(File_t *file)
449
 
-
 
450
//________________________________________________________________________________________________________________________________________
-
 
451
// Funtion:     fputs_(File *file, s8 *string);
-
 
452
//
-
 
453
// Description: This function writes a string to the specified file.
-
 
454
//
-
 
455
//________________________________________________________________________________________________________________________________________
-
 
456
 
-
 
457
u8 fputs_(File *file,s8 * string)
-
 
458
{
-
 
459
        u8 i=0;
-
 
460
 
620
{
461
        while(string[i] != 0)
-
 
462
        {
-
 
463
                fputchar_(file,string[i]);
-
 
464
                i++;
621
        uint32_t fat_sector;                            // current sector within the fat relative to the first sector of the fat.
465
        }
-
 
466
        return(0);
622
        uint32_t curr_sector;                           // current sector
467
}
623
        uint16_t fat_entry;                                     // index to an fatentry within the actual sector (256 fatentries are possible within one sector).
468
 
-
 
469
//________________________________________________________________________________________________________________________________________
-
 
470
// Funtion:     s8 * fgets_(s8 *string, s16 count, File *file)
624
        uint16_t free_cluster = 0;                      // next free cluster number.
-
 
625
        Fat16Entry_t * fat;
471
//
626
 
472
// Description: This function reads a string from the file to the specifies string.
627
        if((!Partition.IsValid) || (file == NULL)) return(0);
-
 
628
 
473
//
629
        // start searching for an empty cluster at the beginning of the fat.
474
// Returnvalue: A pointer to the string written from the file.
630
        fat_sector = 0;
-
 
631
        do
475
//________________________________________________________________________________________________________________________________________
632
        {
476
 
633
                curr_sector = Partition.FirstFatSector + fat_sector;    // calculate sector to read
477
s8 * fgets_(s8 *string, s16 count, File *file)
634
                file->SectorInCache = curr_sector;                                              // upate the sector number of file cache.
478
{
635
                if( SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))              // read sector of fat from sd-card.
479
        s16 buff_pnt = 0;
-
 
480
        s16 buff_tmp = 0;
-
 
481
        u8 state = 0;                                                                                                                   // Variable used for a statemachine to recognize the end of a line.
-
 
482
 
636
                {
483
        while(count > 1)                                                                                                                                        // read the count-1 characters from the file to the string.
637
                        Fat16_Deinit();
484
        {
638
                        return(free_cluster);
485
                buff_tmp = fgetchar_(file);                                                                                                             // read a character from the opened file.
-
 
486
                switch(state)
-
 
487
                {
-
 
488
                        case 0:
-
 
489
                                if(buff_tmp == 0x0D) state++;
-
 
490
                        break;
-
 
491
 
-
 
492
                        case 1:
-
 
493
                                if(buff_tmp == 0x0A)
-
 
-
 
639
                }
-
 
640
 
-
 
641
                fat = (Fat16Entry_t *)file->Cache;                                              // set fat pointer to file cache
-
 
642
 
-
 
643
                for(fat_entry = 0; fat_entry < FAT16_ENTRIES_PER_SECTOR; fat_entry++)                                           // look for an free cluster at all entries in this sector of the fat.
-
 
644
                {
-
 
645
                        if(fat[fat_entry].NextCluster == FAT16_CLUSTER_FREE)            // empty cluster found!!
494
                                {
646
                        {
495
                                        count = 1;
647
                                fat[fat_entry].NextCluster = FAT16_CLUSTER_LAST_MAX;    // mark this fat-entry as used
496
                                }
648
                                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))               // and save the sector at the sd-card.
497
                                state = 0;
-
 
498
                        break;
649
                                {
-
 
650
                                        Fat16_Deinit();
499
                }
651
                                        return(free_cluster);
500
                if(buff_tmp == EOF) {buff_tmp = 0; count = 1;}                                                                  // is the end of the file reached, terminate the string with zero.
652
                                }
501
                string[buff_pnt] = (s8) buff_tmp;
653
                                free_cluster = (uint16_t)(fat_sector * FAT16_ENTRIES_PER_SECTOR + (uint32_t)fat_entry);
502
                count--;
-
 
503
                buff_pnt++;
654
                                fat_entry = FAT16_ENTRIES_PER_SECTOR;                                   // terminate the search for a free cluster in this sector.
504
        }
655
                        }
-
 
656
                }
-
 
657
                fat_sector++;                                                                                                   // continue the search in next fat sector
505
        string[buff_pnt] = 0;
658
        // repeat until the end of the fat is  reached and no free cluster has been found so far
-
 
659
        }while((fat_sector < Partition.SectorsPerFat) && (!free_cluster));
506
        return(string);
660
        return(free_cluster);
507
}
-
 
508
 
661
}
509
//________________________________________________________________________________________________________________________________________
-
 
510
// Funtion:     u8 fexist_(u8*, File *file);
-
 
511
//
-
 
512
// Description: This function searches the specified file and returns 0 if the file was not found.
-
 
513
//
662
 
514
//
663
 
515
//      Return:         0 = file does not exist
-
 
516
//                              1 = file exists
-
 
517
//________________________________________________________________________________________________________________________________________
-
 
518
 
-
 
519
u8 fexist_(s8 *fname)
-
 
520
{
-
 
521
        File *file;
-
 
522
 
-
 
523
        file = ReserveFilePointer();
-
 
524
 
-
 
525
        if(file != NULL)
-
 
526
        {
-
 
527
                if((file = fopen_(fname,'r'))!= NULL)
664
/****************************************************************************************************************************************************/
528
                {
-
 
529
                        fclose_(file);
-
 
530
                        return(1);
-
 
531
                }
-
 
532
                else
-
 
533
                {
-
 
534
                        return(0);
-
 
535
                }
-
 
536
        }
-
 
537
        else
-
 
538
        {
-
 
539
                return(0);
-
 
540
        }
-
 
541
}
-
 
542
 
-
 
543
//________________________________________________________________________________________________________________________________________
-
 
544
// Funtion:     GetNextCluster(File *file);
-
 
545
//
-
 
546
// Description: This function finds the next datacluster of the file specified with File *file.
-
 
547
//
-
 
548
// Returnvalue: The function returns "0" if the last cluster has already been reached.
-
 
549
//________________________________________________________________________________________________________________________________________
-
 
550
 
-
 
551
u16 GetNextCluster(File *file)
-
 
552
{
-
 
553
        u32 fat_pointer                 =       0;
-
 
-
 
665
/* Function:    int16_t fseek_(File_t *, int32_t *, uint8_t)                                                                                                                                                                                                            */
-
 
666
/*                                                                                                                                                                                                                                                                                                      */
-
 
667
/* Description: This function sets the pointer of the stream relative to the position                                                                                                                           */
-
 
668
/*                              specified by origin (SEEK_SET, SEEK_CUR, SEEK_END)                                                                                                                                                                      */
-
 
669
/* Returnvalue: Is 1 if seek was successful                                                                                                                                                                                                                                                                     */
-
 
670
/****************************************************************************************************************************************************/
-
 
671
int16_t fseek_(File_t *file, int32_t offset, int16_t origin)
-
 
672
{
554
        u32 fat_sector_offset   =       0;
673
        int32_t         fposition       = 0;
555
        u32 ul_tmp                              =       0;
674
        int16_t         retvalue        = 1;
-
 
675
 
-
 
676
        if((!Partition.IsValid) || (file == NULL)) return(0);
-
 
677
        switch(origin)
-
 
678
        {
556
        u8 retvalue                             =       0;                                                                              // no new cluster found yet.
679
                case SEEK_SET:                          // Fileposition relative to the beginning of the file.
557
 
680
                        fposition = 0;
-
 
681
                        break;
-
 
682
                case SEEK_END:                          // Fileposition relative to the end of the file.
-
 
683
                        fposition = (int32_t)file->Size;
-
 
684
                        break;
-
 
685
                case SEEK_CUR:                          // Fileposition relative to the current position of the file.
-
 
686
                default:
-
 
687
                        fposition = file->Position;
-
 
688
                        break;
-
 
689
        }
-
 
690
 
-
 
691
        fposition += offset;
-
 
692
 
-
 
693
        if((fposition >= 0) && (fposition <= (int32_t)file->Size))              // is the pointer still within the file?
558
 
694
        {
559
        if((file->cluster_pointer >= RootDirectory) && (file->cluster_pointer < (RootDirectory + 31)))
695
                // reset file position to start of the file
-
 
696
                file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster;
560
        {                                                                                                                                                               // Is the next cluster searched within the rootdirectory and available?
697
                file->SectorOfCurrCluster       = 0;
561
                file->cluster_pointer++;                                                                                                        // the rootdirectory is a linear adress space of 32 clusters.
698
                file->ByteOfCurrSector          = 0;
562
                retvalue = 1;                                                                                                                           // and the next cluster has been found.
699
                file->Position                          = 0;
563
        }
700
 
564
        else if(file->cluster_pointer > (RootDirectory + 31))                                                   // The specified cluster is within the FAT.
701
                while(file->Position < fposition)       // repeat until the current position is less than target
565
        {
702
                {
566
                fat_sector_offset   = ((file->cluster_pointer) - (FirstDataCluster));           // Calculate index of actual cluster within the FAT.
703
                        file->Position++;                               // increment file position
567
                fat_sector_offset  /= SectorsPerCluster;                                                                        // calculate the index of the actual sector within the FAT.
704
                        file->ByteOfCurrSector++;               // next byte in current sector
568
                fat_sector_offset  += 2;                                                                                                        // In Fat16 clusterpositions have an offset of two.
705
                        if(file->ByteOfCurrSector >= BYTES_PER_SECTOR)
569
                fat_pointer = (fat_sector_offset%0x100);                                                                        // Calculate the sector within the cluster.
706
                        {
570
                fat_sector_offset   = (fat_sector_offset>>8);                                                           // and the position within the sector.
707
                                file->ByteOfCurrSector = 0;                                                                             // reading at the beginning of new sector.
571
 
708
                                file->SectorOfCurrCluster++;                                                                    // continue reading in next sector
572
                SDC_GetSector((u32)(FileAllocationTable + fat_sector_offset),file->buffer);
709
                                if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster)    // if end of cluster is reached, the next datacluster has to be searched in the FAT.
573
                file->sector_in_buffer = (FileAllocationTable + fat_sector_offset);                     // Mark that new sector has been read.
710
                                {
574
 
711
                                        if(GetNextCluster(file))                                                                        // Sets the clusterpointer of the file to the next datacluster.
575
                ul_tmp  = (u32)file->buffer[((fat_pointer << 1)+1)];                            // Read next sector information from calculated clusterposition.
712
                                        {
576
                ul_tmp  = (ul_tmp << 8);
713
                                                file->SectorOfCurrCluster = 0;
577
                ul_tmp |= (u32)file->buffer[(fat_pointer << 1)];
714
                                        }
578
                ul_tmp -=2;                                                                                                                                     // next datacluster is clusterposition in fat - 2.
715
                                        else // the last cluster was allready reached
579
                ul_tmp *= SectorsPerCluster;                                                                                            // calculate sectorposition of new cluster
716
                                        {
580
                ul_tmp += FirstDataCluster;                                                                                                     // in relation to first datacluster of the disk.
717
                                                file->SectorOfCurrCluster--;                                                    // jump back to the ast sector in the last cluster
581
 
718
                                                file->ByteOfCurrSector = BYTES_PER_SECTOR;                              // set ByteOfCurrSector one byte over sector end
582
                if(ul_tmp < 0xfff7)                                                                                                                     // has a new cluster been read or was the end of the fat reached?
719
                                        }
583
                {
720
                                }
584
                        file->cluster_pointer = (u32) ul_tmp;                                           // continue reading the file at the beginning of new datacluster.
721
                        }
585
                        retvalue = 1;                                                                                                                   // a new cluster was found.
722
                }
586
                }
723
        }
587
        }
724
        if(file->Position == fposition) retvalue = 0;
588
        return(retvalue);
725
        return(retvalue);
589
}
726
}
590
 
-
 
591
//________________________________________________________________________________________________________________________________________
-
 
592
// Funtion:     u16 FindNextFreeCluster(void)
-
 
593
//
-
 
594
// Description: This function looks in the FAT to find the next free datacluster
-
 
595
//
-
 
596
// Returnvalue: The function returns the adress of the next free cluster found within the fAT.
-
 
597
//________________________________________________________________________________________________________________________________________
-
 
598
 
-
 
599
u16 FindNextFreeCluster(File *file)
-
 
600
{
-
 
601
        u32 fat_pointer                 =       0;                                                      // Pointer to the first sector of the FAT.
-
 
602
        u32 ul_tmp                              =       0;                                                      // temporary variable used to calculate a sectornumber.
-
 
603
        u16  fat_sector_offset  =       0;                                                      // index to a sector within the FAT.
-
 
604
        u16  fat_entry                  =       0;                                                      // index to an fatentry within the actual sector (256 fatentries are possible within one sector).
-
 
605
        u16  free_cluster                       =       0;                                                      // a pointer to the first sector of the next free cluster.
-
 
606
 
-
 
607
        fat_pointer = (u32) FileAllocationTable;                                        // start searching for empty cluster at the beginning of the fat.
-
 
608
                                                                                                                                                // if the end of the fat is not reached yet and no free cluster has been found
-
 
609
        while((fat_sector_offset < SectorsPerFat) && (!free_cluster))
-
 
610
        {
-
 
611
                ul_tmp = (u32) ((u32)fat_pointer + (u32)fat_sector_offset);
-
 
612
                SDC_GetSector((u32)ul_tmp,file->buffer);                                // read next sector of FAT.
-
 
613
                file->sector_in_buffer = ul_tmp;                                                                // remember the number of the sector in FileBuffer.
-
 
614
                Fat = (struct FatEntry *)file->buffer;
-
 
615
                for(fat_entry=0;fat_entry<256;fat_entry++)                                              // look for an free cluster at all entries in this sector of the fat.
-
 
616
                {
-
 
617
                        if(Fat[fat_entry].next_cluster == 0x0000)                                       // empty cluster found!!
-
 
618
                        {
-
 
619
                                Fat[fat_entry].next_cluster = 0xffff;                                   // mark this fat-entry as used and save it to the device.
-
 
620
                                SDC_PutSector((u32)file->sector_in_buffer,file->buffer);
-
 
621
                                free_cluster  = fat_entry;                                                              // the relative position of the free cluster found in this sector of the FAT.
-
 
622
                                free_cluster += (fat_sector_offset << 8);                               // calculate the absolute position of the free cluster in the FAT;
-
 
623
                                fat_entry = 256;                                                                                // terminate the search for a free cluster in this sector.
-
 
624
                        }
-
 
625
                }
-
 
626
                fat_sector_offset++;
-
 
627
        }
-
 
628
return(free_cluster);
-
 
629
}
-
 
630
 
-
 
631
//________________________________________________________________________________________________________________________________________
-
 
632
// Funtion:     u16 AppendCluster(File *file);
-
 
633
//
-
 
634
// Description: This function finds the next free datacluster on the disk and appends it to the specified file.
-
 
635
//
-
 
636
// Returnvalue: This funktion returns 1 if a cluster was appended to the specified file.
-
 
637
//________________________________________________________________________________________________________________________________________
-
 
638
 
-
 
639
u8 AppendCluster(File *file)
-
 
640
{
-
 
641
        u16  free_cluster = 0;
-
 
642
        u32 fat_pointer = 0;
-
 
643
        u8 retvalue     = 0;
-
 
644
 
-
 
645
        free_cluster = FindNextFreeCluster(file);                                                                                               // the next free cluster found on the disk.
-
 
646
        if(free_cluster) retvalue = 1;                                                                                                                  // A free cluster was found and can be added to the end of the file.
-
 
647
        fat_pointer  = FileAllocationTable;                                                                                                             // Set Pointer to the beginnig of the FAT.
-
 
648
        fat_pointer += (u32)((u32)GetFatClusterOffset(file) >> 8);                                                              // find the sector in the FAT with 256 entries per sector.
-
 
649
 
-
 
650
    SDC_GetSector(fat_pointer,file->buffer);
-
 
651
        Fat = (struct FatEntry *)file->buffer;
-
 
652
        Fat[GetFatSectorIndex(file)].next_cluster = free_cluster;                                                               // append the free cluster to the end of the file in the FAT.
-
 
653
        SDC_PutSector((u32)fat_pointer,file->buffer);                                                                                   // save the modified sector to the FAT.
-
 
654
 
-
 
655
        fat_pointer  = (u32)free_cluster;
-
 
656
        fat_pointer -= 2;
-
 
657
        fat_pointer *= SectorsPerCluster;
-
 
658
        fat_pointer += FirstDataCluster;
-
 
659
 
-
 
660
        file->cluster_pointer = fat_pointer;                                                                                                    // continue wrtiting to the file in the new and free datacluster.
-
 
661
        return(retvalue);                                                                                                                                               // return 1 when a new cluster was appended to the file
-
 
662
}
-
 
663
 
-
 
664
 
-
 
665
//________________________________________________________________________________________________________________________________________
-
 
666
// Funtion:     void DeleteClusterChain(u16 startcluster);
-
 
667
//
-
 
668
// Description: This function frees all the clusters used for file from the fat.
-
 
669
//
-
 
670
// Returnvalue: none
-
 
671
//________________________________________________________________________________________________________________________________________
-
 
672
 
-
 
673
void DeleteClusterChain(u16 startcluster)
-
 
674
{
-
 
675
        u16 fat_index                   =       0;
-
 
676
        u16 fat_sector_offset   =       0;
-
 
677
        u32 sector_in_buffer    =       0;
-
 
678
        u32 ul_temp                             =       0;
-
 
679
        u8 buffer[512];
-
 
680
 
-
 
681
        fat_index = (startcluster % 0x100);                                                                                             // Calculate the sector within the cluster.
-
 
682
        fat_sector_offset = (startcluster >> 8);                                                                                        // and the position within the sector.
-
 
683
        ul_temp = (u32)(FileAllocationTable + fat_sector_offset);
-
 
-
 
727
 
-
 
728
 
-
 
729
/****************************************************************************************************************************************/
-
 
730
/* Function:    uint16_t DeleteClusterChain(File *file);                                                                                                                                                                                */
-
 
731
/*                                                                                                                                                                                                                                                                              */
-
 
732
/* Description: This function trances along a cluster chain in the fat and frees all clusters visited.                                                                  */
-
 
733
/*                                                                                                                                                                                                                                                                              */
-
 
734
/****************************************************************************************************************************************/
-
 
735
uint8_t DeleteClusterChain(uint16_t StartCluster)
-
 
736
{
-
 
737
        uint16_t cluster;
-
 
738
        uint32_t fat_byte_offset, sector, byte;
-
 
739
        Fat16Entry_t * fat;
-
 
740
        uint8_t buffer[BYTES_PER_SECTOR];
-
 
741
        uint32_t sector_in_buffer = 0;
-
 
742
        uint8_t repeat = 0;
-
 
743
 
-
 
744
        if(!Partition.IsValid) return 0;
-
 
745
 
-
 
746
        cluster = StartCluster; // init chain trace
-
 
747
        // calculate byte offset in the fat for corresponding entry
-
 
748
        fat_byte_offset = ((uint32_t)cluster)<<1; // two FAT bytes (16 bits) for every cluster
-
 
749
        // calculate the sector that contains the current cluster within the fat
-
 
750
        sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR);
684
 
751
        // calculate byte offset of the current cluster within that fat sector
685
 
752
        byte = fat_byte_offset % BYTES_PER_SECTOR;
686
        do
753
        do
687
        {
754
        {
-
 
755
                if(sector != sector_in_buffer)
688
                if(sector_in_buffer != ul_temp)
756
                {
689
                {
757
                        // read this sector to buffer
690
                        sector_in_buffer = ul_temp;
758
                        sector_in_buffer = sector;
-
 
759
                        if(SD_SUCCESS != SDC_GetSector(sector_in_buffer, buffer)) return 0;     // read sector from sd-card
691
                        SDC_GetSector(ul_temp,buffer);
760
                }
692
                }
-
 
693
                Fat = (struct FatEntry *)buffer;
761
                // read the next cluster from cache
694
 
762
                fat = (Fat16Entry_t *)(&(buffer[byte]));
-
 
763
                cluster = fat->NextCluster;
695
                startcluster = Fat[fat_index].next_cluster;
764
                if((FAT16_CLUSTER_USED_MIN <= cluster) && (cluster <= FAT16_CLUSTER_USED_MAX) ) repeat = 1;
-
 
765
                else repeat = 0;
696
                Fat[fat_index].next_cluster = 0x0000;                                                                                   // free the cluster within the fat.
766
 
-
 
767
                fat->NextCluster =      FAT16_CLUSTER_FREE; // mark current cluster as free
697
 
768
                // calculate byte offset in the fat for corresponding entry
-
 
769
                fat_byte_offset = ((uint32_t)cluster)<<1; // two FAT bytes (16 bits) for every cluster
-
 
770
                // calculate the sector that contains the current cluster within the fat
-
 
771
                sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR);
698
                fat_index = (startcluster % 0x100);                                                                                             // Calculate the sector within the cluster.
772
                // calculate byte offset of the current cluster within that fat sector
699
                fat_sector_offset = (startcluster >> 8);                                                                                        // and the position within the sector.
773
                byte = fat_byte_offset % BYTES_PER_SECTOR;
700
                ul_temp = (u32)(FileAllocationTable + fat_sector_offset);
774
                // if new sector is not the sector in buffer or the last cluster in the chain was traced
701
                if((sector_in_buffer != ul_temp) || (startcluster == 0xffff))
775
                if((sector != sector_in_buffer) || !repeat)
702
                {
776
                {       // write sector in buffer
703
                        SDC_PutSector(sector_in_buffer,buffer);
-
 
704
                }
-
 
705
        }
-
 
706
        while(startcluster != 0xffff);                                                                                                                  // last cluster has been deleted.
-
 
707
}
-
 
708
 
-
 
709
 
-
 
710
//________________________________________________________________________________________________________________________________________
-
 
711
// Funtion:     u16 GetFatClusterIndex(File *file);
-
 
712
//
-
 
713
// Description: This function returns the clusterindex of the cluster specified by file->cluster_pointer of the specified file.
-
 
714
//
-
 
715
//________________________________________________________________________________________________________________________________________
-
 
716
 
-
 
717
u16 GetFatClusterOffset(File *file)
-
 
718
{
-
 
719
        u32 fat_sector_offset   =       0;
-
 
720
 
-
 
721
        fat_sector_offset   = ((file->cluster_pointer) - (FirstDataCluster));                           // Calculate index of actual cluster in FAT.
-
 
722
        fat_sector_offset  /= SectorsPerCluster;
-
 
723
        fat_sector_offset  += 2;                                                                                                                        // In Fat16 clusterpositions have an offset of two.
-
 
724
 
-
 
725
        return((u16)fat_sector_offset);
-
 
726
}
-
 
727
 
-
 
728
//________________________________________________________________________________________________________________________________________
-
 
729
// Funtion:     u16 GetFatSectorIndex(File *file);
-
 
730
//
-
 
731
// Description: This function returns the sectorindex of the cluster specified by file->cluster_pointer of the specified file.
-
 
732
//
-
 
733
//________________________________________________________________________________________________________________________________________
-
 
734
 
777
                        if(SD_SUCCESS != SDC_PutSector(sector_in_buffer,buffer)) return 0;
735
u16 GetFatSectorIndex(File *file)
-
 
736
{
-
 
737
        u16 fat_pointer                 =       0;
-
 
738
 
778
                }
739
        fat_pointer = GetFatClusterOffset(file);
779
        }
740
        fat_pointer = fat_pointer % 0x100;                                                                                                      // Calculate the clusterposition in the fat
-
 
741
 
-
 
742
        return(fat_pointer);
-
 
743
}
-
 
744
 
-
 
745
//________________________________________________________________________________________________________________________________________
-
 
746
// Funtion:     u16 CreateFileInDirectory(u8 *, File *)
-
 
747
//
-
 
748
// Description: This function looks for the next free position in the directory and creates an entry. The type of an directoryentry is
-
 
749
//                              specified by the attribute.
-
 
750
//                              bit0:   unused
-
 
751
//                              bit1:   archive
-
 
-
 
780
        while(repeat);
-
 
781
 
-
 
782
        return 1;
-
 
783
}
-
 
784
 
-
 
785
 
-
 
786
/****************************************************************************************************************************************/
752
//                              bit2:   read_only
787
/* Function:    uint16_t AppendCluster(File *file);                                                                                                                                                                                     */
753
//                              bit3:   system
788
/*                                                                                                                                                                                                                                                                              */
754
//                              bit4:   directory
-
 
755
//                              bit5:   volume
-
 
756
//________________________________________________________________________________________________________________________________________
-
 
757
 
-
 
758
u8 CreateFileInDirectory(s8 *fname, File *file)
789
/* Description: This function looks in the fat to find the next free cluster and appends it to the file.                                                                */
759
{
790
/*                                                                                                                                                                                                                                                                              */
760
        u16     rootentry       = 0;                                                                                                    // index to an entry in the rootdirectory.
791
/* Returnvalue: The function returns the appened cluster number or 0 of no cluster was appended.                                                                                                                                                */
761
        u16     cnt_enries_searched = 0;                                                                                        // count the number of rootentries which have been searched already.
-
 
762
        u8              i = 0;
792
/****************************************************************************************************************************************/
763
        u16     sector_offset = 0;                                                                                                      // index to the sector of the Rootentry which is searched momentarily
793
uint16_t AppendCluster(File_t *file)
764
        u8              retvalue = 0;
794
{
765
        u32             cluster_temp = 0;
795
        uint16_t last_cluster, new_cluster = 0;
766
        u16             cluster = 0;
-
 
767
        s8 name[11]     = "           ";
796
        uint32_t fat_byte_offset, sector, byte;
768
 
797
        Fat16Entry_t * fat;
769
        SeperateFileName(fname,name);
798
 
770
 
799
        if((!Partition.IsValid) || (file == NULL)) return(new_cluster);
771
        cluster_temp = (u32)FindNextFreeCluster(file);                                                          // the next free cluster on the disk.
800
 
772
 
-
 
773
        if(cluster_temp)                                                                                                                        // if a free cluster is available:
-
 
774
        {
-
 
775
                cluster = (u16)cluster_temp;                                                                                    // remember the index of the free datacluster found for the directory entry.
-
 
776
                cluster_temp -=2;                                                                                                               // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved.
-
 
777
                cluster_temp *= SectorsPerCluster;                                                                              // Calculate relative sectorindex of first datacluster.
-
 
778
                file->start_cluster   = (FirstDataCluster + cluster_temp);                              // Calculate absolute sectorposition of first datacluster.
801
        new_cluster = FindNextFreeCluster(file);        // the next free cluster found on the disk.
779
                file->cluster_pointer = file->start_cluster;                                                    // start reading the file with the first sector of the first datacluster.
802
        if(new_cluster)
780
 
-
 
781
                                                                                                                                                                // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory.
-
 
782
                do
-
 
783
                {                                                                                                                                                       // search the next 16 rootentries in this sector of the roordirectory.
-
 
784
                        rootentry=0;
-
 
785
                        SDC_GetSector((u32)(CWD + sector_offset),file->buffer); // Read the Rootdirectory.
-
 
786
                        DirectoryEntry = (struct DirEntry *)file->buffer;
803
        {       // A free cluster was found and can be added to the end of the file.
787
                        while((rootentry<16) && (!retvalue))
-
 
788
                        {
-
 
789
                                if((DirectoryEntry[rootentry].attribute == 0) || (DirectoryEntry[rootentry].attribute == 0xE5)) // empty directory entry found
-
 
790
                                {
-
 
791
                                        for(i=0;i<11;i++) DirectoryEntry[rootentry].name[i] = name[i];          // Kopie the filename and the file extension to the directoryentry.
-
 
792
                                        DirectoryEntry[rootentry].attribute    = _FILE;                                         // Set the fileattribute to archive to reserve the directoryentry.
-
 
793
                                        DirectoryEntry[rootentry].startcluster = cluster;                                               // copy the location of the first datacluster to the directoryentry.
-
 
794
                                        DirectoryEntry[rootentry].size     = 0;                                                                 // the new createted file has no content yet.
804
                fseek_(file, 0, SEEK_END);                                                                                                      // jump to the end of the file
795
                                        file->directory_sector = (u32) (CWD + sector_offset);
805
                last_cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster);            // determine current file cluster
796
                                        file->directory_index  = (u8) rootentry;
806
                fat_byte_offset = ((uint32_t)last_cluster)<<1;
797
                                        retvalue = 1;
807
                sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR);
798
                                        SDC_PutSector((u32)(CWD + sector_offset),file->buffer);
808
                byte = fat_byte_offset % BYTES_PER_SECTOR;
799
                                }
809
 
800
                                rootentry++;
810
                if(file->SectorInCache != sector)
-
 
811
                {
-
 
812
                        file->SectorInCache = sector;                                           // update sector stored in buffer
801
                                cnt_enries_searched++;
813
                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))       // read sector from sd-card
-
 
814
                        {
-
 
815
                                Fat16_Deinit();
-
 
816
                                return(0);
-
 
817
                        }
-
 
818
                }
-
 
819
                fat = (Fat16Entry_t *)(&(file->Cache[byte]));
802
                        }
820
                fat->NextCluster = new_cluster;                                                 // append the free cluster to the end of the file in the FAT.
803
                        if(!retvalue)                                                                                                                                   // file not found in this sector so take next sector.
821
                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))               // save the modified sector to the FAT.
804
                        {
822
                {
805
                                rootentry = 0;
823
                        Fat16_Deinit();
806
                                sector_offset++;
824
                        return(0);
807
                        }
-
 
-
 
825
                }
808
                }
826
                file->FirstSectorOfCurrCluster = Fat16ClusterToSector(new_cluster);
809
                while((cnt_enries_searched< PossibleRootEntries) && (!retvalue));
-
 
-
 
827
                file->SectorOfCurrCluster = 0;
810
 
828
                file->ByteOfCurrSector = 0;
811
        }
829
        }
812
        return(retvalue);                                                                                                                                       // return 1 if file has been created otherwise return 0.
-
 
813
}
830
        return(new_cluster);
814
 
831
}
815
//________________________________________________________________________________________________________________________________________
-
 
816
// Funtion:     u16 SeekFileInDirectory(s8 *fname, File *)
832
 
817
//
-
 
818
// Description: this function searches all possible entries withint the actual directory for the specified object.
-
 
819
//
833
/****************************************************************************************************************************************************/
820
// Returnvalue: This function returns 1 if the directoryentry specified was found.
-
 
-
 
834
/* Function:    DirectoryEntryExist(int8_t *, uint8_t, uint8_t, File_t *)                                                                                                                                                                                       */
-
 
835
/*                                                                                                                                                                                                                                                                                                      */
821
//________________________________________________________________________________________________________________________________________
836
/* Description: This function searches all possible dir entries until the file or directory is found or the end of the directory is reached                     */
-
 
837
/*                                                                                                                                                                                                                                                                                                      */
-
 
838
/* Returnvalue: This function returns 1 if the directory entry specified was found.                                                                                                                                     */
822
 
839
/****************************************************************************************************************************************************/
823
u8 SeekFileInDirectory(s8 *fname, File *file)
-
 
824
{
-
 
825
        u16     rootentry=0;
840
uint8_t DirectoryEntryExist(int8_t *dirname, uint8_t attribfilter, uint8_t attribmask, File_t *file)
826
        u16     end_of_directory_not_reached = 0;                                                                       // the directory has been read completely without a result.
-
 
827
        u8              i=0;
841
{
828
        u8      retvalue = 0;
842
        uint32_t        dir_sector, max_dir_sector, curr_sector;
-
 
843
        uint16_t        dir_entry = 0;
-
 
844
 
829
        u32     cluster_temp = 0;
845
        uint16_t        end_of_directory_not_reached = 0;
-
 
846
        uint8_t         i = 0;
-
 
847
        uint8_t         direntry_exist = 0;
-
 
848
        DirEntry_t * dir;
-
 
849
 
-
 
850
        // if incomming pointers are useless return immediatly
-
 
851
        if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return(direntry_exist);
-
 
852
 
-
 
853
        // dir entries can be searched only in filesclusters that have
-
 
854
        // a corresponding dir entry with adir-flag set in its attribute
-
 
855
        // or direct within the root directory area
-
 
856
 
-
 
857
        file->FirstSectorOfFirstCluster = 0;
-
 
858
        // no current directory exist therefore assume searching in the root
-
 
859
        if(file->DirectorySector == 0)
-
 
860
        {
-
 
861
                max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
-
 
862
                file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector;
-
 
863
        }
-
 
864
        // within the root directory area we can read sectors sequentially until the end of this area
-
 
865
        else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector))
-
 
866
        {
830
        s8 name[11]     = "           ";
867
                max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
831
 
-
 
832
        SeperateFileName(fname,name);
-
 
833
 
-
 
834
        file->cluster_pointer = CWD;                                                                                            // start looking for the file in the actual directory.
-
 
835
 
-
 
836
                                                                                                                                                                // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory.
-
 
837
        do
-
 
838
        {                                                                                                                                                       // search the next 16 rootentries in this sector of the roordirectory.
-
 
839
                rootentry=0;
-
 
840
                SDC_GetSector((u32) file->cluster_pointer,file->buffer);                                        // Read the Rootdirectory.
-
 
841
                DirectoryEntry = (struct DirEntry *)file->buffer;
-
 
842
 
-
 
843
                while((!retvalue)&&(rootentry<16))
-
 
844
                {
-
 
845
                        i=0;
-
 
846
                        if(DirectoryEntry[rootentry].name[0] != 0xe5)                                                   // ignore deleted items.
-
 
847
                        {
-
 
848
                                while((i<=10)&&(DirectoryEntry[rootentry].name[i] == name[i]))
-
 
849
                                {
-
 
850
                                        i++;
868
        }
851
                                }
-
 
852
                        }
869
        // within the data clusters we can read sectors sequentially only within the cluster
853
 
870
        else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector))
854
                        if((i==11) && (DirectoryEntry[rootentry].attribute & _FILE))            // entry found!! -> reading startcluster of entry from offset 26.
871
        {
-
 
872
                max_dir_sector = Partition.SectorsPerCluster;                           // limit max secters before next cluster
855
                        {
873
        }
-
 
874
        else return (direntry_exist); // bad sector range for directory sector of the file
-
 
875
        // if search area is not defined yet
-
 
876
        if(file->FirstSectorOfFirstCluster == 0)
856
                                cluster_temp = (u32)DirectoryEntry[rootentry].startcluster;
877
        {
-
 
878
                // check if the directory entry of current file is existent and has the dir-flag set
-
 
879
                file->SectorInCache = file->DirectorySector;                            // update the sector number of file cache.
-
 
880
                if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
-
 
881
                {
-
 
882
                        Fat16_Deinit();
-
 
883
                        return(direntry_exist);
-
 
884
                }
-
 
885
                dir = (DirEntry_t *)file->Cache;                                                        // set pointer to directory
857
                                cluster_temp -=2;                                                                                                                       // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved.
886
                switch((uint8_t)dir[file->DirectoryIndex].Name[0])                                      // check if current directory exist
-
 
887
                {
858
                                cluster_temp *= (u32)SectorsPerCluster;                                                 // Calculate positon of first cluster.
888
                        case SLOT_EMPTY:
859
                                file->start_cluster   = (FirstDataCluster + cluster_temp);
-
 
860
                                file->directory_sector = (u32) file->cluster_pointer;
-
 
861
                                file->cluster_pointer = file->start_cluster;                                                            // start reading the file with the first cluster.
-
 
862
                                file->filesize = (u32) DirectoryEntry[rootentry].size;
-
 
-
 
889
                        case SLOT_DELETED:
-
 
890
                                // the directrory pointer of this file points to a deleted or not existen directory
-
 
891
                                // therefore no file or subdirectory can be created
-
 
892
                                return (direntry_exist);
863
                                file->directory_index  = (u8) rootentry;
-
 
864
                                retvalue = 1;
-
 
865
                        }
-
 
866
                        rootentry++;
-
 
867
                }
-
 
868
                if(!retvalue)                                                                                   // file not found in this sector so take next sector.
-
 
869
                {
-
 
870
                        end_of_directory_not_reached = GetNextCluster(file);
893
                                break;
871
                }
894
                        default:        // and is a real directory
872
        }
-
 
873
        while((end_of_directory_not_reached) && (!retvalue));
-
 
874
        return(retvalue);
-
 
875
}
-
 
876
 
-
 
877
 
-
 
878
 
-
 
879
//________________________________________________________________________________________________________________________________________
895
                                if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY)
880
// Funtion:     void SeperateFileName(u8*);
896
                                {       // current file is not a directory therefore no file or subdirectory can be created here
881
//
897
                                        return (direntry_exist);
882
// Description: This function seperates the filename and the fileattribute and brings them into the needed format ('test.txt' -> 'TEST    TXT');
898
                                }
883
//
-
 
884
//________________________________________________________________________________________________________________________________________
899
                                break;
885
 
-
 
886
void SeperateFileName(s8 *fname, s8 *name)
900
                }
887
{
-
 
888
        u8 readpointer  = 0;
-
 
889
        u8 writepointer = 0;
901
                file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[file->DirectoryIndex].StartCluster);
-
 
902
        }
-
 
903
 
-
 
904
        // update current file data area position to start of first cluster
-
 
905
        file->FirstSectorOfCurrCluster  = file->FirstSectorOfFirstCluster;
-
 
906
        file->SectorOfCurrCluster               = 0;
-
 
907
        file->ByteOfCurrSector                  = 0;
-
 
908
 
890
        u8 attribute    = 1;
909
        do // loop over all data clusters of the current directory entry
891
        u8 i = 0;
910
        {
-
 
911
                dir_sector = 0; // reset sector counter within a new cluster
-
 
912
                do // loop over all sectors of a cluster or all sectors of the root directory
892
 
913
                {
893
 
914
                        curr_sector = file->FirstSectorOfCurrCluster + dir_sector;      // calculate sector number
-
 
915
                        file->SectorInCache = curr_sector;                                                      // upate the sector number of file cache.
-
 
916
                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector
-
 
917
                        {
-
 
918
                                Fat16_Deinit();
894
        while((writepointer<=10) && (fname[readpointer]!=0))    // the rootdirectoryentry is 8bytes for filename and 3bytes for fileattribute.
919
                                return(direntry_exist);
-
 
920
                        }
-
 
921
                        dir = (DirEntry_t *)file->Cache;                                                        // set pointer to directory
-
 
922
                        // search all directory entries within that sector
-
 
923
                        for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++)
-
 
924
                        {   // check for existing dir entry
-
 
925
                                switch((uint8_t)dir[dir_entry].Name[0])
-
 
926
                                {
-
 
927
                                        case SLOT_EMPTY:
-
 
928
                                        case SLOT_DELETED:
-
 
929
                                                // ignore empty or deleted dir entries
-
 
930
                                                break;
-
 
931
                                        default:
-
 
932
                                                // if existing check attributes before names are compared will safe performance
-
 
933
                                                if ((dir[dir_entry].Attribute & attribmask) != attribfilter) break; // attribute must match
895
        {                                                                                                               // the filename in the rootdirectory is in the format "TEST    TXT" without the dot.
934
                                                // then compare the name to the giveb dirname (first 11 characters include 8 chars of basename and 3 chars extension.)
-
 
935
                                                i = 0;
-
 
936
                                                while((i < 11) && (dir[dir_entry].Name[i] == dirname[i])) i++;
-
 
937
                                                if (i < 10) break; // names does not match
-
 
938
                                                // if dirname and attribute have matched
-
 
939
                                                file->Attribute = dir[dir_entry].Attribute; // store attribute of found dir entry
-
 
940
                                                file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[dir_entry].StartCluster); // set sector of first data cluster
-
 
941
                                                file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster;
-
 
942
                                                file->SectorOfCurrCluster = 0;
-
 
943
                                                file->ByteOfCurrSector = 0;
-
 
944
                                                file->DirectorySector = curr_sector; // current sector
-
 
945
                                                file->DirectoryIndex  = dir_entry; // current direntry in current sector
-
 
946
                                                file->Size = dir[dir_entry].Size;
-
 
947
                                                direntry_exist = 1; // mark as found
-
 
948
                                                dir_entry = DIRENTRIES_PER_SECTOR;      // stop for-loop
-
 
949
                                } // end of first byte of name check
-
 
950
                        }
-
 
951
                        dir_sector++; // search next sector
-
 
952
                // stop if we reached the end of the cluster or the end of the root dir
-
 
953
                }while((dir_sector < max_dir_sector) && (!direntry_exist));
-
 
954
 
-
 
955
                // if we are seaching in the data area and the file not found in this cluster so take next cluster.
-
 
956
                if(!direntry_exist && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster))
-
 
957
                {
-
 
958
                        end_of_directory_not_reached = GetNextCluster(file);  // updates File->FirstSectorOfCurrCluster
-
 
959
                }
-
 
960
        }while((end_of_directory_not_reached) && (!direntry_exist)); // repeat until a next cluster exist an no
-
 
961
        return(direntry_exist);
-
 
962
}
-
 
963
 
-
 
964
 
-
 
965
/****************************************************************************************************************************************/
-
 
966
/*      Function:               CreateDirectoryEntry(int8_t *, uint16_t, File_t *)                                                                                                                                                      */
-
 
967
/*                                                                                                                                                                                                                                                                              */
-
 
968
/*      Description:    This function looks for the next free position in the directory and creates an entry.                                                           */
-
 
969
/*                                      The type of an directory entry is specified by the file attribute.                                                                                                      */
-
 
970
/*                                                                                                                                                                                                                                                                              */
-
 
971
/*      Returnvalue:    Return 0 on error                                                                                                                                                                                                       */
-
 
972
/****************************************************************************************************************************************/
-
 
973
uint8_t CreateDirectoryEntry(int8_t *dirname, uint8_t attrib, File_t *file)
-
 
974
{
-
 
975
        uint32_t        dir_sector, max_dir_sector, curr_sector;
-
 
976
        uint16_t        dir_entry       = 0;
-
 
977
        uint16_t        subdircluster, dircluster = 0;
-
 
978
        uint16_t        end_of_directory_not_reached = 0;
-
 
979
        uint8_t         i                       = 0;
-
 
980
        uint8_t         retvalue        = 0;
-
 
981
        DirEntry_t*     dir;
-
 
982
 
-
 
983
        if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return (retvalue);
-
 
984
        // It is not checked here that the dir entry that should be created is already existent!
-
 
985
 
-
 
986
        // Dir entries can be created only in file-clusters that have
-
 
987
        // the dir-flag set in its attribute or within the root directory
-
 
988
 
-
 
989
        file->FirstSectorOfFirstCluster = 0;
-
 
990
        // no current directory exist therefore assume creating in the root
-
 
991
        if(file->DirectorySector == 0)
-
 
992
        {
-
 
993
                max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
-
 
994
                dircluster = 0;
-
 
995
                file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector;
-
 
996
        }
-
 
997
        // within the root directory area we can read sectors sequentially until the end of this area
-
 
998
        else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector))
-
 
999
        {
-
 
1000
                max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
-
 
1001
        }
-
 
1002
        // within the data clusters we can read sectors sequentially only within the cluster
-
 
1003
        else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector))
-
 
1004
        {
-
 
1005
                max_dir_sector = Partition.SectorsPerCluster;
-
 
1006
        }
-
 
1007
        else return (retvalue); // bad sector range for directory sector of the file
-
 
1008
        // if search area is not defined yet
-
 
1009
        if(file->FirstSectorOfFirstCluster == 0)
-
 
1010
        {
-
 
1011
            // check if the directory entry of current file is existent and has the dir-flag set
-
 
1012
                file->SectorInCache = file->DirectorySector;                            // update the sector number of file cache.
896
                if(fname[readpointer]=='.')                                             // seperating filename and attribute.
1013
                if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
897
                {
1014
                {
898
                        if(attribute)                                                                   // is the filename "." or ".." ?
-
 
899
                        {
-
 
900
                                name[writepointer] = fname[readpointer];
1015
                        Fat16_Deinit();
-
 
1016
                        return(retvalue);
-
 
1017
                }
-
 
1018
                dir = (DirEntry_t *)file->Cache;                                                        // set pointer to directory
-
 
1019
                switch((uint8_t)dir[file->DirectoryIndex].Name[0])                                      // check if current directory exist
901
                                readpointer++;
1020
                {
-
 
1021
                        case SLOT_EMPTY:
-
 
1022
                        case SLOT_DELETED:
-
 
1023
                                return (retvalue);
-
 
1024
                                break;
-
 
1025
                        default:        // and is a real directory
902
                                writepointer++;
1026
                                if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY)
903
                        }
1027
                                {       // current file is not a directory therefore no file or subdirectory can be created here
-
 
1028
                                        return (retvalue);
904
                        else
1029
                                }
-
 
1030
                                break;
-
 
1031
                }
-
 
1032
                dircluster = dir[file->DirectoryIndex].StartCluster;
-
 
1033
                file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dircluster);
-
 
1034
        }
905
                        {
1035
 
-
 
1036
        subdircluster = FindNextFreeCluster(file);      // get the next free cluster on the disk and mark it as used.
-
 
1037
        if(subdircluster)
-
 
1038
        {
-
 
1039
                file->FirstSectorOfCurrCluster  = file->FirstSectorOfFirstCluster;
-
 
1040
                file->SectorOfCurrCluster               = 0;
-
 
1041
                do // loop over all clusters of current directory
-
 
1042
                {
-
 
1043
                        dir_sector = 0; // reset sector counter within a new cluster
-
 
1044
                        do // loop over all sectors of a cluster or all sectors of the root directory
-
 
1045
                        {
-
 
1046
                                curr_sector = file->FirstSectorOfCurrCluster + dir_sector;      // calculate sector number
-
 
1047
                                file->SectorInCache = curr_sector;                                                      // upate the sector number of file cache.
-
 
1048
                                if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
-
 
1049
                                {
-
 
1050
                                        Fat16_Deinit();
-
 
1051
                                        return(retvalue);
-
 
1052
                                }
-
 
1053
                                dir = (DirEntry_t *)file->Cache;                                                        // set pointer to directory
-
 
1054
                                // search all directory entries of a sector
-
 
1055
                                for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++)
-
 
1056
                                {       // check if current direntry is available
-
 
1057
                                        if(((uint8_t)dir[dir_entry].Name[0] == SLOT_EMPTY) || ((uint8_t)dir[dir_entry].Name[0] == SLOT_DELETED))
-
 
1058
                                        {       // a free direntry was found
-
 
1059
                                                for(i = 0; i < 11; i++) dir[dir_entry].Name[i] = dirname[i];            // Set dir name
-
 
1060
                                                dir[dir_entry].Attribute    = attrib;                                                           // Set the attribute of the new directoryentry.
-
 
1061
                                                dir[dir_entry].StartCluster = subdircluster;                                            // copy the location of the first datacluster to the directoryentry.
-
 
1062
                                                dir[dir_entry].DateTime         = FileDateTime(&SystemTime);                    // set date/time
-
 
1063
                                                dir[dir_entry].Size             = 0;                                                                    // the new createted file has no content yet.
-
 
1064
                                                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))       // write back to card
-
 
1065
                                                {
-
 
1066
                                                        Fat16_Deinit();
-
 
1067
                                                        return(retvalue);
-
 
1068
                                                }
-
 
1069
                                                file->FirstSectorOfFirstCluster = Fat16ClusterToSector(subdircluster);  // Calculate absolute sectorposition of first datacluster.
-
 
1070
                                                file->FirstSectorOfCurrCluster  = file->FirstSectorOfFirstCluster;      // Start reading the file with the first sector of the first datacluster.
-
 
1071
                                                file->SectorOfCurrCluster               = 0;                                                            // reset sector of cureen cluster
-
 
1072
                                                file->ByteOfCurrSector                  = 0;                                                            // reset the byte location within the current sector
-
 
1073
                                                file->Attribute                                 = attrib;                                               // set file attribute to dir attribute
-
 
1074
                                                file->Size                                              = 0;                                                        // new file has no size
-
 
1075
                                                file->DirectorySector                   = curr_sector;
-
 
1076
                                                file->DirectoryIndex                    = dir_entry;
-
 
1077
                                                if((attrib & ATTR_SUBDIRECTORY) == ATTR_SUBDIRECTORY)                           // if a new directory was created then initilize the data area
-
 
1078
                                                {
-
 
1079
                                                        ClearCurrCluster(file); // fill cluster with zeros
-
 
1080
                                                        file->SectorInCache = file->FirstSectorOfFirstCluster;
-
 
1081
                                                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector.
-
 
1082
                                                        {
-
 
1083
                                                                Fat16_Deinit();
-
 
1084
                                                                return(retvalue);
-
 
1085
                                                        }
-
 
1086
                                                        dir = (DirEntry_t *)file->Cache;
-
 
1087
                                                        // create direntry "." to current dir
-
 
1088
                                                        dir[0].Name[0] = 0x2E;
-
 
1089
                                                        for(i = 1; i < 11; i++) dir[0].Name[i] = ' ';
-
 
1090
                                                        dir[0].Attribute = ATTR_SUBDIRECTORY;
-
 
1091
                                                        dir[0].StartCluster = subdircluster;
906
                                if(fname[(readpointer-1)] != '*')
1092
                                                        dir[0].DateTime = 0;
-
 
1093
                                                        dir[0].Size = 0;
-
 
1094
                                                        // create direntry ".." to the upper dir
-
 
1095
                                                        dir[1].Name[0] = 0x2E;
-
 
1096
                                                        dir[1].Name[1] = 0x2E;
-
 
1097
                                                        for(i = 2; i < 11; i++) dir[1].Name[i] = ' ';
-
 
1098
                                                        dir[1].Attribute = ATTR_SUBDIRECTORY;
-
 
1099
                                                        dir[1].StartCluster = dircluster;
-
 
1100
                                                        dir[1].DateTime = 0;
-
 
1101
                                                        dir[1].Size = 0;
-
 
1102
                                                        if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// read in the sector.
-
 
1103
                                                        {
-
 
1104
                                                                Fat16_Deinit();
-
 
1105
                                                                return(retvalue);
-
 
1106
                                                        }
-
 
1107
                                                }
-
 
1108
                                                retvalue = 1;
-
 
1109
                                                dir_entry = DIRENTRIES_PER_SECTOR;      // stop for-loop
-
 
1110
                                        }
-
 
1111
                                }
-
 
1112
                                dir_sector++; // search next sector
-
 
1113
                        // stop if we reached the end of the cluster or the end of the root dir
-
 
1114
                        }while((dir_sector < max_dir_sector) && (!retvalue));
-
 
1115
 
-
 
1116
                        // if we are seaching in the data area and the file not found in this cluster so take next cluster.
-
 
1117
                        if(!retvalue && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster))
-
 
1118
                        {
-
 
1119
                                end_of_directory_not_reached = GetNextCluster(file);  // updates File->FirstSectorOfCurrCluster
-
 
1120
                        }
-
 
1121
                }while((end_of_directory_not_reached) && (!retvalue));
-
 
1122
                // Perhaps we are at the end of the last cluster of a directory file an have no free direntry found.
-
 
1123
                // Then we would need to add a cluster to that file and create the new direntry there.
-
 
1124
                // This code is not implemented yet, because its occurs only if more that 32*32=1024 direntries are
-
 
1125
                // within a subdirectory of root.
-
 
1126
        }
-
 
1127
        return(retvalue);       // return 1 if file has been created otherwise return 0.
-
 
1128
}
-
 
1129
 
-
 
1130
/********************************************************************************************************************************************/
-
 
1131
/*      Function:               FileExist(const int8_t* filename, uint8_t attribfilter, uint8_t attribmask, File_t *file);                                                                                      */
-
 
1132
/*                                                                                                                                                                                                                                                                                      */
-
 
1133
/*      Description:    This function looks for the specified file including its subdirectories beginning                                                                               */
-
 
1134
/*                                      in the rootdirectory of the drive. If the file is found the Filepointer properties are                                                                  */
-
 
1135
/*                                      updated.                                                                                                                                                                                                                                */
-
 
1136
/*                                                                                                                                                                                                                                                                                      */
-
 
1137
/*      Returnvalue:    1 if file is found else 0.                                                                                                                                                                                              */
-
 
1138
/********************************************************************************************************************************************/
-
 
1139
uint8_t FileExist(const int8_t* filename, const uint8_t attribfilter, const uint8_t attribmask, File_t *file)
-
 
1140
{
-
 
1141
        int8_t* path = 0;
-
 
1142
        int8_t* subpath = 0;
-
 
1143
        uint8_t af, am, file_exist = 0;
-
 
1144
        int8_t  dirname[12]; // 8+3 + temination character
-
 
1145
 
-
 
1146
        // if incomming pointers are useless return immediatly
907
                                {
1147
        if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0;
908
                                        for(i=writepointer;i<8;i++)
1148
 
909
                                        {
1149
        // trace along the filepath
-
 
1150
        path = (int8_t*)filename;                                                               // start a the beginning of the filename string
910
                                                name[i] = ' ';
1151
        file->DirectorySector = 0;                                                              // start at RootDirectory with file search
911
                                        }
1152
        file->DirectoryIndex = 0;
912
                                }
-
 
913
                                readpointer++;
-
 
914
                                writepointer = 8;
-
 
915
                        }
-
 
916
                }
1153
        // as long as the file was not found and the remaining path is not empty
917
                else if(fname[readpointer] == '*')                                      // wildcard found within the filename + extension.
1154
        while((*path != 0) && !file_exist)
918
                {
1155
        {       // separate dirname and subpath from filepath string
919
                        if(writepointer < 8)                                            // in extension.
1156
                subpath = SeperateDirName(path, dirname);
920
                        {
1157
                if(subpath != NULL)
921
                                readpointer++;
1158
                {
922
                                writepointer = 8;
1159
                        if(*subpath == 0)
923
                        }
1160
                        {       // empty subpath indicates last element of dir chain
924
                        else                                                                            // in filename.
1161
                                af = attribfilter;
925
                        {
1162
                                am = attribmask;
926
                                writepointer = 11;                                              // jump to the end of the string to terminate this function.
1163
                        }
927
                        }
1164
                        else  // it must be a subdirectory and no volume label
928
                        attribute = 0;
1165
                        {
929
                }
1166
                                af = ATTR_SUBDIRECTORY;
930
                else
1167
                                am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL;
931
                {
1168
                        }
932
                        if((fname[readpointer]>96) && (fname[readpointer]<123))
1169
                        if(!DirectoryEntryExist(dirname, af, am, file))
933
                        {
1170
                        {
934
                                name[writepointer]=(fname[readpointer] - 32);                                   // all characters must be upper case.
1171
                                return (file_exist); // subdirectory does not exist
935
                        }
1172
                        }
936
                        else
1173
                        else
937
                        {
1174
                        {
938
                                name[writepointer]=fname[readpointer];
1175
                                if (*subpath == 0)
-
 
1176
                                {
-
 
1177
                                        file_exist = 1; // last element of path chain was found with the given attribute filter
-
 
1178
                                }
939
                        }
1179
                        }
940
                        readpointer++;
-
 
941
                        writepointer++;
-
 
942
                        attribute = 0;
-
 
943
                }
1180
                }
-
 
1181
                else // error seperating the subpath
-
 
1182
                {
-
 
1183
                        return file_exist; // bad subdir format
-
 
1184
                }
-
 
1185
                path = subpath;
-
 
1186
                subpath = 0;
944
        }
1187
        }
-
 
1188
        return (file_exist);
945
}
1189
}
946
 
-
 
947
//________________________________________________________________________________________________________________________________________
-
 
948
// Funtion:     File * ReserveFilePointer_(void);
-
 
949
//
-
 
950
// Description: This function looks for a free filepointer and reserves it.
-
 
951
//
-
 
952
//
-
 
953
//      Return:         NULL = faild to reserve a filepointer
-
 
954
//                              otherwise filepointer
-
 
955
//________________________________________________________________________________________________________________________________________
-
 
956
 
-
 
957
File * ReserveFilePointer(void)
-
 
958
{
-
 
959
        File *file;
-
 
960
        file = NULL;
-
 
-
 
1190
 
-
 
1191
 
-
 
1192
/********************************************************************************************************************************************/
-
 
1193
/*      Function:               FileCreate(const s8* filename, u8 attrib, File_t *file);                                                                                                                                */
-
 
1194
/*                                                                                                                                                                                                                                                                                      */
-
 
1195
/*      Description:    This function looks for the specified file including its subdirectories beginning                                                                               */
-
 
1196
/*                                      in the rootdirectory of the partition. If the file is found the Filepointer properties are                                                              */
-
 
1197
/*                                      updated. If file or its subdirectories are not found they will be created                                                                                               */
-
 
1198
/*                                                                                                                                                                                                                                                                                      */
-
 
1199
/*      Returnvalue:    1 if file was created else 0.                                                                                                                                                                                   */
-
 
1200
/********************************************************************************************************************************************/
-
 
1201
uint8_t FileCreate(const int8_t* filename, const uint8_t attrib, File_t *file)
-
 
1202
{
-
 
1203
        int8_t *path = 0;
-
 
1204
        int8_t *subpath = 0;
-
 
1205
        uint8_t af, am, file_created = 0;
-
 
1206
        int8_t dirname[12];
-
 
1207
 
-
 
1208
        // if incomming pointers are useless return immediatly
-
 
1209
        if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0;
-
 
1210
 
-
 
1211
        // trace along the filepath
-
 
1212
        path = (int8_t*)filename;                                                                       // start a the beginning of the filename string
-
 
1213
        file->DirectorySector = 0;                                                              // start at RootDirectory with file search
-
 
1214
        file->DirectoryIndex = 0;
-
 
1215
        // as long as the file was not created and the remaining file path is not empty
961
        u8      temp;
1216
        while((*path != 0) && !file_created)
-
 
1217
        {   // separate dirname and subpath from filepath string
962
 
1218
                subpath = SeperateDirName(path, dirname);
-
 
1219
                if(subpath != NULL)
-
 
1220
                {
-
 
1221
                        if(*subpath == 0)
-
 
1222
                        {       // empty subpath indicates last element of dir chain
-
 
1223
                                af = ATTR_NONE;
-
 
1224
                                am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL;  // any file that is no subdir or volume label
-
 
1225
                        }
-
 
1226
                        else  // it must be a subdirectory and no volume label
-
 
1227
                        {
-
 
1228
                                af = ATTR_SUBDIRECTORY;
963
        for(temp = 0;temp<__MAX_FILES_USED;temp++)
1229
                                am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL;
-
 
1230
                        }
-
 
1231
                        if(!DirectoryEntryExist(dirname, af, am, file)) // if subdir or file is not existent
-
 
1232
                        {  // try to create subdir or file
-
 
1233
                                if(*subpath == 0) af = attrib; // if last element in dir chain take the given attribute
-
 
1234
                                if(!CreateDirectoryEntry(dirname, af, file))
-
 
1235
                                {       // could not be created
-
 
1236
                                        return(file_created);
-
 
1237
                                }
-
 
1238
                                else if (*subpath == 0) file_created = 1; // last element of path chain was created
-
 
1239
                        }
964
        {
1240
                }
965
                if(FilePointer[temp].state == _UNUSED)                                                                          // free filepointer found?
-
 
966
                {
1241
                else // error seperating the subpath
967
                        file = &FilePointer[temp];
-
 
968
                        FilePointer[temp].state                                 = _USED;                                                // mark as used.
-
 
969
                        FilePointer[temp].mode                                  = 0;                                                    // type of access (read/write) not defined yet.
-
 
970
                        FilePointer[temp].start_cluster                 = 0;                                                    // Sectorpointer to the first sector of the first datacluster of the file.
-
 
971
                        FilePointer[temp].cluster_pointer               = 0;                                                    // Pointer to the cluster which is edited at the moment.
-
 
972
                        FilePointer[temp].sector_index                  = 0;                                                    // The sector which is edited at the moment (cluster_pointer + sector_index).
-
 
973
                        FilePointer[temp].byte_index                    = 0;                                                    // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index).
-
 
974
                        FilePointer[temp].filesize                              = 0;                                                    // the size of the opend file in bytes.
-
 
975
                        FilePointer[temp].fileposition                  = 0;                                                    // pointer to a character within the file 0 < fileposition < filesize
-
 
976
                        FilePointer[temp].sector_in_buffer              = 0;                                                    // the last sector read, wich is still in the sectorbuffer.
-
 
977
                        FilePointer[temp].directory_sector              = 0;                                                    // the sectorposition where the directoryentry has been made.
-
 
978
                        FilePointer[temp].directory_index               = 0;                                                    // the index to the directoryentry within the specified sector.
-
 
979
                        FilePointer[temp].attribute                     = 0;                                                    // the attribute of the file opened.
1242
                {
-
 
1243
                        return file_created; // bad subdir format
-
 
1244
                }
980
                        break;
1245
                path = subpath;
981
                }
1246
                subpath = 0;
982
        }
1247
        }
983
        return(file);
1248
        return (file_created);
984
}
1249
}
985
 
1250
 
-
 
1251
 
-
 
1252
/********************************************************************************************************************************************/
-
 
1253
/*      Function:               File_t * fopen_(int8_t* filename, int8_t mode);                                                                                                                                                                 */
986
 
1254
/*                                                                                                                                                                                                                                                                                      */
-
 
1255
/*      Description:    This function looks for the specified file in the rootdirectory of the drive. If the file is found the number of the    */
-
 
1256
/*                                      corrosponding filepointer is returned. Only modes 'r' (reading) and 'a' append are implemented yet.                                             */
-
 
1257
/*                                                                                                                                                                                                                                                                                      */
-
 
1258
/*      Returnvalue:    The filepointer to the file or 0 if faild.                                                                                                                                                              */
987
//________________________________________________________________________________________________________________________________________
1259
/********************************************************************************************************************************************/
988
// Funtion:     void FreeFilePointer_(File *);
1260
File_t * fopen_(const int8_t *filename, const int8_t mode)
989
//
1261
{
990
// Description: This function free's the filepointer by marking it as unused.
1262
        File_t *file    = 0;
-
 
1263
 
991
//
1264
        if((!Partition.IsValid) || (filename == 0)) return(file);
-
 
1265
 
992
//
1266
        // Look for an unused filepointer in the file pointer list?
-
 
1267
        file = LockFilePointer();
-
 
1268
        // if no unused file pointer was found return 0
993
//      Return:         none
1269
        if(file == NULL) return(file);
-
 
1270
 
-
 
1271
        // now we have found a free filepointer and claimed it
-
 
1272
        // so let initiate its property values
-
 
1273
        file->FirstSectorOfFirstCluster = 0;            // Sectorpointer to the first sector of the first datacluster of the file.
-
 
1274
        file->FirstSectorOfCurrCluster  = 0;            // Pointer to the cluster which is edited at the moment.
994
//
1275
        file->SectorOfCurrCluster               = 0;            // The sector which is edited at the moment (cluster_pointer + sector_index).
-
 
1276
        file->ByteOfCurrSector                  = 0;            // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index).
-
 
1277
        file->Mode                                              = mode;         // mode of fileoperation (read,write)
-
 
1278
        file->Size                                              = 0;            // the size of the opened file in bytes.
-
 
1279
        file->Position                                  = 0;            // pointer to a byte within the file 0 < fileposition < filesize
-
 
1280
        file->SectorInCache                             = 0;            // the last sector read, wich is still in the sectorbuffer.
-
 
1281
        file->DirectorySector                   = 0;            // the sectorposition where the directoryentry has been made.
-
 
1282
        file->DirectoryIndex                    = 0;            // the index to the directoryentry within the specified sector.
995
//________________________________________________________________________________________________________________________________________
1283
        file->Attribute                                 = 0;            // the attribute of the file opened.
-
 
1284
 
-
 
1285
        // check if a real file (no directory) to the given filename exist
-
 
1286
        if(FileExist(filename, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, file))
996
 
1287
        {  // file exist
997
void FreeFilePointer(File *file)
1288
                switch(mode)  // check mode
-
 
1289
                {
-
 
1290
                        case 'a':       // if mode is: append to file
-
 
1291
                                if((file->Attribute & ATTR_READONLY) == ATTR_READONLY)
-
 
1292
                                {       // file is marked as readonly --> do not open this file
-
 
1293
                                        fclose_(file);
-
 
1294
                                        file = NULL;
-
 
1295
                                }
-
 
1296
                                else
-
 
1297
                                {       // file is not marked as read only --> goto end of file
-
 
1298
                                        fseek_(file, 0, SEEK_END);              // point to the end of the file
-
 
1299
                                }
-
 
1300
                                break;
-
 
1301
                        case 'w':       // if mode is: write to file
-
 
1302
                                if((file->Attribute & ATTR_READONLY) == ATTR_READONLY)
-
 
1303
                                {       // file is marked as readonly --> do not open this file
-
 
1304
                                        fclose_(file);
-
 
1305
                                        file = NULL;
-
 
1306
                                }
-
 
1307
                                else
-
 
1308
                                {       // file is not marked as read only --> goto start of file
-
 
1309
                                        // free all clusters of that file
-
 
1310
                                        DeleteClusterChain(SectorToFat16Cluster(file->FirstSectorOfFirstCluster));
-
 
1311
                                        // mar an empy cluster as the last one and store the corresponding sector
-
 
1312
                                        file->FirstSectorOfFirstCluster = Fat16ClusterToSector(FindNextFreeCluster(file));
-
 
1313
                                        file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster;
-
 
1314
                                        file->SectorOfCurrCluster = 0;
-
 
1315
                                        file->ByteOfCurrSector = 0;
-
 
1316
                                        file->Size = 0;
-
 
1317
                                        file->Position = 0;
-
 
1318
                                        fseek_(file, 0, SEEK_SET);
-
 
1319
                                }
-
 
1320
                                break;
-
 
1321
                        case 'r':       // if mode is: read from file
-
 
1322
                                // goto end of file
-
 
1323
                                fseek_(file, 0, SEEK_SET);
-
 
1324
                                break;
-
 
1325
                        default: // other modes are not supported
998
{
1326
                                fclose_(file);
-
 
1327
                                file = NULL;
-
 
1328
                        break;
-
 
1329
                }
999
        u8 cnt = 0;
1330
                return(file);
1000
 
1331
        }
1001
        for(cnt=0;cnt<__MAX_FILES_USED;cnt++)                                                                                   // Is the filepointeradress vaild?
1332
        else // file does not exist
1002
        {
1333
        {
1003
                if(&FilePointer[cnt] == file)                                                                                           // filepointer found therefore it must be valid
1334
                switch(mode)  // check mode
-
 
1335
                {
1004
                {
1336
                        case 'a':
-
 
1337
                        case 'w': // if mode is write or append
-
 
1338
                                // try to create the file
-
 
1339
                                if(!FileCreate(filename, ATTR_ARCHIVE, file))
-
 
1340
                                { // if it could not be created
-
 
1341
                                        fclose_(file);
-
 
1342
                                        file = NULL;
-
 
1343
                                }
-
 
1344
                                break;
-
 
1345
                        case 'r': // else opened for 'r'
-
 
1346
                        default:  // of unsupported mode
-
 
1347
                                fclose_(file);
-
 
1348
                                file = NULL;
1005
                        FilePointer[cnt].state = _UNUSED;                                                                               // and can be marked as unused.
1349
                                break;
-
 
1350
                }
1006
                }
1351
                return(file);
-
 
1352
        }
-
 
1353
        // we should never come to this point
-
 
1354
        fclose_(file);
-
 
1355
        file = NULL;
1007
        }
1356
        return(file);
-
 
1357
}
-
 
1358
 
-
 
1359
/****************************************************************************************************************************************************/
-
 
1360
/* Function:    fflush_(File *);                                                                                                                                                                                                                                        */
-
 
1361
/*                                                                                                                                                                                                                                                                                                      */
-
 
1362
/* Description: This function writes the data already in the buffer but not yet written to the file.                                                                                            */
-
 
1363
/*                                                                                                                                                                                                                                                                                                      */
-
 
1364
/* Returnvalue: 0 on success EOF on error                                                                                                                                                                                                                       */
-
 
1365
/****************************************************************************************************************************************************/
-
 
1366
int16_t fflush_(File_t *file)
-
 
1367
{
-
 
1368
        DirEntry_t *dir;
-
 
1369
 
-
 
1370
        if((file == NULL) || (!Partition.IsValid)) return (EOF);
-
 
1371
 
-
 
1372
        switch(file->Mode)
-
 
1373
        {
-
 
1374
                case 'a':
-
 
1375
                case 'w':
-
 
1376
                        if(file->ByteOfCurrSector > 0)                                                                          // has data been added to the file?
-
 
1377
                        {
-
 
1378
                                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// save the data still in the buffer
-
 
1379
                                {
-
 
1380
                                        Fat16_Deinit();
-
 
1381
                                        return(EOF);
-
 
1382
                                }
-
 
1383
                        }
-
 
1384
                        file->SectorInCache     = file->DirectorySector;
-
 
1385
                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))                                       // read the directory entry for this file.
-
 
1386
                        {
-
 
1387
                                Fat16_Deinit();
1008
}
-
 
1009
 
1388
                                return(EOF);
1010
 
-
 
1011
//________________________________________________________________________________________________________________________________________
1389
                        }
1012
// Funtion:     void DelteDirectoryEntry(Find *)
-
 
1013
//
-
 
1014
// Description: This function deletes the directoryentry of the specified item.
1390
 
1015
//
1391
                        dir = (DirEntry_t *)file->Cache;
1016
//
1392
                        dir[file->DirectoryIndex].Size = file->Size;                                            // update file size
1017
// returnvalue: 1 if the directory could be created.
1393
                        dir[file->DirectoryIndex].DateTime = FileDateTime(&SystemTime);         // update date time
-
 
1394
                        if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))       // write back to sd-card
1018
//________________________________________________________________________________________________________________________________________
1395
                        {
1019
 
1396
                                Fat16_Deinit();
1020
void DeleteDirectoryEntry(Find *item)
1397
                                return(EOF);
1021
{
1398
                        }
1022
        u8 buffer[512];
-
 
1023
 
1399
                        break;
-
 
1400
                case 'r':
1024
 
-
 
1025
        SDC_GetSector((u32) item->cluster_pointer,buffer);                                                              // Read the Rootdirectory.
-
 
-
 
1401
                default:
1026
        DirectoryEntry = (struct DirEntry *)buffer;
1402
                        return(EOF);
1027
 
1403
                        break;
-
 
1404
 
-
 
1405
        }
-
 
1406
        return(0);
-
 
1407
}
-
 
1408
 
-
 
1409
/****************************************************************************************************************************************/
-
 
1410
/*      Function:               fclose_(File *file);                                                                                                                                                                                            */
-
 
1411
/*                                                                                                                                                                                                                                                                              */
-
 
1412
/*      Description:    This function closes the open file by writing the remaining data                                                                                                        */
-
 
1413
/*                                      from the buffer to the device and entering the filesize in the directory entry.                                                                         */
-
 
1414
/*                                                                                                                                                                                                                                                                              */
-
 
1415
/*      Returnvalue:    0 on success EOF on error                                                                                                                                                                                       */
-
 
1416
/****************************************************************************************************************************************/
-
 
1417
int16_t fclose_(File_t *file)
-
 
1418
{
-
 
1419
        int16_t returnvalue = EOF;
1028
        DirectoryEntry[(item->directory_index)-1].attribute = 0;                                                        // free the directoryentry.
-
 
1029
        DirectoryEntry[(item->directory_index)-1].name[0] = 0xE5;                                                       // free the directoryentry.
1420
 
-
 
1421
        if(file == NULL) return(returnvalue);
1030
        SDC_PutSector((u32) item->cluster_pointer,buffer);                                                              // Read the Rootdirectory.
1422
        returnvalue = fflush_(file);
1031
}
-
 
1032
 
1423
        UnlockFilePointer(file);
1033
 
-
 
1034
 
-
 
-
 
1424
        return(returnvalue);
-
 
1425
}
1035
 
1426
 
1036
//________________________________________________________________________________________________________________________________________
1427
/********************************************************************************************************************************************/
1037
// Funtion:     u8 CreateSubDirectory(u8 *)
-
 
1038
//
1428
/*      Function:               fgetc_(File *file);                                                                                                                                                                                                             */
1039
// Description: This function creates an directory within the directory specified by CWD
1429
/*                                                                                                                                                                                                                                                                                      */
1040
//
-
 
1041
//
-
 
1042
// returnvalue: 1 if the directory could be created.
1430
/*      Description:    This function reads and returns one character from the specified file. Is the end of the actual sector reached the              */
1043
//________________________________________________________________________________________________________________________________________
-
 
1044
 
-
 
1045
u8 CreateSubDirectory(s8 *fname)
1431
/*                                      next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT.    */
1046
{
-
 
1047
        u16     index   = 0;                                                                                                                            // index to an entry in the rootdirectory.
-
 
1048
        u16     cnt_entries_searched = 0;                                                                                                               // count the number of rootentries which have been searched already.
-
 
1049
        u16             i = 0;
1432
/*                                                                                                                                                                                                                                                                                      */
1050
        u16     sector_offset = 0;                                                                                                                              // index to the sector of the entry which is searched momentarily
1433
/*      Returnvalue:    The function returns the character read from the specified memorylocation as u8 casted to s16 or EOF.                                   */
1051
        u8              retvalue = 0;
-
 
1052
        u32             cluster_temp = 0;
1434
/********************************************************************************************************************************************/
1053
        u16             cluster = 0;
1435
int16_t fgetc_(File_t *file)
1054
        File    file;
1436
{
1055
        u8              name[11] = {"           "};
-
 
1056
 
1437
        int16_t c = EOF;
1057
        SeperateFileName(fname,name);
-
 
1058
        cluster_temp = (u32)FindNextFreeCluster(&file);                                                                         // the next free cluster on the disk.
-
 
1059
 
-
 
1060
        if(cluster_temp)                                                                                                                                                // if a free cluster is available:
-
 
1061
        {
-
 
1062
                cluster = (u16)cluster_temp;                                                                                                            // remember the index of the free datacluster found for the directory entry.
1438
        uint32_t curr_sector;
1063
                cluster_temp -=2;                                                                                                                                       // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved.
-
 
1064
                cluster_temp *= SectorsPerCluster;                                                                                                      // Calculate relative sectorindex of first datacluster.
-
 
1065
                file.start_cluster   = (FirstDataCluster + cluster_temp);                                                       // Calculate absolute sectorposition of first datacluster.
1439
 
-
 
1440
        if( (!Partition.IsValid) || (file == NULL)) return(c);
1066
                file.cluster_pointer = file.start_cluster;                                                                                      // start reading the file with the first sector of the first datacluster.
1441
        // if the end of the file is not reached, get the next character.
-
 
1442
        if((0 < file->Size) && ((file->Position+1) < file->Size) )
-
 
1443
        {
-
 
1444
                curr_sector  = file->FirstSectorOfCurrCluster;          // calculate the sector of the next character to be read.
-
 
1445
                curr_sector += file->SectorOfCurrCluster;
1067
 
1446
 
1068
// -Initialise new cluster to zero--------------------------------------------------------
-
 
1069
                for(i=0;i<512;i++)
1447
                if(file->SectorInCache != curr_sector)
1070
                {
-
 
1071
                        file.buffer[i] = 0;                                                                                                                             // initialise buffer to zero
1448
                {
1072
                }
1449
                        file->SectorInCache = curr_sector;
1073
                for(sector_offset=0;sector_offset<SectorsPerCluster;sector_offset++)                            // initialise all sectors of new cluster with buffer.
-
 
1074
                {
-
 
1075
                        SDC_PutSector((u32)(file.start_cluster + sector_offset),file.buffer);                   // save the initialised sector to the card.
-
 
1076
                }
1450
                        if(SD_SUCCESS != SDC_GetSector(file->SectorInCache,file->Cache))
1077
// -Create directoryentry "." -------------------------------------------------------------
-
 
1078
                DirectoryEntry = (struct DirEntry *)file.buffer;
1451
                        {
1079
                DirectoryEntry[0].name[0] = '.';
-
 
1080
                DirectoryEntry[0].attribute = _DIRECTORY;
-
 
1081
                DirectoryEntry[0].startcluster = cluster;
-
 
1082
// -Create directoryentry "." -------------------------------------------------------------
-
 
1083
                DirectoryEntry[1].name[0] = '.';
1452
                                Fat16_Deinit();
1084
                DirectoryEntry[1].name[1] = '.';
-
 
1085
                DirectoryEntry[1].attribute = _DIRECTORY;
-
 
1086
                if(CWD == RootDirectory)
1453
                                return(c);
1087
                {
-
 
1088
                        DirectoryEntry[1].startcluster = 0;
1454
                        }
1089
                }
-
 
1090
                else
-
 
1091
                {
-
 
1092
                        cluster_temp = (CWD - FirstDataCluster);
-
 
1093
                        cluster_temp /= SectorsPerCluster;
-
 
1094
                        cluster_temp -= 2;
-
 
1095
                        DirectoryEntry[1].startcluster = (u16) cluster_temp;
-
 
1096
                }
-
 
1097
                SDC_PutSector((u32) file.start_cluster,file.buffer);                                                                                    // save the initialised sector to the card.
-
 
1098
// -create directoryentry within the cwd --------------------------------------------------
1455
                }
-
 
1456
                c = (int16_t) file->Cache[file->ByteOfCurrSector];
1099
                sector_offset = 0;
1457
                file->Position++;                                                                       // increment file position
1100
                cnt_entries_searched = 0;
1458
                file->ByteOfCurrSector++;                                                       // goto next byte in sector
1101
                do
1459
                if(file->ByteOfCurrSector >= BYTES_PER_SECTOR)          // if end of sector
-
 
1460
                {
1102
                {                                                                                                                                                                       // search the next 16 rootentries in this sector of the roordirectory.
1461
                        file->ByteOfCurrSector = 0;                                             //  reset byte location
-
 
1462
                        file->SectorOfCurrCluster++;                                    //      next sector
1103
                        index=0;
1463
                        if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster)    // if end of cluster is reached, the next datacluster has to be searched in the FAT.
1104
                        SDC_GetSector((u32)(CWD + sector_offset),file.buffer);                                                  // Read the actual directory.
1464
                        {
1105
                        DirectoryEntry = (struct DirEntry *)file.buffer;
-
 
1106
                        while((index<16) && (!retvalue))
-
 
1107
                        {
-
 
1108
                                if((DirectoryEntry[index].attribute == 0) || (DirectoryEntry[index].attribute == 0xE5)) // empty directory entry found
-
 
1109
                                {
1465
 
1110
                                        for(i=0;i<11;i++) DirectoryEntry[index].name[i] = name[i];              // Kopie the filename and the file extension to the directoryentry.
-
 
1111
                                        DirectoryEntry[index].attribute    = _DIRECTORY;                                        // Set the fileattribute to archive to reserve the directoryentry.
-
 
1112
                                        DirectoryEntry[index].startcluster = cluster;                                           // copy the location of the first datacluster to the directoryentry.
-
 
1113
                                        DirectoryEntry[index].size     = 0;                                                                     // the new createted file has no content yet.
-
 
1114
                                        file.directory_sector = (u32) (CWD + sector_offset);
-
 
1115
                                        file.directory_index  = (u8) index;
-
 
1116
                                        retvalue = 1;
-
 
1117
                                        SDC_PutSector((u32)(CWD + sector_offset),file.buffer);
1466
                                if(GetNextCluster(file))                                                                                // Sets the clusterpointer of the file to the next datacluster.
1118
                                }
1467
                                {
1119
                                index++;
-
 
1120
                                cnt_entries_searched++;
-
 
1121
                        }
1468
                                        file->SectorOfCurrCluster = 0;                                                          // start reading new cluster at first sector of the cluster.
1122
                        if(!retvalue)                                                                                                                                   // file not found in this sector so take next sector.
1469
                                }
1123
                        {
1470
                                else // the last cluster was allready reached
1124
                                index = 0;
-
 
1125
                                sector_offset++;
1471
                                {
-
 
1472
                                        file->SectorOfCurrCluster--;                                                    // jump back to the last sector in the last cluster
1126
                        }
1473
                                        file->ByteOfCurrSector = BYTES_PER_SECTOR;                              // set ByteOfCurrSector one byte over sector end
1127
                }
-
 
1128
                while((cnt_entries_searched< PossibleRootEntries) && (!retvalue));
1474
                                }
1129
 
-
 
-
 
1475
                        }
-
 
1476
                }
1130
        }
1477
        }
1131
        return(retvalue);                                                                                                                                       // return 1 if file has been created otherwise return 0.
1478
        return(c);
1132
}
-
 
1133
 
1479
}
1134
 
1480
 
1135
//________________________________________________________________________________________________________________________________________
-
 
1136
// Funtion:     u16 SeekSubDirectory(s8 *fname)
-
 
1137
//
-
 
1138
// Description: looks for the specified directory within the CWD.
1481
/********************************************************************************************************************************************/
1139
//
-
 
1140
// Returnvalue: If the specified directory was found the startcluster is returned. otherwise 0.
-
 
1141
//________________________________________________________________________________________________________________________________________
1482
/*      Function:               fputc_( const s8 c, File *file);                                                                                                                                                                                */
1142
 
1483
/*                                                                                                                                                                                                                                                                                      */
1143
u16 SeekSubDirectory(s8 *fname)
1484
/*      Description:    This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries.                                 */
-
 
1485
/*                                      next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT.    */
-
 
1486
/*                                                                                                                                                                                                                                                                                      */
-
 
1487
/*      Returnvalue:    The function returns the character written to the stream or EOF on error.                                                                                               */
-
 
1488
/********************************************************************************************************************************************/
-
 
1489
int16_t fputc_(const int8_t c, File_t *file)
1144
{
1490
{
-
 
1491
        uint32_t curr_sector  = 0;
-
 
1492
 
1145
        u16     index = 0;
1493
        if((!Partition.IsValid) || (file == NULL)) return(EOF);
-
 
1494
 
1146
        u16     end_of_directory_not_reached = 0;                                                                       // the directory has been read completely without a result.
1495
        // If file position equals to file size, then the end of file has reached.
1147
        u8              i = 0;
1496
        // In this chase it has to be checked that the ByteOfCurrSector is BYTES_PER_SECTOR
1148
        u16     cluster_temp = 0;
1497
        // and a new cluster should be appended.
1149
        s8 name[11]     = "           ";
1498
        if((file->Position >= file->Size) && (file->ByteOfCurrSector >= BYTES_PER_SECTOR))
-
 
1499
        {
-
 
1500
                if(!AppendCluster(file)) return(EOF);
-
 
1501
        }
-
 
1502
 
-
 
1503
        curr_sector  = file->FirstSectorOfCurrCluster;
-
 
1504
        curr_sector += file->SectorOfCurrCluster;
-
 
1505
        if(file->SectorInCache != curr_sector)
1150
        File    file;
1506
        {
-
 
1507
                file->SectorInCache = curr_sector;
1151
 
1508
                if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))
-
 
1509
                {
-
 
1510
                        Fat16_Deinit();
1152
        SeperateFileName(fname,name);
1511
                        return(EOF);
1153
 
1512
                }
-
 
1513
        }
-
 
1514
 
1154
        file.cluster_pointer = CWD;                                                                                                     // start looking for the file in the actual directory.
1515
        file->Cache[file->ByteOfCurrSector] = (uint8_t)c;                       // write databyte into the buffer. The byte will be written to the device at once
-
 
1516
        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.
1155
        file.start_cluster   = CWD;                                                                                                     // start looking for the file in the actual directory.
1517
        file->Position++;                                                                       // the actual positon within the file.
-
 
1518
        file->ByteOfCurrSector++;                                                       // goto next byte in sector
1156
 
1519
        if(file->ByteOfCurrSector >= BYTES_PER_SECTOR)          // if the end of this sector is reached yet
-
 
1520
        {       // save the sector to the sd-card
-
 
1521
                if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))
1157
                                                                                                                                                                // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory.
1522
                {
1158
        do
1523
                        Fat16_Deinit();
1159
        {                                                                                                                                                       // search the next 16 rootentries in this sector of the roordirectory.
1524
                        return(EOF);
1160
                index=0;
-
 
1161
                SDC_GetSector((u32) file.cluster_pointer,file.buffer);                                  // Read the Rootdirectory.
1525
                }
1162
                DirectoryEntry = (struct DirEntry *)file.buffer;
1526
                file->ByteOfCurrSector = 0;                                             //  reset byte location
1163
 
1527
                file->SectorOfCurrCluster++;                                    //      next sector
1164
                while((!cluster_temp)&&(index<16))
1528
                if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster)// if end of cluster is reached, the next datacluster has to be searched in the FAT.
1165
                {
-
 
1166
                        i=0;
-
 
1167
                        if(DirectoryEntry[index].name[0] != 0xe5)                                                       // ignore deleted items.
-
 
1168
                        {
-
 
1169
                                while((i<=10)&&(DirectoryEntry[index].name[i] == name[i]))
-
 
1170
                                {
1529
                {
1171
                                        i++;
1530
                        if(!GetNextCluster(file))                                                               // Sets the clusterpointer of the file to the next datacluster.
1172
                                }
-
 
1173
                        }
1531
                        { // if current cluster was the last cluster of the file
1174
 
1532
                                if(!AppendCluster(file))                                                // append a new and free cluster at the end of the file.
1175
                        if((i==11) && (DirectoryEntry[index].attribute & _DIRECTORY))   // entry found!! -> reading startcluster of entry from offset 26.
1533
                                {
1176
                        {
1534
                                        file->SectorOfCurrCluster--;                            // jump back to last sector of last cluster
1177
                                cluster_temp = (u16)DirectoryEntry[index].startcluster;
1535
                                        file->ByteOfCurrSector = BYTES_PER_SECTOR;      // set byte location to 1 byte over sector len
1178
                        }
1536
                                        return(EOF);
1179
                        index++;
1537
                                }
1180
                }
1538
                        }
1181
                if(!cluster_temp)                                                                                       // file not found in this sector so take next sector.
1539
                        else // next cluster
1182
                {
1540
                        {
1183
                        end_of_directory_not_reached = GetNextCluster(&file);
1541
                                file->SectorOfCurrCluster = 0;                                                  // start reading new cluster at first sector of the cluster.
1184
                }
1542
                        }
1185
        }
1543
                }
1186
        while((end_of_directory_not_reached) && (!cluster_temp));
1544
        }
1187
        return(cluster_temp);
1545
        return(0);
1188
}
1546
}
1189
 
1547
 
-
 
1548
 
-
 
1549
/****************************************************************************************************************************************/
-
 
1550
/*      Function:               fread_(void *buffer, uint32_t size, uint32_t count, File *File);                                                                                                                                */
-
 
1551
/*                                                                                                                                                                                                                                                                              */
-
 
1552
/*      Description:    This function reads count objects of the specified size                                                                                                                         */
-
 
1553
/*                                      from the actual position of the file to the specified buffer.                                                                                                           */
-
 
1554
/*                                                                                                                                                                                                                                                                              */
-
 
1555
/*      Returnvalue:    The function returns the number of objects (not bytes) read from the file.                                                                                      */
-
 
1556
/****************************************************************************************************************************************/
-
 
1557
uint32_t fread_(void *buffer, uint32_t size, uint32_t count, File_t *file)
-
 
1558
{
-
 
1559
        uint32_t object_cnt     = 0;                                                                                    // count the number of objects read from the file.
-
 
1560
        uint32_t object_size = 0;                                                                                       // count the number of bytes read from the actual object.
-
 
1561
        uint8_t *pbuff          = 0;                                                                                    // a pointer to the actual bufferposition.
-
 
1562
        uint8_t success      = 1;                                                                                       // no error occured during read operation to the file.
1190
 
-
 
1191
 
-
 
1192
//________________________________________________________________________________________________________________________________________
-
 
1193
// Funtion:     u8 mkdir_(u8 *)
-
 
1194
//
-
 
1195
// Description: This function checks if the directory to be created already exists. If not the directory will be created.
-
 
1196
//
1563
        int16_t c;
1197
//
-
 
1198
// returnvalue: 1 if the directory could be created.
-
 
1199
//________________________________________________________________________________________________________________________________________
-
 
1200
 
-
 
1201
u8      mkdir_(s8 *fname)
-
 
1202
{
1564
 
1203
        u8 retvalue = 0;
-
 
1204
 
-
 
1205
        retvalue = SeekSubDirectory(fname);                                                                                     // check wether the specified directory already exists.
1565
        if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0);
1206
 
-
 
1207
        if(!retvalue)
-
 
1208
        {
-
 
1209
                CreateSubDirectory(fname);                                                                                              // if directory doesn't exist, create it.
1566
 
-
 
1567
        pbuff = (uint8_t *) buffer;                                                                                     // cast the void pointer to an u8 *
-
 
1568
 
-
 
1569
        while((object_cnt < count) && success)
-
 
1570
        {
-
 
1571
                object_size = size;
-
 
1572
                while((size > 0) && success)
-
 
1573
                {
-
 
1574
                        c = fgetc_(file);
-
 
1575
                        if(c != EOF)
-
 
1576
                        {
-
 
1577
                                *pbuff = (uint8_t)c;                                                                    // read a byte from the buffer to the opened file.
-
 
1578
                                pbuff++;
1210
                retvalue = 1;
1579
                                size--;
-
 
1580
                        }
-
 
1581
                        else // error or end of file reached
-
 
1582
                        {
1211
        }
1583
                                success = 0;
1212
        else
-
 
1213
        {
1584
                        }
1214
                retvalue = 0;
1585
                }
1215
        }
1586
                if(success) object_cnt++;
1216
 
1587
        }
1217
        return(retvalue);
1588
        return(object_cnt);                                                                                             // return the number of objects succesfully read from the file
1218
}
1589
}
1219
 
1590
 
1220
 
1591
 
-
 
1592
/****************************************************************************************************************************************/
1221
//________________________________________________________________________________________________________________________________________
1593
/*      Function:               fwrite_(void *buffer, uint32_t size, uint32_t count, File *file);                                                                                                                               */
1222
// Funtion:     u8 chdir_(u8 *)
-
 
1223
//
1594
/*                                                                                                                                                                                                                                                                              */
1224
// Description: This function changes the CWD to the directory specified.
-
 
1225
//
-
 
1226
//
1595
/*      Description:    This function writes count objects of the specified size                                                                                                                        */
1227
// returnvalue: 1 if the directory could be changed.
-
 
1228
//________________________________________________________________________________________________________________________________________
-
 
1229
 
-
 
1230
u8      chdir_(s8 *fname)
-
 
1231
{
-
 
1232
        u8      retvalue = 0;
1596
/*                                      from the buffer pointer to the actual position in the file.                                                                                                                     */
1233
        u8  name[11] = {"           "};
-
 
1234
 
-
 
1235
        u32 ultemp = 0;
-
 
1236
 
1597
/*                                                                                                                                                                                                                                                                              */
1237
        SeperateFileName(fname,name);
-
 
-
 
1598
/*      Returnvalue:    The function returns the number of objects (not bytes) read from the file.                                                                                      */
1238
 
1599
/****************************************************************************************************************************************/
1239
        ultemp = (u32)SeekSubDirectory(name);
-
 
1240
        if(ultemp >= 2)
1600
uint32_t fwrite_(void *buffer, uint32_t size, uint32_t count, File_t *file)
1241
        {
1601
{
1242
                ultemp -=2;                                                                                                                                             // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved.
1602
        uint32_t object_cnt     = 0;                                                                                                            // count the number of objects written to the file.
1243
                ultemp *= SectorsPerCluster;                                                                                                    // Calculate relative sectorindex of first datacluster.
1603
        uint32_t object_size = 0;                                                                                                               // count the number of bytes written from the actual object.
1244
                ultemp += FirstDataCluster;
-
 
1245
                CWD = ultemp;
-
 
1246
                retvalue = 1;
-
 
1247
        }
-
 
1248
        else
-
 
1249
        {
1604
        uint8_t *pbuff      = 0;                                                                                                                // a pointer to the actual bufferposition.
1250
                CWD = RootDirectory;
1605
        uint8_t success      = 1;                                                                                                               // no error occured during write operation to the file.
1251
                retvalue = 1;
-
 
1252
        }
-
 
1253
 
-
 
1254
        return(retvalue);
-
 
-
 
1606
        int16_t c;
1255
}
-
 
1256
 
-
 
1257
 
-
 
1258
//________________________________________________________________________________________________________________________________________
-
 
1259
// Funtion:     u8 FindItem(s8 *fname, Find *)
-
 
1260
//
-
 
1261
// Description: finds an item (file or directory) within common working directory (cwd). Wildcards '*' or '?' will be considered.
-
 
1262
//
-
 
1263
// Returnvalue: If an item was found this function returns '1' else '0'.
-
 
1264
//________________________________________________________________________________________________________________________________________
-
 
1265
 
-
 
1266
u8 FindItem(Find *item)
1607
 
1267
{
-
 
1268
        u16     index = 0;
-
 
1269
        u16     end_of_directory_not_reached = 0;                                                                       // the directory has been read completely without a result.
-
 
1270
        u8              i = 0;
-
 
1271
        u8              readpointer = 0;
-
 
1272
        u8              writepointer = 0;
-
 
1273
        u8              retvalue = 0;
-
 
1274
        File    file;
-
 
1275
 
-
 
1276
 
-
 
1277
        file.cluster_pointer = item->cluster_pointer;
-
 
1278
        file.start_cluster   = item->cluster_pointer;
-
 
1279
        index                            = item->directory_index;
-
 
1280
                                                                                                                                                                // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory.
-
 
1281
        do
-
 
-
 
1608
        if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0);
-
 
1609
 
-
 
1610
        pbuff = (uint8_t *) buffer;                                                                                                             // cast the void pointer to an u8 *
1282
        {                                                                                                                                                       // search the next 16 rootentries in this sector of the roordirectory.
1611
 
1283
                SDC_GetSector((u32) file.cluster_pointer,file.buffer);                                  // Read the Rootdirectory.
1612
        while((object_cnt < count) && success)
-
 
1613
        {
1284
                DirectoryEntry = (struct DirEntry *)file.buffer;
1614
                object_size = size;
1285
 
-
 
1286
                while((!retvalue)&&(index<16))
1615
                while((size > 0) && success)
1287
                {
-
 
1288
                        i=0;
-
 
1289
                        if(DirectoryEntry[index].name[0] != 0xe5)                                                       // ignore deleted items.
1616
                {
1290
                        {
1617
                        c = fputc_(*pbuff, file);                                                                               // write a byte from the buffer to the opened file.
1291
                                while((i<=10)&&((DirectoryEntry[index].name[i] == item->searchstring[i]) || (item->searchstring[i]=='*') || item->searchstring[i]=='?'))
1618
                        if(c != EOF)
1292
                                {
1619
                        {
1293
                                        i++;
-
 
1294
                                }
1620
                                pbuff++;
1295
                        }
-
 
1296
 
-
 
1297
                        if((i==11) && (DirectoryEntry[index].attribute & item->attribute))
-
 
1298
                        {
-
 
1299
                                for(readpointer=0;readpointer<=10;readpointer++)
-
 
1300
                                {
-
 
1301
                                        if(DirectoryEntry[index].name[readpointer] != ' ')
-
 
1302
                                        {
-
 
1303
                                                item->name[writepointer] = DirectoryEntry[index].name[readpointer];     // copy the name of the item found to the find_structure.
-
 
1304
                                                writepointer++;
-
 
1305
                                        }
-
 
1306
                                        else if((readpointer==7) && (DirectoryEntry[index].attribute == _FILE)) // if the item found is a file
-
 
1307
                                        {
-
 
1308
                                                if(DirectoryEntry[index].name[readpointer] != ' ')
-
 
1309
                                                {
-
 
1310
                                                        item->name[writepointer] = DirectoryEntry[index].name[readpointer];     // copy the name of the item found to the find_structure.
-
 
1311
                                                        writepointer++;
-
 
1312
                                                }
-
 
1313
                                                item->name[writepointer] = '.';                                         // then seperate the name and the extension by a '.' at index 8.
-
 
1314
                                                writepointer++;
-
 
1315
                                        }
-
 
1316
                                }
1621
                                size--;
1317
                                item->startcluster = (u16)DirectoryEntry[index].startcluster;
1622
                        }
1318
                                item->directory_index = ++index;
-
 
1319
                                item->cluster_pointer = file.cluster_pointer;
-
 
1320
                                retvalue = 1;
-
 
1321
                        }
-
 
1322
                        index++;
-
 
1323
                }
1623
                        else
1324
                if(!retvalue)                                                                                                                   // file not found in this sector so take next sector.
1624
                        {
1325
                {
1625
                                success = 0;
1326
                        end_of_directory_not_reached = GetNextCluster(&file);
-
 
1327
                }
1626
                        }
1328
                index = 0;
1627
                }
1329
        }
1628
                if(success) object_cnt++;
1330
        while((end_of_directory_not_reached) && (!retvalue));
1629
        }
1331
 
1630
 
1332
        return(retvalue);
1631
        return(object_cnt);                                                                                                                             // return the number of objects succesfully written to the file
1333
}
1632
}
1334
 
1633
 
1335
 
1634
 
1336
//________________________________________________________________________________________________________________________________________
1635
/****************************************************************************************************************************************/
1337
// Funtion:     u8 findfirst(s8 *fname, Find *)
-
 
-
 
1636
/*      Function:               fputs_(const int8_t *string, File_t *File);                                                                                                                                                             */
1338
//
1637
/*                                                                                                                                                                                                                                                                              */
1339
// Description: finds the first item (file or directory) within common working directory (cwd). Wildcards '*' or '?' will be considered.
-
 
-
 
1638
/*      Description:    This function writes a string to the specified file.                                                                                                                            */
1340
//
1639
/*                                                                                                                                                                                                                                                                              */
1341
// Returnvalue: If an item was found this function returns '1' else '0'.
1640
/*      Returnvalue:    The function returns a no negative value or EOF on error.                                                                                                                       */
1342
//________________________________________________________________________________________________________________________________________
-
 
1343
 
1641
/****************************************************************************************************************************************/
1344
u8 findfirst_(s8 *fname, Find *item, u8 attribute)
1642
int16_t fputs_(const int8_t *string, File_t *file)
1345
{
1643
{
1346
        u8 retvalue = 0;
1644
        uint8_t i=0;
-
 
1645
        int16_t c = 0;
-
 
1646
 
1347
        u8 i = 0;
1647
        if((!Partition.IsValid) || (file == NULL) || (string == NULL)) return(0);
1348
 
1648
 
1349
        for(i=0;i<=11;i++)
1649
        while((string[i] != 0)&& (c != EOF))
1350
        {
1650
        {
1351
                item->searchstring[i] = '*';                                                                                            // initialise the searchstring with wildcards.
1651
                c = fputc_(string[i], file);
1352
                item->name[i] = 0;
-
 
1353
        }
-
 
1354
 
-
 
1355
        SeperateFileName(fname,item->searchstring);
-
 
1356
 
-
 
1357
        item->cluster_pointer = CWD;                                                                                                    // findfirst_ starts at the beginning of the cwd.
-
 
1358
        item->directory_index = 0;
-
 
1359
        item->attribute = attribute;
-
 
1360
 
-
 
1361
        retvalue = FindItem(item);
1652
                i++;
1362
 
1653
        }
1363
        return(retvalue);
-
 
1364
}
1654
        return(c);
-
 
1655
}
1365
 
1656
 
1366
 
-
 
1367
//________________________________________________________________________________________________________________________________________
1657
/****************************************************************************************************************************************/
1368
// Funtion:     u8 findnext(Find *)
-
 
-
 
1658
/*      Function:               fgets_(int8 *, int16_t , File_t *);                                                                                                                                                                             */
1369
//
1659
/*                                                                                                                                                                                                                                                                              */
1370
// Description: finds the first item (file or directory) within common working directory (cwd). Wildcards '*' or '?' will be considered.
1660
/*      Description:    This function reads a string from the file to the specifies string.                                                                                             */
-
 
1661
/*                                                                                                                                                                                                                                                                              */
1371
//
1662
/*      Returnvalue:    A pointer to the string read from the file or 0 on error.                                                                                                                       */
-
 
1663
/****************************************************************************************************************************************/
-
 
1664
uint8_t * fgets_(int8_t *string, int16_t length, File_t *file)
1372
// Returnvalue: If an item was found this function returns '1' else '0'.
1665
{
-
 
1666
        uint8_t *pbuff;
1373
//________________________________________________________________________________________________________________________________________
1667
        int16_t c = 0;
-
 
1668
 
1374
 
1669
        if((!Partition.IsValid) || (file == NULL) || (string == NULL) || (length = 0)) return (0);
-
 
1670
        pbuff = string;
-
 
1671
        while(length > 1)                                                       // read the count-1 characters from the file to the string.
-
 
1672
        {
1375
 
1673
                c = fgetc_(file);                                               // read a character from the opened file.
-
 
1674
                switch(c)
-
 
1675
                {
1376
u8 findnext_(Find *item)
1676
                        case 0x0A:
1377
{
1677
                                c = 0;                                                  // set string terminator
1378
        u8 retvalue = 0;
1678
                                length = 1;                                             // stop loop
-
 
1679
                                break;
-
 
1680
 
-
 
1681
                        case EOF:
1379
        u8 i = 0;
1682
                                c = 0;                                                  // set string terminator
-
 
1683
                                length = 1;                                             // stop loop
1380
 
1684
                                break;
-
 
1685
                }
1381
        for(i=0;i<=11;i++)
1686
                *pbuff = (uint8_t)c;                                                    // copy byte to string
1382
        {
-
 
1383
                item->name[i] = 0;
-
 
1384
        }
-
 
1385
 
1687
                length--;
1386
        retvalue = FindItem(item);
1688
                pbuff++;
1387
 
-
 
1388
        return(retvalue);
-
 
1389
}
1689
        }
1390
 
1690
        return(string);
1391
 
-
 
-
 
1691
}
1392
 
1692
 
-
 
1693
/****************************************************************************************************************************************/
-
 
1694
/*      Function:               fexist_(const int8_t*);                                                                                                                                                                                                 */
-
 
1695
/*                                                                                                                                                                                                                                                                              */
-
 
1696
/*      Description:    This function checks if a file already exist.                                                                                                                                           */
1393
//________________________________________________________________________________________________________________________________________
1697
/*                                                                                                                                                                                                                                                                              */
-
 
1698
/*      Returnvalue:    1 if the file exist else 0.                                                                                                                                                                                     */
1394
// Funtion:     u8 fdelete(s8 *fname)
1699
/****************************************************************************************************************************************/
-
 
1700
uint8_t fexist_(const int8_t* filename)
1395
//
1701
{
-
 
1702
        uint8_t exist = 0;
-
 
1703
        File_t *file = 0;
1396
// Description: Deletes the file specified by fname.
1704
        file = LockFilePointer();
1397
//
1705
        exist = FileExist(filename, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, file);
-
 
1706
        UnlockFilePointer(file);
-
 
1707
        return(exist);
-
 
1708
}
-
 
1709
 
-
 
1710
/****************************************************************************************************************************************/
-
 
1711
/*      Function:               feof_(File_t *File);                                                                                                                                                                                            */
-
 
1712
/*                                                                                                                                                                                                                                                                              */
1398
// Returnvalue:
1713
/*      Description:    This function checks wether the end of the file has been reached.                                                                                                                                               */
1399
//________________________________________________________________________________________________________________________________________
1714
/*                                                                                                                                                                                                                                                                              */
1400
 
-
 
1401
 
-
 
1402
u8 fdelete_(s8 *fname)
-
 
1403
{
-
 
1404
        u8 retvalue = 0;
-
 
1405
        Find item;
-
 
1406
 
1715
/*      Returnvalue:    0 if the end of the file was not reached otherwise 1.                                                                                                                                                                           */
1407
        printf("\n\rDeleting file");
1716
/****************************************************************************************************************************************/
1408
        retvalue = findfirst_(fname,&item, _FILE);                                                                              // look for the item to be deleted.
1717
uint8_t feof_(File_t *file)
1409
 
-
 
1410
        if(retvalue);                                                                                                                           // item found?
-
 
1411
        {
-
 
1412
                printf("\n\r");
-
 
1413
                printf("%s", item.name);
-
 
1414
 
-
 
1415
                DeleteClusterChain(item.startcluster);                                                                  // delete all fatentries of the item.
-
 
1416
                DeleteDirectoryEntry(&item);                                                                                    // free the directoryentry.
-
 
1417
 
-
 
1418
                do
-
 
1419
                {
-
 
1420
                        printf("\n\r");
-
 
1421
                        printf("%s", item.name);
-
 
1422
                        retvalue = findnext_(&item);
-
 
1423
                        if(retvalue)
-
 
1424
                        {
-
 
1425
                                DeleteClusterChain(item.startcluster);                                                                  // delete all fatentries of the item.
-
 
1426
                                DeleteDirectoryEntry(&item);                                                                                    // free the directoryentry.
1718
{
1427
                        }
-
 
1428
                }
-
 
1429
                while(retvalue);
-
 
1430
        }
-
 
1431
 
-
 
1432
        printf("\n\rDone");
-
 
1433
 
-
 
1434
        return(retvalue);
-
 
1435
}
-
 
1436
 
-
 
1437
 
-
 
1438
 
-
 
1439
//________________________________________________________________________________________________________________________________________
-
 
1440
// Funtion:     u8 rmdir(s8 *fname)
-
 
1441
//
-
 
1442
// Description: Deletes the directory specified by dname.
-
 
1443
//
-
 
1444
// Returnvalue:
-
 
1445
//________________________________________________________________________________________________________________________________________
-
 
1446
 
-
 
1447
 
1719
        if(((file->Position)+1) < (file->Size))
1448
u8 rmdir_(s8 *dname)
-
 
1449
{
-
 
1450
        u8 retvalue = 0;
-
 
1451
        Find item;
-
 
1452
 
-
 
1453
        printf("\n\rDeleting folder");
1720
        {
1454
        retvalue = findfirst_(dname,&item, _DIRECTORY);                                                                         // look for the item to be deleted.
1721
                return(0);
1455
 
-
 
1456
        if(retvalue);                                                                                                                                           // item found?
-
 
1457
        {
-
 
1458
                printf("\n\r");
-
 
1459
                printf("%s", item.name);
-
 
1460
 
-
 
1461
                DeleteClusterChain(item.startcluster);                                                                                  // delete all fatentries of the item.
-
 
1462
                DeleteDirectoryEntry(&item);                                                                                                    // free the directoryentry.
-
 
1463
 
-
 
1464
                do
-
 
1465
                {
-
 
1466
                        printf("\n\r");
-
 
1467
                        printf("%s", item.name);
-
 
1468
                        retvalue = findnext_(&item);
-
 
1469
                        if(retvalue)
-
 
1470
                        {
-
 
1471
                                DeleteClusterChain(item.startcluster);                                                                  // delete all fatentries of the item.
-
 
1472
                                DeleteDirectoryEntry(&item);                                                                                    // free the directoryentry.
1722
        }
1473
                        }
-
 
1474
                }
-
 
1475
                while(retvalue);
-
 
1476
        }
-
 
1477
 
1723
        else
-
 
1724
        {
1478
        printf("\n\rDone");
1725
                return(1);
1479
 
1726
        }
1480
        return(retvalue);
1727
}
1481
}
1728
 
1482
 
1729
 
1483
 
1730