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.

202 line
5.5KB

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