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

174 行
5.1KB

  1. /*
  2. * This sketch is a simple binary write/read benchmark.
  3. */
  4. #include <SPI.h>
  5. #include <SdFat.h>
  6. #include <SdFatUtil.h>
  7. // SD chip select pin
  8. const uint8_t chipSelect = SS;
  9. // Size of read/write.
  10. const size_t BUF_SIZE = 512;
  11. // File size in MB where MB = 1,000,000 bytes.
  12. const uint32_t FILE_SIZE_MB = 5;
  13. // Write pass count.
  14. const uint8_t WRITE_COUNT = 10;
  15. // Read pass count.
  16. const uint8_t READ_COUNT = 5;
  17. //==============================================================================
  18. // End of configuration constants.
  19. //------------------------------------------------------------------------------
  20. // File size in bytes.
  21. const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB;
  22. uint8_t buf[BUF_SIZE];
  23. // file system
  24. SdFat sd;
  25. // test file
  26. SdFile file;
  27. // Serial output stream
  28. ArduinoOutStream cout(Serial);
  29. //------------------------------------------------------------------------------
  30. // store error strings in flash to save RAM
  31. #define error(s) sd.errorHalt(F(s))
  32. //------------------------------------------------------------------------------
  33. void cidDmp() {
  34. cid_t cid;
  35. if (!sd.card()->readCID(&cid)) {
  36. error("readCID failed");
  37. }
  38. cout << pstr("\nManufacturer ID: ");
  39. cout << hex << int(cid.mid) << dec << endl;
  40. cout << pstr("OEM ID: ") << cid.oid[0] << cid.oid[1] << endl;
  41. cout << pstr("Product: ");
  42. for (uint8_t i = 0; i < 5; i++) {
  43. cout << cid.pnm[i];
  44. }
  45. cout << pstr("\nVersion: ");
  46. cout << int(cid.prv_n) << '.' << int(cid.prv_m) << endl;
  47. cout << pstr("Serial number: ") << hex << cid.psn << dec << endl;
  48. cout << pstr("Manufacturing date: ");
  49. cout << int(cid.mdt_month) << '/';
  50. cout << (2000 + cid.mdt_year_low + 10 * cid.mdt_year_high) << endl;
  51. cout << endl;
  52. }
  53. //------------------------------------------------------------------------------
  54. void setup() {
  55. Serial.begin(9600);
  56. while (!Serial){} // wait for Leonardo
  57. cout << pstr("\nUse a freshly formatted SD for best performance.\n");
  58. // use uppercase in hex and use 0X base prefix
  59. cout << uppercase << showbase << endl;
  60. }
  61. //------------------------------------------------------------------------------
  62. void loop() {
  63. float s;
  64. uint32_t t;
  65. uint32_t maxLatency;
  66. uint32_t minLatency;
  67. uint32_t totalLatency;
  68. // discard any input
  69. while (Serial.read() >= 0) {}
  70. // pstr stores strings in flash to save RAM
  71. cout << pstr("Type any character to start\n");
  72. while (Serial.read() <= 0) {}
  73. delay(400); // catch Due reset problem
  74. cout << pstr("Free RAM: ") << FreeRam() << endl;
  75. // initialize the SD card at SPI_FULL_SPEED for best performance.
  76. // try SPI_HALF_SPEED if bus errors occur.
  77. if (!sd.begin(chipSelect, SPI_FULL_SPEED)) sd.initErrorHalt();
  78. cout << pstr("Type is FAT") << int(sd.vol()->fatType()) << endl;
  79. cout << pstr("Card size: ") << sd.card()->cardSize()*512E-9;
  80. cout << pstr(" GB (GB = 1E9 bytes)") << endl;
  81. cidDmp();
  82. // open or create file - truncate existing file.
  83. if (!file.open("BENCH.DAT", O_CREAT | O_TRUNC | O_RDWR)) {
  84. error("open failed");
  85. }
  86. // fill buf with known data
  87. for (uint16_t i = 0; i < (BUF_SIZE-2); i++) {
  88. buf[i] = 'A' + (i % 26);
  89. }
  90. buf[BUF_SIZE-2] = '\r';
  91. buf[BUF_SIZE-1] = '\n';
  92. cout << pstr("File size ") << FILE_SIZE_MB << pstr(" MB\n");
  93. cout << pstr("Buffer size ") << BUF_SIZE << pstr(" bytes\n");
  94. cout << pstr("Starting write test, please wait.") << endl << endl;
  95. // do write test
  96. uint32_t n = FILE_SIZE/sizeof(buf);
  97. cout <<pstr("write speed and latency") << endl;
  98. cout << pstr("speed,max,min,avg") << endl;
  99. cout << pstr("KB/Sec,usec,usec,usec") << endl;
  100. for (uint8_t nTest = 0; nTest < WRITE_COUNT; nTest++) {
  101. file.truncate(0);
  102. maxLatency = 0;
  103. minLatency = 9999999;
  104. totalLatency = 0;
  105. t = millis();
  106. for (uint32_t i = 0; i < n; i++) {
  107. uint32_t m = micros();
  108. if (file.write(buf, sizeof(buf)) != sizeof(buf)) {
  109. error("write failed");
  110. }
  111. m = micros() - m;
  112. if (maxLatency < m) maxLatency = m;
  113. if (minLatency > m) minLatency = m;
  114. totalLatency += m;
  115. }
  116. file.sync();
  117. t = millis() - t;
  118. s = file.fileSize();
  119. cout << s/t <<',' << maxLatency << ',' << minLatency;
  120. cout << ',' << totalLatency/n << endl;
  121. }
  122. cout << endl << pstr("Starting read test, please wait.") << endl;
  123. cout << endl <<pstr("read speed and latency") << endl;
  124. cout << pstr("speed,max,min,avg") << endl;
  125. cout << pstr("KB/Sec,usec,usec,usec") << endl;
  126. // do read test
  127. for (uint8_t nTest = 0; nTest < READ_COUNT; nTest++) {
  128. file.rewind();
  129. maxLatency = 0;
  130. minLatency = 9999999;
  131. totalLatency = 0;
  132. t = millis();
  133. for (uint32_t i = 0; i < n; i++) {
  134. buf[BUF_SIZE-1] = 0;
  135. uint32_t m = micros();
  136. if (file.read(buf, sizeof(buf)) != sizeof(buf)) {
  137. error("read failed");
  138. }
  139. m = micros() - m;
  140. if (maxLatency < m) maxLatency = m;
  141. if (minLatency > m) minLatency = m;
  142. totalLatency += m;
  143. if (buf[BUF_SIZE-1] != '\n') {
  144. error("data check");
  145. }
  146. }
  147. t = millis() - t;
  148. cout << s/t <<',' << maxLatency << ',' << minLatency;
  149. cout << ',' << totalLatency/n << endl;
  150. }
  151. cout << endl << pstr("Done") << endl;
  152. file.close();
  153. }