Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

141 rinda
3.5KB

  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 < 6; 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(float)\n");
  70. break;
  71. case 5:
  72. cout << pstr("Test of printField(float, char)\n");
  73. break;
  74. }
  75. uint32_t t = millis();
  76. for (uint16_t i = 0; i < N_PRINT; i++) {
  77. uint32_t m = micros();
  78. switch(test) {
  79. case 0:
  80. file.println(i);
  81. break;
  82. case 1:
  83. file.printField(i, '\n');
  84. break;
  85. case 2:
  86. file.println(12345678UL + i);
  87. break;
  88. case 3:
  89. file.printField(12345678UL + i, '\n');
  90. break;
  91. case 4:
  92. file.println((float)0.01*i);
  93. break;
  94. case 5:
  95. file.printField((float)0.01*i, '\n');
  96. break;
  97. }
  98. if (file.writeError) {
  99. error("write failed");
  100. }
  101. m = micros() - m;
  102. if (maxLatency < m) maxLatency = m;
  103. if (minLatency > m) minLatency = m;
  104. totalLatency += m;
  105. }
  106. file.close();
  107. t = millis() - t;
  108. double s = file.fileSize();
  109. cout << pstr("Time ") << 0.001*t << pstr(" sec\n");
  110. cout << pstr("File size ") << 0.001*s << pstr(" KB\n");
  111. cout << pstr("Write ") << s/t << pstr(" KB/sec\n");
  112. cout << pstr("Maximum latency: ") << maxLatency;
  113. cout << pstr(" usec, Minimum Latency: ") << minLatency;
  114. cout << pstr(" usec, Avg Latency: ");
  115. cout << totalLatency/N_PRINT << pstr(" usec\n\n");
  116. }
  117. cout << pstr("Done!\n\n");
  118. }