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.

120 lines
2.9KB

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