Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
9 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Example use of three SD cards.
  3. */
  4. #include <SPI.h>
  5. #include "SdFat.h"
  6. #include "FreeStack.h"
  7. #if SD_SPI_CONFIGURATION >= 3 // Must be set in SdFat/SdFatConfig.h
  8. // SD1 is a microSD on hardware SPI pins 50-52
  9. // Using my fast custom SPI
  10. SdFat sd1;
  11. const uint8_t SD1_CS = 53;
  12. // SD2 is a Catalex shield on hardware SPI pins 50-52
  13. // Using the standard Arduino SPI library
  14. SdFatLibSpi sd2;
  15. const uint8_t SD2_CS = 4;
  16. // SD3 is a Adafruit data logging shield on pins 10-13
  17. // Using Software SPI
  18. SdFatSoftSpi<12, 11, 13> sd3;
  19. const uint8_t SD3_CS = 10;
  20. const uint8_t BUF_DIM = 100;
  21. uint8_t buf[BUF_DIM];
  22. const uint32_t FILE_SIZE = 1000000;
  23. const uint16_t NWRITE = FILE_SIZE/BUF_DIM;
  24. //------------------------------------------------------------------------------
  25. // print error msg, any SD error codes, and halt.
  26. // store messages in flash
  27. #define errorExit(msg) errorHalt(F(msg))
  28. #define initError(msg) initErrorHalt(F(msg))
  29. //------------------------------------------------------------------------------
  30. void list() {
  31. // list current directory on all cards
  32. Serial.println(F("------sd1-------"));
  33. sd1.ls("/", LS_SIZE|LS_R);
  34. Serial.println(F("------sd2-------"));
  35. sd2.ls("/", LS_SIZE|LS_R);
  36. Serial.println(F("------sd3-------"));
  37. sd3.ls("/", LS_SIZE|LS_R);
  38. Serial.println(F("---------------------"));
  39. }
  40. //------------------------------------------------------------------------------
  41. void setup() {
  42. Serial.begin(9600);
  43. // Wait for USB Serial
  44. while (!Serial) {
  45. SysCall::yield();
  46. }
  47. Serial.print(F("FreeStack: "));
  48. Serial.println(FreeStack());
  49. // fill buffer with known data
  50. for (size_t i = 0; i < sizeof(buf); i++) {
  51. buf[i] = i;
  52. }
  53. Serial.println(F("type any character to start"));
  54. while (!Serial.available()) {
  55. SysCall::yield();
  56. }
  57. // disable sd2 while initializing sd1
  58. pinMode(SD2_CS, OUTPUT);
  59. digitalWrite(SD2_CS, HIGH);
  60. // initialize the first card
  61. if (!sd1.begin(SD1_CS)) {
  62. sd1.initError("sd1:");
  63. }
  64. // initialize the second card
  65. if (!sd2.begin(SD2_CS)) {
  66. sd2.initError("sd2:");
  67. }
  68. // initialize the third card
  69. if (!sd3.begin(SD3_CS)) {
  70. sd3.initError("sd3:");
  71. }
  72. Serial.println(F("Cards OK - creating directories"));
  73. // create Dir1 on sd1 if it does not exist
  74. if (!sd1.exists("/Dir1")) {
  75. if (!sd1.mkdir("/Dir1")) {
  76. sd1.errorExit("sd1.mkdir");
  77. }
  78. }
  79. // make /Dir1 the default directory for sd1
  80. if (!sd1.chdir("/Dir1")) {
  81. sd1.errorExit("sd1.chdir");
  82. }
  83. // create Dir2 on sd2 if it does not exist
  84. if (!sd2.exists("/Dir2")) {
  85. if (!sd2.mkdir("/Dir2")) {
  86. sd2.errorExit("sd2.mkdir");
  87. }
  88. }
  89. // make /Dir2 the default directory for sd2
  90. if (!sd2.chdir("/Dir2")) {
  91. sd2.errorExit("sd2.chdir");
  92. }
  93. // create Dir3 on sd3 if it does not exist
  94. if (!sd3.exists("/Dir3")) {
  95. if (!sd3.mkdir("/Dir3")) {
  96. sd2.errorExit("sd3.mkdir");
  97. }
  98. }
  99. // make /Dir3 the default directory for sd3
  100. if (!sd3.chdir("/Dir3")) {
  101. sd3.errorExit("sd3.chdir");
  102. }
  103. Serial.println(F("Directories created - removing old files"));
  104. if (sd1.exists("TEST1.bin")) {
  105. if (!sd1.remove("TEST1.bin")) {
  106. sd1.errorExit("sd1.remove");
  107. }
  108. }
  109. if (sd2.exists("TEST2.bin")) {
  110. if (!sd2.remove("TEST2.bin")) {
  111. sd2.errorExit("sd2.remove");
  112. }
  113. }
  114. if (sd3.exists("TEST3.bin")) {
  115. if (!sd3.remove("TEST3.bin")) {
  116. sd2.errorExit("sd3.remove");
  117. }
  118. }
  119. Serial.println("Initial SD directories");
  120. list();
  121. // create or open /Dir1/TEST1.bin and truncate it to zero length
  122. SdFile file1;
  123. if (!file1.open(&sd1, "TEST1.bin", O_RDWR | O_CREAT | O_TRUNC)) {
  124. sd1.errorExit("file1");
  125. }
  126. Serial.println(F("Writing SD1:/Dir1/TEST1.bin"));
  127. // write data to /Dir1/TEST1.bin on sd1
  128. for (uint16_t i = 0; i < NWRITE; i++) {
  129. if (file1.write(buf, sizeof(buf)) != sizeof(buf)) {
  130. sd1.errorExit("sd1.write");
  131. }
  132. }
  133. file1.sync();
  134. list();
  135. // create or open /Dir2/TEST2.bin and truncate it to zero length
  136. SdFile file2;
  137. if (!file2.open(&sd2, "TEST2.bin", O_RDWR | O_CREAT | O_TRUNC)) {
  138. sd2.errorExit("file2");
  139. }
  140. Serial.println(F("Copying SD1:/Dir1/TEST1.bin to SD2::/Dir2/TEST2.bin"));
  141. // copy file1 to file2
  142. file1.rewind();
  143. uint32_t t = millis();
  144. while (1) {
  145. int n = file1.read(buf, sizeof(buf));
  146. if (n < 0) {
  147. sd1.errorExit("read1");
  148. }
  149. if (n == 0) {
  150. break;
  151. }
  152. if ((int)file2.write(buf, n) != n) {
  153. sd2.errorExit("write3");
  154. }
  155. }
  156. t = millis() - t;
  157. file2.sync();
  158. Serial.print(F("File size: "));
  159. Serial.println(file2.fileSize());
  160. Serial.print(F("Copy time: "));
  161. Serial.print(t);
  162. Serial.println(F(" millis"));
  163. list();
  164. // create or open /Dir3/TEST3.bin and truncate it to zero length
  165. SdFile file3;
  166. if (!file3.open(&sd3, "TEST3.bin", O_RDWR | O_CREAT | O_TRUNC)) {
  167. sd3.errorExit("file3");
  168. }
  169. file2.rewind();
  170. Serial.println(F("Copying SD2:/Dir2/TEST2.bin to SD3:/Dir3/TEST3.bin"));
  171. while (1) {
  172. int n = file2.read(buf, sizeof(buf));
  173. if (n == 0) {
  174. break;
  175. }
  176. if (n != sizeof(buf)) {
  177. sd2.errorExit("read2");
  178. }
  179. if ((int)file3.write(buf, n) != n) {
  180. sd3.errorExit("write2");
  181. }
  182. }
  183. file3.sync();
  184. list();
  185. // Verify content of file3
  186. file3.rewind();
  187. Serial.println(F("Verifying content of TEST3.bin"));
  188. for (uint16_t i = 0; i < NWRITE; i++) {
  189. if (file3.read(buf, sizeof(buf)) != sizeof(buf)) {
  190. sd3.errorExit("sd3.read");
  191. }
  192. for (size_t j = 0; j < sizeof(buf); j++) {
  193. if (j != buf[j]) {
  194. sd3.errorExit("Verify error");
  195. }
  196. }
  197. }
  198. Serial.println(F("Done - Verify OK"));
  199. file1.close();
  200. file2.close();
  201. file3.close();
  202. }
  203. //------------------------------------------------------------------------------
  204. void loop() {}
  205. #else // SD_SPI_CONFIGURATION >= 3
  206. #error SD_SPI_CONFIGURATION must be set to 3 in SdFat/SdFatConfig.h
  207. #endif //SD_SPI_CONFIGURATION >= 3