No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

173 líneas
5.1KB

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