您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * This program is a simple binary write/read benchmark.
  3. */
  4. #include <SPI.h>
  5. #include "SdFat.h"
  6. #include "FreeStack.h"
  7. // Set USE_SDIO to zero for SPI card access.
  8. #define USE_SDIO 0
  9. // SD chip select pin
  10. const uint8_t chipSelect = SS;
  11. // Size of read/write.
  12. const size_t BUF_SIZE = 512;
  13. // File size in MB where MB = 1,000,000 bytes.
  14. const uint32_t FILE_SIZE_MB = 5;
  15. // Write pass count.
  16. const uint8_t WRITE_COUNT = 2;
  17. // Read pass count.
  18. const uint8_t READ_COUNT = 2;
  19. //==============================================================================
  20. // End of configuration constants.
  21. //------------------------------------------------------------------------------
  22. // File size in bytes.
  23. const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB;
  24. uint8_t buf[BUF_SIZE];
  25. // file system
  26. #if USE_SDIO
  27. // Traditional DMA version.
  28. // SdFatSdio sd;
  29. // Faster version.
  30. SdFatSdioEX sd;
  31. #else // USE_SDIO
  32. SdFat sd;
  33. #endif // USE_SDIO
  34. // Set ENABLE_EXTENDED_TRANSFER_CLASS to use extended SD I/O.
  35. // Requires dedicated use of the SPI bus.
  36. // SdFatEX sd;
  37. // Set ENABLE_SOFTWARE_SPI_CLASS to use software SPI.
  38. // Args are misoPin, mosiPin, sckPin.
  39. // SdFatSoftSpi<6, 7, 5> sd;
  40. // test file
  41. SdFile file;
  42. // Serial output stream
  43. ArduinoOutStream cout(Serial);
  44. //------------------------------------------------------------------------------
  45. // Store error strings in flash to save RAM.
  46. #define error(s) sd.errorHalt(F(s))
  47. //------------------------------------------------------------------------------
  48. void cidDmp() {
  49. cid_t cid;
  50. if (!sd.card()->readCID(&cid)) {
  51. error("readCID failed");
  52. }
  53. cout << F("\nManufacturer ID: ");
  54. cout << hex << int(cid.mid) << dec << endl;
  55. cout << F("OEM ID: ") << cid.oid[0] << cid.oid[1] << endl;
  56. cout << F("Product: ");
  57. for (uint8_t i = 0; i < 5; i++) {
  58. cout << cid.pnm[i];
  59. }
  60. cout << F("\nVersion: ");
  61. cout << int(cid.prv_n) << '.' << int(cid.prv_m) << endl;
  62. cout << F("Serial number: ") << hex << cid.psn << dec << endl;
  63. cout << F("Manufacturing date: ");
  64. cout << int(cid.mdt_month) << '/';
  65. cout << (2000 + cid.mdt_year_low + 10 * cid.mdt_year_high) << endl;
  66. cout << endl;
  67. }
  68. //------------------------------------------------------------------------------
  69. void setup() {
  70. Serial.begin(9600);
  71. // Wait for USB Serial
  72. while (!Serial) {
  73. SysCall::yield();
  74. }
  75. delay(1000);
  76. cout << F("\nUse a freshly formatted SD for best performance.\n");
  77. // use uppercase in hex and use 0X base prefix
  78. cout << uppercase << showbase << endl;
  79. }
  80. //------------------------------------------------------------------------------
  81. void loop() {
  82. float s;
  83. uint32_t t;
  84. uint32_t maxLatency;
  85. uint32_t minLatency;
  86. uint32_t totalLatency;
  87. // Discard any input.
  88. do {
  89. delay(10);
  90. } while (Serial.available() && Serial.read() >= 0);
  91. // F( stores strings in flash to save RAM
  92. cout << F("Type any character to start\n");
  93. while (!Serial.available()) {
  94. SysCall::yield();
  95. }
  96. cout << F("FreeStack: ") << FreeStack() << endl;
  97. #if USE_SDIO
  98. if (!sd.begin()) {
  99. sd.initErrorHalt();
  100. }
  101. #else // USE_SDIO
  102. // Initialize at the highest speed supported by the board that is
  103. // not over 50 MHz. Try a lower speed if SPI errors occur.
  104. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  105. sd.initErrorHalt();
  106. }
  107. #endif // USE_SDIO
  108. cout << F("Type is FAT") << int(sd.vol()->fatType()) << endl;
  109. cout << F("Card size: ") << sd.card()->cardSize()*512E-9;
  110. cout << F(" GB (GB = 1E9 bytes)") << endl;
  111. cidDmp();
  112. // open or create file - truncate existing file.
  113. if (!file.open("bench.dat", O_CREAT | O_TRUNC | O_RDWR)) {
  114. error("open failed");
  115. }
  116. // fill buf with known data
  117. for (uint16_t i = 0; i < (BUF_SIZE-2); i++) {
  118. buf[i] = 'A' + (i % 26);
  119. }
  120. buf[BUF_SIZE-2] = '\r';
  121. buf[BUF_SIZE-1] = '\n';
  122. cout << F("File size ") << FILE_SIZE_MB << F(" MB\n");
  123. cout << F("Buffer size ") << BUF_SIZE << F(" bytes\n");
  124. cout << F("Starting write test, please wait.") << endl << endl;
  125. // do write test
  126. uint32_t n = FILE_SIZE/sizeof(buf);
  127. cout <<F("write speed and latency") << endl;
  128. cout << F("speed,max,min,avg") << endl;
  129. cout << F("KB/Sec,usec,usec,usec") << endl;
  130. for (uint8_t nTest = 0; nTest < WRITE_COUNT; nTest++) {
  131. file.truncate(0);
  132. maxLatency = 0;
  133. minLatency = 9999999;
  134. totalLatency = 0;
  135. t = millis();
  136. for (uint32_t i = 0; i < n; i++) {
  137. uint32_t m = micros();
  138. if (file.write(buf, sizeof(buf)) != sizeof(buf)) {
  139. sd.errorPrint("write failed");
  140. file.close();
  141. return;
  142. }
  143. m = micros() - m;
  144. if (maxLatency < m) {
  145. maxLatency = m;
  146. }
  147. if (minLatency > m) {
  148. minLatency = m;
  149. }
  150. totalLatency += m;
  151. }
  152. file.sync();
  153. t = millis() - t;
  154. s = file.fileSize();
  155. cout << s/t <<',' << maxLatency << ',' << minLatency;
  156. cout << ',' << totalLatency/n << endl;
  157. }
  158. cout << endl << F("Starting read test, please wait.") << endl;
  159. cout << endl <<F("read speed and latency") << endl;
  160. cout << F("speed,max,min,avg") << endl;
  161. cout << F("KB/Sec,usec,usec,usec") << endl;
  162. // do read test
  163. for (uint8_t nTest = 0; nTest < READ_COUNT; nTest++) {
  164. file.rewind();
  165. maxLatency = 0;
  166. minLatency = 9999999;
  167. totalLatency = 0;
  168. t = millis();
  169. for (uint32_t i = 0; i < n; i++) {
  170. buf[BUF_SIZE-1] = 0;
  171. uint32_t m = micros();
  172. int32_t nr = file.read(buf, sizeof(buf));
  173. if (nr != sizeof(buf)) {
  174. sd.errorPrint("read failed");
  175. file.close();
  176. return;
  177. }
  178. m = micros() - m;
  179. if (maxLatency < m) {
  180. maxLatency = m;
  181. }
  182. if (minLatency > m) {
  183. minLatency = m;
  184. }
  185. totalLatency += m;
  186. if (buf[BUF_SIZE-1] != '\n') {
  187. error("data check");
  188. }
  189. }
  190. s = file.fileSize();
  191. t = millis() - t;
  192. cout << s/t <<',' << maxLatency << ',' << minLatency;
  193. cout << ',' << totalLatency/n << endl;
  194. }
  195. cout << endl << F("Done") << endl;
  196. file.close();
  197. }