diff --git a/libraries/SD/examples/CardInfo/CardInfo.ino b/libraries/SD/examples/CardInfo/CardInfo.ino index 4e9743f4..d69b720b 100644 --- a/libraries/SD/examples/CardInfo/CardInfo.ino +++ b/libraries/SD/examples/CardInfo/CardInfo.ino @@ -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 #include diff --git a/libraries/SD/examples/Datalogger/Datalogger.ino b/libraries/SD/examples/Datalogger/Datalogger.ino index bde1a08c..b9fd1e76 100644 --- a/libraries/SD/examples/Datalogger/Datalogger.ino +++ b/libraries/SD/examples/Datalogger/Datalogger.ino @@ -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 #include @@ -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; diff --git a/libraries/SD/examples/DumpFile/DumpFile.ino b/libraries/SD/examples/DumpFile/DumpFile.ino index b06d5cfc..ff8c9d4e 100644 --- a/libraries/SD/examples/DumpFile/DumpFile.ino +++ b/libraries/SD/examples/DumpFile/DumpFile.ino @@ -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 #include @@ -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; diff --git a/libraries/SD/examples/Files/Files.ino b/libraries/SD/examples/Files/Files.ino index a4970e6e..1b176d3e 100644 --- a/libraries/SD/examples/Files/Files.ino +++ b/libraries/SD/examples/Files/Files.ino @@ -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 #include @@ -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; } diff --git a/libraries/SD/examples/ReadWrite/ReadWrite.ino b/libraries/SD/examples/ReadWrite/ReadWrite.ino index 1de0377f..23ea0aa3 100644 --- a/libraries/SD/examples/ReadWrite/ReadWrite.ino +++ b/libraries/SD/examples/ReadWrite/ReadWrite.ino @@ -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 #include @@ -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; } diff --git a/libraries/SD/examples/listfiles/listfiles.ino b/libraries/SD/examples/listfiles/listfiles.ino index 663c24a8..3f068d56 100644 --- a/libraries/SD/examples/listfiles/listfiles.ino +++ b/libraries/SD/examples/listfiles/listfiles.ino @@ -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 #include @@ -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) {