|
| 1 | +// RP2040 PIO SDIO setup and test. |
| 2 | +/* |
| 3 | +This example requires a SDIO Card socket with the following six lines. |
| 4 | +
|
| 5 | +CLK - A clock signal sent to the card by the MCU. |
| 6 | +CMD - A bidirectional line for for commands and responses. |
| 7 | +DAT[0:3] - Four bidirectional lines for data transfer. |
| 8 | +
|
| 9 | +CLK and CMD can be connected to any GPIO pins. DAT[0:3] can be connected |
| 10 | +to any four consecutive GPIO pins in the order DAT0, DAT1, DAT2, DAT3. |
| 11 | +
|
| 12 | +For testing, I use several RP2040/RP3350 boards. |
| 13 | +The Adafruit Metro RP2040 which has a builtin SDIO socket. |
| 14 | +
|
| 15 | +https://learn.adafruit.com/adafruit-metro-rp2040 |
| 16 | +
|
| 17 | +I use this SD socket breakout board for other boards. |
| 18 | +
|
| 19 | +https://learn.adafruit.com/adafruit-microsd-spi-sdio |
| 20 | +
|
| 21 | +Wires should be short since signals can be as faster than 50 MHz. |
| 22 | +*/ |
| 23 | +#define DISABLE_FS_H_WARNING // Disable warning for type File not defined. |
| 24 | +#include "SdFat.h" |
| 25 | +//------------------------------------------------------------------------------ |
| 26 | +// Example GPIO definitions I use for debug. Edit for your setup. |
| 27 | +// Run this example as is to print the symbol for your variant. |
| 28 | +// |
| 29 | +#if defined(ARDUINO_ADAFRUIT_METRO_RP2040) |
| 30 | +#define RP_CLK_GPIO 18 |
| 31 | +#define RP_CMD_GPIO 19 |
| 32 | +#define RP_DAT0_GPIO 20 // DAT1: GPIO21, DAT2: GPIO22, DAT3: GPIO23. |
| 33 | +#elif defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_RASPBERRY_PI_PICO_2) |
| 34 | +#define RP_CLK_GPIO 16 |
| 35 | +#define RP_CMD_GPIO 17 |
| 36 | +#define RP_DAT0_GPIO 18 // DAT1: GPIO19, DAT2: GPIO20, DAT3: GPIO21. |
| 37 | +#elif defined(ARDUINO_ADAFRUIT_FEATHER_RP2350_HSTX) |
| 38 | +#define RP_CLK_GPIO 11 |
| 39 | +#define RP_CMD_GPIO 10 |
| 40 | +#define RP_DAT0_GPIO 22 // DAT1: GPIO23, DAT2: GPIO24, DAT3: GPIO25. |
| 41 | +#endif // defined(ARDUINO_ADAFRUIT_METRO_RP2040)) |
| 42 | + |
| 43 | +#if defined(RP_CLK_GPIO) && defined(RP_CMD_GPIO) && defined(RP_DAT0_GPIO) |
| 44 | +#define SD_CONFIG SdioConfig(RP_CLK_GPIO, RP_CMD_GPIO, RP_DAT0_GPIO) |
| 45 | +#else // defined(RP_CLK_GPIO) && defined(RP_CMD_GPIO) && defined(RP_DAT0_GPIO) |
| 46 | +#warning "Undefined SD_CONFIG. Run this program for the Variant Symbol." |
| 47 | +#endif // defined(RP_CLK_GPIO) && defined(RP_CMD_GPIO) && defined(RP_DAT0_GPIO) |
| 48 | +//------------------------------------------------------------------------------ |
| 49 | +// Class File is not defined by SdFat since the RP2040 system defines it. |
| 50 | +// 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT. |
| 51 | +#define SD_FAT_TYPE 3 |
| 52 | + |
| 53 | +#if SD_FAT_TYPE == 1 |
| 54 | +SdFat32 sd; |
| 55 | +File32 file; |
| 56 | +#elif SD_FAT_TYPE == 2 |
| 57 | +SdExFat sd; |
| 58 | +ExFile file; |
| 59 | +#elif SD_FAT_TYPE == 3 |
| 60 | +SdFs sd; |
| 61 | +FsFile file; |
| 62 | +#else // SD_FAT_TYPE |
| 63 | +#error Invalid SD_FAT_TYPE |
| 64 | +#endif // SD_FAT_TYPE |
| 65 | + |
| 66 | +void setup() { |
| 67 | + Serial.begin(9600); |
| 68 | + while (!Serial) { |
| 69 | + yield(); |
| 70 | + } |
| 71 | + Serial.println("Type any character to start\n"); |
| 72 | + while (!Serial.available()) { |
| 73 | + yield(); |
| 74 | + } |
| 75 | + Serial.print("Variant Symbol: "); |
| 76 | + Serial.print("ARDUINO_"); |
| 77 | + Serial.println(BOARD_NAME); |
| 78 | + Serial.println(); |
| 79 | +#if defined(SD_CONFIG) |
| 80 | + if (!sd.begin(SD_CONFIG)) { |
| 81 | + sd.initErrorHalt(&Serial); |
| 82 | + } |
| 83 | + Serial.println("Card successfully initialized."); |
| 84 | + Serial.println("\nls:"); |
| 85 | + sd.ls(LS_A | LS_DATE | LS_SIZE); // Add LS_R for recursive list. |
| 86 | + Serial.println("\nDone! Try the bench example next."); |
| 87 | +#else // #if defined(SD_CONFIG) |
| 88 | + Serial.println("Error: SD_CONFIG undefined for your board."); |
| 89 | + Serial.println("Define RP_CLK_GPIO, RP_CMD_GPIO, and RP_DAT0_GPIO above."); |
| 90 | +#endif |
| 91 | +} |
| 92 | + |
| 93 | +void loop() {} |
0 commit comments