STM32Test.ino 4.7 KB

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