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.

152 line
3.5KB

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