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.

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <SPI.h>
  14. #include <SdFat.h>
  15. #include <SdFatUtil.h>
  16. const uint8_t SD_CHIP_SELECT = SS;
  17. SdFat sd;
  18. // store error strings in flash to save RAM
  19. #define error(s) sd.errorHalt(F(s))
  20. /*
  21. * create enough files to force a cluster to be allocated to dir.
  22. */
  23. void dirAllocTest(FatFile* dir) {
  24. char buf[32], name[32];
  25. SdFile file;
  26. uint16_t n;
  27. uint32_t size = dir->dirSize();
  28. // create files and write name to file
  29. for (n = 0; ; n++){
  30. // make file name
  31. sprintf(name, "%u.TXT", n);
  32. // open start time
  33. uint32_t t0 = millis();
  34. if (!file.open(dir, name, O_WRITE | O_CREAT | O_EXCL)) {
  35. error("open for write failed");
  36. }
  37. // open end time and write start time
  38. uint32_t t1 = millis();
  39. // write file name to file
  40. file.print(name);
  41. if (!file.close()) error("close write");
  42. // write end time
  43. uint32_t t2 = millis();
  44. Serial.print(F("WR "));
  45. Serial.print(n);
  46. Serial.write(' ');
  47. // print time to create file
  48. Serial.print(t1 - t0);
  49. Serial.write(' ');
  50. // print time to write file
  51. Serial.println(t2 - t1);
  52. // directory size will change when a cluster is added
  53. if (dir->curPosition() > size) break;
  54. }
  55. // read files and check content
  56. for (uint16_t i = 0; i <= n; i++) {
  57. sprintf(name, "%u.TXT", i);
  58. // open start time
  59. uint32_t t0 = millis();
  60. if (!file.open(dir, name, O_READ)) {
  61. error("open for read failed");
  62. }
  63. // open end time and read start time
  64. uint32_t t1 = millis();
  65. int16_t nr = file.read(buf, sizeof(buf));
  66. if (nr < 5) error("file.read failed");
  67. // read end time
  68. uint32_t t2 = millis();
  69. // check file content
  70. if (strlen(name) != (size_t)nr || strncmp(name, buf, nr)) {
  71. error("content compare failed");
  72. }
  73. if (!file.close()) error("close read failed");
  74. Serial.print(F("RD "));
  75. Serial.print(i);
  76. Serial.write(' ');
  77. // print open time
  78. Serial.print(t1 - t0);
  79. Serial.write(' ');
  80. // print read time
  81. Serial.println(t2 - t1);
  82. }
  83. }
  84. void setup() {
  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)) sd.initErrorHalt();
  93. uint32_t m = millis();
  94. // write files to root if FAT32
  95. if (sd.vol()->fatType() == 32) {
  96. Serial.println(F("Writing files to root"));
  97. dirAllocTest(sd.vwd());
  98. }
  99. // create sub1 and write files
  100. SdFile sub1;
  101. if (!sub1.mkdir(sd.vwd(), "SUB1")) error("makdeDir SUB1 failed");
  102. Serial.println(F("Writing files to SUB1"));
  103. dirAllocTest(&sub1);
  104. // create sub2 and write files
  105. SdFile sub2;
  106. if (!sub2.mkdir(&sub1, "SUB2")) error("mkdir SUB2 failed");
  107. Serial.println(F("Writing files to SUB2"));
  108. dirAllocTest(&sub2);
  109. m = millis() - m;
  110. Serial.print(F("Done millis: "));
  111. Serial.println(m);
  112. }
  113. void loop() { }