PrintBenchmark.ino 3.5 KB

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