Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

125 lines
2.7KB

  1. /*
  2. * This program is a simple Print benchmark.
  3. */
  4. #include <SPI.h>
  5. #include <SD.h>
  6. // SD chip select pin
  7. const uint8_t chipSelect = SS;
  8. // number of lines to print
  9. const uint16_t N_PRINT = 20000;
  10. // test file
  11. File file;
  12. //------------------------------------------------------------------------------
  13. void error(const char* s) {
  14. Serial.println(s);
  15. while (1) {
  16. yield();
  17. }
  18. }
  19. //------------------------------------------------------------------------------
  20. void setup() {
  21. Serial.begin(9600);
  22. // Wait for USB Serial
  23. while (!Serial) {
  24. yield();
  25. }
  26. }
  27. //------------------------------------------------------------------------------
  28. void loop() {
  29. uint32_t maxLatency;
  30. uint32_t minLatency;
  31. uint32_t totalLatency;
  32. // Read any existing Serial data.
  33. do {
  34. delay(10);
  35. } while (Serial.available() && Serial.read() >= 0);
  36. // F() stores strings in flash to save RAM
  37. Serial.println(F("Type any character to start"));
  38. while (!Serial.available()) {
  39. yield();
  40. }
  41. // initialize the SD card
  42. if (!SD.begin(chipSelect)) {
  43. error("begin");
  44. }
  45. Serial.println(F("Starting print test. Please wait.\n"));
  46. // do write test
  47. for (int test = 0; test < 2; test++) {
  48. file = SD.open("bench.txt", FILE_WRITE);
  49. if (!file) {
  50. error("open failed");
  51. }
  52. switch(test) {
  53. case 0:
  54. Serial.println(F("Test of println(uint16_t)"));
  55. break;
  56. case 1:
  57. Serial.println(F("Test of println(double)"));
  58. break;
  59. }
  60. maxLatency = 0;
  61. minLatency = 999999;
  62. totalLatency = 0;
  63. uint32_t t = millis();
  64. for (uint16_t i = 0; i < N_PRINT; i++) {
  65. uint32_t m = micros();
  66. switch(test) {
  67. case 0:
  68. file.println(i);
  69. break;
  70. case 1:
  71. file.println((double)0.01*i);
  72. break;
  73. }
  74. if (file.getWriteError()) {
  75. error("write failed");
  76. }
  77. m = micros() - m;
  78. if (maxLatency < m) {
  79. maxLatency = m;
  80. }
  81. if (minLatency > m) {
  82. minLatency = m;
  83. }
  84. totalLatency += m;
  85. }
  86. file.flush();
  87. t = millis() - t;
  88. double s = file.size();
  89. Serial.print(F("Time "));
  90. Serial.print(0.001*t);
  91. Serial.println(F(" sec"));
  92. Serial.print(F("File size "));
  93. Serial.print(0.001*s);
  94. Serial.print(F(" KB\n"));
  95. Serial.print(F("Write "));
  96. Serial.print(s/t);
  97. Serial.print(F(" KB/sec\n"));
  98. Serial.print(F("Maximum latency: "));
  99. Serial.print(maxLatency);
  100. Serial.print(F(" usec, Minimum Latency: "));
  101. Serial.print(minLatency);
  102. Serial.print(F(" usec, Avg Latency: "));
  103. Serial.print(totalLatency/N_PRINT);
  104. Serial.println(F(" usec\n"));
  105. SD.remove("bench.txt");
  106. }
  107. file.close();
  108. Serial.println(F("Done!\n"));
  109. }