-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsd_manager.cpp
More file actions
141 lines (116 loc) · 3.64 KB
/
sd_manager.cpp
File metadata and controls
141 lines (116 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "sd_manager.h"
SDManager sdManager;
SDManager::SDManager() : expander(nullptr), initialized(false) {
}
SDManager::~SDManager() {
deinit();
}
bool SDManager::init() {
if (initialized) {
return true;
}
Serial.println("Initialize IO expander for SD");
// Initialize IO expander
expander = new esp_expander::CH422G(EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN, EXAMPLE_I2C_ADDR);
if (!expander) {
Serial.println("Failed to create IO expander");
return false;
}
if (!expander->init()) {
Serial.println("Failed to initialize IO expander");
delete expander;
expander = nullptr;
return false;
}
expander->begin();
expander->multiPinMode(TP_RST | LCD_BL | LCD_RST | SD_CS | USB_SEL, OUTPUT);
expander->multiDigitalWrite(TP_RST | LCD_BL | LCD_RST, HIGH);
// Use extended GPIO for SD card
expander->digitalWrite(SD_CS, LOW);
// Turn off backlight initially
expander->digitalWrite(LCD_BL, LOW);
// When USB_SEL is HIGH, it enables FSUSB42UMX chip and gpio19, gpio20 wired CAN_TX CAN_RX
expander->digitalWrite(USB_SEL, LOW);
// Initialize SPI
SPI.setHwCs(false);
SPI.begin(SD_CLK, SD_MISO, SD_MOSI, SD_SS);
if (!SD.begin(SD_SS)) {
Serial.println("SD Card Mount Failed");
delete expander;
expander = nullptr;
return false;
}
initialized = true;
Serial.println("SD Manager initialized successfully");
return true;
}
void SDManager::deinit() {
if (initialized) {
SD.end();
if (expander) {
delete expander;
expander = nullptr;
}
initialized = false;
Serial.println("SD Manager deinitialized");
}
}
void SDManager::printCardInfo() {
if (!initialized) {
Serial.println("SD Manager not initialized");
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if (cardType == CARD_MMC) {
Serial.println("MMC");
} else if (cardType == CARD_SD) {
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
}
void SDManager::runFileSystemTest() {
if (!initialized) {
Serial.println("SD Manager not initialized");
return;
}
Serial.println("Running file system test...");
// Testing file system functionality
listDir(SD, "/", 0);
createDir(SD, "/mydir");
listDir(SD, "/", 0);
removeDir(SD, "/mydir");
listDir(SD, "/", 2);
writeFile(SD, "/hello.txt", "Hello ");
appendFile(SD, "/hello.txt", "World!\n");
readFile(SD, "/hello.txt");
deleteFile(SD, "/foo.txt");
renameFile(SD, "/hello.txt", "/foo.txt");
readFile(SD, "/foo.txt");
testFileIO(SD, "/test.txt");
Serial.println("File system test completed");
}
void SDManager::runSDTest() {
if (!initialized) {
Serial.println("SD Manager not initialized - attempting to initialize...");
if (!init()) {
Serial.println("Failed to initialize SD Manager");
return;
}
}
Serial.println("=== SD Card Test Started ===");
printCardInfo();
runFileSystemTest();
Serial.println("=== SD Card Test Completed ===");
}