Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

198 lines
5.3KB

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