You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.6KB

  1. /*
  2. * Print size, modify date/time, and name for all files in root.
  3. */
  4. #include "SdFat.h"
  5. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  6. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  7. #define SD_FAT_TYPE 0
  8. /*
  9. Change the value of SD_CS_PIN if you are using SPI and
  10. your hardware does not use the default value, SS.
  11. Common values are:
  12. Arduino Ethernet shield: pin 4
  13. Sparkfun SD shield: pin 8
  14. Adafruit SD shields and modules: pin 10
  15. */
  16. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  17. #ifndef SDCARD_SS_PIN
  18. const uint8_t SD_CS_PIN = SS;
  19. #else // SDCARD_SS_PIN
  20. // Assume built-in SD is used.
  21. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  22. #endif // SDCARD_SS_PIN
  23. // Try to select the best SD card configuration.
  24. #if HAS_SDIO_CLASS
  25. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  26. #elif ENABLE_DEDICATED_SPI
  27. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
  28. #else // HAS_SDIO_CLASS
  29. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
  30. #endif // HAS_SDIO_CLASS
  31. #if SD_FAT_TYPE == 0
  32. SdFat sd;
  33. File dir;
  34. File file;
  35. #elif SD_FAT_TYPE == 1
  36. SdFat32 sd;
  37. File32 dir;
  38. File32 file;
  39. #elif SD_FAT_TYPE == 2
  40. SdExFat sd;
  41. ExFile dir;
  42. ExFile file;
  43. #elif SD_FAT_TYPE == 3
  44. SdFs sd;
  45. FsFile dir;
  46. FsFile file;
  47. #else // SD_FAT_TYPE
  48. #error invalid SD_FAT_TYPE
  49. #endif // SD_FAT_TYPE
  50. //------------------------------------------------------------------------------
  51. // Store error strings in flash to save RAM.
  52. #define error(s) sd.errorHalt(&Serial, F(s))
  53. //------------------------------------------------------------------------------
  54. void setup() {
  55. Serial.begin(9600);
  56. // Wait for USB Serial
  57. while (!Serial) {
  58. SysCall::yield();
  59. }
  60. Serial.println("Type any character to start");
  61. while (!Serial.available()) {
  62. SysCall::yield();
  63. }
  64. // Initialize the SD.
  65. if (!sd.begin(SD_CONFIG)) {
  66. sd.initErrorHalt(&Serial);
  67. }
  68. // Open root directory
  69. if (!dir.open("/")){
  70. error("dir.open failed");
  71. }
  72. // Open next file in root.
  73. // Warning, openNext starts at the current position of dir so a
  74. // rewind may be necessary in your application.
  75. while (file.openNext(&dir, O_RDONLY)) {
  76. file.printFileSize(&Serial);
  77. Serial.write(' ');
  78. file.printModifyDateTime(&Serial);
  79. Serial.write(' ');
  80. file.printName(&Serial);
  81. if (file.isDir()) {
  82. // Indicate a directory.
  83. Serial.write('/');
  84. }
  85. Serial.println();
  86. file.close();
  87. }
  88. if (dir.getError()) {
  89. Serial.println("openNext failed");
  90. } else {
  91. Serial.println("Done!");
  92. }
  93. }
  94. //------------------------------------------------------------------------------
  95. void loop() {}