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.

156 satır
4.0KB

  1. /*
  2. * Example use of chdir(), ls(), mkdir(), and rmdir().
  3. */
  4. #include "SdFat.h"
  5. #include "sdios.h"
  6. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  7. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  8. #define SD_FAT_TYPE 0
  9. /*
  10. Change the value of SD_CS_PIN if you are using SPI and
  11. your hardware does not use the default value, SS.
  12. Common values are:
  13. Arduino Ethernet shield: pin 4
  14. Sparkfun SD shield: pin 8
  15. Adafruit SD shields and modules: pin 10
  16. */
  17. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  18. #ifndef SDCARD_SS_PIN
  19. const uint8_t SD_CS_PIN = SS;
  20. #else // SDCARD_SS_PIN
  21. // Assume built-in SD is used.
  22. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  23. #endif // SDCARD_SS_PIN
  24. // Try to select the best SD card configuration.
  25. #if HAS_SDIO_CLASS
  26. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  27. #elif ENABLE_DEDICATED_SPI
  28. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
  29. #else // HAS_SDIO_CLASS
  30. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
  31. #endif // HAS_SDIO_CLASS
  32. //------------------------------------------------------------------------------
  33. #if SD_FAT_TYPE == 0
  34. SdFat sd;
  35. File file;
  36. File root;
  37. #elif SD_FAT_TYPE == 1
  38. SdFat32 sd;
  39. File32 file;
  40. File32 root;
  41. #elif SD_FAT_TYPE == 2
  42. SdExFat sd;
  43. ExFile file;
  44. ExFile root;
  45. #elif SD_FAT_TYPE == 3
  46. SdFs sd;
  47. FsFile file;
  48. FsFile root;
  49. #endif // SD_FAT_TYPE
  50. // Create a Serial output stream.
  51. ArduinoOutStream cout(Serial);
  52. //------------------------------------------------------------------------------
  53. // Store error strings in flash to save RAM.
  54. #define error(s) sd.errorHalt(&Serial, F(s))
  55. //------------------------------------------------------------------------------
  56. void setup() {
  57. Serial.begin(9600);
  58. // Wait for USB Serial
  59. while (!Serial) {
  60. SysCall::yield();
  61. }
  62. delay(1000);
  63. cout << F("Type any character to start\n");
  64. while (!Serial.available()) {
  65. SysCall::yield();
  66. }
  67. // Initialize the SD card.
  68. if (!sd.begin(SD_CONFIG)) {
  69. sd.initErrorHalt(&Serial);
  70. }
  71. if (sd.exists("Folder1")
  72. || sd.exists("Folder1/file1.txt")
  73. || sd.exists("Folder1/File2.txt")) {
  74. error("Please remove existing Folder1, file1.txt, and File2.txt");
  75. }
  76. int rootFileCount = 0;
  77. if (!root.open("/")) {
  78. error("open root");
  79. }
  80. while (file.openNext(&root, O_RDONLY)) {
  81. if (!file.isHidden()) {
  82. rootFileCount++;
  83. }
  84. file.close();
  85. if (rootFileCount > 10) {
  86. error("Too many files in root. Please use an empty SD.");
  87. }
  88. }
  89. if (rootFileCount) {
  90. cout << F("\nPlease use an empty SD for best results.\n\n");
  91. delay(1000);
  92. }
  93. // Create a new folder.
  94. if (!sd.mkdir("Folder1")) {
  95. error("Create Folder1 failed");
  96. }
  97. cout << F("Created Folder1\n");
  98. // Create a file in Folder1 using a path.
  99. if (!file.open("Folder1/file1.txt", O_WRONLY | O_CREAT)) {
  100. error("create Folder1/file1.txt failed");
  101. }
  102. file.close();
  103. cout << F("Created Folder1/file1.txt\n");
  104. // Change volume working directory to Folder1.
  105. if (!sd.chdir("Folder1")) {
  106. error("chdir failed for Folder1.\n");
  107. }
  108. cout << F("chdir to Folder1\n");
  109. // Create File2.txt in current directory.
  110. if (!file.open("File2.txt", O_WRONLY | O_CREAT)) {
  111. error("create File2.txt failed");
  112. }
  113. file.close();
  114. cout << F("Created File2.txt in current directory\n");
  115. cout << F("\nList of files on the SD.\n");
  116. sd.ls("/", LS_R);
  117. // Remove files from current directory.
  118. if (!sd.remove("file1.txt") || !sd.remove("File2.txt")) {
  119. error("remove failed");
  120. }
  121. cout << F("\nfile1.txt and File2.txt removed.\n");
  122. // Change current directory to root.
  123. if (!sd.chdir()) {
  124. error("chdir to root failed.\n");
  125. }
  126. cout << F("\nList of files on the SD.\n");
  127. sd.ls(LS_R);
  128. // Remove Folder1.
  129. if (!sd.rmdir("Folder1")) {
  130. error("rmdir for Folder1 failed\n");
  131. }
  132. cout << F("\nFolder1 removed.\n");
  133. cout << F("\nList of files on the SD.\n");
  134. sd.ls(LS_R);
  135. cout << F("Done!\n");
  136. }
  137. //------------------------------------------------------------------------------
  138. // Nothing happens in loop.
  139. void loop() {}