Advertisement
hwthinker

Sdcard_test_V2.1-1.6

Feb 14th, 2019
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.    Connect the SD card to the following pins:
  3.  
  4.    SD Card | ESP32
  5.       D2       -
  6.       D3       SS
  7.       CMD      MOSI
  8.       VSS      GND
  9.       VDD      3.3V
  10.       CLK      SCK
  11.       VSS      GND
  12.       D0       MISO
  13.       D1       -
  14. */
  15. #include "FS.h"
  16. #include "SD.h"
  17. #include "SPI.h"
  18. enum { sd_sck = 14, sd_miso = 2, sd_mosi = 15, sd_ss = 13 };
  19.  
  20. void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
  21.   Serial.printf("Listing directory: %s\n", dirname);
  22.  
  23.   File root = fs.open(dirname);
  24.   if (!root) {
  25.     Serial.println("Failed to open directory");
  26.     return;
  27.   }
  28.   if (!root.isDirectory()) {
  29.     Serial.println("Not a directory");
  30.     return;
  31.   }
  32.  
  33.   File file = root.openNextFile();
  34.   while (file) {
  35.     if (file.isDirectory()) {
  36.       Serial.print("  DIR : ");
  37.       Serial.println(file.name());
  38.       if (levels) {
  39.         listDir(fs, file.name(), levels - 1);
  40.       }
  41.     } else {
  42.       Serial.print("  FILE: ");
  43.       Serial.print(file.name());
  44.       Serial.print("  SIZE: ");
  45.       Serial.println(file.size());
  46.     }
  47.     file = root.openNextFile();
  48.   }
  49. }
  50.  
  51. void createDir(fs::FS &fs, const char * path) {
  52.   Serial.printf("Creating Dir: %s\n", path);
  53.   if (fs.mkdir(path)) {
  54.     Serial.println("Dir created");
  55.   } else {
  56.     Serial.println("mkdir failed");
  57.   }
  58. }
  59.  
  60. void removeDir(fs::FS &fs, const char * path) {
  61.   Serial.printf("Removing Dir: %s\n", path);
  62.   if (fs.rmdir(path)) {
  63.     Serial.println("Dir removed");
  64.   } else {
  65.     Serial.println("rmdir failed");
  66.   }
  67. }
  68.  
  69. void readFile(fs::FS &fs, const char * path) {
  70.   Serial.printf("Reading file: %s\n", path);
  71.  
  72.   File file = fs.open(path);
  73.   if (!file) {
  74.     Serial.println("Failed to open file for reading");
  75.     return;
  76.   }
  77.  
  78.   Serial.print("Read from file: ");
  79.   while (file.available()) {
  80.     Serial.write(file.read());
  81.   }
  82.   file.close();
  83. }
  84.  
  85. void writeFile(fs::FS &fs, const char * path, const char * message) {
  86.   Serial.printf("Writing file: %s\n", path);
  87.  
  88.   File file = fs.open(path, FILE_WRITE);
  89.   if (!file) {
  90.     Serial.println("Failed to open file for writing");
  91.     return;
  92.   }
  93.   if (file.print(message)) {
  94.     Serial.println("File written");
  95.   } else {
  96.     Serial.println("Write failed");
  97.   }
  98.   file.close();
  99. }
  100.  
  101. void appendFile(fs::FS &fs, const char * path, const char * message) {
  102.   Serial.printf("Appending to file: %s\n", path);
  103.  
  104.   File file = fs.open(path, FILE_APPEND);
  105.   if (!file) {
  106.     Serial.println("Failed to open file for appending");
  107.     return;
  108.   }
  109.   if (file.print(message)) {
  110.     Serial.println("Message appended");
  111.   } else {
  112.     Serial.println("Append failed");
  113.   }
  114.   file.close();
  115. }
  116.  
  117. void renameFile(fs::FS &fs, const char * path1, const char * path2) {
  118.   Serial.printf("Renaming file %s to %s\n", path1, path2);
  119.   if (fs.rename(path1, path2)) {
  120.     Serial.println("File renamed");
  121.   } else {
  122.     Serial.println("Rename failed");
  123.   }
  124. }
  125.  
  126. void deleteFile(fs::FS &fs, const char * path) {
  127.   Serial.printf("Deleting file: %s\n", path);
  128.   if (fs.remove(path)) {
  129.     Serial.println("File deleted");
  130.   } else {
  131.     Serial.println("Delete failed");
  132.   }
  133. }
  134.  
  135. void testFileIO(fs::FS &fs, const char * path) {
  136.   File file = fs.open(path);
  137.   static uint8_t buf[512];
  138.   size_t len = 0;
  139.   uint32_t start = millis();
  140.   uint32_t end = start;
  141.   if (file) {
  142.     len = file.size();
  143.     size_t flen = len;
  144.     start = millis();
  145.     while (len) {
  146.       size_t toRead = len;
  147.       if (toRead > 512) {
  148.         toRead = 512;
  149.       }
  150.       file.read(buf, toRead);
  151.       len -= toRead;
  152.     }
  153.     end = millis() - start;
  154.     Serial.printf("%u bytes read for %u ms\n", flen, end);
  155.     file.close();
  156.   } else {
  157.     Serial.println("Failed to open file for reading");
  158.   }
  159.  
  160.  
  161.   file = fs.open(path, FILE_WRITE);
  162.   if (!file) {
  163.     Serial.println("Failed to open file for writing");
  164.     return;
  165.   }
  166.  
  167.   size_t i;
  168.   start = millis();
  169.   for (i = 0; i < 2048; i++) {
  170.     file.write(buf, 512);
  171.   }
  172.   end = millis() - start;
  173.   Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
  174.   file.close();
  175. }
  176.  
  177. void setup() {
  178.   Serial.begin(115200);
  179.   //******
  180.   SPI.end();
  181.   SPI.begin(sd_sck, sd_miso, sd_mosi, sd_ss);
  182.   SD.begin(sd_ss, SPI, 24000000);
  183.   if (!SD.begin(sd_ss, SPI)) {
  184.     Serial.println("Card Mount Failed");
  185.     return;
  186.   }
  187.   //****
  188.  
  189.   uint8_t cardType = SD.cardType();
  190.  
  191.   if (cardType == CARD_NONE) {
  192.     Serial.println("No SD card attached");
  193.     return;
  194.   }
  195.  
  196.   Serial.print("SD Card Type: ");
  197.   if (cardType == CARD_MMC) {
  198.     Serial.println("MMC");
  199.   } else if (cardType == CARD_SD) {
  200.     Serial.println("SDSC");
  201.   } else if (cardType == CARD_SDHC) {
  202.     Serial.println("SDHC");
  203.   } else {
  204.     Serial.println("UNKNOWN");
  205.   }
  206.  
  207.   uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  208.   Serial.printf("SD Card Size: %lluMB\n", cardSize);
  209.  
  210.   listDir(SD, "/", 0);
  211.   createDir(SD, "/mydir");
  212.   listDir(SD, "/", 0);
  213.   removeDir(SD, "/mydir");
  214.   listDir(SD, "/", 2);
  215.   writeFile(SD, "/hello.txt", "Hello ");
  216.   appendFile(SD, "/hello.txt", "World!\n");
  217.   readFile(SD, "/hello.txt");
  218.   deleteFile(SD, "/foo.txt");
  219.   renameFile(SD, "/hello.txt", "/foo.txt");
  220.   readFile(SD, "/foo.txt");
  221.   testFileIO(SD, "/test.txt");
  222.   Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
  223.   Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
  224. }
  225.  
  226. void loop() {
  227.  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement