Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

185 lines
5.1KB

  1. /*
  2. * This program 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 << F("\nManufacturer ID: ");
  39. cout << hex << int(cid.mid) << dec << endl;
  40. cout << F("OEM ID: ") << cid.oid[0] << cid.oid[1] << endl;
  41. cout << F("Product: ");
  42. for (uint8_t i = 0; i < 5; i++) {
  43. cout << cid.pnm[i];
  44. }
  45. cout << F("\nVersion: ");
  46. cout << int(cid.prv_n) << '.' << int(cid.prv_m) << endl;
  47. cout << F("Serial number: ") << hex << cid.psn << dec << endl;
  48. cout << F("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. delay(1000);
  58. cout << F("\nUse a freshly formatted SD for best performance.\n");
  59. // use uppercase in hex and use 0X base prefix
  60. cout << uppercase << showbase << endl;
  61. }
  62. //------------------------------------------------------------------------------
  63. void loop() {
  64. float s;
  65. uint32_t t;
  66. uint32_t maxLatency;
  67. uint32_t minLatency;
  68. uint32_t totalLatency;
  69. // discard any input
  70. while (Serial.read() >= 0) {}
  71. // F( stores strings in flash to save RAM
  72. cout << F("Type any character to start\n");
  73. while (Serial.read() <= 0) {}
  74. delay(400); // catch Due reset problem
  75. cout << F("Free RAM: ") << FreeRam() << endl;
  76. // initialize the SD card at SPI_FULL_SPEED for best performance.
  77. // try SPI_HALF_SPEED if bus errors occur.
  78. if (!sd.begin(chipSelect, SPI_FULL_SPEED)) {
  79. sd.initErrorHalt();
  80. }
  81. cout << F("Type is FAT") << int(sd.vol()->fatType()) << endl;
  82. cout << F("Card size: ") << sd.card()->cardSize()*512E-9;
  83. cout << F(" GB (GB = 1E9 bytes)") << endl;
  84. cidDmp();
  85. // open or create file - truncate existing file.
  86. if (!file.open("bench.dat", O_CREAT | O_TRUNC | O_RDWR)) {
  87. error("open failed");
  88. }
  89. // fill buf with known data
  90. for (uint16_t i = 0; i < (BUF_SIZE-2); i++) {
  91. buf[i] = 'A' + (i % 26);
  92. }
  93. buf[BUF_SIZE-2] = '\r';
  94. buf[BUF_SIZE-1] = '\n';
  95. cout << F("File size ") << FILE_SIZE_MB << F(" MB\n");
  96. cout << F("Buffer size ") << BUF_SIZE << F(" bytes\n");
  97. cout << F("Starting write test, please wait.") << endl << endl;
  98. // do write test
  99. uint32_t n = FILE_SIZE/sizeof(buf);
  100. cout <<F("write speed and latency") << endl;
  101. cout << F("speed,max,min,avg") << endl;
  102. cout << F("KB/Sec,usec,usec,usec") << endl;
  103. for (uint8_t nTest = 0; nTest < WRITE_COUNT; nTest++) {
  104. file.truncate(0);
  105. maxLatency = 0;
  106. minLatency = 9999999;
  107. totalLatency = 0;
  108. t = millis();
  109. for (uint32_t i = 0; i < n; i++) {
  110. uint32_t m = micros();
  111. if (file.write(buf, sizeof(buf)) != sizeof(buf)) {
  112. error("write failed");
  113. }
  114. m = micros() - m;
  115. if (maxLatency < m) {
  116. maxLatency = m;
  117. }
  118. if (minLatency > m) {
  119. minLatency = m;
  120. }
  121. totalLatency += m;
  122. }
  123. file.sync();
  124. t = millis() - t;
  125. s = file.fileSize();
  126. cout << s/t <<',' << maxLatency << ',' << minLatency;
  127. cout << ',' << totalLatency/n << endl;
  128. }
  129. cout << endl << F("Starting read test, please wait.") << endl;
  130. cout << endl <<F("read speed and latency") << endl;
  131. cout << F("speed,max,min,avg") << endl;
  132. cout << F("KB/Sec,usec,usec,usec") << endl;
  133. // do read test
  134. for (uint8_t nTest = 0; nTest < READ_COUNT; nTest++) {
  135. file.rewind();
  136. maxLatency = 0;
  137. minLatency = 9999999;
  138. totalLatency = 0;
  139. t = millis();
  140. for (uint32_t i = 0; i < n; i++) {
  141. buf[BUF_SIZE-1] = 0;
  142. uint32_t m = micros();
  143. if (file.read(buf, sizeof(buf)) != sizeof(buf)) {
  144. error("read failed");
  145. }
  146. m = micros() - m;
  147. if (maxLatency < m) {
  148. maxLatency = m;
  149. }
  150. if (minLatency > m) {
  151. minLatency = m;
  152. }
  153. totalLatency += m;
  154. if (buf[BUF_SIZE-1] != '\n') {
  155. error("data check");
  156. }
  157. }
  158. t = millis() - t;
  159. cout << s/t <<',' << maxLatency << ',' << minLatency;
  160. cout << ',' << totalLatency/n << endl;
  161. }
  162. cout << endl << F("Done") << endl;
  163. file.close();
  164. }