8000 Updated listfiles example and added CardInfo example (#1914) · LinusHeu/arduino-pico@24f6302 · GitHub
[go: up one dir, main page]

Skip to content

Commit 24f6302

Browse files
authored
Updated listfiles example and added CardInfo example (earlephilhower#1914)
1 parent 15eb459 commit 24f6302

File tree

2 files changed

+222
-15
lines changed

2 files changed

+222
-15
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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+
}

libraries/SD/examples/listfiles/listfiles.ino

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@
55
directory on a SD card
66
77
The circuit:
8-
SD card attached to SPI bus as follows:
9-
** MISO - pin 4
10-
** MOSI - pin 7
11-
** CS - pin 5
12-
** SCK - pin 6
8+
SD card attached to SPI bus as follows on RP2040:
9+
************ SPI0 ************
10+
** MISO (AKA RX) - pin 0, 4, or 16
11+
** MOSI (AKA TX) - pin 3, 7, or 19
12+
** CS - pin 1, 5, or 17
13+
** SCK - pin 2, 6, or 18
14+
************ SPI1 ************
15+
** MISO (AKA RX) - pin 8 or 12
16+
** MOSI (AKA TX) - pin 11 or 15
17+
** CS - pin 9 or 13
18+
** SCK - pin 10 or 14
1319
1420
created Nov 2010
1521
by David A. Mellis
1622
modified 9 Apr 2012
1723
by Tom Igoe
1824
modified 2 Feb 2014
1925
by Scott Fitzgerald
26+
modified 12 Feb 2023
27+
by Earle F. Philhower, III
28+
modified 26 Dec 2023
29+
by Richard Teel
2030
2131
This example code is in the public domain.
2232
@@ -27,8 +37,8 @@
2737
// Only certain pins can be used by the SPI hardware, so if you change
2838
// these be sure they are legal or the program will crash.
2939
// See: https://datasheets.raspberrypi.com/picow/PicoW-A4-Pinout.pdf
30-
const int _MISO = 4;
31-
const int _MOSI = 7;
40+
const int _MISO = 4; // AKA SPI RX
41+
const int _MOSI = 7; // AKA SPI TX
3242
const int _CS = 5;
3343
const int _SCK = 6;
3444

@@ -41,14 +51,31 @@ void setup() {
4151
// Open serial communications and wait for port to open:
4252
Serial.begin(115200);
4353

44-
Serial.print("Initializing SD card...");
54+
while (!Serial) {
55+
delay(1); // wait for serial port to connect. Needed for native USB port only
56+
}
57+
58+
Serial.println("\nInitializing SD card...");
4559

60+
bool sdInitialized = false;
4661
// Ensure the SPI pinout the SD card is connected to is configured properly
47-
SPI.setRX(_MISO);
48-
SPI.setTX(_MOSI);
49-
SPI.setSCK(_SCK);
62+
// Select the correct SPI based on _MISO pin for the RP2040
63+
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
64+
SPI.setRX(_MISO);
65+
SPI.setTX(_MOSI);
66+
SPI.setSCK(_SCK);
67+
sdInitialized = SD.begin(_CS);
68+
} else if (_MISO == 8 || _MISO == 12) {
69+
SPI1.setRX(_MISO);
70+
SPI1.setTX(_MOSI);
71+
SPI1.setSCK(_SCK);
72+
sdInitialized = SD.begin(_CS, SPI1);
73+
} else {
74+
Serial.println(F("ERROR: Unknown SPI Configuration"));
75+
return;
76+
}
5077

51-
if (!SD.begin(_CS)) {
78+
if (!sdInitialized) {
5279
Serial.println("initialization failed!");
5380
return;
5481
}
@@ -68,8 +95,8 @@ void loop() {
6895
void printDirectory(File dir, int numTabs) {
6996
while (true) {
7097

71-
File entry = dir.openNextFile();
72-
if (! entry) {
98+
File entry = dir.openNextFile();
99+
if (!entry) {
73100
// no more files
74101
break;
75102
}
@@ -86,7 +113,7 @@ void printDirectory(File dir, int numTabs) {
86113
Serial.print(entry.size(), DEC);
87114
time_t cr = entry.getCreationTime();
88115
time_t lw = entry.getLastWrite();
89-
struct tm * tmstruct = localtime(&cr);
116+
struct tm* tmstruct = localtime(&cr);
90117
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);
91118
tmstruct = localtime(&lw);
92119
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);

0 commit comments

Comments
 (0)
0