Go Down

Topic: "Large" files on SD cart (Read 998 times) previous topic - next topic

Sulya

Jul 18, 2013, 05:59 pm Last Edit: Jul 18, 2013, 06:21 pm by Sulya Reason: 1
Is anyone else experiencing any problems with "large" (1KB+) files on SD card? I am working on my own LCD+SD shield (160x128 lcd from an epson printer) and I have a problem with reading image from sd card while sending it to lcd. I seem to only get ~400 bytes and then it just stops.
I've hooked everything to logic sniffer and I found out something interesting. Despite my code is designed to read each byte and send it to lcd what ardu actually does is read part of the file when "inFile=SD.open("1.lcd");" is called and does not communicate with SD card on "myFile.read();" calls.
How can I actually read something outside the area that was read on "open()"?
Code: [Select]
inFile = SD.open("1.lcd");
 
 digitalWrite(CS_LCD, 1);  
 writeLcd(startDraw);
 while(inFile.available()){
   digitalWrite(CS_LCD, 0);
   buffer = inFile.read();
   digitalWrite(CS_LCD, 1);
   SPI.begin();
   SPI.transfer(buffer);
   SPI.end();
 }


fat16lib

#1
Jul 18, 2013, 06:38 pm Last Edit: Jul 18, 2013, 06:40 pm by fat16lib Reason: 1
SD cards are block devices.  When you read the first byte, 512 bytes are read from the file into a block buffer. the next 511 bytes are read from the block buffer.

When you call SPI.end() the SPI controller is disabled.  The SD.h library does not expect this so it fails on the next block read.

Put the SPI.begin() in setup() and remove the SPI.end().

You could even omit the SPI.begin() since SD.begin() initializes the SPI bus.


Sulya

#2
Jul 18, 2013, 07:24 pm Last Edit: Jul 18, 2013, 07:52 pm by Sulya Reason: 1
Thanks, that explains a lot. Meantime I've already tried using SdFat library instead (without removing SPI begin and end calls) and It worked, but a bit slower. Now when SPI begin and end calls are removed it works fine and a bit faster then with SdFat.

fat16lib

Quote
I've already tried using SdFat library instead (without removing SPI begin and end calls) and It worked, but a bit slower.

Yes, I modified SdFat to cope with SPI.end() calls.  SD.h is a wrapper for a very old version of SdFat that does not have that feature.

The call to SPI.begin() is very time consuming and is only required once.  It adds about 20 microseconds when you call it for every byte. 

SPI.end() only clears the enable bit in the SPI controller so it adds no time to re-enable the SPI controller since I write that controller register anyhow in SdFat.

Sulya

Thank you for quick and complete answers and great libraries, my lcd + sd module is working now.

Go Up