Skip to content

Commit 09f9a4d

Browse files
committed
File object structure (FIL) must be a pointer
Using the TFT library TFTBitmapLogo example, SD file opened is passed to read function. If FIL is not a pointer (as it is copied thanks the copy constructor), file read/write pointer (fptr) is never incremented so always reading from beginning of the file. Fix stm32duino#1 Signed-off-by: Frederic Pillon <[email protected]>
1 parent 94eca67 commit 09f9a4d

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/SD.cpp

+22-17
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ File SDClass::open(const char *filepath)
134134
{
135135
File file = File(filepath);
136136

137-
if(f_open(&file._fil, filepath, FA_READ) != FR_OK)
137+
if(f_open(file._fil, filepath, FA_READ) != FR_OK)
138138
{
139139
f_opendir(&file._dir, filepath);
140140
}
@@ -156,7 +156,7 @@ File SDClass::open(const char *filepath, uint8_t mode)
156156
mode = mode | FA_CREATE_ALWAYS;
157157
}
158158

159-
if(f_open(&file._fil, filepath, mode) != FR_OK)
159+
if(f_open(file._fil, filepath, mode) != FR_OK)
160160
{
161161
f_opendir(&file._dir, filepath);
162162
}
@@ -194,16 +194,20 @@ File SDClass::openRoot(void)
194194
File::File()
195195
{
196196
_name = NULL;
197-
_fil.fs = 0;
198-
_dir.fs = 0;
197+
_fil = (FIL*)malloc(sizeof(FIL));
198+
assert(_fil != NULL );
199+
_fil->fs = 0;
200+
_dir.fs = 0;
199201
}
200202

201203
File::File(const char* name)
202204
{
203205
_name = (char*)malloc(strlen(name) +1);
204206
assert(_name != NULL );
205207
sprintf(_name, "%s", name);
206-
_fil.fs = 0;
208+
_fil = (FIL*)malloc(sizeof(FIL));
209+
assert(_fil != NULL );
210+
_fil->fs = 0;
207211
_dir.fs = 0;
208212
}
209213

@@ -345,7 +349,7 @@ int File::read()
345349
{
346350
uint8_t byteread;
347351
int8_t data;
348-
f_read(&_fil, (void *)&data, 1, (UINT *)&byteread);
352+
f_read(_fil, (void *)&data, 1, (UINT *)&byteread);
349353
return data;
350354
}
351355

@@ -359,7 +363,7 @@ int File::read(void* buf, size_t len)
359363
{
360364
uint8_t bytesread;
361365

362-
f_read(&_fil, buf, len, (UINT *)&bytesread);
366+
f_read(_fil, buf, len, (UINT *)&bytesread);
363367
return bytesread;
364368

365369
}
@@ -373,12 +377,13 @@ void File::close()
373377
{
374378
if(_name)
375379
{
376-
if(_fil.fs != 0) {
380+
if(_fil && _fil->fs != 0) {
377381
/* Flush the file before close */
378-
f_sync(&_fil);
382+
f_sync(_fil);
379383

380384
/* Close the file */
381-
f_close(&_fil);
385+
f_close(_fil);
386+
free(_fil);
382387
}
383388

384389
if(_dir.fs != 0) {
@@ -397,7 +402,7 @@ void File::close()
397402
*/
398403
void File::flush()
399404
{
400-
f_sync(&_fil);
405+
f_sync(_fil);
401406
}
402407

403408
/**
@@ -421,7 +426,7 @@ int File::peek()
421426
uint32_t File::position()
422427
{
423428
uint32_t filepos = 0;
424-
filepos = f_tell(&_fil);
429+
filepos = f_tell(_fil);
425430
return filepos;
426431
}
427432

@@ -438,7 +443,7 @@ uint8_t File::seek(uint32_t pos)
438443
}
439444
else
440445
{
441-
if(f_lseek(&_fil, pos) != FR_OK)
446+
if(f_lseek(_fil, pos) != FR_OK)
442447
{
443448
return FALSE;
444449
}
@@ -458,12 +463,12 @@ uint32_t File::size()
458463
{
459464
uint32_t file_size = 0;
460465

461-
file_size = f_size(&_fil);
466+
file_size = f_size(_fil);
462467
return(file_size);
463468
}
464469

465470
File::operator bool() {
466-
return ((_name == NULL) || ((_fil.fs == 0) && (_dir.fs == 0))) ? FALSE : TRUE;
471+
return ((_name == NULL) || ((_fil == NULL) && (_dir.fs == 0)) || ((_fil->fs == 0) && (_dir.fs == 0))) ? FALSE : TRUE;
467472
}
468473
/**
469474
* @brief Write data to the file
@@ -484,7 +489,7 @@ size_t File::write(uint8_t data)
484489
size_t File::write(const char *buf, size_t size)
485490
{
486491
size_t byteswritten;
487-
f_write(&_fil, (const void *)buf, size, (UINT *)&byteswritten);
492+
f_write(_fil, (const void *)buf, size, (UINT *)&byteswritten);
488493
return byteswritten;
489494
}
490495

@@ -563,7 +568,7 @@ uint8_t File::isDirectory()
563568
assert(_name != NULL );
564569
if (_dir.fs != 0)
565570
return TRUE;
566-
else if (_fil.fs != 0)
571+
else if (_fil->fs != 0)
567572
return FALSE;
568573
// if not init get info
569574
if (f_stat(_name, &fno) == FR_OK)

src/STM32SD.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class File {
6868

6969

7070
char *_name = NULL; //file or dir name
71-
FIL _fil = {}; // init all fields to 0
71+
FIL* _fil = NULL; // underlying file pointer
7272
DIR _dir = {}; // init all fields to 0
7373

7474
};

0 commit comments

Comments
 (0)