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.

201 line
5.4KB

  1. #include <SdFat.h>
  2. #include <StdioStream.h>
  3. // Define PRINT_FIELD nonzero to use printField.
  4. #define PRINT_FIELD 0
  5. // Number of lines to list on Serial.
  6. #define STDIO_LIST_COUNT 0
  7. #define VERIFY_CONTENT 0
  8. const uint8_t SD_CS_PIN = SS;
  9. SdFat sd;
  10. SdFile printFile;
  11. StdioStream stdioFile;
  12. float f[100];
  13. char buf[20];
  14. char* label[] =
  15. {"uint8_t 0 to 255, 100 times ", "uint16_t 0 to 20000",
  16. "uint32_t 0 to 20000", "uint32_t 1000000000 to 1000010000",
  17. "float nnn.ffff, 10000 times"};
  18. //------------------------------------------------------------------------------
  19. void setup() {
  20. uint32_t m;
  21. uint32_t printSize;
  22. uint32_t stdioSize;
  23. uint32_t printTime;
  24. uint32_t stdioTime;
  25. Serial.begin(9600);
  26. Serial.println(F("Type any character to start"));
  27. while (!Serial.available());
  28. Serial.println(F("Starting test"));
  29. if (!sd.begin(SD_CS_PIN)) sd.errorHalt();
  30. for (uint8_t i = 0; i < 100; i++) {
  31. f[i] = 123.0 + 0.12345*i;
  32. }
  33. for (uint8_t dataType = 0; dataType < 5; dataType++) {
  34. for (uint8_t fileType = 0; fileType < 2; fileType++) {
  35. if (!fileType) {
  36. if (!printFile.open("PRRINT.TXT", O_CREAT | O_RDWR | O_TRUNC)) {
  37. Serial.println("open fail");
  38. return;
  39. }
  40. printTime = millis();
  41. switch (dataType) {
  42. case 0:
  43. for (uint16_t i =0; i < 100; i++) {
  44. for (uint8_t j = 0; j < 255; j++) {
  45. printFile.println(j);
  46. }
  47. }
  48. break;
  49. case 1:
  50. for (uint16_t i = 0; i < 20000; i++) {
  51. printFile.println(i);
  52. }
  53. break;
  54. case 2:
  55. for (uint32_t i = 0; i < 20000; i++) {
  56. printFile.println(i);
  57. }
  58. break;
  59. case 3:
  60. for (uint16_t i = 0; i < 10000; i++) {
  61. printFile.println(i + 1000000000UL);
  62. }
  63. break;
  64. case 4:
  65. for (int j = 0; j < 100; j++) {
  66. for (uint8_t i = 0; i < 100; i++) {
  67. printFile.println(f[i], 4);
  68. }
  69. }
  70. break;
  71. default:
  72. break;
  73. }
  74. printFile.sync();
  75. printTime = millis() - printTime;
  76. printFile.rewind();
  77. printSize = printFile.fileSize();
  78. } else {
  79. if (!stdioFile.fopen("STREAM.TXT", "w+")) {
  80. Serial.println("fopen fail");
  81. return;
  82. }
  83. stdioTime = millis();
  84. switch (dataType) {
  85. case 0:
  86. for (uint16_t i =0; i < 100; i++) {
  87. for (uint8_t j = 0; j < 255; j++) {
  88. #if PRINT_FIELD
  89. stdioFile.printField(j, '\n');
  90. #else // PRINT_FIELD
  91. stdioFile.println(j);
  92. #endif // PRINT_FIELD
  93. }
  94. }
  95. break;
  96. case 1:
  97. for (uint16_t i = 0; i < 20000; i++) {
  98. #if PRINT_FIELD
  99. stdioFile.printField(i, '\n');
  100. #else // PRINT_FIELD
  101. stdioFile.println(i);
  102. #endif // PRINT_FIELD
  103. }
  104. break;
  105. case 2:
  106. for (uint32_t i = 0; i < 20000; i++) {
  107. #if PRINT_FIELD
  108. stdioFile.printField(i, '\n');
  109. #else // PRINT_FIELD
  110. stdioFile.println(i);
  111. #endif // PRINT_FIELD
  112. }
  113. break;
  114. case 3:
  115. for (uint16_t i = 0; i < 10000; i++) {
  116. #if PRINT_FIELD
  117. stdioFile.printField(i + 1000000000UL, '\n');
  118. #else // PRINT_FIELD
  119. stdioFile.println(i + 1000000000UL);
  120. #endif // PRINT_FIELD
  121. }
  122. break;
  123. case 4:
  124. for (int j = 0; j < 100; j++) {
  125. for (uint8_t i = 0; i < 100; i++) {
  126. #if PRINT_FIELD
  127. stdioFile.printField(f[i], '\n', 4);
  128. #else // PRINT_FIELD
  129. stdioFile.println(f[i], 4);
  130. #endif // PRINT_FIELD
  131. }
  132. }
  133. break;
  134. default:
  135. break;
  136. }
  137. stdioFile.fflush();
  138. stdioTime = millis() - stdioTime;
  139. stdioSize = stdioFile.ftell();
  140. if (STDIO_LIST_COUNT) {
  141. size_t len;
  142. stdioFile.rewind();
  143. for (int i = 0; i < STDIO_LIST_COUNT; i++) {
  144. stdioFile.fgets(buf, sizeof(buf), &len);
  145. Serial.print(len);Serial.print(',');
  146. Serial.print(buf);
  147. }
  148. }
  149. }
  150. }
  151. Serial.println(label[dataType]);
  152. if (VERIFY_CONTENT && printSize == stdioSize) {
  153. printFile.rewind();
  154. stdioFile.rewind();
  155. for (uint32_t i = 0; i < stdioSize; i++) {
  156. if (printFile.read() != stdioFile.getc()) {
  157. Serial.print(F("Files differ at pos: "));
  158. Serial.println(i);
  159. return;
  160. }
  161. }
  162. }
  163. Serial.print("fileSize: ");
  164. if (printSize != stdioSize) {
  165. Serial.print(printSize);
  166. Serial.print(" != ");
  167. }
  168. Serial.println(stdioSize);
  169. Serial.print("print millis: ");
  170. Serial.println(printTime);
  171. Serial.print("stdio millis: ");
  172. Serial.println(stdioTime);
  173. Serial.print("ratio: ");
  174. Serial.println((float)printTime/(float)stdioTime);
  175. Serial.println();
  176. printFile.close();
  177. stdioFile.fclose();
  178. }
  179. Serial.println("Done");
  180. }
  181. void loop() {}