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.

136 lines
3.3KB

  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. cout << pstr("Starting print test. Please wait.\n\n");
  44. // do write test
  45. for (int test = 0; test < 5; test++) {
  46. char fileName[13] = "BENCH0.TXT";
  47. fileName[5] = '0' + test;
  48. // open or create file - truncate existing file.
  49. if (!file.open(fileName, O_CREAT | O_TRUNC | O_RDWR)) {
  50. error("open failed");
  51. }
  52. maxLatency = 0;
  53. minLatency = 999999;
  54. totalLatency = 0;
  55. switch(test) {
  56. case 0:
  57. cout << pstr("Test of println(uint16_t)\n");
  58. break;
  59. case 1:
  60. cout << pstr("Test of printField(uint16_t, char)\n");
  61. break;
  62. case 2:
  63. cout << pstr("Test of println(uint32_t)\n");
  64. break;
  65. case 3:
  66. cout << pstr("Test of printField(uint32_t, char)\n");
  67. break;
  68. case 4:
  69. cout << pstr("Test of println(double)\n");
  70. break;
  71. }
  72. uint32_t t = millis();
  73. for (uint16_t i = 0; i < N_PRINT; i++) {
  74. uint32_t m = micros();
  75. switch(test) {
  76. case 0:
  77. file.println(i);
  78. break;
  79. case 1:
  80. file.printField(i, '\n');
  81. break;
  82. case 2:
  83. file.println(12345678UL + i);
  84. break;
  85. case 3:
  86. file.printField(12345678UL + i, '\n');
  87. break;
  88. case 4:
  89. file.println((double)0.01*i);
  90. break;
  91. }
  92. if (file.writeError) {
  93. error("write failed");
  94. }
  95. m = micros() - m;
  96. if (maxLatency < m) maxLatency = m;
  97. if (minLatency > m) minLatency = m;
  98. totalLatency += m;
  99. }
  100. file.close();
  101. t = millis() - t;
  102. double s = file.fileSize();
  103. cout << pstr("Time ") << 0.001*t << pstr(" sec\n");
  104. cout << pstr("File size ") << 0.001*s << pstr(" KB\n");
  105. cout << pstr("Write ") << s/t << pstr(" KB/sec\n");
  106. cout << pstr("Maximum latency: ") << maxLatency;
  107. cout << pstr(" usec, Minimum Latency: ") << minLatency;
  108. cout << pstr(" usec, Avg Latency: ");
  109. cout << totalLatency/N_PRINT << pstr(" usec\n\n");
  110. }
  111. cout << pstr("Done!\n\n");
  112. }