Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

142 lines
3.5KB

  1. /*
  2. * This sketch is a simple Print benchmark.
  3. */
  4. #include <SPI.h>
  5. #include <SdFat.h>
  6. #include <SdFatUtil.h>
  7. // SD chip select pin
  8. const uint8_t chipSelect = SS;
  9. // number of lines to print
  10. const uint16_t N_PRINT = 20000;
  11. // file system
  12. SdFat sd;
  13. // test file
  14. SdFile file;
  15. // Serial output stream
  16. ArduinoOutStream cout(Serial);
  17. //------------------------------------------------------------------------------
  18. // store error strings in flash to save RAM
  19. #define error(s) sd.errorHalt(F(s))
  20. //------------------------------------------------------------------------------
  21. void setup() {
  22. Serial.begin(9600);
  23. while (!Serial) {
  24. // wait for Leonardo
  25. }
  26. }
  27. //------------------------------------------------------------------------------
  28. void loop() {
  29. uint32_t maxLatency;
  30. uint32_t minLatency;
  31. uint32_t totalLatency;
  32. while (Serial.read() >= 0) {
  33. }
  34. // pstr stores strings in flash to save RAM
  35. cout << pstr("Type any character to start\n");
  36. while (Serial.read() <= 0) {
  37. }
  38. delay(400); // catch Due reset problem
  39. cout << pstr("Free RAM: ") << FreeRam() << endl;
  40. // initialize the SD card at SPI_FULL_SPEED for best performance.
  41. // try SPI_HALF_SPEED if bus errors occur.
  42. if (!sd.begin(chipSelect, SPI_FULL_SPEED)) sd.initErrorHalt();
  43. cout << pstr("Type is FAT") << int(sd.vol()->fatType()) << endl;
  44. cout << pstr("Starting print test. Please wait.\n\n");
  45. // do write test
  46. for (int test = 0; test < 6; test++) {
  47. char fileName[13] = "BENCH0.TXT";
  48. fileName[5] = '0' + test;
  49. // open or create file - truncate existing file.
  50. if (!file.open(fileName, O_CREAT | O_TRUNC | O_RDWR)) {
  51. error("open failed");
  52. }
  53. maxLatency = 0;
  54. minLatency = 999999;
  55. totalLatency = 0;
  56. switch(test) {
  57. case 0:
  58. cout << pstr("Test of println(uint16_t)\n");
  59. break;
  60. case 1:
  61. cout << pstr("Test of printField(uint16_t, char)\n");
  62. break;
  63. case 2:
  64. cout << pstr("Test of println(uint32_t)\n");
  65. break;
  66. case 3:
  67. cout << pstr("Test of printField(uint32_t, char)\n");
  68. break;
  69. case 4:
  70. cout << pstr("Test of println(float)\n");
  71. break;
  72. case 5:
  73. cout << pstr("Test of printField(float, char)\n");
  74. break;
  75. }
  76. uint32_t t = millis();
  77. for (uint16_t i = 0; i < N_PRINT; i++) {
  78. uint32_t m = micros();
  79. switch(test) {
  80. case 0:
  81. file.println(i);
  82. break;
  83. case 1:
  84. file.printField(i, '\n');
  85. break;
  86. case 2:
  87. file.println(12345678UL + i);
  88. break;
  89. case 3:
  90. file.printField(12345678UL + i, '\n');
  91. break;
  92. case 4:
  93. file.println((float)0.01*i);
  94. break;
  95. case 5:
  96. file.printField((float)0.01*i, '\n');
  97. break;
  98. }
  99. if (file.getWriteError()) {
  100. error("write failed");
  101. }
  102. m = micros() - m;
  103. if (maxLatency < m) maxLatency = m;
  104. if (minLatency > m) minLatency = m;
  105. totalLatency += m;
  106. }
  107. file.close();
  108. t = millis() - t;
  109. double s = file.fileSize();
  110. cout << pstr("Time ") << 0.001*t << pstr(" sec\n");
  111. cout << pstr("File size ") << 0.001*s << pstr(" KB\n");
  112. cout << pstr("Write ") << s/t << pstr(" KB/sec\n");
  113. cout << pstr("Maximum latency: ") << maxLatency;
  114. cout << pstr(" usec, Minimum Latency: ") << minLatency;
  115. cout << pstr(" usec, Avg Latency: ");
  116. cout << totalLatency/N_PRINT << pstr(" usec\n\n");
  117. }
  118. cout << pstr("Done!\n\n");
  119. }