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.

STM32Test.ino 4.5KB

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