STM32Test.ino 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // Chip select PA4, shared SPI, 18 MHz, port 1.
  9. #define SD1_CONFIG SdSpiConfig(PA4, SHARED_SPI, SD_SCK_MHZ(18), &SPI)
  10. SdFs sd1;
  11. FsFile file1;
  12. // Use mySPI2 since SPI2 is used in SPI.h as a different type.
  13. static SPIClass mySPI2(2);
  14. // Chip select PB21, dedicated SPI, 18 MHz, port 2.
  15. #if ENABLE_DEDICATED_SPI
  16. #define SD2_CONFIG SdSpiConfig(PB12, DEDICATED_SPI, SD_SCK_MHZ(18), &mySPI2)
  17. #else // ENABLE_DEDICATED_SPI
  18. #define SD2_CONFIG SdSpiConfig(PB12, SHARED_SPI, SD_SCK_MHZ(18), &mySPI2)
  19. #endif // ENABLE_DEDICATED_SPI
  20. SdFs sd2;
  21. FsFile file2;
  22. const uint8_t BUF_DIM = 100;
  23. uint8_t buf[BUF_DIM];
  24. const uint32_t FILE_SIZE = 1000000;
  25. const uint32_t NWRITE = FILE_SIZE/BUF_DIM;
  26. //------------------------------------------------------------------------------
  27. // print error msg, any SD error codes, and halt.
  28. // store messages in flash
  29. #define error(msg) {Serial.println(msg); errorHalt();}
  30. void errorHalt() {
  31. if (sd1.sdErrorCode()) {
  32. sd1.errorHalt();
  33. }
  34. sd2.errorHalt();
  35. }
  36. //------------------------------------------------------------------------------
  37. void setup() {
  38. Serial.begin(9600);
  39. // Wait for USB Serial
  40. while (!Serial) {
  41. SysCall::yield();
  42. }
  43. Serial.print(F("FreeStack: "));
  44. Serial.println(FreeStack());
  45. // fill buffer with known data
  46. for (size_t i = 0; i < sizeof(buf); i++) {
  47. buf[i] = i;
  48. }
  49. Serial.println(F("type any character to start"));
  50. while (!Serial.available()) {
  51. SysCall::yield();
  52. }
  53. // initialize the first card
  54. if (!sd1.begin(SD1_CONFIG)) {
  55. error("sd1.begin");
  56. }
  57. // create Dir1 on sd1 if it does not exist
  58. if (!sd1.exists("/Dir1")) {
  59. if (!sd1.mkdir("/Dir1")) {
  60. error("sd1.mkdir");
  61. }
  62. }
  63. // Make Dir1 the working directory on sd1.
  64. if (!sd1.chdir("Dir1")) {
  65. error("dsd1.chdir");
  66. }
  67. // initialize the second card
  68. if (!sd2.begin(SD2_CONFIG)) {
  69. error("sd2.begin");
  70. }
  71. // create Dir2 on sd2 if it does not exist
  72. if (!sd2.exists("/Dir2")) {
  73. if (!sd2.mkdir("/Dir2")) {
  74. error("sd2.mkdir");
  75. }
  76. }
  77. // Make Dir2 the working directory on sd2.
  78. if (!sd2.chdir("Dir2")) {
  79. error("sd2.chdir");
  80. }
  81. // remove test.bin from /Dir1 directory of sd1
  82. if (sd1.exists("test.bin")) {
  83. if (!sd1.remove("test.bin")) {
  84. error("remove test.bin");
  85. }
  86. }
  87. // remove rename.bin from /Dir2 directory of sd2
  88. if (sd2.exists("rename.bin")) {
  89. if (!sd2.remove("rename.bin")) {
  90. error("remove rename.bin");
  91. }
  92. }
  93. // list directories.
  94. Serial.println(F("------sd1 Dir1-------"));
  95. sd1.ls("/", LS_R | LS_SIZE);
  96. Serial.println(F("------sd2 Dir2-------"));
  97. sd2.ls("/", LS_R | LS_SIZE);
  98. Serial.println(F("---------------------"));
  99. // create or open /Dir1/test.bin and truncate it to zero length
  100. if (!file1.open(&sd1, "test.bin", O_RDWR | O_CREAT | O_TRUNC)) {
  101. error("file1.open");
  102. }
  103. Serial.println(F("Writing test.bin to sd1"));
  104. // write data to /Dir1/test.bin on sd1
  105. for (uint32_t i = 0; i < NWRITE; i++) {
  106. if (file1.write(buf, sizeof(buf)) != sizeof(buf)) {
  107. error("file1.write");
  108. }
  109. }
  110. // create or open /Dir2/copy.bin and truncate it to zero length
  111. if (!file2.open(&sd2, "copy.bin", O_WRONLY | O_CREAT | O_TRUNC)) {
  112. error("file2.open");
  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. error("file1.read");
  122. }
  123. if (n == 0) {
  124. break;
  125. }
  126. if ((int)file2.write(buf, n) != n) {
  127. error("file2.write");
  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. // sync copy.bin so ls works.
  139. file2.close();
  140. // list directories.
  141. Serial.println(F("------sd1 -------"));
  142. sd1.ls("/", LS_R | LS_SIZE);
  143. Serial.println(F("------sd2 -------"));
  144. sd2.ls("/", LS_R | LS_SIZE);
  145. Serial.println(F("---------------------"));
  146. Serial.println(F("Renaming copy.bin"));
  147. // Rename copy.bin. The renamed file will be in Dir2.
  148. if (!sd2.rename("copy.bin", "rename.bin")) {
  149. error("rename copy.bin");
  150. }
  151. file2.close();
  152. // list directories.
  153. Serial.println(F("------sd1 -------"));
  154. sd1.ls("/", LS_R | LS_SIZE);
  155. Serial.println(F("------sd2 -------"));
  156. sd2.ls("/", LS_R | LS_SIZE);
  157. Serial.println(F("---------------------"));
  158. Serial.println(F("Done"));
  159. }
  160. //------------------------------------------------------------------------------
  161. void loop() {}