2011-04-18 23:58:54 +02:00
|
|
|
/*
|
2013-10-21 09:58:40 +02:00
|
|
|
SD card test
|
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
This example shows how use the utility libraries on which the'
|
|
|
|
SD library is based in order to get info about your SD card.
|
|
|
|
Very useful for testing a card when you're not sure whether its working or not.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
The circuit:
|
|
|
|
* SD card attached to SPI bus as follows:
|
|
|
|
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
|
|
|
|
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
|
|
|
|
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
|
2013-10-21 09:58:40 +02:00
|
|
|
** CS - depends on your SD card shield or module.
|
2011-03-16 18:53:39 +01:00
|
|
|
Pin 4 used here for consistency with other Arduino examples
|
2011-04-18 23:58:54 +02:00
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
created 28 Mar 2011
|
2013-10-21 09:58:40 +02:00
|
|
|
by Limor Fried
|
2012-04-09 15:51:35 +02:00
|
|
|
modified 9 Apr 2012
|
2011-03-16 18:53:39 +01:00
|
|
|
by Tom Igoe
|
2011-04-18 23:58:54 +02:00
|
|
|
*/
|
2013-10-21 09:58:40 +02:00
|
|
|
// include the SD library:
|
2012-09-25 18:29:19 +02:00
|
|
|
#include <SPI.h>
|
2011-04-18 23:58:54 +02:00
|
|
|
#include <SD.h>
|
|
|
|
|
|
|
|
// set up variables using the SD utility library functions:
|
|
|
|
Sd2Card card;
|
|
|
|
SdVolume volume;
|
|
|
|
SdFile root;
|
|
|
|
|
|
|
|
// change this to match your SD shield or module;
|
|
|
|
// Arduino Ethernet shield: pin 4
|
|
|
|
// Adafruit SD shields and modules: pin 10
|
|
|
|
// Sparkfun SD shield: pin 8
|
2013-10-21 09:58:40 +02:00
|
|
|
const int chipSelect = 4;
|
2011-04-18 23:58:54 +02:00
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void setup() {
|
2013-10-21 09:58:40 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2011-04-18 23:58:54 +02:00
|
|
|
Serial.begin(9600);
|
2013-10-21 09:58:40 +02:00
|
|
|
while (!Serial) {
|
2015-09-21 14:44:19 +02:00
|
|
|
; // wait for serial port to connect. Needed for native USB port only
|
2012-04-09 15:51:35 +02:00
|
|
|
}
|
|
|
|
|
2012-04-02 15:07:58 +02:00
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
Serial.print("\nInitializing SD card...");
|
|
|
|
|
|
|
|
// we'll use the initialization code from the utility libraries
|
|
|
|
// since we're just testing if the card is working!
|
|
|
|
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
|
|
|
|
Serial.println("initialization failed. Things to check:");
|
2015-04-01 19:43:32 +02:00
|
|
|
Serial.println("* is a card inserted?");
|
|
|
|
Serial.println("* is your wiring correct?");
|
2011-04-18 23:58:54 +02:00
|
|
|
Serial.println("* did you change the chipSelect pin to match your shield or module?");
|
|
|
|
return;
|
|
|
|
} else {
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println("Wiring is correct and a card is present.");
|
2011-04-18 23:58:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// print the type of card
|
|
|
|
Serial.print("\nCard type: ");
|
2013-10-21 09:58:40 +02:00
|
|
|
switch (card.type()) {
|
2011-04-18 23:58:54 +02:00
|
|
|
case SD_CARD_TYPE_SD1:
|
|
|
|
Serial.println("SD1");
|
|
|
|
break;
|
|
|
|
case SD_CARD_TYPE_SD2:
|
|
|
|
Serial.println("SD2");
|
|
|
|
break;
|
|
|
|
case SD_CARD_TYPE_SDHC:
|
|
|
|
Serial.println("SDHC");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Serial.println("Unknown");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
|
|
|
|
if (!volume.init(card)) {
|
|
|
|
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// print the type and size of the first FAT-type volume
|
2011-09-09 22:57:00 +02:00
|
|
|
uint32_t volumesize;
|
2011-04-18 23:58:54 +02:00
|
|
|
Serial.print("\nVolume type is FAT");
|
|
|
|
Serial.println(volume.fatType(), DEC);
|
|
|
|
Serial.println();
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
|
|
|
|
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
|
|
|
|
volumesize *= 512; // SD card blocks are always 512 bytes
|
|
|
|
Serial.print("Volume size (bytes): ");
|
|
|
|
Serial.println(volumesize);
|
|
|
|
Serial.print("Volume size (Kbytes): ");
|
|
|
|
volumesize /= 1024;
|
|
|
|
Serial.println(volumesize);
|
|
|
|
Serial.print("Volume size (Mbytes): ");
|
|
|
|
volumesize /= 1024;
|
|
|
|
Serial.println(volumesize);
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
|
|
|
|
root.openRoot(volume);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
// list all files in the card with date and size
|
|
|
|
root.ls(LS_R | LS_DATE | LS_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void loop(void) {
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-04-18 23:58:54 +02:00
|
|
|
}
|