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.

136 lines
3.3KB

  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. #include <SdFatUtil.h>
  15. const uint8_t SD_CHIP_SELECT = SS;
  16. SdFat sd;
  17. // store error strings in flash to save RAM
  18. #define error(s) sd.errorHalt_P(PSTR(s))
  19. /*
  20. * create enough files to force a cluster to be allocated to dir.
  21. */
  22. void dirAllocTest(SdBaseFile* dir) {
  23. char buf[13], name[13];
  24. SdFile file;
  25. uint16_t n;
  26. uint32_t size = dir->fileSize();
  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_WRITE | 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. PgmPrint("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->fileSize() != 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_READ)) {
  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, 13);
  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) != nr || strncmp(name, buf, nr)) {
  70. error("content compare failed");
  71. }
  72. if (!file.close()) error("close read failed");
  73. PgmPrint("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. Serial.begin(9600);
  85. while (!Serial) {} // wait for Leonardo
  86. PgmPrintln("Type any character to start");
  87. while (Serial.read() <= 0) {}
  88. delay(200); // Catch Due reset problem
  89. // initialize the SD card at SPI_FULL_SPEED for best performance.
  90. // try SPI_HALF_SPEED if bus errors occur.
  91. if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) sd.initErrorHalt();
  92. // write files to root if FAT32
  93. if (sd.vol()->fatType() == 32) {
  94. PgmPrintln("Writing files to root");
  95. dirAllocTest(sd.vwd());
  96. }
  97. // create sub1 and write files
  98. SdFile sub1;
  99. if (!sub1.makeDir(sd.vwd(), "SUB1")) error("makdeDir SUB1 failed");
  100. PgmPrintln("Writing files to SUB1");
  101. dirAllocTest(&sub1);
  102. // create sub2 and write files
  103. SdFile sub2;
  104. if (!sub2.makeDir(&sub1, "SUB2")) error("makeDir SUB2 failed");
  105. PgmPrintln("Writing files to SUB2");
  106. dirAllocTest(&sub2);
  107. PgmPrintln("Done");
  108. }
  109. void loop() { }