Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Warning This example requires extra RAM and may crash on Uno.
  3. * Example use of two SD cards.
  4. */
  5. #include <SPI.h>
  6. #include "SdFat.h"
  7. #include "FreeStack.h"
  8. SdFat sd1;
  9. const uint8_t SD1_CS = 10; // chip select for sd1
  10. SdFat sd2;
  11. const uint8_t SD2_CS = 4; // chip select for sd2
  12. const uint8_t BUF_DIM = 100;
  13. uint8_t buf[BUF_DIM];
  14. const uint32_t FILE_SIZE = 1000000;
  15. const uint32_t NWRITE = FILE_SIZE/BUF_DIM;
  16. //------------------------------------------------------------------------------
  17. // print error msg, any SD error codes, and halt.
  18. // store messages in flash
  19. #define errorExit(msg) errorHalt(F(msg))
  20. #define initError(msg) initErrorHalt(F(msg))
  21. //------------------------------------------------------------------------------
  22. void setup() {
  23. Serial.begin(9600);
  24. // Wait for USB Serial
  25. while (!Serial) {
  26. SysCall::yield();
  27. }
  28. Serial.print(F("FreeStack: "));
  29. Serial.println(FreeStack());
  30. // fill buffer with known data
  31. for (size_t i = 0; i < sizeof(buf); i++) {
  32. buf[i] = i;
  33. }
  34. Serial.println(F("type any character to start"));
  35. while (!Serial.available()) {
  36. SysCall::yield();
  37. }
  38. // disable sd2 while initializing sd1
  39. pinMode(SD2_CS, OUTPUT);
  40. digitalWrite(SD2_CS, HIGH);
  41. // initialize the first card
  42. if (!sd1.begin(SD1_CS)) {
  43. sd1.initError("sd1:");
  44. }
  45. // create Dir1 on sd1 if it does not exist
  46. if (!sd1.exists("/Dir1")) {
  47. if (!sd1.mkdir("/Dir1")) {
  48. sd1.errorExit("sd1.mkdir");
  49. }
  50. }
  51. // initialize the second card
  52. if (!sd2.begin(SD2_CS)) {
  53. sd2.initError("sd2:");
  54. }
  55. // create Dir2 on sd2 if it does not exist
  56. if (!sd2.exists("/Dir2")) {
  57. if (!sd2.mkdir("/Dir2")) {
  58. sd2.errorExit("sd2.mkdir");
  59. }
  60. }
  61. // list root directory on both cards
  62. Serial.println(F("------sd1 root-------"));
  63. sd1.ls();
  64. Serial.println(F("------sd2 root-------"));
  65. sd2.ls();
  66. // make /Dir1 the default directory for sd1
  67. if (!sd1.chdir("/Dir1")) {
  68. sd1.errorExit("sd1.chdir");
  69. }
  70. // make /Dir2 the default directory for sd2
  71. if (!sd2.chdir("/Dir2")) {
  72. sd2.errorExit("sd2.chdir");
  73. }
  74. // list current directory on both cards
  75. Serial.println(F("------sd1 Dir1-------"));
  76. sd1.ls();
  77. Serial.println(F("------sd2 Dir2-------"));
  78. sd2.ls();
  79. Serial.println(F("---------------------"));
  80. // remove rename.bin from /Dir2 directory of sd2
  81. if (sd2.exists("rename.bin")) {
  82. if (!sd2.remove("rename.bin")) {
  83. sd2.errorExit("remove rename.bin");
  84. }
  85. }
  86. // set the current working directory for open() to sd1
  87. sd1.chvol();
  88. // create or open /Dir1/test.bin and truncate it to zero length
  89. SdFile file1;
  90. if (!file1.open("test.bin", O_RDWR | O_CREAT | O_TRUNC)) {
  91. sd1.errorExit("file1");
  92. }
  93. Serial.println(F("Writing test.bin to sd1"));
  94. // write data to /Dir1/test.bin on sd1
  95. for (uint32_t i = 0; i < NWRITE; i++) {
  96. if (file1.write(buf, sizeof(buf)) != sizeof(buf)) {
  97. sd1.errorExit("sd1.write");
  98. }
  99. }
  100. // set the current working directory for open() to sd2
  101. sd2.chvol();
  102. // create or open /Dir2/copy.bin and truncate it to zero length
  103. SdFile file2;
  104. if (!file2.open("copy.bin", O_WRONLY | O_CREAT | O_TRUNC)) {
  105. sd2.errorExit("file2");
  106. }
  107. Serial.println(F("Copying test.bin to copy.bin"));
  108. // copy file1 to file2
  109. file1.rewind();
  110. uint32_t t = millis();
  111. while (1) {
  112. int n = file1.read(buf, sizeof(buf));
  113. if (n < 0) {
  114. sd1.errorExit("read1");
  115. }
  116. if (n == 0) {
  117. break;
  118. }
  119. if ((int)file2.write(buf, n) != n) {
  120. sd2.errorExit("write2");
  121. }
  122. }
  123. t = millis() - t;
  124. Serial.print(F("File size: "));
  125. Serial.println(file2.fileSize());
  126. Serial.print(F("Copy time: "));
  127. Serial.print(t);
  128. Serial.println(F(" millis"));
  129. // close test.bin
  130. file1.close();
  131. file2.close();
  132. // list current directory on both cards
  133. Serial.println(F("------sd1 -------"));
  134. sd1.ls("/", LS_R | LS_DATE | LS_SIZE);
  135. Serial.println(F("------sd2 -------"));
  136. sd2.ls("/", LS_R | LS_DATE | LS_SIZE);
  137. Serial.println(F("---------------------"));
  138. Serial.println(F("Renaming copy.bin"));
  139. // rename the copy
  140. if (!sd2.rename("copy.bin", "rename.bin")) {
  141. sd2.errorExit("sd2.rename");
  142. }
  143. // list current directory on both cards
  144. Serial.println(F("------sd1 -------"));
  145. sd1.ls("/", LS_R | LS_DATE | LS_SIZE);
  146. Serial.println(F("------sd2 -------"));
  147. sd2.ls("/", LS_R | LS_DATE | LS_SIZE);
  148. Serial.println(F("---------------------"));
  149. Serial.println(F("Done"));
  150. }
  151. //------------------------------------------------------------------------------
  152. void loop() {}