Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

141 Zeilen
3.7KB

  1. /*
  2. * Example use of two SD cards.
  3. */
  4. #include <SdFat.h>
  5. #include <SdFatUtil.h>
  6. #if !USE_MULTIPLE_CARDS
  7. #error You must set USE_MULTIPLE_CARDS nonzero in SdFatConfig.h
  8. #endif
  9. SdFat sd1;
  10. const uint8_t SD1_CS = 10; // chip select for sd1
  11. SdFat sd2;
  12. const uint8_t SD2_CS = 9; // chip select for sd2
  13. const uint8_t BUF_DIM = 100;
  14. uint8_t buf[BUF_DIM];
  15. const uint32_t FILE_SIZE = 1000000;
  16. const uint16_t NWRITE = FILE_SIZE/BUF_DIM;
  17. //------------------------------------------------------------------------------
  18. // print error msg, any SD error codes, and halt.
  19. // store messages in flash
  20. #define errorExit(msg) errorHalt_P(PSTR(msg))
  21. #define initError(msg) initErrorHalt_P(PSTR(msg))
  22. //------------------------------------------------------------------------------
  23. void setup() {
  24. Serial.begin(9600);
  25. while (!Serial) {} // wait for Leonardo
  26. PgmPrint("FreeRam: ");
  27. Serial.println(FreeRam());
  28. // fill buffer with known data
  29. for (int i = 0; i < sizeof(buf); i++) buf[i] = i;
  30. PgmPrintln("type any character to start");
  31. while (Serial.read() <= 0) {}
  32. delay(400); // catch Due reset problem
  33. // disable sd2 while initializing sd1
  34. pinMode(SD2_CS, OUTPUT);
  35. digitalWrite(SD2_CS, HIGH);
  36. // initialize the first card
  37. if (!sd1.begin(SD1_CS)) {
  38. sd1.initError("sd1:");
  39. }
  40. // create DIR1 on sd1 if it does not exist
  41. if (!sd1.exists("/DIR1")) {
  42. if (!sd1.mkdir("/DIR1")) sd1.errorExit("sd1.mkdir");
  43. }
  44. // initialize the second card
  45. if (!sd2.begin(SD2_CS)) {
  46. sd2.initError("sd2:");
  47. }
  48. // create DIR2 on sd2 if it does not exist
  49. if (!sd2.exists("/DIR2")) {
  50. if (!sd2.mkdir("/DIR2")) sd2.errorExit("sd2.mkdir");
  51. }
  52. // list root directory on both cards
  53. PgmPrintln("------sd1 root-------");
  54. sd1.ls();
  55. PgmPrintln("------sd2 root-------");
  56. sd2.ls();
  57. // make /DIR1 the default directory for sd1
  58. if (!sd1.chdir("/DIR1")) sd1.errorExit("sd1.chdir");
  59. // make /DIR2 the default directory for sd2
  60. if (!sd2.chdir("/DIR2")) sd2.errorExit("sd2.chdir");
  61. // list current directory on both cards
  62. PgmPrintln("------sd1 DIR1-------");
  63. sd1.ls();
  64. PgmPrintln("------sd2 DIR2-------");
  65. sd2.ls();
  66. PgmPrintln("---------------------");
  67. // remove RENAME.BIN from /DIR2 directory of sd2
  68. if (sd2.exists("RENAME.BIN")) {
  69. if (!sd2.remove("RENAME.BIN")) {
  70. sd2.errorExit("remove RENAME.BIN");
  71. }
  72. }
  73. // set the current working directory for open() to sd1
  74. sd1.chvol();
  75. // create or open /DIR1/TEST.BIN and truncate it to zero length
  76. SdFile file1;
  77. if (!file1.open("TEST.BIN", O_RDWR | O_CREAT | O_TRUNC)) {
  78. sd1.errorExit("file1");
  79. }
  80. PgmPrintln("Writing TEST.BIN to sd1");
  81. // write data to /DIR1/TEST.BIN on sd1
  82. for (int i = 0; i < NWRITE; i++) {
  83. if (file1.write(buf, sizeof(buf)) != sizeof(buf)) {
  84. sd1.errorExit("sd1.write");
  85. }
  86. }
  87. // set the current working directory for open() to sd2
  88. sd2.chvol();
  89. // create or open /DIR2/COPY.BIN and truncate it to zero length
  90. SdFile file2;
  91. if (!file2.open("COPY.BIN", O_WRITE | O_CREAT | O_TRUNC)) {
  92. sd2.errorExit("file2");
  93. }
  94. PgmPrintln("Copying TEST.BIN to COPY.BIN");
  95. // copy file1 to file2
  96. file1.rewind();
  97. uint32_t t = millis();
  98. while (1) {
  99. int n = file1.read(buf, sizeof(buf));
  100. if (n < 0) sd1.errorExit("read1");
  101. if (n == 0) break;
  102. if (file2.write(buf, n) != n) sd2.errorExit("write2");
  103. }
  104. t = millis() - t;
  105. PgmPrint("File size: ");
  106. Serial.println(file2.fileSize());
  107. PgmPrint("Copy time: ");
  108. Serial.print(t);
  109. PgmPrintln(" millis");
  110. // close TEST.BIN
  111. file1.close();
  112. // rename the copy
  113. file2.close();
  114. if (!sd2.rename("COPY.BIN", "RENAME.BIN")) {
  115. sd2.errorExit("sd2.rename");
  116. }
  117. PgmPrintln("Done");
  118. }
  119. //------------------------------------------------------------------------------
  120. void loop() {}