[136] | 1 | /*
|
---|
| 2 | SD card test
|
---|
| 3 |
|
---|
| 4 | This example shows how use the utility libraries on which the'
|
---|
| 5 | SD library is based in order to get info about your SD card.
|
---|
| 6 | Very useful for testing a card when you're not sure whether its working or not.
|
---|
| 7 |
|
---|
| 8 | The circuit:
|
---|
| 9 | * SD card attached to SPI bus as follows:
|
---|
| 10 | ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
|
---|
| 11 | ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
|
---|
| 12 | ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
|
---|
| 13 | ** CS - depends on your SD card shield or module.
|
---|
| 14 | Pin 4 used here for consistency with other Arduino examples
|
---|
| 15 |
|
---|
| 16 |
|
---|
| 17 | created 28 Mar 2011
|
---|
| 18 | by Limor Fried
|
---|
| 19 | modified 9 Apr 2012
|
---|
| 20 | by Tom Igoe
|
---|
| 21 | */
|
---|
| 22 | // include the SD library:
|
---|
| 23 | #include <SPI.h>
|
---|
| 24 | #include <SD.h>
|
---|
| 25 |
|
---|
| 26 | // set up variables using the SD utility library functions:
|
---|
| 27 | Sd2Card card;
|
---|
| 28 | SdVolume volume;
|
---|
| 29 | SdFile root;
|
---|
| 30 |
|
---|
| 31 | // change this to match your SD shield or module;
|
---|
| 32 | // Arduino Ethernet shield: pin 4
|
---|
| 33 | // Adafruit SD shields and modules: pin 10
|
---|
| 34 | // Sparkfun SD shield: pin 8
|
---|
| 35 | const int chipSelect = 4;
|
---|
| 36 |
|
---|
| 37 | void setup()
|
---|
| 38 | {
|
---|
| 39 | // Open serial communications and wait for port to open:
|
---|
| 40 | Serial.begin(9600);
|
---|
| 41 | while (!Serial) {
|
---|
| 42 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | Serial.print("\nInitializing SD card...");
|
---|
| 47 | // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
|
---|
| 48 | // Note that even if it's not used as the CS pin, the hardware SS pin
|
---|
| 49 | // (10 on most Arduino boards, 53 on the Mega, 14 on Leonardo) must be left as an output
|
---|
| 50 | // or the SD library functions will not work.
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | // we'll use the initialization code from the utility libraries
|
---|
| 54 | // since we're just testing if the card is working!
|
---|
| 55 | if (!card.init(SPI_HALF_SPEED, chipSelect)) {
|
---|
| 56 | Serial.println("initialization failed. Things to check:");
|
---|
| 57 | Serial.println("* is a card is inserted?");
|
---|
| 58 | Serial.println("* Is your wiring correct?");
|
---|
| 59 | Serial.println("* did you change the chipSelect pin to match your shield or module?");
|
---|
| 60 | return;
|
---|
| 61 | } else {
|
---|
| 62 | Serial.println("Wiring is correct and a card is present.");
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | // print the type of card
|
---|
| 66 | Serial.print("\nCard type: ");
|
---|
| 67 | switch (card.type()) {
|
---|
| 68 | case SD_CARD_TYPE_SD1:
|
---|
| 69 | Serial.println("SD1");
|
---|
| 70 | break;
|
---|
| 71 | case SD_CARD_TYPE_SD2:
|
---|
| 72 | Serial.println("SD2");
|
---|
| 73 | break;
|
---|
| 74 | case SD_CARD_TYPE_SDHC:
|
---|
| 75 | Serial.println("SDHC");
|
---|
| 76 | break;
|
---|
| 77 | default:
|
---|
| 78 | Serial.println("Unknown");
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
|
---|
| 82 | if (!volume.init(card)) {
|
---|
| 83 | Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
|
---|
| 84 | return;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | // print the type and size of the first FAT-type volume
|
---|
| 89 | uint32_t volumesize;
|
---|
| 90 | Serial.print("\nVolume type is FAT");
|
---|
| 91 | Serial.println(volume.fatType(), DEC);
|
---|
| 92 | Serial.println();
|
---|
| 93 |
|
---|
| 94 | volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
|
---|
| 95 | volumesize *= volume.clusterCount(); // we'll have a lot of clusters
|
---|
| 96 | volumesize *= 512; // SD card blocks are always 512 bytes
|
---|
| 97 | Serial.print("Volume size (bytes): ");
|
---|
| 98 | Serial.println(volumesize);
|
---|
| 99 | Serial.print("Volume size (Kbytes): ");
|
---|
| 100 | volumesize /= 1024;
|
---|
| 101 | Serial.println(volumesize);
|
---|
| 102 | Serial.print("Volume size (Mbytes): ");
|
---|
| 103 | volumesize /= 1024;
|
---|
| 104 | Serial.println(volumesize);
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 | Serial.println("\nFiles found on the card (name, date and size in bytes): ");
|
---|
| 108 | root.openRoot(volume);
|
---|
| 109 |
|
---|
| 110 | // list all files in the card with date and size
|
---|
| 111 | root.ls(LS_R | LS_DATE | LS_SIZE);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | void loop(void) {
|
---|
| 116 |
|
---|
| 117 | }
|
---|