microSD - Esplain it to me Lucy?

Breakout boards, sensors, Drawdio, Game of Life, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

microSD - Esplain it to me Lucy?

Postby micfiz » Sun Apr 08, 2012 11:05 am

I had a heck of a time making the microsSD breakout work for at least, card info. After looking for days at H/W causes, it was S/W!

SD Card Libraries, why so many, which one to use, and why
Pick one of three ???

- Arduino IDE v. 1.0 SD example CardInfo.pde "Absolutely" does not work for my AdaFruit microSD breakout board.
http://arduino.cc/en/Reference/SD There are other links inside Arduino web that confuse further- v.0022.

- AdaFruit suggested in microSD tutorial - https://github.com/adafruit/SD

- Forum:
Re: SDMicro Breakout - volume.init(card) fails
Posted by fat16lib » Sat Mar 10, 2012 7:59 am -http://code.google.com/p/sdfatlib/downloads/list

Pain and pinch -> Before discovering all the Library options (software) I tried all H/W causes for failure.(For Days!!!)
- swapped out everything but the microsSD breakout board while only using Arduino 1.0 IDE SD library in all tests,
None of the 4 examples inside Arduino IDE v1.0 would work. Output looks like what wrong baud rate looks like-sort of.
There are no long wires cause I plugged the damn thing directly into the Arduino after clipping 3.3v pin off on header and bending out and away the 5v pin which I jumped.
IMAG0764.jpg
IMAG0764.jpg (256.62 KiB) Viewed 1248 times


Link to the forum post that was so informative.
http://forums.adafruit.com/viewtopic.php?f=19&t=26856&hilit=microSD+breakout

with sdfatlib quickstart and a 2 gig SD micro card the output =

type any character to start
init time: 9 ms
Card type: SD2
Manufacturer ID: 0X3
OEM ID: SD
Product: SU02G
Version: 8.0
Serial number: 1420285729
Manufacturing date: 10/2009

cardSize: 3862528 (512 byte blocks)
flashEraseSize: 32 blocks
eraseSingleBlock: true

SD Partition Table
part,boot,type,start,length
1,0X0,0X6,135,3858489
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0

Volume is FAT16
blocksPerCluster: 64
clusterCount: 60281
freeClusters: 49169
fatStartBlock: 136
fatCount: 2
blocksPerFat: 236
rootDirStart: 608
dataStartBlock: 640
micfiz
 
Posts: 10
Joined: Sun May 30, 2010 10:42 am

Re: microSD - Esplain it to me Lucy?

Postby adafruit » Sun Apr 08, 2012 2:26 pm

you should use Arduino v1.0 with the built in SD library
User avatar
adafruit
 
Posts: 10483
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: microSD - Esplain it to me Lucy?

Postby micfiz » Sun Apr 08, 2012 8:02 pm

adafruit wrote:you should use Arduino v1.0 with the built in SD library


But remember I said -

- Arduino IDE v. 1.0 SD example CardInfo.pde "Absolutely" does not work for my AdaFruit microSD breakout board. (or any other of the 4 sketches)

Have not tried the adafruit library referenced.

The other "sdfatlib"does work. I was hoping someone could explain why the official Arduino IDE version of the SD library doesn't work yet my system is functional at least since it works for sdfatlib.
micfiz
 
Posts: 10
Joined: Sun May 30, 2010 10:42 am

Re: microSD - Esplain it to me Lucy?

Postby adafruit » Mon Apr 09, 2012 7:27 pm

could be something with your initialization - the SD library is based on sdfatlib and we test -every- microSD card holder using the SD library examples
User avatar
adafruit
 
Posts: 10483
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: microSD - Esplain it to me Lucy?

Postby micfiz » Tue Apr 10, 2012 5:41 am

When you say "could be something with your Initialization" I think this to mean the include and setup sections. What ever is there is not mine. I just selected the example sketch and compiled and uploaded.

The only alteration I made if at all was to set the ChipSelect pin as 10. I have a plain arduino with no shields.

Obviously there is something amiss here. If you test all microSD breakouts there with the standard Arduino v 1.0 IDE and the SD library packaged then I should be able to do the same.

Yet part of me wants to move on and just use the library that works which at the moment is SDFATLIB. I have not tried the SD library referenced in the ADAFruit microSD tutorial as https://github.com/adafruit/SD

I have worked with all kinds of breakout boards and shields and none including the Ethernet Shield before R3 using the XPort device has been as difficult as this microSD!

From what I find on the net with google there are many others having trouble. I really need to make this work as I bought all this stuff from ADAFRUIT for a project ( TTL serial camera, MicroSD brkout, Motor shield, etc) based on the tutorial. Used the tutorials and followed to the letter.

I will provide full disclosure to my situation = code, pictures, output, etc. Whatever is asked for to help get to the bottom of this. I have done so in the start of this topic other than the code.

THIS CODE WORKS. Came from sdfatlib. example sketch is called SdInfo. The Quickstart skethc worked too.
Code: Select all
/*
* This sketch attempts to initialize an SD card and analyze its structure.
*/
#include <SdFat.h>
/*
* SD chip select pin.  Common values are:
*
* Arduino Ethernet shield, pin 4.
* SparkFun SD shield, pin 8.
* Adafruit SD shields and modules, pin 10.
* Default SD chip select is the SPI SS pin.
*/
const uint8_t SdChipSelect = 10;

Sd2Card card;
SdVolume vol;

// serial output steam
ArduinoOutStream cout(Serial);

// global for card erase size
uint32_t eraseSize;
//------------------------------------------------------------------------------
// store error strings in flash
#define sdErrorMsg(msg) sdErrorMsg_P(PSTR(msg));
void sdErrorMsg_P(const char* str) {
  cout << pgm(str) << endl;
  if (card.errorCode()) {
    cout << pstr("SD errorCode: ");
    cout << hex << int(card.errorCode()) << endl;
    cout << pstr("SD errorData: ");
    cout << int(card.errorData()) << dec << endl;
  }
}
//------------------------------------------------------------------------------
uint8_t cidDmp() {
  cid_t cid;
  if (!card.readCID(&cid)) {
    sdErrorMsg("readCID failed");
    return false;
  }
  cout << pstr("\nManufacturer ID: ");
  cout << hex << int(cid.mid) << dec << endl;
  cout << pstr("OEM ID: ") << cid.oid[0] << cid.oid[1] << endl;
  cout << pstr("Product: ");
  for (uint8_t i = 0; i < 5; i++) {
    cout << cid.pnm[i];
  }
  cout << pstr("\nVersion: ");
  cout << int(cid.prv_n) << '.' << int(cid.prv_m) << endl;
  cout << pstr("Serial number: ") << cid.psn << endl;
  cout << pstr("Manufacturing date: ");
  cout << int(cid.mdt_month) << '/';
  cout << (2000 + cid.mdt_year_low + 10 * cid.mdt_year_high) << endl;
  cout << endl;
  return true;
}
//------------------------------------------------------------------------------
uint8_t csdDmp() {
  csd_t csd;
  uint8_t eraseSingleBlock;
  uint32_t cardSize = card.cardSize();
  if (cardSize == 0 || !card.readCSD(&csd)) {
    sdErrorMsg("readCSD failed");
    return false;
  }
  if (csd.v1.csd_ver == 0) {
    eraseSingleBlock = csd.v1.erase_blk_en;
    eraseSize = (csd.v1.sector_size_high << 1) | csd.v1.sector_size_low;
  } else if (csd.v2.csd_ver == 1) {
    eraseSingleBlock = csd.v2.erase_blk_en;
    eraseSize = (csd.v2.sector_size_high << 1) | csd.v2.sector_size_low;
  } else {
    cout << pstr("csd version error\n");
    return false;
  }
  eraseSize++;
  cout << pstr("cardSize: ") << cardSize << pstr(" (512 byte blocks)\n");
  cout << pstr("flashEraseSize: ") << int(eraseSize) << pstr(" blocks\n");
  cout << pstr("eraseSingleBlock: ");
  if (eraseSingleBlock) {
    cout << pstr("true\n");
  } else {
    cout << pstr("false\n");
  }
  return true;
}
//------------------------------------------------------------------------------
// print partition table
uint8_t partDmp() {
  cache_t *p = vol.cacheClear();
  if (!card.readBlock(0, p->data)) {
      sdErrorMsg("read MBR failed");
      return false;
  }
  cout << pstr("\nSD Partition Table\n");
  cout << pstr("part,boot,type,start,length\n");
  for (uint8_t ip = 1; ip < 5; ip++) {
    part_t *pt = &p->mbr.part[ip - 1];
    cout << int(ip) << ',' << hex << int(pt->boot) << ',' << int(pt->type);
    cout << dec << ',' << pt->firstSector <<',' << pt->totalSectors << endl;
  }
  return true;
}
//------------------------------------------------------------------------------
void volDmp() {
  cout << pstr("\nVolume is FAT") << int(vol.fatType()) << endl;
  cout << pstr("blocksPerCluster: ") << int(vol.blocksPerCluster()) << endl;
  cout << pstr("clusterCount: ") << vol.clusterCount() << endl;
  cout << pstr("freeClusters: ") << vol.freeClusterCount() << endl;
  cout << pstr("fatStartBlock: ") << vol.fatStartBlock() << endl;
  cout << pstr("fatCount: ") << int(vol.fatCount()) << endl;
  cout << pstr("blocksPerFat: ") << vol.blocksPerFat() << endl;
  cout << pstr("rootDirStart: ") << vol.rootDirStart() << endl;
  cout << pstr("dataStartBlock: ") << vol.dataStartBlock() << endl;
  if (vol.dataStartBlock() % eraseSize) {
    cout << pstr("Data area is not aligned on flash erase boundaries!\n");
    cout << pstr("Download and use formatter from www.sdcard.org/consumer!\n");
  }
}
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);

  // use uppercase in hex and use 0X base prefix
  cout << uppercase << showbase << endl;
 
  // pstr stores strings in flash to save RAM
  cout << pstr("SdFat version: ") << SD_FAT_VERSION << endl;
}
//------------------------------------------------------------------------------
void loop() {
  // read any existing Serial data
  while (Serial.read() >= 0) {}
 
  // pstr stores strings in flash to save RAM
  cout << pstr("\ntype any character to start\n");
  while (Serial.read() < 0) {}

  uint32_t t = millis();
  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!card.init(SPI_HALF_SPEED, SdChipSelect)) {
    sdErrorMsg("\ncard.init failed");
    return;
  }
  t = millis() - t;
  cout << pstr("\ninit time: ") << t << " ms" << endl;
  cout << pstr("\nCard type: ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      cout << pstr("SD1\n");
      break;

    case SD_CARD_TYPE_SD2:
      cout << pstr("SD2\n");
      break;

    case SD_CARD_TYPE_SDHC:
      cout << pstr("SDHC\n");
      break;

    default:
      cout << pstr("Unknown\n");
  }
  if (!cidDmp()) return;
  if (!csdDmp()) return;
  if (!partDmp()) return;
  if (!vol.init(&card)) {
    sdErrorMsg("\nvol.init failed");
    return;
  }
  volDmp();
micfiz
 
Posts: 10
Joined: Sun May 30, 2010 10:42 am

Re: microSD - Esplain it to me Lucy?

Postby adafruit » Tue Apr 10, 2012 4:19 pm

lets start over. clean out ALL the libraries you have downloaded and installed. wire up JUST the breakout on a breadboard or so that the CS pin is connected to Arduino Digital 4. run ONLY the cardinfo sketch from the DEFAULT SD card library the comes with Arduino 1.0

Post the exact output from the serial monitor
User avatar
adafruit
 
Posts: 10483
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: microSD - Esplain it to me Lucy?

Postby fat16lib » Tue Apr 10, 2012 7:33 pm

If you continue to have problems try another brand/model SD card.

Both the Adafruit and Arduino 1.0 libraries have a number of bugs that cause a few cards to fail. Failure is rare with with a simple setup. Failure is more common if the SPI bus is shared with another device.

These libraries are based on a very old version of SdFat that does not correctly handle the SD protocol. Most SD cards are tolerant of these bugs.

I am the author of SdFat and have since totally rewritten the basic SD access code in SdFat.
fat16lib
 
Posts: 586
Joined: Wed Dec 24, 2008 12:54 pm

Re: microSD - Esplain it to me Lucy?

Postby micfiz » Wed Apr 11, 2012 1:59 am

Excellent. Thanks for responding. This makes some sense.

I find that your library works. Is this the best place to get SdFat and the latest updates as you make changes?
http://code.google.com/p/sdfatlib/downloads/list

In your experience with various vendors of microSD cards, which is the more compatible and robust with regard to playing nice with libraries?
micfiz
 
Posts: 10
Joined: Sun May 30, 2010 10:42 am

Re: microSD - Esplain it to me Lucy?

Postby fat16lib » Wed Apr 11, 2012 10:01 am

The stable version is at http://code.google.com/p/sdfatlib/downloads/list.

New versions are at http://code.google.com/p/beta-lib/downloads/list.

I test with many brands/models of SD cards but tend to use SanDisk for my projects.

I can't really recommend a card because they change so fast. They look the same on the outside but the controller or flash chips seem to change every few months. Guess it's the problem with a high volume product.

I bought a number of SanDisk Extreme cards over a period of a few months and when I tested them I found three different versions. They all looked the same.
fat16lib
 
Posts: 586
Joined: Wed Dec 24, 2008 12:54 pm

Re: microSD - Esplain it to me Lucy?

Postby micfiz » Thu Apr 12, 2012 1:28 am

Well, I must say, your work is greatly appreciated. This adventure has brought my understanding of libraries a little more into focus.
micfiz
 
Posts: 10
Joined: Sun May 30, 2010 10:42 am


Return to Other Adafruit products

Who is online

Users browsing this forum: Arctic_Eddie, Google [Bot] and 2 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [105]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[108]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[69]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]