STM32Test.ino 4.7 KB

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