|
| 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 | + * SD card attached |
| 9 | +
|
| 10 | + */ |
| 11 | +// include the SD library: |
| 12 | +#include <STM32SD.h> |
| 13 | + |
| 14 | +Sd2Card card; |
| 15 | +SdFatFs fatFs; |
| 16 | + |
| 17 | +void setup() |
| 18 | +{ |
| 19 | + bool disp = false; |
| 20 | + // Open serial communications and wait for port to open: |
| 21 | + Serial.begin(9600); |
| 22 | + |
| 23 | + while (!Serial); |
| 24 | + Serial.print("\nInitializing SD card..."); |
| 25 | + while(!card.init(SD_DETECT_PIN)) { |
| 26 | + if (!disp) { |
| 27 | + Serial.println("initialization failed. Is a card inserted?"); |
| 28 | + disp = true; |
| 29 | + } |
| 30 | + delay(10); |
| 31 | + } |
| 32 | + |
| 33 | + Serial.println("A card is present."); |
| 34 | + |
| 35 | + // print the type of card |
| 36 | + Serial.print("\nCard type: "); |
| 37 | + switch (card.type()) { |
| 38 | + case SD_CARD_TYPE_SD1: |
| 39 | + Serial.println("SD1"); |
| 40 | + break; |
| 41 | + case SD_CARD_TYPE_SD2: |
| 42 | + Serial.println("SD2"); |
| 43 | + break; |
| 44 | + case SD_CARD_TYPE_SDHC: |
| 45 | + Serial.println("SDHC"); |
| 46 | + break; |
| 47 | + default: |
| 48 | + Serial.println("Unknown"); |
| 49 | + } |
| 50 | + |
| 51 | + // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32 |
| 52 | + if (!fatFs.init()) { |
| 53 | + Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // print the type and size of the first FAT-type volume |
| 58 | + uint64_t volumesize; |
| 59 | + Serial.print("\nVolume type is FAT"); |
| 60 | + Serial.println(fatFs.fatType(), DEC); |
| 61 | + Serial.println(); |
| 62 | + |
| 63 | + volumesize = fatFs.blocksPerCluster(); // clusters are collections of blocks |
| 64 | + volumesize *= fatFs.clusterCount(); // we'll have a lot of clusters |
| 65 | + volumesize *= 512; // SD card blocks are always 512 bytes |
| 66 | + Serial.print("Volume size (bytes): "); |
| 67 | + Serial.println(volumesize); |
| 68 | + Serial.print("Volume size (Kbytes): "); |
| 69 | + volumesize /= 1024; |
| 70 | + Serial.println(volumesize); |
| 71 | + Serial.print("Volume size (Mbytes): "); |
| 72 | + volumesize /= 1024; |
| 73 | + Serial.println(volumesize); |
| 74 | + |
| 75 | + |
| 76 | + Serial.println("\nFiles found on the card (name, date and size in bytes): "); |
| 77 | + File root = SD.openRoot(); |
| 78 | + |
| 79 | + // list all files in the card with date and size |
| 80 | + root.ls(LS_R | LS_DATE | LS_SIZE); |
| 81 | + Serial.println("###### End of the SD tests ######"); |
| 82 | +} |
| 83 | + |
| 84 | +void loop(void) { |
| 85 | + // do nothing |
| 86 | +} |
0 commit comments