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.

174 line
4.7KB

  1. /*
  2. * Example use of two SPI ports on an STM32 board.
  3. * Note SPI speed is limited to 18 MHz.
  4. */
  5. #include <SPI.h>
  6. #include "SdFat.h"
  7. #include "FreeStack.h"
  8. // set ENABLE_EXTENDED_TRANSFER_CLASS non-zero to use faster EX classes
  9. // Use first SPI port
  10. SdFat sd1(1);
  11. // SdFatEX sd1(1);
  12. const uint8_t SD1_CS = PA4; // chip select for sd1
  13. // Use second SPI port
  14. SdFat sd2(2);
  15. // SdFatEX sd2(2);
  16. const uint8_t SD2_CS = PB12; // chip select for sd2
  17. const uint8_t BUF_DIM = 100;
  18. uint8_t buf[BUF_DIM];
  19. const uint32_t FILE_SIZE = 1000000;
  20. const uint16_t NWRITE = FILE_SIZE/BUF_DIM;
  21. //------------------------------------------------------------------------------
  22. // print error msg, any SD error codes, and halt.
  23. // store messages in flash
  24. #define errorExit(msg) errorHalt(F(msg))
  25. #define initError(msg) initErrorHalt(F(msg))
  26. //------------------------------------------------------------------------------
  27. void setup() {
  28. Serial.begin(9600);
  29. // Wait for USB Serial
  30. while (!Serial) {
  31. }
  32. // fill buffer with known data
  33. for (size_t i = 0; i < sizeof(buf); i++) {
  34. buf[i] = i;
  35. }
  36. Serial.println(F("type any character to start"));
  37. while (!Serial.available()) {
  38. }
  39. Serial.print(F("FreeStack: "));
  40. Serial.println(FreeStack());
  41. // initialize the first card
  42. if (!sd1.begin(SD1_CS, SD_SCK_MHZ(18))) {
  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, SD_SCK_MHZ(18))) {
  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. // remove test.bin from /Dir1 directory of sd1
  71. if (sd1.exists("test.bin")) {
  72. if (!sd1.remove("test.bin")) {
  73. sd2.errorExit("remove test.bin");
  74. }
  75. }
  76. // make /Dir2 the default directory for sd2
  77. if (!sd2.chdir("/Dir2")) {
  78. sd2.errorExit("sd2.chdir");
  79. }
  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. // list current directory on both cards
  87. Serial.println(F("------sd1 Dir1-------"));
  88. sd1.ls();
  89. Serial.println(F("------sd2 Dir2-------"));
  90. sd2.ls();
  91. Serial.println(F("---------------------"));
  92. // set the current working directory for open() to sd1
  93. sd1.chvol();
  94. // create or open /Dir1/test.bin and truncate it to zero length
  95. SdFile file1;
  96. if (!file1.open("test.bin", O_RDWR | O_CREAT | O_TRUNC)) {
  97. sd1.errorExit("file1");
  98. }
  99. Serial.println(F("Writing test.bin to sd1"));
  100. // write data to /Dir1/test.bin on sd1
  101. for (uint16_t i = 0; i < NWRITE; i++) {
  102. if (file1.write(buf, sizeof(buf)) != sizeof(buf)) {
  103. sd1.errorExit("sd1.write");
  104. }
  105. }
  106. // set the current working directory for open() to sd2
  107. sd2.chvol();
  108. // create or open /Dir2/copy.bin and truncate it to zero length
  109. SdFile file2;
  110. if (!file2.open("copy.bin", O_WRONLY | O_CREAT | O_TRUNC)) {
  111. sd2.errorExit("file2");
  112. }
  113. Serial.println(F("Copying test.bin to copy.bin"));
  114. // copy file1 to file2
  115. file1.rewind();
  116. uint32_t t = millis();
  117. while (1) {
  118. int n = file1.read(buf, sizeof(buf));
  119. if (n < 0) {
  120. sd1.errorExit("read1");
  121. }
  122. if (n == 0) {
  123. break;
  124. }
  125. if ((int)file2.write(buf, n) != n) {
  126. sd2.errorExit("write2");
  127. }
  128. }
  129. t = millis() - t;
  130. Serial.print(F("File size: "));
  131. Serial.println(file2.fileSize());
  132. Serial.print(F("Copy time: "));
  133. Serial.print(t);
  134. Serial.println(F(" millis"));
  135. // close test.bin
  136. file1.close();
  137. file2.close();
  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("Renaming copy.bin"));
  145. // rename the copy
  146. if (!sd2.rename("copy.bin", "rename.bin")) {
  147. sd2.errorExit("sd2.rename");
  148. }
  149. // list current directory on both cards
  150. Serial.println(F("------sd1 -------"));
  151. sd1.ls("/", LS_R | LS_DATE | LS_SIZE);
  152. Serial.println(F("------sd2 -------"));
  153. sd2.ls("/", LS_R | LS_DATE | LS_SIZE);
  154. Serial.println(F("---------------------"));
  155. Serial.println(F("Done"));
  156. }
  157. //------------------------------------------------------------------------------
  158. void loop() {}