Skip to content

Commit c7ce5c1

Browse files
Update SD examples to all support SPI0, SPI1, SDIO
1 parent f48f5e8 commit c7ce5c1

File tree

6 files changed

+131
-35
lines changed

6 files changed

+131
-35
lines changed

libraries/SD/examples/CardInfo/CardInfo.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ const int RP_CLK_GPIO = -1; // Set to CLK GPIO
4242
const int RP_CMD_GPIO = -1; // Set to CMD GPIO
4343
const int RP_DAT0_GPIO = -1; // Set to DAT0 GPIO. DAT1..3 must be consecutively connected.
4444

45-
4645
// include the SD library:
4746
#include <SPI.h>
4847
#include <SD.h>

libraries/SD/examples/Datalogger/Datalogger.ino

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const int _MOSI = 7;
3030
const int _CS = 5;
3131
const int _SCK = 6;
3232

33+
// If you have all 4 DAT pins wired up to the Pico you can use SDIO mode
34+
const int RP_CLK_GPIO = -1; // Set to CLK GPIO
35+
const int RP_CMD_GPIO = -1; // Set to CMD GPIO
36+
const int RP_DAT0_GPIO = -1; // Set to DAT0 GPIO. DAT1..3 must be consecutively connected.
37+
3338
#include <SPI.h>
3439
#include <SD.h>
3540

@@ -39,13 +44,31 @@ void setup() {
3944

4045
Serial.print("Initializing SD card...");
4146

42-
// Ensure the SPI pinout the SD card is connected to is configured properly
43-
SPI.setRX(_MISO);
44-
SPI.setTX(_MOSI);
45-
SPI.setSCK(_SCK);
47+
bool sdInitialized = false;
48+
if (RP_CLK_GPIO >= 0) {
49+
// No special requirements on pin locations, this is PIO programmed
50+
sdInitialized = SD.begin(RP_CLK_GPIO, RP_CMD_GPIO, RP_DAT0_GPIO);
51+
} else {
52+
// Ensure the SPI pinout the SD card is connected to is configured properly
53+
// Select the correct SPI based on _MISO pin for the RP2040
54+
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
55+
SPI.setRX(_MISO);
56+
SPI.setTX(_MOSI);
57+
SPI.setSCK(_SCK);
58+
sdInitialized = SD.begin(_CS);
59+
} else if (_MISO == 8 || _MISO == 12) {
60+
SPI1.setRX(_MISO);
61+
SPI1.setTX(_MOSI);
62+
SPI1.setSCK(_SCK);
63+
sdInitialized = SD.begin(_CS, SPI1);
64+
} else {
65+
Serial.println(F("ERROR: Unknown SPI Configuration"));
66+
return;
67+
}
68+
}
4669

4770
// see if the card is present and can be initialized:
48-
if (!SD.begin(_CS)) {
71+
if (!sdInitialized) {
4972
Serial.println("Card failed, or not present");
5073
// don't do anything more:
5174
return;

libraries/SD/examples/DumpFile/DumpFile.ino

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const int _MOSI = 7;
3030
const int _CS = 5;
3131
const int _SCK = 6;
3232

33+
// If you have all 4 DAT pins wired up to the Pico you can use SDIO mode
34+
const int RP_CLK_GPIO = -1; // Set to CLK GPIO
35+
const int RP_CMD_GPIO = -1; // Set to CMD GPIO
36+
const int RP_DAT0_GPIO = -1; // Set to DAT0 GPIO. DAT1..3 must be consecutively connected.
37+
3338
#include <SPI.h>
3439
#include <SD.h>
3540

@@ -39,13 +44,31 @@ void setup() {
3944

4045
Serial.print("Initializing SD card...");
4146

42-
// Ensure the SPI pinout the SD card is connected to is configured properly
43-
SPI.setRX(_MISO);
44-
SPI.setTX(_MOSI);
45-
SPI.setSCK(_SCK);
47+
bool sdInitialized = false;
48+
if (RP_CLK_GPIO >= 0) {
49+
// No special requirements on pin locations, this is PIO programmed
50+
sdInitialized = SD.begin(RP_CLK_GPIO, RP_CMD_GPIO, RP_DAT0_GPIO);
51+
} else {
52+
// Ensure the SPI pinout the SD card is connected to is configured properly
53+
// Select the correct SPI based on _MISO pin for the RP2040
54+
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
55+
SPI.setRX(_MISO);
56+
SPI.setTX(_MOSI);
57+
SPI.setSCK(_SCK);
58+
sdInitialized = SD.begin(_CS);
59+
} else if (_MISO == 8 || _MISO == 12) {
60+
SPI1.setRX(_MISO);
61+
SPI1.setTX(_MOSI);
62+
SPI1.setSCK(_SCK);
63+
sdInitialized = SD.begin(_CS, SPI1);
64+
} else {
65+
Serial.println(F("ERROR: Unknown SPI Configuration"));
66+
return;
67+
}
68+
}
4669

4770
// see if the card is present and can be initialized:
48-
if (!SD.begin(_CS)) {
71+
if (!sdInitialized) {
4972
Serial.println("Card failed, or not present");
5073
// don't do anything more:
5174
return;

libraries/SD/examples/Files/Files.ino

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,30 @@ void setup() {
3939

4040
Serial.print("Initializing SD card...");
4141

42-
// Ensure the SPI pinout the SD card is connected to is configured properly
43-
SPI.setRX(_MISO);
44-
SPI.setTX(_MOSI);
45-
SPI.setSCK(_SCK);
42+
bool sdInitialized = false;
43+
if (RP_CLK_GPIO >= 0) {
44+
// No special requirements on pin locations, this is PIO programmed
45+
sdInitialized = SD.begin(RP_CLK_GPIO, RP_CMD_GPIO, RP_DAT0_GPIO);
46+
} else {
47+
// Ensure the SPI pinout the SD card is connected to is configured properly
48+
// Select the correct SPI based on _MISO pin for the RP2040
49+
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
50+
SPI.setRX(_MISO);
51+
SPI.setTX(_MOSI);
52+
SPI.setSCK(_SCK);
53+
sdInitialized = SD.begin(_CS);
54+
} else if (_MISO == 8 || _MISO == 12) {
55+
SPI1.setRX(_MISO);
56+
SPI1.setTX(_MOSI);
57+
SPI1.setSCK(_SCK);
58+
sdInitialized = SD.begin(_CS, SPI1);
59+
} else {
60+
Serial.println(F("ERROR: Unknown SPI Configuration"));
61+
return;
62+
}
63+
}
4664

47-
if (!SD.begin(_CS)) {
65+
if (!sdInitialized) {
4866
Serial.println("initialization failed!");
4967
return;
5068
}

libraries/SD/examples/ReadWrite/ReadWrite.ino

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ const int _MOSI = 7;
2828
const int _CS = 5;
2929
const int _SCK = 6;
3030

31+
// If you have all 4 DAT pins wired up to the Pico you can use SDIO mode
32+
const int RP_CLK_GPIO = -1; // Set to CLK GPIO
33+
const int RP_CMD_GPIO = -1; // Set to CMD GPIO
34+
const int RP_DAT0_GPIO = -1; // Set to DAT0 GPIO. DAT1..3 must be consecutively connected.
35+
3136
#include <SPI.h>
3237
#include <SD.h>
3338

@@ -39,12 +44,30 @@ void setup() {
3944

4045
Serial.print("Initializing SD card...");
4146

42-
// Ensure the SPI pinout the SD card is connected to is configured properly
43-
SPI.setRX(_MISO);
44-
SPI.setTX(_MOSI);
45-
SPI.setSCK(_SCK);
47+
bool sdInitialized = false;
48+
if (RP_CLK_GPIO >= 0) {
49+
// No special requirements on pin locations, this is PIO programmed
50+
sdInitialized = SD.begin(RP_CLK_GPIO, RP_CMD_GPIO, RP_DAT0_GPIO);
51+
} else {
52+
// Ensure the SPI pinout the SD card is connected to is configured properly
53+
// Select the correct SPI based on _MISO pin for the RP2040
54+
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
55+
SPI.setRX(_MISO);
56+
SPI.setTX(_MOSI);
57+
SPI.setSCK(_SCK);
58+
sdInitialized = SD.begin(_CS);
59+
} else if (_MISO == 8 || _MISO == 12) {
60+
SPI1.setRX(_MISO);
61+
SPI1.setTX(_MOSI);
62+
SPI1.setSCK(_SCK);
63+
sdInitialized = SD.begin(_CS, SPI1);
64+
} else {
65+
Serial.println(F("ERROR: Unknown SPI Configuration"));
66+
return;
67+
}
68+
}
4669

47-
if (!SD.begin(_CS)) {
70+
if (!sdInitialized) {
4871
Serial.println("initialization failed!");
4972
return;
5073
}

libraries/SD/examples/listfiles/listfiles.ino

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ const int _MOSI = 7; // AKA SPI TX
4242
const int _CS = 5;
4343
const int _SCK = 6;
4444

45+
// If you have all 4 DAT pins wired up to the Pico you can use SDIO mode
46+
const int RP_CLK_GPIO = -1; // Set to CLK GPIO
47+
const int RP_CMD_GPIO = -1; // Set to CMD GPIO
48+
const int RP_DAT0_GPIO = -1; // Set to DAT0 GPIO. DAT1..3 must be consecutively connected.
49+
4550
#include <SPI.h>
4651
#include <SD.h>
4752

@@ -58,21 +63,26 @@ void setup() {
5863
Serial.println("\nInitializing SD card...");
5964

6065
bool sdInitialized = false;
61-
// Ensure the SPI pinout the SD card is connected to is configured properly
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);
66+
if (RP_CLK_GPIO >= 0) {
67+
// No special requirements on pin locations, this is PIO programmed
68+
sdInitialized = SD.begin(RP_CLK_GPIO, RP_CMD_GPIO, RP_DAT0_GPIO);
7369
} else {
74-
Serial.println(F("ERROR: Unknown SPI Configuration"));
75-
return;
70+
// Ensure the SPI pinout the SD card is connected to is configured properly
71+
// Select the correct SPI based on _MISO pin for the RP2040
72+
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
73+
SPI.setRX(_MISO);
74+
SPI.setTX(_MOSI);
75+
SPI.setSCK(_SCK);
76+
sdInitialized = SD.begin(_CS);
77+
} else if (_MISO == 8 || _MISO == 12) {
78+
SPI1.setRX(_MISO);
79+
SPI1.setTX(_MOSI);
80+
SPI1.setSCK(_SCK);
81+
sdInitialized = SD.begin(_CS, SPI1);
82+
} else {
83+
Serial.println(F("ERROR: Unknown SPI Configuration"));
84+
return;
85+
}
7686
}
7787

7888
if (!sdInitialized) {

0 commit comments

Comments
 (0)