V1.ino 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * pin 1 - not used | Micro SD card |
  3. * pin 2 - CS (SS) | /
  4. * pin 3 - DI (MOSI) | |__
  5. * pin 4 - VDD (3.3V) | |
  6. * pin 5 - SCK (SCLK) | 8 7 6 5 4 3 2 1 /
  7. * pin 6 - VSS (GND) | ▄ ▄ ▄ ▄ ▄ ▄ ▄ ▄ /
  8. * pin 7 - DO (MISO) | ▀ ▀ █ ▀ █ ▀ ▀ ▀ |
  9. * pin 8 - not used |_________________|
  10. * ║ ║ ║ ║ ║ ║ ║ ║
  11. * ╔═══════╝ ║ ║ ║ ║ ║ ║ ╚═════════╗
  12. * ║ ║ ║ ║ ║ ║ ╚══════╗ ║
  13. * ║ ╔═════╝ ║ ║ ║ ╚═════╗ ║ ║
  14. * Connections for ║ ║ ╔═══╩═║═║═══╗ ║ ║ ║
  15. * full-sized ║ ║ ║ ╔═╝ ║ ║ ║ ║ ║
  16. * SD card ║ ║ ║ ║ ║ ║ ║ ║ ║
  17. * Pin name | - DO VSS SCK VDD VSS DI CS - |
  18. * SD pin number | 8 7 6 5 4 3 2 1 9 /
  19. * | █/
  20. * |__▍___▊___█___█___█___█___█___█___/
  21. *
  22. * Note: The SPI pins can be manually configured by using `SPI.begin(sck, miso, mosi, cs).`
  23. * Alternatively, you can change the CS pin and use the other default settings by using `SD.begin(cs)`.
  24. *
  25. * +--------------+---------+-------+----------+----------+----------+----------+----------+
  26. * | SPI Pin Name | ESP8266 | ESP32 | ESP32‑S2 | ESP32‑S3 | ESP32‑C3 | ESP32‑C6 | ESP32‑H2 |
  27. * +==============+=========+=======+==========+==========+==========+==========+==========+
  28. * | CS (SS) | GPIO15 | GPIO5 | GPIO34 | GPIO10 | GPIO7 | GPIO18 | GPIO0 |
  29. * +--------------+---------+-------+----------+----------+----------+----------+----------+
  30. * | DI (MOSI) | GPIO13 | GPIO23| GPIO35 | GPIO11 | GPIO6 | GPIO19 | GPIO25 |
  31. * +--------------+---------+-------+----------+----------+----------+----------+----------+
  32. * | DO (MISO) | GPIO12 | GPIO19| GPIO37 | GPIO13 | GPIO5 | GPIO20 | GPIO11 |
  33. * +--------------+---------+-------+----------+----------+----------+----------+----------+
  34. * | SCK (SCLK) | GPIO14 | GPIO18| GPIO36 | GPIO12 | GPIO4 | GPIO21 | GPIO10 |
  35. * +--------------+---------+-------+----------+----------+----------+----------+----------+
  36. *
  37. * For more info see file README.md in this library or on URL:
  38. * https://github.com/espressif/arduino-esp32/tree/master/libraries/SD
  39. */
  40. #include "FS.h"
  41. #include "SD.h"
  42. #include "SPI.h"
  43. /*
  44. Uncomment and set up if you want to use custom pins for the SPI communication
  45. #define REASSIGN_PINS
  46. int sck = -1;
  47. int miso = -1;
  48. int mosi = -1;
  49. int cs = -1;
  50. */
  51. void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
  52. Serial.printf("Listing directory: %s\n", dirname);
  53. File root = fs.open(dirname);
  54. if (!root) {
  55. Serial.println("Failed to open directory");
  56. return;
  57. }
  58. if (!root.isDirectory()) {
  59. Serial.println("Not a directory");
  60. return;
  61. }
  62. File file = root.openNextFile();
  63. while (file) {
  64. if (file.isDirectory()) {
  65. Serial.print(" DIR : ");
  66. Serial.println(file.name());
  67. if (levels) {
  68. listDir(fs, file.path(), levels - 1);
  69. }
  70. } else {
  71. Serial.print(" FILE: ");
  72. Serial.print(file.name());
  73. Serial.print(" SIZE: ");
  74. Serial.println(file.size());
  75. }
  76. file = root.openNextFile();
  77. }
  78. }
  79. void createDir(fs::FS &fs, const char *path) {
  80. Serial.printf("Creating Dir: %s\n", path);
  81. if (fs.mkdir(path)) {
  82. Serial.println("Dir created");
  83. } else {
  84. Serial.println("mkdir failed");
  85. }
  86. }
  87. void removeDir(fs::FS &fs, const char *path) {
  88. Serial.printf("Removing Dir: %s\n", path);
  89. if (fs.rmdir(path)) {
  90. Serial.println("Dir removed");
  91. } else {
  92. Serial.println("rmdir failed");
  93. }
  94. }
  95. void readFile(fs::FS &fs, const char *path) {
  96. Serial.printf("Reading file: %s\n", path);
  97. File file = fs.open(path);
  98. if (!file) {
  99. Serial.println("Failed to open file for reading");
  100. return;
  101. }
  102. Serial.print("Read from file: ");
  103. while (file.available()) {
  104. Serial.write(file.read());
  105. }
  106. file.close();
  107. }
  108. void writeFile(fs::FS &fs, const char *path, const char *message) {
  109. Serial.printf("Writing file: %s\n", path);
  110. File file = fs.open(path, FILE_WRITE);
  111. if (!file) {
  112. Serial.println("Failed to open file for writing");
  113. return;
  114. }
  115. if (file.print(message)) {
  116. Serial.println("File written");
  117. } else {
  118. Serial.println("Write failed");
  119. }
  120. file.close();
  121. }
  122. void appendFile(fs::FS &fs, const char *path, const char *message) {
  123. Serial.printf("Appending to file: %s\n", path);
  124. File file = fs.open(path, FILE_APPEND);
  125. if (!file) {
  126. Serial.println("Failed to open file for appending");
  127. return;
  128. }
  129. if (file.print(message)) {
  130. Serial.println("Message appended");
  131. } else {
  132. Serial.println("Append failed");
  133. }
  134. file.close();
  135. }
  136. void renameFile(fs::FS &fs, const char *path1, const char *path2) {
  137. Serial.printf("Renaming file %s to %s\n", path1, path2);
  138. if (fs.rename(path1, path2)) {
  139. Serial.println("File renamed");
  140. } else {
  141. Serial.println("Rename failed");
  142. }
  143. }
  144. void deleteFile(fs::FS &fs, const char *path) {
  145. Serial.printf("Deleting file: %s\n", path);
  146. if (fs.remove(path)) {
  147. Serial.println("File deleted");
  148. } else {
  149. Serial.println("Delete failed");
  150. }
  151. }
  152. void testFileIO(fs::FS &fs, const char *path) {
  153. File file = fs.open(path);
  154. static uint8_t buf[512];
  155. size_t len = 0;
  156. uint32_t start = millis();
  157. uint32_t end = start;
  158. if (file) {
  159. len = file.size();
  160. size_t flen = len;
  161. start = millis();
  162. while (len) {
  163. size_t toRead = len;
  164. if (toRead > 512) {
  165. toRead = 512;
  166. }
  167. file.read(buf, toRead);
  168. len -= toRead;
  169. }
  170. end = millis() - start;
  171. Serial.printf("%u bytes read for %lu ms\n", flen, end);
  172. file.close();
  173. } else {
  174. Serial.println("Failed to open file for reading");
  175. }
  176. file = fs.open(path, FILE_WRITE);
  177. if (!file) {
  178. Serial.println("Failed to open file for writing");
  179. return;
  180. }
  181. size_t i;
  182. start = millis();
  183. for (i = 0; i < 2048; i++) {
  184. file.write(buf, 512);
  185. }
  186. end = millis() - start;
  187. Serial.printf("%u bytes written for %lu ms\n", 2048 * 512, end);
  188. file.close();
  189. }
  190. void setup() {
  191. Serial.begin(115200);
  192. #ifdef REASSIGN_PINS
  193. SPI.begin(sck, miso, mosi, cs);
  194. if (!SD.begin(cs)) {
  195. #else
  196. if (!SD.begin()) {
  197. #endif
  198. Serial.println("Card Mount Failed");
  199. return;
  200. }
  201. uint8_t cardType = SD.cardType();
  202. if (cardType == CARD_NONE) {
  203. Serial.println("No SD card attached");
  204. return;
  205. }
  206. Serial.print("SD Card Type: ");
  207. if (cardType == CARD_MMC) {
  208. Serial.println("MMC");
  209. } else if (cardType == CARD_SD) {
  210. Serial.println("SDSC");
  211. } else if (cardType == CARD_SDHC) {
  212. Serial.println("SDHC");
  213. } else {
  214. Serial.println("UNKNOWN");
  215. }
  216. uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  217. Serial.printf("SD Card Size: %lluMB\n", cardSize);
  218. listDir(SD, "/", 0);
  219. createDir(SD, "/mydir");
  220. listDir(SD, "/", 0);
  221. removeDir(SD, "/mydir");
  222. listDir(SD, "/", 2);
  223. writeFile(SD, "/hello.txt", "Hello ");
  224. appendFile(SD, "/hello.txt", "World!\n");
  225. readFile(SD, "/hello.txt");
  226. deleteFile(SD, "/foo.txt");
  227. renameFile(SD, "/hello.txt", "/foo.txt");
  228. readFile(SD, "/foo.txt");
  229. testFileIO(SD, "/test.txt");
  230. Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
  231. Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
  232. }
  233. void loop() {}