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.

206 line
5.6KB

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