您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

149 行
3.5KB

  1. /*
  2. * This program is a simple binary write/read benchmark
  3. * for the standard Arduino SD.h library.
  4. */
  5. #include <SPI.h>
  6. #include <SD.h>
  7. // SD chip select pin
  8. const uint8_t chipSelect = SS;
  9. #define FILE_SIZE_MB 5
  10. #define FILE_SIZE (1000000UL*FILE_SIZE_MB)
  11. #define BUF_SIZE 100
  12. uint8_t buf[BUF_SIZE];
  13. // test file
  14. File file;
  15. //------------------------------------------------------------------------------
  16. void error(const char* s) {
  17. Serial.println(s);
  18. while (1) {
  19. yield();
  20. }
  21. }
  22. //------------------------------------------------------------------------------
  23. void setup() {
  24. Serial.begin(9600);
  25. // Wait for USB Serial
  26. while (!Serial) {
  27. yield();
  28. }
  29. }
  30. //------------------------------------------------------------------------------
  31. void loop() {
  32. uint32_t maxLatency;
  33. uint32_t minLatency;
  34. uint32_t totalLatency;
  35. // Discard any input.
  36. do {
  37. delay(10);
  38. } while (Serial.available() && Serial.read() >= 0);
  39. // F() stores strings in flash to save RAM
  40. Serial.println(F("Type any character to start"));
  41. while (!Serial.available()) {
  42. yield();
  43. }
  44. if (!SD.begin(chipSelect)) {
  45. error("begin");
  46. }
  47. // open or create file - truncate existing file.
  48. file = SD.open("Bench.dat", O_RDWR | O_TRUNC | O_CREAT);
  49. if (!file) {
  50. error("open failed");
  51. }
  52. // fill buf with known data
  53. for (size_t_t i = 0; i < (BUF_SIZE-2); i++) {
  54. buf[i] = 'A' + (i % 26);
  55. }
  56. buf[BUF_SIZE-2] = '\r';
  57. buf[BUF_SIZE-1] = '\n';
  58. Serial.print(F("File size "));
  59. Serial.print(FILE_SIZE_MB);
  60. Serial.println(F("MB"));
  61. Serial.print(F("Buffer size "));
  62. Serial.print(BUF_SIZE);
  63. Serial.println(F(" bytes"));
  64. Serial.println(F("Starting write test. Please wait up to a minute"));
  65. // do write test
  66. uint32_t n = FILE_SIZE/sizeof(buf);
  67. maxLatency = 0;
  68. minLatency = 999999;
  69. totalLatency = 0;
  70. uint32_t t = millis();
  71. for (uint32_t i = 0; i < n; i++) {
  72. uint32_t m = micros();
  73. if (file.write(buf, sizeof(buf)) != sizeof(buf)) {
  74. error("write failed");
  75. }
  76. m = micros() - m;
  77. if (maxLatency < m) {
  78. maxLatency = m;
  79. }
  80. if (minLatency > m) {
  81. minLatency = m;
  82. }
  83. totalLatency += m;
  84. }
  85. file.flush();
  86. t = millis() - t;
  87. double s = file.size();
  88. Serial.print(F("Write "));
  89. Serial.print(s/t);
  90. Serial.print(F(" KB/sec\n"));
  91. Serial.print(F("Maximum latency: "));
  92. Serial.print(maxLatency);
  93. Serial.print(F(" usec, Minimum Latency: "));
  94. Serial.print(minLatency);
  95. Serial.print(F(" usec, Avg Latency: "));
  96. Serial.print(totalLatency/n);
  97. Serial.print(F(" usec\n\n"));
  98. Serial.println(F("Starting read test. Please wait up to a minute"));
  99. // do read test
  100. file.seek(0);
  101. maxLatency = 0;
  102. minLatency = 99999;
  103. totalLatency = 0;
  104. t = millis();
  105. for (uint32_t i = 0; i < n; i++) {
  106. buf[BUF_SIZE-1] = 0;
  107. uint32_t m = micros();
  108. if (file.read(buf, sizeof(buf)) != sizeof(buf)) {
  109. error("read failed");
  110. }
  111. m = micros() - m;
  112. if (maxLatency < m) {
  113. maxLatency = m;
  114. }
  115. if (minLatency > m) {
  116. minLatency = m;
  117. }
  118. totalLatency += m;
  119. if (buf[BUF_SIZE-1] != '\n') {
  120. error("data check");
  121. }
  122. }
  123. t = millis() - t;
  124. Serial.print(F("Read "));
  125. Serial.print(s/t);
  126. Serial.print(F(" KB/sec\n"));
  127. Serial.print(F("Maximum latency: "));
  128. Serial.print(maxLatency);
  129. Serial.print(F(" usec, Minimum Latency: "));
  130. Serial.print(minLatency);
  131. Serial.print(F(" usec, Avg Latency: "));
  132. Serial.print(totalLatency/n);
  133. Serial.print(F(" usec\n\n"));
  134. Serial.print(F("Done\n\n"));
  135. file.close();
  136. }