123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #include <SPI.h>
- #include <SdFat.h>
- #include <SdFatUtil.h>
- const uint8_t SD_CHIP_SELECT = SS;
- SdFat sd;
- #define error(s) sd.errorHalt(F(s))
- void dirAllocTest(FatFile* dir) {
- char buf[32], name[32];
- SdFile file;
- uint16_t n;
- uint32_t size = dir->dirSize();
-
-
- for (n = 0; ; n++){
-
- sprintf(name, "%u.TXT", n);
-
-
- uint32_t t0 = millis();
- if (!file.open(dir, name, O_WRITE | O_CREAT | O_EXCL)) {
- error("open for write failed");
- }
-
-
- uint32_t t1 = millis();
-
- file.print(name);
- if (!file.close()) error("close write");
-
-
- uint32_t t2 = millis();
- Serial.print(F("WR "));
- Serial.print(n);
- Serial.write(' ');
-
-
- Serial.print(t1 - t0);
- Serial.write(' ');
-
-
- Serial.println(t2 - t1);
-
-
- if (dir->curPosition() > size) break;
- }
-
- for (uint16_t i = 0; i <= n; i++) {
- sprintf(name, "%u.TXT", i);
-
-
- uint32_t t0 = millis();
- if (!file.open(dir, name, O_READ)) {
- error("open for read failed");
- }
-
-
- uint32_t t1 = millis();
- int16_t nr = file.read(buf, sizeof(buf));
- if (nr < 5) error("file.read failed");
-
-
- uint32_t t2 = millis();
-
-
- if (strlen(name) != (size_t)nr || strncmp(name, buf, nr)) {
- error("content compare failed");
- }
- if (!file.close()) error("close read failed");
-
- Serial.print(F("RD "));
- Serial.print(i);
- Serial.write(' ');
-
-
- Serial.print(t1 - t0);
- Serial.write(' ');
-
-
- Serial.println(t2 - t1);
- }
- }
- void setup() {
- Serial.begin(9600);
- while (!Serial) {}
- Serial.println(F("Type any character to start"));
- while (Serial.read() <= 0) {}
- delay(200);
-
-
-
- if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) sd.initErrorHalt();
-
- uint32_t m = millis();
-
- if (sd.vol()->fatType() == 32) {
- Serial.println(F("Writing files to root"));
- dirAllocTest(sd.vwd());
- }
-
-
- SdFile sub1;
- if (!sub1.mkdir(sd.vwd(), "SUB1")) error("makdeDir SUB1 failed");
- Serial.println(F("Writing files to SUB1"));
- dirAllocTest(&sub1);
-
- SdFile sub2;
- if (!sub2.mkdir(&sub1, "SUB2")) error("mkdir SUB2 failed");
- Serial.println(F("Writing files to SUB2"));
- dirAllocTest(&sub2);
- m = millis() - m;
- Serial.print(F("Done millis: "));
- Serial.println(m);
- }
- void loop() { }
|