|
| 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 28 |
| 11 | + ** MISO - pin 26 |
| 12 | + ** CLK - pin 27 |
| 13 | + ** CS - pin 29 |
| 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 | + modified for k210 30 Mar 2019 |
| 22 | + by Neucrack |
| 23 | +*/ |
| 24 | +// include the SD library: |
| 25 | +#include <SPI.h> |
| 26 | +#include <SD.h> |
| 27 | + |
| 28 | +// set up variables using the SD utility library functions: |
| 29 | +Sd2Card card; // use object SPI (use SPI1) by default, |
| 30 | + // you can change it by: |
| 31 | + // SPIClass spi(SPI1, 27, 26, 28, -1); |
| 32 | + // Sd2Card card(spi); |
| 33 | +SdVolume volume; |
| 34 | +SdFile root; |
| 35 | + |
| 36 | +// change this to match your SD shield or module; |
| 37 | +// pin 29 on Maix Go or Maix One Dock or Maix Bit |
| 38 | +const int chipSelect = 29; |
| 39 | + |
| 40 | +void setup() { |
| 41 | + // Open serial communications and wait for port to open: |
| 42 | + Serial.begin(9600); |
| 43 | + while (!Serial) { |
| 44 | + ; // wait for serial port to connect. Needed for native USB port only |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + Serial.print("\nInitializing SD card..."); |
| 49 | + |
| 50 | + // we'll use the initialization code from the utility libraries |
| 51 | + // since we're just testing if the card is working! |
| 52 | + if (!card.init(SPI_HALF_SPEED, chipSelect)) { |
| 53 | + Serial.println("initialization failed. Things to check:"); |
| 54 | + Serial.println("* is a card inserted?"); |
| 55 | + Serial.println("* is your wiring correct?"); |
| 56 | + Serial.println("* did you change the chipSelect pin to match your shield or module?"); |
| 57 | + while (1); |
| 58 | + } else { |
| 59 | + Serial.println("Wiring is correct and a card is present."); |
| 60 | + } |
| 61 | + |
| 62 | + // print the type of card |
| 63 | + Serial.println(); |
| 64 | + Serial.print("Card type: "); |
| 65 | + switch (card.type()) { |
| 66 | + case SD_CARD_TYPE_SD1: |
| 67 | + Serial.println("SD1"); |
| 68 | + break; |
| 69 | + case SD_CARD_TYPE_SD2: |
| 70 | + Serial.println("SD2"); |
| 71 | + break; |
| 72 | + case SD_CARD_TYPE_SDHC: |
| 73 | + Serial.println("SDHC"); |
| 74 | + break; |
| 75 | + default: |
| 76 | + Serial.println("Unknown"); |
| 77 | + } |
| 78 | + |
| 79 | + // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32 |
| 80 | + if (!volume.init(card)) { |
| 81 | + Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card"); |
| 82 | + while (1); |
| 83 | + } |
| 84 | + |
| 85 | + Serial.print("Clusters: "); |
| 86 | + Serial.println(volume.clusterCount()); |
| 87 | + Serial.print("Blocks x Cluster: "); |
| 88 | + Serial.println(volume.blocksPerCluster()); |
| 89 | + |
| 90 | + Serial.print("Total Blocks: "); |
| 91 | + Serial.println(volume.blocksPerCluster() * volume.clusterCount()); |
| 92 | + Serial.println(); |
| 93 | + |
| 94 | + // print the type and size of the first FAT-type volume |
| 95 | + uint32_t volumesize; |
| 96 | + Serial.print("Volume type is: FAT"); |
| 97 | + Serial.println(volume.fatType(), DEC); |
| 98 | + |
| 99 | + volumesize = volume.blocksPerCluster(); // clusters are collections of blocks |
| 100 | + volumesize *= volume.clusterCount(); // we'll have a lot of clusters |
| 101 | + volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB) |
| 102 | + Serial.print("Volume size (Kb): "); |
| 103 | + Serial.println(volumesize); |
| 104 | + Serial.print("Volume size (Mb): "); |
| 105 | + volumesize /= 1024; |
| 106 | + Serial.println(volumesize); |
| 107 | + Serial.print("Volume size (Gb): "); |
| 108 | + Serial.println((float)volumesize / 1024.0); |
| 109 | + |
| 110 | + Serial.println("\nFiles found on the card (name, date and size in bytes): "); |
| 111 | + root.openRoot(volume); |
| 112 | + |
| 113 | + // list all files in the card with date and size |
| 114 | + root.ls(LS_R | LS_DATE | LS_SIZE); |
| 115 | +} |
| 116 | + |
| 117 | +void loop(void) { |
| 118 | +} |
0 commit comments