|
| 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 on RP2040: |
| 10 | + ************ SPI0 ************ |
| 11 | + ** MISO (AKA RX) - pin 0, 4, or 16 |
| 12 | + ** MOSI (AKA TX) - pin 3, 7, or 19 |
| 13 | + ** CS - pin 1, 5, or 17 |
| 14 | + ** SCK - pin 2, 6, or 18 |
| 15 | + ************ SPI1 ************ |
| 16 | + ** MISO (AKA RX) - pin 8 or 12 |
| 17 | + ** MOSI (AKA TX) - pin 11 or 15 |
| 18 | + ** CS - pin 9 or 13 |
| 19 | + ** SCK - pin 10 or 14 |
| 20 | +
|
| 21 | + created 28 Mar 2011 |
| 22 | + by Limor Fried |
| 23 | + modified 9 Apr 2012 |
| 24 | + by Tom Igoe |
| 25 | + modified 26 Dec 2023 |
| 26 | + by Richard Teel from code provided by Renzo Mischianti |
| 27 | + SOURCE: https://mischianti.org/raspberry-pi-pico-and-rp2040-boards-how-to-use-sd-card-5/ |
| 28 | +*/ |
| 29 | + |
| 30 | +// This are GP pins for SPI0 on the Raspberry Pi Pico board, and connect |
| 31 | +// to different *board* level pinouts. Check the PCB while wiring. |
| 32 | +// Only certain pins can be used by the SPI hardware, so if you change |
| 33 | +// these be sure they are legal or the program will crash. |
| 34 | +// See: https://datasheets.raspberrypi.com/picow/PicoW-A4-Pinout.pdf |
| 35 | +const int _MISO = 4; // AKA SPI RX |
| 36 | +const int _MOSI = 7; // AKA SPI TX |
| 37 | +const int _CS = 5; |
| 38 | +const int _SCK = 6; |
| 39 | + |
| 40 | +// include the SD library: |
| 41 | +#include <SPI.h> |
| 42 | +#include <SD.h> |
| 43 | + |
| 44 | +File root; |
| 45 | + |
| 46 | +void setup() { |
| 47 | + // Open serial communications and wait for port to open: |
| 48 | + Serial.begin(115200); |
| 49 | + |
| 50 | + while (!Serial) { |
| 51 | + delay(1); // wait for serial port to connect. Needed for native USB port only |
| 52 | + } |
| 53 | + |
| 54 | + Serial.println("\nInitializing SD card..."); |
| 55 | + |
| 56 | + bool sdInitialized = false; |
| 57 | + // Ensure the SPI pinout the SD card is connected to is configured properly |
| 58 | + // Select the correct SPI based on _MISO pin for the RP2040 |
| 59 | + if (_MISO == 0 || _MISO == 4 || _MISO == 16) { |
| 60 | + SPI.setRX(_MISO); |
| 61 | + SPI.setTX(_MOSI); |
| 62 | + SPI.setSCK(_SCK); |
| 63 | + sdInitialized = SD.begin(_CS); |
| 64 | + } else if (_MISO == 8 || _MISO == 12) { |
| 65 | + SPI1.setRX(_MISO); |
| 66 | + SPI1.setTX(_MOSI); |
| 67 | + SPI1.setSCK(_SCK); |
| 68 | + sdInitialized = SD.begin(_CS, SPI1); |
| 69 | + } else { |
| 70 | + Serial.println(F("ERROR: Unknown SPI Configuration")); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (!sdInitialized) { |
| 75 | + Serial.println("initialization failed. Things to check:"); |
| 76 | + Serial.println("* is a card inserted?"); |
| 77 | + Serial.println("* is your wiring correct?"); |
| 78 | + Serial.println("* did you change the chipSelect pin to match your shield or module?"); |
| 79 | + return; |
| 80 | + } else { |
| 81 | + Serial.println("Wiring is correct and a card is present."); |
| 82 | + } |
| 83 | + // 0 - SD V1, 1 - SD V2, or 3 - SDHC/SDXC |
| 84 | + // print the type of card |
| 85 | + Serial.println(); |
| 86 | + Serial.print("Card type: "); |
| 87 | + switch (SD.type()) { |
| 88 | + case 0: |
| 89 | + Serial.println("SD1"); |
| 90 | + break; |
| 91 | + case 1: |
| 92 | + Serial.println("SD2"); |
| 93 | + break; |
| 94 | + case 3: |
| 95 | + Serial.println("SDHC/SDXC"); |
| 96 | + break; |
| 97 | + default: |
| 98 | + Serial.println("Unknown"); |
| 99 | + } |
| 100 | + |
| 101 | + Serial.print("Cluster size: "); |
| 102 | + Serial.println(SD.clusterSize()); |
| 103 | + Serial.print("Blocks x Cluster: "); |
| 104 | + Serial.println(SD.blocksPerCluster()); |
| 105 | + Serial.print("Blocks size: "); |
| 106 | + Serial.println(SD.blockSize()); |
| 107 | + |
| 108 | + Serial.print("Total Blocks: "); |
| 109 | + Serial.println(SD.totalBlocks()); |
| 110 | + Serial.println(); |
| 111 | + |
| 112 | + Serial.print("Total Cluster: "); |
| 113 | + Serial.println(SD.totalClusters()); |
| 114 | + Serial.println(); |
| 115 | + |
| 116 | + // print the type and size of the first FAT-type volume |
| 117 | + uint32_t volumesize; |
| 118 | + Serial.print("Volume type is: FAT"); |
| 119 | + Serial.println(SD.fatType(), DEC); |
| 120 | + |
| 121 | + volumesize = SD.totalClusters(); |
| 122 | + volumesize *= SD.clusterSize(); |
| 123 | + volumesize /= 1000; |
| 124 | + Serial.print("Volume size (Kb): "); |
| 125 | + Serial.println(volumesize); |
| 126 | + Serial.print("Volume size (Mb): "); |
| 127 | + volumesize /= 1024; |
| 128 | + Serial.println(volumesize); |
| 129 | + Serial.print("Volume size (Gb): "); |
| 130 | + Serial.println((float)volumesize / 1024.0); |
| 131 | + |
| 132 | + Serial.print("Card size: "); |
| 133 | + Serial.println((float)SD.size() / 1000); |
| 134 | + |
| 135 | + FSInfo fs_info; |
| 136 | + SDFS.info(fs_info); |
| 137 | + |
| 138 | + Serial.print("Total bytes: "); |
| 139 | + Serial.println(fs_info.totalBytes); |
| 140 | + |
| 141 | + Serial.print("Used bytes: "); |
| 142 | + Serial.println(fs_info.usedBytes); |
| 143 | + |
| 144 | + root = SD.open("/"); |
| 145 | + printDirectory(root, 0); |
| 146 | +} |
| 147 | + |
| 148 | +void loop(void) { |
| 149 | + // nothing happens after setup finishes. |
| 150 | +} |
| 151 | + |
| 152 | +void printDirectory(File dir, int numTabs) { |
| 153 | + while (true) { |
| 154 | + |
| 155 | + File entry = dir.openNextFile(); |
| 156 | + if (!entry) { |
| 157 | + // no more files |
| 158 | + break; |
| 159 | + } |
| 160 | + for (uint8_t i = 0; i < numTabs; i++) { |
| 161 | + Serial.print('\t'); |
| 162 | + } |
| 163 | + Serial.print(entry.name()); |
| 164 | + if (entry.isDirectory()) { |
| 165 | + Serial.println("/"); |
| 166 | + printDirectory(entry, numTabs + 1); |
| 167 | + } else { |
| 168 | + // files have sizes, directories do not |
| 169 | + Serial.print("\t\t"); |
| 170 | + Serial.print(entry.size(), DEC); |
| 171 | + time_t cr = entry.getCreationTime(); |
| 172 | + time_t lw = entry.getLastWrite(); |
| 173 | + struct tm* tmstruct = localtime(&cr); |
| 174 | + Serial.printf("\tCREATION: %d-%02d-%02d %02d:%02d:%02d", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec); |
| 175 | + tmstruct = localtime(&lw); |
| 176 | + Serial.printf("\tLAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec); |
| 177 | + } |
| 178 | + entry.close(); |
| 179 | + } |
| 180 | +} |
0 commit comments