您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

139 行
3.8KB

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