Skip to content

Commit

Permalink
Update SD examples to all support SPI0, SPI1, SDIO
Browse files Browse the repository at this point in the history
  • Loading branch information
earlephilhower committed Jan 17, 2025
1 parent f48f5e8 commit 438869c
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 35 deletions.
1 change: 0 additions & 1 deletion libraries/SD/examples/CardInfo/CardInfo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const int RP_CLK_GPIO = -1; // Set to CLK GPIO
const int RP_CMD_GPIO = -1; // Set to CMD GPIO
const int RP_DAT0_GPIO = -1; // Set to DAT0 GPIO. DAT1..3 must be consecutively connected.


// include the SD library:
#include <SPI.h>
#include <SD.h>
Expand Down
33 changes: 28 additions & 5 deletions libraries/SD/examples/Datalogger/Datalogger.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const int _MOSI = 7;
const int _CS = 5;
const int _SCK = 6;

// If you have all 4 DAT pins wired up to the Pico you can use SDIO mode
const int RP_CLK_GPIO = -1; // Set to CLK GPIO
const int RP_CMD_GPIO = -1; // Set to CMD GPIO
const int RP_DAT0_GPIO = -1; // Set to DAT0 GPIO. DAT1..3 must be consecutively connected.

#include <SPI.h>
#include <SD.h>

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

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

// Ensure the SPI pinout the SD card is connected to is configured properly
SPI.setRX(_MISO);
SPI.setTX(_MOSI);
SPI.setSCK(_SCK);
bool sdInitialized = false;
if (RP_CLK_GPIO >= 0) {
// No special requirements on pin locations, this is PIO programmed
sdInitialized = SD.begin(RP_CLK_GPIO, RP_CMD_GPIO, RP_DAT0_GPIO);
} else {
// Ensure the SPI pinout the SD card is connected to is configured properly
// Select the correct SPI based on _MISO pin for the RP2040
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
SPI.setRX(_MISO);
SPI.setTX(_MOSI);
SPI.setSCK(_SCK);
sdInitialized = SD.begin(_CS);
} else if (_MISO == 8 || _MISO == 12) {
SPI1.setRX(_MISO);
SPI1.setTX(_MOSI);
SPI1.setSCK(_SCK);
sdInitialized = SD.begin(_CS, SPI1);
} else {
Serial.println(F("ERROR: Unknown SPI Configuration"));
return;
}
}

// see if the card is present and can be initialized:
if (!SD.begin(_CS)) {
if (!sdInitialized) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
Expand Down
33 changes: 28 additions & 5 deletions libraries/SD/examples/DumpFile/DumpFile.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const int _MOSI = 7;
const int _CS = 5;
const int _SCK = 6;

// If you have all 4 DAT pins wired up to the Pico you can use SDIO mode
const int RP_CLK_GPIO = -1; // Set to CLK GPIO
const int RP_CMD_GPIO = -1; // Set to CMD GPIO
const int RP_DAT0_GPIO = -1; // Set to DAT0 GPIO. DAT1..3 must be consecutively connected.

#include <SPI.h>
#include <SD.h>

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

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

// Ensure the SPI pinout the SD card is connected to is configured properly
SPI.setRX(_MISO);
SPI.setTX(_MOSI);
SPI.setSCK(_SCK);
bool sdInitialized = false;
if (RP_CLK_GPIO >= 0) {
// No special requirements on pin locations, this is PIO programmed
sdInitialized = SD.begin(RP_CLK_GPIO, RP_CMD_GPIO, RP_DAT0_GPIO);
} else {
// Ensure the SPI pinout the SD card is connected to is configured properly
// Select the correct SPI based on _MISO pin for the RP2040
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
SPI.setRX(_MISO);
SPI.setTX(_MOSI);
SPI.setSCK(_SCK);
sdInitialized = SD.begin(_CS);
} else if (_MISO == 8 || _MISO == 12) {
SPI1.setRX(_MISO);
SPI1.setTX(_MOSI);
SPI1.setSCK(_SCK);
sdInitialized = SD.begin(_CS, SPI1);
} else {
Serial.println(F("ERROR: Unknown SPI Configuration"));
return;
}
}

// see if the card is present and can be initialized:
if (!SD.begin(_CS)) {
if (!sdInitialized) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
Expand Down
33 changes: 28 additions & 5 deletions libraries/SD/examples/Files/Files.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const int _MOSI = 7;
const int _CS = 5;
const int _SCK = 6;

// If you have all 4 DAT pins wired up to the Pico you can use SDIO mode
const int RP_CLK_GPIO = -1; // Set to CLK GPIO
const int RP_CMD_GPIO = -1; // Set to CMD GPIO
const int RP_DAT0_GPIO = -1; // Set to DAT0 GPIO. DAT1..3 must be consecutively connected.

#include <SPI.h>
#include <SD.h>

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

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

// Ensure the SPI pinout the SD card is connected to is configured properly
SPI.setRX(_MISO);
SPI.setTX(_MOSI);
SPI.setSCK(_SCK);
bool sdInitialized = false;
if (RP_CLK_GPIO >= 0) {
// No special requirements on pin locations, this is PIO programmed
sdInitialized = SD.begin(RP_CLK_GPIO, RP_CMD_GPIO, RP_DAT0_GPIO);
} else {
// Ensure the SPI pinout the SD card is connected to is configured properly
// Select the correct SPI based on _MISO pin for the RP2040
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
SPI.setRX(_MISO);
SPI.setTX(_MOSI);
SPI.setSCK(_SCK);
sdInitialized = SD.begin(_CS);
} else if (_MISO == 8 || _MISO == 12) {
SPI1.setRX(_MISO);
SPI1.setTX(_MOSI);
SPI1.setSCK(_SCK);
sdInitialized = SD.begin(_CS, SPI1);
} else {
Serial.println(F("ERROR: Unknown SPI Configuration"));
return;
}
}

if (!SD.begin(_CS)) {
if (!sdInitialized) {
Serial.println("initialization failed!");
return;
}
Expand Down
33 changes: 28 additions & 5 deletions libraries/SD/examples/ReadWrite/ReadWrite.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const int _MOSI = 7;
const int _CS = 5;
const int _SCK = 6;

// If you have all 4 DAT pins wired up to the Pico you can use SDIO mode
const int RP_CLK_GPIO = -1; // Set to CLK GPIO
const int RP_CMD_GPIO = -1; // Set to CMD GPIO
const int RP_DAT0_GPIO = -1; // Set to DAT0 GPIO. DAT1..3 must be consecutively connected.

#include <SPI.h>
#include <SD.h>

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

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

// Ensure the SPI pinout the SD card is connected to is configured properly
SPI.setRX(_MISO);
SPI.setTX(_MOSI);
SPI.setSCK(_SCK);
bool sdInitialized = false;
if (RP_CLK_GPIO >= 0) {
// No special requirements on pin locations, this is PIO programmed
sdInitialized = SD.begin(RP_CLK_GPIO, RP_CMD_GPIO, RP_DAT0_GPIO);
} else {
// Ensure the SPI pinout the SD card is connected to is configured properly
// Select the correct SPI based on _MISO pin for the RP2040
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
SPI.setRX(_MISO);
SPI.setTX(_MOSI);
SPI.setSCK(_SCK);
sdInitialized = SD.begin(_CS);
} else if (_MISO == 8 || _MISO == 12) {
SPI1.setRX(_MISO);
SPI1.setTX(_MOSI);
SPI1.setSCK(_SCK);
sdInitialized = SD.begin(_CS, SPI1);
} else {
Serial.println(F("ERROR: Unknown SPI Configuration"));
return;
}
}

if (!SD.begin(_CS)) {
if (!sdInitialized) {
Serial.println("initialization failed!");
return;
}
Expand Down
38 changes: 24 additions & 14 deletions libraries/SD/examples/listfiles/listfiles.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const int _MOSI = 7; // AKA SPI TX
const int _CS = 5;
const int _SCK = 6;

// If you have all 4 DAT pins wired up to the Pico you can use SDIO mode
const int RP_CLK_GPIO = -1; // Set to CLK GPIO
const int RP_CMD_GPIO = -1; // Set to CMD GPIO
const int RP_DAT0_GPIO = -1; // Set to DAT0 GPIO. DAT1..3 must be consecutively connected.

#include <SPI.h>
#include <SD.h>

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

bool sdInitialized = false;
// Ensure the SPI pinout the SD card is connected to is configured properly
// Select the correct SPI based on _MISO pin for the RP2040
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
SPI.setRX(_MISO);
SPI.setTX(_MOSI);
SPI.setSCK(_SCK);
sdInitialized = SD.begin(_CS);
} else if (_MISO == 8 || _MISO == 12) {
SPI1.setRX(_MISO);
SPI1.setTX(_MOSI);
SPI1.setSCK(_SCK);
sdInitialized = SD.begin(_CS, SPI1);
if (RP_CLK_GPIO >= 0) {
// No special requirements on pin locations, this is PIO programmed
sdInitialized = SD.begin(RP_CLK_GPIO, RP_CMD_GPIO, RP_DAT0_GPIO);
} else {
Serial.println(F("ERROR: Unknown SPI Configuration"));
return;
// Ensure the SPI pinout the SD card is connected to is configured properly
// Select the correct SPI based on _MISO pin for the RP2040
if (_MISO == 0 || _MISO == 4 || _MISO == 16) {
SPI.setRX(_MISO);
SPI.setTX(_MOSI);
SPI.setSCK(_SCK);
sdInitialized = SD.begin(_CS);
} else if (_MISO == 8 || _MISO == 12) {
SPI1.setRX(_MISO);
SPI1.setTX(_MOSI);
SPI1.setSCK(_SCK);
sdInitialized = SD.begin(_CS, SPI1);
} else {
Serial.println(F("ERROR: Unknown SPI Configuration"));
return;
}
}

if (!sdInitialized) {
Expand Down

0 comments on commit 438869c

Please sign in to comment.