|
| 1 | +/** |
| 2 | + * @file AtomicTFCard.ino |
| 3 | + * @author SeanKwok ([email protected]) |
| 4 | + * @brief M5AtomS3 Atomic TFCard Base Test |
| 5 | + * @version 0.1 |
| 6 | + * @date 2023-12-14 |
| 7 | + * |
| 8 | + * |
| 9 | + * @Hardwares: M5AtomS3 + Atomic TFCard Base + MicroSD Card |
| 10 | + * @Platform Version: Arduino M5Stack Board Manager v2.0.9 |
| 11 | + * @Dependent Library: |
| 12 | + * M5GFX: https://github.com/m5stack/M5GFX |
| 13 | + * M5Unified: https://github.com/m5stack/M5Unified |
| 14 | + * M5AtomS3: https://github.com/m5stack/M5AtomS3 |
| 15 | + */ |
| 16 | + |
| 17 | +#include "M5AtomS3.h" |
| 18 | +#include <Arduino.h> |
| 19 | +#include <SPI.h> |
| 20 | +#include "FS.h" |
| 21 | +#include "SD.h" |
| 22 | + |
| 23 | +#define SCK 7 |
| 24 | +#define MISO 8 |
| 25 | +#define MOSI 6 |
| 26 | + |
| 27 | +// Listing directory. 列出目录 |
| 28 | +void listDir(fs::FS &fs, const char *dirname, uint8_t levels) { |
| 29 | + Serial.printf("Listing directory: %s\n", dirname); |
| 30 | + |
| 31 | + File root = fs.open(dirname); |
| 32 | + if (!root) { |
| 33 | + Serial.println("Failed to open directory"); |
| 34 | + return; |
| 35 | + } |
| 36 | + if (!root.isDirectory()) { |
| 37 | + Serial.println("Not a directory"); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + File file = root.openNextFile(); |
| 42 | + while (file) { |
| 43 | + if (file.isDirectory()) { |
| 44 | + Serial.print(" DIR : "); |
| 45 | + Serial.println(file.name()); |
| 46 | + if (levels) { |
| 47 | + listDir(fs, file.name(), levels - 1); |
| 48 | + } |
| 49 | + } else { |
| 50 | + Serial.print(" FILE: "); |
| 51 | + Serial.print(file.name()); |
| 52 | + Serial.print(" SIZE: "); |
| 53 | + Serial.println(file.size()); |
| 54 | + } |
| 55 | + file = root.openNextFile(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Creating Dir. 创建目录 |
| 60 | +void createDir(fs::FS &fs, const char *path) { |
| 61 | + Serial.printf("Creating Dir: %s\n", path); |
| 62 | + if (fs.mkdir(path)) { |
| 63 | + Serial.println("Dir created"); |
| 64 | + } else { |
| 65 | + Serial.println("mkdir failed"); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Removing Dir. 删除目录 |
| 70 | +void removeDir(fs::FS &fs, const char *path) { |
| 71 | + Serial.printf("Removing Dir: %s\n", path); |
| 72 | + if (fs.rmdir(path)) { |
| 73 | + Serial.println("Dir removed"); |
| 74 | + } else { |
| 75 | + Serial.println("rmdir failed"); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Reading file. 读取文件 |
| 80 | +void readFile(fs::FS &fs, const char *path) { |
| 81 | + Serial.printf("Reading file: %s\n", path); |
| 82 | + |
| 83 | + File file = fs.open(path); |
| 84 | + if (!file) { |
| 85 | + Serial.println("Failed to open file for reading"); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + Serial.print("Read from file: "); |
| 90 | + while (file.available()) { |
| 91 | + Serial.write(file.read()); |
| 92 | + } |
| 93 | + file.close(); |
| 94 | +} |
| 95 | + |
| 96 | +// Writing file 写入文件 |
| 97 | +void writeFile(fs::FS &fs, const char *path, const char *message) { |
| 98 | + Serial.printf("Writing file: %s\n", path); |
| 99 | + |
| 100 | + File file = fs.open(path, FILE_WRITE); |
| 101 | + if (!file) { |
| 102 | + Serial.println("Failed to open file for writing"); |
| 103 | + return; |
| 104 | + } |
| 105 | + if (file.print(message)) { |
| 106 | + Serial.println("File written"); |
| 107 | + } else { |
| 108 | + Serial.println("Write failed"); |
| 109 | + } |
| 110 | + file.close(); |
| 111 | +} |
| 112 | + |
| 113 | +// Appending to file 添加到文件 |
| 114 | +void appendFile(fs::FS &fs, const char *path, const char *message) { |
| 115 | + Serial.printf("Appending to file: %s\n", path); |
| 116 | + |
| 117 | + File file = fs.open(path, FILE_APPEND); |
| 118 | + if (!file) { |
| 119 | + Serial.println("Failed to open file for appending"); |
| 120 | + return; |
| 121 | + } |
| 122 | + if (file.print(message)) { |
| 123 | + Serial.println("Message appended"); |
| 124 | + } else { |
| 125 | + Serial.println("Append failed"); |
| 126 | + } |
| 127 | + file.close(); |
| 128 | +} |
| 129 | + |
| 130 | +// Renaming file 重命名文件 |
| 131 | +void renameFile(fs::FS &fs, const char *path1, const char *path2) { |
| 132 | + Serial.printf("Renaming file %s to %s\n", path1, path2); |
| 133 | + if (fs.rename(path1, path2)) { |
| 134 | + Serial.println("File renamed"); |
| 135 | + } else { |
| 136 | + Serial.println("Rename failed"); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// Deleting file 删除文件 |
| 141 | +void deleteFile(fs::FS &fs, const char *path) { |
| 142 | + Serial.printf("Deleting file: %s\n", path); |
| 143 | + if (fs.remove(path)) { |
| 144 | + Serial.println("File deleted"); |
| 145 | + } else { |
| 146 | + Serial.println("Delete failed"); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +void testFileIO(fs::FS &fs, const char *path) { |
| 151 | + File file = fs.open(path); |
| 152 | + static uint8_t buf[512]; |
| 153 | + size_t len = 0; |
| 154 | + uint32_t start = millis(); |
| 155 | + uint32_t end = start; |
| 156 | + if (file) { |
| 157 | + len = file.size(); |
| 158 | + size_t flen = len; |
| 159 | + start = millis(); |
| 160 | + while (len) { |
| 161 | + size_t toRead = len; |
| 162 | + if (toRead > 512) { |
| 163 | + toRead = 512; |
| 164 | + } |
| 165 | + file.read(buf, toRead); |
| 166 | + len -= toRead; |
| 167 | + } |
| 168 | + end = millis() - start; |
| 169 | + Serial.printf("%u bytes read for %u ms\n", flen, end); |
| 170 | + file.close(); |
| 171 | + } else { |
| 172 | + Serial.println("Failed to open file for reading"); |
| 173 | + } |
| 174 | + |
| 175 | + file = fs.open(path, FILE_WRITE); |
| 176 | + if (!file) { |
| 177 | + Serial.println("Failed to open file for writing"); |
| 178 | + return; |
| 179 | + } |
| 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 %u ms\n", 2048 * 512, end); |
| 188 | + file.close(); |
| 189 | +} |
| 190 | +void setup() { |
| 191 | + auto cfg = M5.config(); |
| 192 | + AtomS3.begin(cfg); |
| 193 | + |
| 194 | + AtomS3.Display.setTextColor(GREEN); |
| 195 | + AtomS3.Display.setTextDatum(middle_center); |
| 196 | + AtomS3.Display.setTextFont(&fonts::FreeSerifItalic12pt7b); |
| 197 | + AtomS3.Display.setTextSize(1); |
| 198 | + AtomS3.Display.drawString("MicroSD", AtomS3.Display.width() / 2, 20); |
| 199 | + |
| 200 | + SPI.begin(SCK, MISO, MOSI, -1); |
| 201 | + if (!SD.begin()) { |
| 202 | + Serial.println("Card Mount Failed"); |
| 203 | + return; |
| 204 | + } |
| 205 | + uint8_t cardType = SD.cardType(); |
| 206 | + |
| 207 | + if (cardType == CARD_NONE) { |
| 208 | + Serial.println("No SD card attached"); |
| 209 | + return; |
| 210 | + } |
| 211 | + |
| 212 | + Serial.print("SD Card Type: "); |
| 213 | + if (cardType == CARD_MMC) { |
| 214 | + Serial.println("MMC"); |
| 215 | + } else if (cardType == CARD_SD) { |
| 216 | + Serial.println("SDSC"); |
| 217 | + } else if (cardType == CARD_SDHC) { |
| 218 | + Serial.println("SDHC"); |
| 219 | + } else { |
| 220 | + Serial.println("UNKNOWN"); |
| 221 | + } |
| 222 | + |
| 223 | + uint64_t cardSize = SD.cardSize() / (1024 * 1024); |
| 224 | + Serial.printf("SD Card Size: %lluMB\n", cardSize); |
| 225 | + |
| 226 | + Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024)); |
| 227 | + Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024)); |
| 228 | + |
| 229 | + AtomS3.Display.drawString(String(SD.usedBytes() / (1024 * 1024)) + " / " + |
| 230 | + String(SD.totalBytes() / (1024 * 1024)), |
| 231 | + AtomS3.Display.width() / 2, 60); |
| 232 | + AtomS3.Display.drawString("MB", AtomS3.Display.width() / 2, 100); |
| 233 | + |
| 234 | + listDir(SD, "/", 0); |
| 235 | + createDir(SD, "/mydir"); |
| 236 | + listDir(SD, "/", 0); |
| 237 | + removeDir(SD, "/mydir"); |
| 238 | + listDir(SD, "/", 2); |
| 239 | + writeFile(SD, "/hello.txt", "Hello "); |
| 240 | + appendFile(SD, "/hello.txt", "World!\n"); |
| 241 | + readFile(SD, "/hello.txt"); |
| 242 | + deleteFile(SD, "/foo.txt"); |
| 243 | + renameFile(SD, "/hello.txt", "/foo.txt"); |
| 244 | + readFile(SD, "/foo.txt"); |
| 245 | + testFileIO(SD, "/test.txt"); |
| 246 | +} |
| 247 | + |
| 248 | +void loop() { |
| 249 | +} |
0 commit comments