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.

166 lines
4.4KB

  1. /*
  2. * Example use of two SD cards.
  3. */
  4. #include <SPI.h>
  5. #include <SdFat.h>
  6. #include <FreeStack.h>
  7. SdFat sd1;
  8. const uint8_t SD1_CS = 10; // chip select for sd1
  9. SdFat sd2;
  10. const uint8_t SD2_CS = 4; // 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("FreeStack: "));
  25. Serial.println(FreeStack());
  26. // fill buffer with known data
  27. for (int i = 0; i < sizeof(buf); i++) {
  28. buf[i] = i;
  29. }
  30. Serial.println(F("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")) {
  43. sd1.errorExit("sd1.mkdir");
  44. }
  45. }
  46. // initialize the second card
  47. if (!sd2.begin(SD2_CS)) {
  48. sd2.initError("sd2:");
  49. }
  50. // create Dir2 on sd2 if it does not exist
  51. if (!sd2.exists("/Dir2")) {
  52. if (!sd2.mkdir("/Dir2")) {
  53. sd2.errorExit("sd2.mkdir");
  54. }
  55. }
  56. // list root directory on both cards
  57. Serial.println(F("------sd1 root-------"));
  58. sd1.ls();
  59. Serial.println(F("------sd2 root-------"));
  60. sd2.ls();
  61. // make /Dir1 the default directory for sd1
  62. if (!sd1.chdir("/Dir1")) {
  63. sd1.errorExit("sd1.chdir");
  64. }
  65. // make /Dir2 the default directory for sd2
  66. if (!sd2.chdir("/Dir2")) {
  67. sd2.errorExit("sd2.chdir");
  68. }
  69. // list current directory on both cards
  70. Serial.println(F("------sd1 Dir1-------"));
  71. sd1.ls();
  72. Serial.println(F("------sd2 Dir2-------"));
  73. sd2.ls();
  74. Serial.println(F("---------------------"));
  75. // remove rename.bin from /Dir2 directory of sd2
  76. if (sd2.exists("rename.bin")) {
  77. if (!sd2.remove("rename.bin")) {
  78. sd2.errorExit("remove rename.bin");
  79. }
  80. }
  81. // set the current working directory for open() to sd1
  82. sd1.chvol();
  83. // create or open /Dir1/test.bin and truncate it to zero length
  84. SdFile file1;
  85. if (!file1.open("test.bin", O_RDWR | O_CREAT | O_TRUNC)) {
  86. sd1.errorExit("file1");
  87. }
  88. Serial.println(F("Writing test.bin to sd1"));
  89. // write data to /Dir1/test.bin on sd1
  90. for (int i = 0; i < NWRITE; i++) {
  91. if (file1.write(buf, sizeof(buf)) != sizeof(buf)) {
  92. sd1.errorExit("sd1.write");
  93. }
  94. }
  95. // set the current working directory for open() to sd2
  96. sd2.chvol();
  97. // create or open /Dir2/copy.bin and truncate it to zero length
  98. SdFile file2;
  99. if (!file2.open("copy.bin", O_WRITE | O_CREAT | O_TRUNC)) {
  100. sd2.errorExit("file2");
  101. }
  102. Serial.println(F("Copying test.bin to copy.bin"));
  103. // copy file1 to file2
  104. file1.rewind();
  105. uint32_t t = millis();
  106. while (1) {
  107. int n = file1.read(buf, sizeof(buf));
  108. if (n < 0) {
  109. sd1.errorExit("read1");
  110. }
  111. if (n == 0) {
  112. break;
  113. }
  114. if (file2.write(buf, n) != n) {
  115. sd2.errorExit("write2");
  116. }
  117. }
  118. t = millis() - t;
  119. Serial.print(F("File size: "));
  120. Serial.println(file2.fileSize());
  121. Serial.print(F("Copy time: "));
  122. Serial.print(t);
  123. Serial.println(F(" millis"));
  124. // close test.bin
  125. file1.close();
  126. file2.close();
  127. // list current directory on both cards
  128. Serial.println(F("------sd1 -------"));
  129. sd1.ls("/", LS_R | LS_DATE | LS_SIZE);
  130. Serial.println(F("------sd2 -------"));
  131. sd2.ls("/", LS_R | LS_DATE | LS_SIZE);
  132. Serial.println(F("---------------------"));
  133. Serial.println(F("Renaming copy.bin"));
  134. // rename the copy
  135. if (!sd2.rename("copy.bin", "rename.bin")) {
  136. sd2.errorExit("sd2.rename");
  137. }
  138. // list current directory on both cards
  139. Serial.println(F("------sd1 -------"));
  140. sd1.ls("/", LS_R | LS_DATE | LS_SIZE);
  141. Serial.println(F("------sd2 -------"));
  142. sd2.ls("/", LS_R | LS_DATE | LS_SIZE);
  143. Serial.println(F("---------------------"));
  144. Serial.println(F("Done"));
  145. }
  146. //------------------------------------------------------------------------------
  147. void loop() {}