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.

TwoCards.ino 4.4KB

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