Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TestMkdir.ino 3.5KB

5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * This sketch is a test of subdirectory and file creation.
  3. * It also tests allocation of clusters to directories.
  4. *
  5. * It will create two subdirectories and create enough files
  6. * to force the allocation of a cluster to each directory.
  7. *
  8. * More than 3000 files may be created on a FAT32 volume.
  9. *
  10. * Note: Some cards may 'stutter' others just get slow due
  11. * to the number of flash erases this program causes.
  12. */
  13. #include <SdFat.h>
  14. const uint8_t SD_CHIP_SELECT = SS;
  15. SdFat sd;
  16. typedef File file_t;
  17. // store error strings in flash to save RAM
  18. #define error(s) sd.errorHalt(&Serial, F(s))
  19. /*
  20. * create enough files to force a cluster to be allocated to dir.
  21. */
  22. void dirAllocTest(file_t* dir) {
  23. char buf[32], name[32];
  24. file_t file;
  25. uint16_t n;
  26. uint32_t size = dir->dirSize();
  27. // create files and write name to file
  28. for (n = 0; ; n++){
  29. // make file name
  30. sprintf(name, "%u.TXT", n);
  31. // open start time
  32. uint32_t t0 = millis();
  33. if (!file.open(dir, name, O_WRONLY | O_CREAT | O_EXCL)) {
  34. error("open for write failed");
  35. }
  36. // open end time and write start time
  37. uint32_t t1 = millis();
  38. // write file name to file
  39. file.print(name);
  40. if (!file.close()) error("close write");
  41. // write end time
  42. uint32_t t2 = millis();
  43. Serial.print(F("WR "));
  44. Serial.print(n);
  45. Serial.write(' ');
  46. // print time to create file
  47. Serial.print(t1 - t0);
  48. Serial.write(' ');
  49. // print time to write file
  50. Serial.println(t2 - t1);
  51. // directory size will change when a cluster is added
  52. if (dir->curPosition() > size) break;
  53. }
  54. // read files and check content
  55. for (uint16_t i = 0; i <= n; i++) {
  56. sprintf(name, "%u.TXT", i);
  57. // open start time
  58. uint32_t t0 = millis();
  59. if (!file.open(dir, name, O_RDONLY)) {
  60. error("open for read failed");
  61. }
  62. // open end time and read start time
  63. uint32_t t1 = millis();
  64. int16_t nr = file.read(buf, sizeof(buf));
  65. if (nr < 5) error("file.read failed");
  66. // read end time
  67. uint32_t t2 = millis();
  68. // check file content
  69. if (strlen(name) != (size_t)nr || strncmp(name, buf, nr)) {
  70. error("content compare failed");
  71. }
  72. if (!file.close()) error("close read failed");
  73. Serial.print(F("RD "));
  74. Serial.print(i);
  75. Serial.write(' ');
  76. // print open time
  77. Serial.print(t1 - t0);
  78. Serial.write(' ');
  79. // print read time
  80. Serial.println(t2 - t1);
  81. }
  82. }
  83. void setup() {
  84. file_t root;
  85. Serial.begin(9600);
  86. while (!Serial) {} // wait for Leonardo
  87. Serial.println(F("Type any character to start"));
  88. while (Serial.read() <= 0) {}
  89. delay(200); // Catch Due reset problem
  90. // initialize the SD card at SPI_FULL_SPEED for best performance.
  91. // try SPI_HALF_SPEED if bus errors occur.
  92. if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) {
  93. sd.initErrorHalt(&Serial);
  94. }
  95. root.openRoot(&sd);
  96. uint32_t m = millis();
  97. // write files to root if not FAT16
  98. if (sd.fatType() != 16) {
  99. Serial.println(F("Writing files to root"));
  100. dirAllocTest(&root);
  101. }
  102. // create sub1 and write files
  103. file_t sub1;
  104. if (!sub1.mkdir(&root, "SUB1")) error("makdeDir SUB1 failed");
  105. Serial.println(F("Writing files to SUB1"));
  106. dirAllocTest(&sub1);
  107. // create sub2 and write files
  108. file_t sub2;
  109. if (!sub2.mkdir(&sub1, "SUB2")) error("mkdir SUB2 failed");
  110. Serial.println(F("Writing files to SUB2"));
  111. dirAllocTest(&sub2);
  112. m = millis() - m;
  113. Serial.print(F("Done millis: "));
  114. Serial.println(m);
  115. }
  116. void loop() { }