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.

149 lines
3.4KB

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