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