You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 line
3.4KB

  1. /*
  2. * This program 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 << F("Type any character to start\n");
  36. while (Serial.read() <= 0) {
  37. }
  38. delay(400); // catch Due reset problem
  39. cout << F("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)) {
  43. sd.initErrorHalt();
  44. }
  45. cout << F("Type is FAT") << int(sd.vol()->fatType()) << endl;
  46. cout << F("Starting print test. Please wait.\n\n");
  47. // do write test
  48. for (int test = 0; test < 6; test++) {
  49. char fileName[13] = "bench0.txt";
  50. fileName[5] = '0' + test;
  51. // open or create file - truncate existing file.
  52. if (!file.open(fileName, O_CREAT | O_TRUNC | O_RDWR)) {
  53. error("open failed");
  54. }
  55. maxLatency = 0;
  56. minLatency = 999999;
  57. totalLatency = 0;
  58. switch(test) {
  59. case 0:
  60. cout << F("Test of println(uint16_t)\n");
  61. break;
  62. case 1:
  63. cout << F("Test of printField(uint16_t, char)\n");
  64. break;
  65. case 2:
  66. cout << F("Test of println(uint32_t)\n");
  67. break;
  68. case 3:
  69. cout << F("Test of printField(uint32_t, char)\n");
  70. break;
  71. case 4:
  72. cout << F("Test of println(float)\n");
  73. break;
  74. case 5:
  75. cout << F("Test of printField(float, char)\n");
  76. break;
  77. }
  78. uint32_t t = millis();
  79. for (uint16_t i = 0; i < N_PRINT; i++) {
  80. uint32_t m = micros();
  81. switch(test) {
  82. case 0:
  83. file.println(i);
  84. break;
  85. case 1:
  86. file.printField(i, '\n');
  87. break;
  88. case 2:
  89. file.println(12345678UL + i);
  90. break;
  91. case 3:
  92. file.printField(12345678UL + i, '\n');
  93. break;
  94. case 4:
  95. file.println((float)0.01*i);
  96. break;
  97. case 5:
  98. file.printField((float)0.01*i, '\n');
  99. break;
  100. }
  101. if (file.getWriteError()) {
  102. error("write failed");
  103. }
  104. m = micros() - m;
  105. if (maxLatency < m) {
  106. maxLatency = m;
  107. }
  108. if (minLatency > m) {
  109. minLatency = m;
  110. }
  111. totalLatency += m;
  112. }
  113. file.close();
  114. t = millis() - t;
  115. double s = file.fileSize();
  116. cout << F("Time ") << 0.001*t << F(" sec\n");
  117. cout << F("File size ") << 0.001*s << F(" KB\n");
  118. cout << F("Write ") << s/t << F(" KB/sec\n");
  119. cout << F("Maximum latency: ") << maxLatency;
  120. cout << F(" usec, Minimum Latency: ") << minLatency;
  121. cout << F(" usec, Avg Latency: ");
  122. cout << totalLatency/N_PRINT << F(" usec\n\n");
  123. }
  124. cout << F("Done!\n\n");
  125. }