Go Down

Topic: Double SD cards - fail to initialize with one slot empty. (Read 10050 times) previous topic - next topic

zigio

Hello everyone,

I am facing a problem related to the software or hardware of the system I create.
The system consists of ATmega 2560 powered by 5V, and 2 Micro Sd card slots, sypported by the SN74LVC125 level converters.

How it should work:

The system should enable information to be saved (about 120 characters every second) on two cards, or any of them if only one of them is placed in any slot. The system will not read information from the cards.

What has been done:

Based on a lot of information found on the network about Arduino SD card readers, I designed a system to support one card using SPI, and then I duplicated it. The effect can be seen in the screenshot in attachement.
Cards ar connected to hardware SPI on 2560 (pin 50, 51, 52), SS of first card is 53, SS of second card is 38.

In addition, I managed to use the SDfat library, specifically the TwoCards.h example, to create a sketch for software support of the above assumptions. Currently, SD card recording works when both are inserted.
For the sketch to work, it was necessary to reduce the SPI speed, as well as adding some delay between cards initialization.

Quote
sd1.begin(SD1_CS), SPI_QUARTER_SPEED)
What's the problem:

The main problem appears when only one card is inserted - it is not possible to initialize the card, neither in the first slot nor in the second.

The sketch I use to check the operation of the system is as follows.

Code: [Select]
#include <SPI.h>
#include "SdFat.h"
#include "FreeStack.h"

SdFat sd1;
const uint8_t SD1_CS = 53;  // chip select for sd1

SdFat sd2;
const uint8_t SD2_CS = 38;   // chip select for sd2

#define SD1_DET 42
#define SD2_DET 49


void setup() {
  Serial.begin(9600);
  // Wait for USB Serial
  while (!Serial) {
    SysCall::yield();
  }

 
  pinMode(SD2_CS, OUTPUT);
  pinMode(SD1_DET, INPUT_PULLUP);
  pinMode(SD2_DET, INPUT_PULLUP);

  // disable sd2 while initializing sd1
  digitalWrite(SD2_CS, HIGH);

  // initialize the first card

  if(digitalRead(SD1_DET) == HIGH)
  {
  Serial.println("SD1 present");
  if (!sd1.begin(SD1_CS), SPI_QUARTER_SPEED)) {
    Serial.println("SD1 init error");
  }
  sd1.chvol();
  }
  else
  Serial.println("No SD1 ");

  SdFile file1;
 
  delay(500);
  // initialize the second card
  digitalWrite(SD1_CS, HIGH);

 
  if(digitalRead(SD2_DET) == HIGH)
  {
   Serial.println("SD2 present");
   if (!sd2.begin(SD2_CS), SPI_QUARTER_SPEED)) {
      Serial.println("SD2 init error");
  }
 
  sd2.chvol();
  }
  else
  Serial.println("No SD2 ");

  SdFile file2;
 
// Try to save some data

  if(digitalRead(SD1_DET) == HIGH)
  {
 
  sd1.chvol();

  file1.open("TEST.txt", FILE_WRITE);
  file1.print(" TEST DATA CARD 1");
  file1.close();
 
  Serial.println("SD1 written");
  }
  else
  Serial.println("SD1 not written");

  if(digitalRead(SD2_DET) == HIGH)
  {
  sd2.chvol();

  file2.open("TEST2.txt", FILE_WRITE);
  file2.print("TEST DATA CARD 2");
  file2.close();
  Serial.println("SD2 written");
  }
  else
  Serial.println("SD2 not written");
  Serial.println();
}

void loop() {
  // put your main code here, to run repeatedly:

}


As I mentioned, when both cards are in place everything works fine. Therefore, I suspect that the lack of one of them causes some disturbance on SPI, which prevents it from working.

I have been trying to solve this problem for two days, I have read hundreds of threads and other documents and still cannot solve the problem. I hope You can help me.

ShermanP

I wonder if the explanation could be that the _DET pins are reversed.  If that's the case, then it would work if both cards are present, but with only one present, it would try to initialize a card which is actually in the other slot.  I guess it's also possible the CS pins are reversed.  Otherwise, I don't see anything wrong.

Edit:  I also wonder about the DET wiring at the holder.  You have pins 9 and 10 of each holder wired together, but I don't see that on the holder's datasheet.  Actually, you might try commenting out all of the DET stuff and see if it works then.  That would tell you if the problem involves the DETs, or something else.

SpaceJohn

Zigio,
I agree with ShermanP. My ATTEND 112 schematic has the detect switch circuit as 9 & 11 with GND as 10 & 12.   

zigio

Hi,

Thank You for response!

I'm sure that SD*_DET pins are corectly wired up to ATTEND112 (even if schematic may be wrong) - i have checekd that with multimeter many times.

Anyway, I will test if they are swaped between first and second card, as well as CS.

I will let You know.

EDIT:

Unfortunately, swapped wiring is not the case - still the same.


ShermanP

Well, you're going to have to work with your code, commenting out all but the most basic functions until you find something that works with one card.  You might also need to physically disconnect one of the card holders.  If it works with both SD cards present, then it certainly should work with only one present.  The logical explanation is that the DET lines or the CS lines, or both, are switched.  But if that's not the case, I don't see anything obvious in your code.

I guess I would start with eliminating all of the DET code, and just try to initialize the first card.  If that works, but it doesn't work adding back in the DET code, then that would tell you something.  You also might try running one of the example sketches on one card, perhaps CardInfo.


zigio

Thank You for help ShermanP.

I will definitely work with it. If I will find something, I will let You know.

Best regards

SpaceJohn

I'm also working on a dual SD card project, which caught my attention to your problem. I'll replace my mini with a 2650 and run your DET code this weekend.     

zigio

Hey,

I solved the problem.
The problem turned out to be of a hardware nature -  half of the GND connections were not connected.
Fortunately, the SD cards have not been damaged. It is surprising that it worked in any way.

A very stupid mistake that made me lose a lot of time. A lesson for the future to check connections 100 times.

Best regards and thanks for the effort.

ShermanP

Yeah, those ground connections will jump up and bite you.

I wonder if you would be willing to test something for me if it's not a burden.  The question has come up as to whether the SD card will tri-state it's own MOSI pin if its CS is not asserted.  The answer doesn't matter in your setup because you're controlling the MISO's 125 gate with CS.  But would it be possible to just connect the card holder's MISO pin directly to the MISO header, bypassing the 125 gate entirely?  The voltage is still going to be 3.3V, so I would expect this to work when both card holders are populated.  But some say the card itself will not release the MISO line even when its CS is de-asserted.  I don't know if this varies by brand of card or what, but I'm pretty sure the SD spec calls for the card to tristate MISO unless it has been selected.  But it would be nice to know what the situation is with your setup since we know your code works.  The attached alternate version of your circuit shows what I have in mind.


Go Up