Skip to content

Commit 0222180

Browse files
committed
File object structure (FIL) member must be a pointer
Using the TFT library TFTBitmapLogo example, BMP file opened is passed to read16/32 functions. If 'FIL _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 0222180

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

examples/Full/Full.ino

+29-21
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,26 @@ void setup()
3939
Serial.println("Creating 'ARDUINO/SD' directory");
4040
SD.mkdir("ARDUINO/SD");
4141

42+
/* Test bool operator method */
43+
Serial.print("Test bool operator...");
44+
if (!MyFile) {
45+
Serial.println("OK");
46+
} else {
47+
Serial.println("Error MyFile should not be initialized!");
48+
}
49+
4250
/* Test open() method */
4351
Serial.println("Opening 'STM32/Toremove.txt' file");
4452
MyFile = SD.open("STM32/Toremove.txt", FILE_WRITE);
45-
if(MyFile) {
53+
if (MyFile) {
4654
Serial.println("Closing 'STM32/Toremove.txt' file");
4755
MyFile.close();
4856
} else {
4957
Serial.println("Error to open 'STM32/Toremove.txt' file");
5058
}
5159
Serial.println("Opening 'ARDUINO/SD/ARDUINO_SD_TEXT.txt' file");
5260
MyFile = SD.open("ARDUINO/SD/ARDUINO_SD_TEXT.txt", FILE_WRITE);
53-
if(MyFile) {
61+
if (MyFile) {
5462
/* Test print() method */
5563
Serial.print("writing \"");
5664
Serial.print((const char*)wtext);
@@ -67,7 +75,7 @@ void setup()
6775

6876
Serial.println("Opening 'ARDUINO/SD/ARDUINO_SD_TEXT.txt' file");
6977
MyFile = SD.open("ARDUINO/SD/ARDUINO_SD_TEXT.txt");
70-
if(MyFile) {
78+
if (MyFile) {
7179
bytesread = MyFile.read(rtext, MyFile.size());
7280
Serial.println("Closing 'ARDUINO/SD/ARDUINO_SD_TEXT.txt' file");
7381
MyFile.close();
@@ -77,7 +85,7 @@ void setup()
7785

7886
Serial.println("Opening 'ARDUINO/SD/TEXT.txt' file");
7987
MyFile = SD.open("ARDUINO/SD/TEXT.txt", FILE_WRITE);
80-
if(MyFile) {
88+
if (MyFile) {
8189
byteswritten = MyFile.print((const char*)rtext);
8290
MyFile.flush();
8391
Serial.println("Closing 'ARDUINO/SD/TEXT.txt' file");
@@ -88,7 +96,7 @@ void setup()
8896

8997
Serial.println("Opening 'ARDUINO/SD/TEXT.txt' file");
9098
MyFile = SD.open("ARDUINO/SD/TEXT.txt");
91-
if(MyFile) {
99+
if (MyFile) {
92100
/* Test size() method */
93101
file_size = MyFile.size();
94102
Serial.print("TEXT.txt size: ");
@@ -97,20 +105,20 @@ void setup()
97105
/* Test position and seek method */
98106
Serial.print("TEXT.txt position value: ");
99107
Serial.println(MyFile.position());
100-
if(!MyFile.seek(MyFile.size()+1)) {
108+
if (!MyFile.seek(MyFile.size() + 1)) {
101109
Serial.println("TEXT.txt seek value over size: OK");
102110
} else {
103111
Serial.println("TEXT.txt seek value over size: KO");
104112
}
105-
if(MyFile.seek(MyFile.size())) {
113+
if (MyFile.seek(MyFile.size())) {
106114
Serial.println("TEXT.txt seek value to size: OK");
107115
} else {
108116
Serial.println("TEXT.txt seek value to size: KO");
109117
}
110118
Serial.print("TEXT.txt position value: ");
111119
Serial.println(MyFile.position());
112120

113-
if(MyFile.seek(0)) {
121+
if (MyFile.seek(0)) {
114122
Serial.println("TEXT.txt seek value to 0: OK");
115123
} else {
116124
Serial.println("TEXT.txt seek value to 0: KO");
@@ -120,7 +128,7 @@ void setup()
120128

121129
/* Test peek() method */
122130
Serial.println("TEXT.txt peek (10 times): ");
123-
for(i = 0; i<10; i++)
131+
for (i = 0; i < 10; i++)
124132
{
125133
peek_val = MyFile.peek();
126134
Serial.print(peek_val);
@@ -132,7 +140,7 @@ void setup()
132140

133141
/* Test available() and read() methods */
134142
Serial.println("TEXT.txt content read byte per byte: ");
135-
while(MyFile.available())
143+
while (MyFile.available())
136144
{
137145
rtext[i] = (uint8_t)MyFile.read();
138146
Serial.print(rtext[i]);
@@ -150,7 +158,7 @@ void setup()
150158

151159
/* Test isDirectory() method */
152160
MyFile = File("STM32");
153-
if(MyFile) {
161+
if (MyFile) {
154162
Serial.print("Is 'STM32' is a dir: ");
155163
if (MyFile.isDirectory())
156164
Serial.println("OK");
@@ -162,7 +170,7 @@ void setup()
162170

163171
Serial.println("Opening 'STM32/Toremove.txt' file");
164172
MyFile = SD.open("STM32/Toremove.txt");
165-
if(MyFile) {
173+
if (MyFile) {
166174
Serial.print("Is 'STM32/Toremove.txt' is a file: ");
167175
if (MyFile.isDirectory())
168176
Serial.println("KO");
@@ -175,23 +183,23 @@ void setup()
175183
}
176184
/* Test exists(), remove() and rmdir() methods */
177185
Serial.print("Removing 'STM32/Toremove.txt' file...");
178-
while(SD.exists("STM32/Toremove.txt") == TRUE)
186+
while (SD.exists("STM32/Toremove.txt") == TRUE)
179187
{
180188
SD.remove("STM32/Toremove.txt");
181-
}
189+
}
182190
Serial.println("done");
183191

184192
Serial.print("Removing 'STM32' dir...");
185-
while(SD.exists("STM32") == TRUE)
193+
while (SD.exists("STM32") == TRUE)
186194
{
187195
SD.rmdir("STM32");
188-
}
196+
}
189197
Serial.println("done");
190198

191199
/* Test println(), println(data) methods */
192200
Serial.println("Opening 'ARDUINO/SD/PRINT.txt' file");
193201
MyFile = SD.open("ARDUINO/SD/PRINT.txt", FILE_WRITE);
194-
if(MyFile) {
202+
if (MyFile) {
195203
String str = String("This is a String object on line 7");
196204
Serial.print("Printing to 'ARDUINO/SD/PRINT.txt' file...");
197205
MyFile.println("This should be line 1");
@@ -211,7 +219,7 @@ void setup()
211219
/* Test write(buf, len) method */
212220
Serial.println("Opening 'ARDUINO/SD/WRITE.txt' file");
213221
MyFile = SD.open("ARDUINO/SD/WRITE.txt", FILE_WRITE);
214-
if(MyFile) {
222+
if (MyFile) {
215223
Serial.print("Writing 'ARDUINO/SD/WRITE.txt' file: ");
216224
byteswritten = MyFile.write(wtext, BUFFERSIZE);
217225
Serial.print(byteswritten);
@@ -225,19 +233,19 @@ void setup()
225233
/* Test read(buf, len) method */
226234
Serial.println("Opening 'ARDUINO/SD/WRITE.txt' file");
227235
MyFile = SD.open("ARDUINO/SD/WRITE.txt");
228-
if(MyFile) {
236+
if (MyFile) {
229237
Serial.println("Reading 'ARDUINO/SD/WRITE.txt' file:");
230238
bytesread = MyFile.read(rtext, MyFile.size());
231239
Serial.println((const char*)rtext);
232240
Serial.println("Closing 'ARDUINO/SD/WRITE.txt' file");
233241
MyFile.close();
234-
} else {
242+
} else {
235243
Serial.println("Error to open 'ARDUINO/SD/WRITE.txt' file");
236244
}
237245
Serial.println("###### End of the SD tests ######");
238246
}
239247

240248
void loop()
241249
{
242-
// do nothing
250+
// do nothing
243251
}

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 != NULL) && (_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 object structure pointer
7272
DIR _dir = {}; // init all fields to 0
7373

7474
};

0 commit comments

Comments
 (0)