Pages: [1]   Go Down
Author Topic: Reading from SD card stops  (Read 630 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

I've written a small program that reads binary files from SD card module and shifts out
bits to shiftOut registers.
There is another implementation of this program, which reads data from usb (PC) and does the same,
so the shiftOut implementation is tested and working solid. However there is a problem with SD module.
I'm not sure where, because I've just recently started sistematically testing all scenarios, but as far as
I got - it has to do something with reading from SD card.

Code sample follows (without shiftOutFast):
Code:
// #include "shiftout.h"

const int DEBUG_LEVEL = 0;
const int DEBUG_SLOMO_OUT = 0;
const int DEBUG_SLOMO_DELAY = 100;
const int SD_READ_BUFFER_SIZE = 512;
const int CHIP_SELECT = 10; // change this to 53 on a mega - 10 default

// nr of outputs, divisible by 8 (for serialEvent)
int nrOfOuts = 64;
unsigned char c = 0;
// currently read chars, and processed outputs (for serialEvent)
int outputs_i8 = 0;

/*-----SD-----*/
#include <SD.h>

// TIME probe measure
// unsigned long time;
// time = micros();
File root;
File entry;
bool SD_inited = false;
byte buff[SD_READ_BUFFER_SIZE];
int bytes_read = 0;
int i = 0;

/*
* initCard
*/
bool initCard() {
if (DEBUG_LEVEL >= 1)
Serial.print("\n[initCard]: SD init");

if ( !SD.begin(CHIP_SELECT) ) {
if (DEBUG_LEVEL >= 1)
Serial.print("\n[initCard]: SD init failed");
return false;
} else {
if (DEBUG_LEVEL >= 1)
Serial.print("\n[initCard]: SD init ok");
}

return true;
}

void readData() {
if (DEBUG_LEVEL >= 2)
Serial.print("\n[readData]: read data");

// open root and list it
root = SD.open("/", O_READ);

// list until end
while (true) {
// open - 90us
entry =  root.openNextFile(O_READ);

// if end reached
if (! entry) {
entry.close();
break;
}

// if it is a file
if (! entry.isDirectory()) {
if (DEBUG_LEVEL >= 1) {
Serial.print("\n[readData]: SD file found: ");
Serial.print(entry.name());
}

// read until EOF
while (entry.available()) {
bytes_read = entry.read((char*)buff, SD_READ_BUFFER_SIZE);

i = 0;

for (i; i < bytes_read; i++) {
// copy from buffer
c = buff[i];

if (outputs_i8 == 0) {
// latchOff();
if (DEBUG_SLOMO_OUT > 0)
delay(DEBUG_SLOMO_DELAY);
if (DEBUG_LEVEL >= 3)
Serial.print("\n[readData]: latchOff");
}

outputs_i8++;

// not relevant here
// shiftOutFast(dataPin, clockPin, c);

if (outputs_i8 >= (nrOfOuts)) {
// latchOn();
if (DEBUG_SLOMO_OUT > 0)
delay(DEBUG_SLOMO_DELAY);
outputs_i8 = 0;
if (DEBUG_LEVEL >= 3)
Serial.print("\n[readData]: latchOn");
}
}
}
// EOF, close file

if (DEBUG_LEVEL >= 1)
Serial.print("\n[readData]: File close");
entry.close();
outputs_i8 = 0;

if (DEBUG_LEVEL >= 3)
Serial.println();
} else {
// if it is not file, close file
if (DEBUG_LEVEL >= 1) {
Serial.print("\n[readData]: Not a file: ");
Serial.print(entry.name());
Serial.print(", closing");
}
entry.close();
}
}
// EOD
if (DEBUG_LEVEL >= 1)
Serial.print("\n[readData]: Root close");
root.close();
}
/*-----SD-----*/

// arduino setup
void setup() {
Serial.begin(115200);
// Serial.begin(9600);

/*-----SD-----*/
SD_inited = initCard();
/*-----SD-----*/

nrOfOuts /= 8;

pinMode(CHIP_SELECT, OUTPUT);     // change this to 53 on a mega - 10 default

//set pins to output
// pinMode(latchPin, OUTPUT);
// pinMode(dataPin, OUTPUT);
// pinMode(clockPin, OUTPUT);
//
// digitalWrite(latchPin,LOW);
// digitalWrite(dataPin,LOW);
// digitalWrite(clockPin,LOW);
}

void loop(void) {
/*-----SD-----*/
if (SD_inited) {
readData();
} else {
if (DEBUG_LEVEL >= 1)
Serial.print("\n[readData]: SD not inited, reinit");
SD_inited = initCard();
}
/*-----SD-----*/
return;
}

Now with turned on debugging I was able to pull this log:
Code:
// OK
[readData]: [readData]: Not a file: IMAGES, closing
[readData]: [readData]: Not a file: SOUNDS, closing
[readData]: [readData]: Not a file: OTHERS, closing
[readData]: [readData]: Not a file: VIDEOS, closing
[readData]: [readData]: Not a file: SYSTEM, closing
[readData]: [readData]: Not a file: PB, closing
[readData]: [readData]: Not a file: LIFEBLOG, closing
[readData]: [readData]: Not a file: PRIVATE, closing
[readData]: SD file found: 21646~1.BWP
[readData]: File close
[readData]: SD file found: 384C4~1.BWP
[readData]: File close
[readData]: SD file found: 13D8F~1.BWP
[readData]: File close
[readData]: [readData]: Root close
// OK
[readData]: [readData]: Not a file: IMAGES, closing
[readData]: [readData]: Not a file: SOUNDS, closing
[readData]: [readData]: Not a file: OTHERS, closing
[readData]: [readData]: Not a file: VIDEOS, closing
[readData]: [readData]: Not a file: SYSTEM, closing
[readData]: [readData]: Not a file: PB, closing
[readData]: [readData]: Not a file: LIFEBLOG, closing
[readData]: [readData]: Not a file: PRIVATE, closing
[readData]: SD file found: 21646~1.BWP
[readData]: File close
[readData]: SD file found: 384C4~1.BWP
[readData]: File close
[readData]: SD file found: 13D8F~1.BWP
[readData]: File close
[readData]: [readData]: Root close
// Here it starts missing out
[readData]: [readData]: Not a file: IMAGES, closing
[readData]: [readData]: Not a file: SOUNDS, closing
[readData]: [readData]: Not a file: OTHERS, closing
[readData]: [readData]: Not a file: VIDEOS, closing
[readData]: [readData]: Not a file: SYSTEM, closing
[readData]: [readData]: Root close
[readData]: [readData]: Root close
[readData]: [readData]: Root close
[readData]: [readData]: Root close
[readData]: [readData]: Root close
[readData]: [readData]: Root close
[readData]: [readData]: Root close
[readData]: [readData]: Root close
...

As you can see, suddenly it stops listing files and the cause of multiple "Root close" messages is
due to entry being false.
Sometimes this occurs after minutes, sometimes hours - very random. Also if I press reset button on
Arduino in such situation, it will report SD init failed - I have to unplug it from USB port or power supply
to completely remedy the situation.

For now I've documented such freeze both on USB and external 12V alimentation. I'll continue testing
on Ericsson USB adapter, since I don't have my PC on all the time.

Is this maybe due to bad SD module, card...(card should be ok, it was used in phone before)?
SD module it's generic with only 5V supply (no 3.3v) lying on a protoboard (shortest cables as possible).
I've tested this with MEGA 2560 and UNO SMD - same freeze.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
SD module it's generic with only 5V supply (no 3.3v) lying on a protoboard (shortest cables as possible).
I think SD cards use 3.3v. If you are powering it with 5v, that could be part of the problem.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The pin is marked as 5V, though yes - sd cards are 3.3v. I've ordered a new module which states in specifications that uses 3.3V for alimentation, will try that.

Thank you!
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If there is no voltage regulator on the old SD card module, then I would try 3.3v power with it.

edit: Most SD modules use a voltage level converter on the input pins for 5v processors. Otherwise, you are overdriving the SD inputs.
« Last Edit: June 29, 2012, 06:38:51 am by SurferTim » Logged

Pages: [1]   Go Up
Jump to: