-
Notifications
You must be signed in to change notification settings - Fork 45
Description
Good morning,
I am facing an issue where I am unable to initialize the SD Card on the Waveshare LCD Touch 7". I realize this may be a little out-of-scope for the purposes of this library, but I am looking for some guidance as I believe the issue is either with conflicting PINs or something to do with how the IO Expander is initialized because I then need to access the IO Expander to initialize and utilize the SD Card. I am programming in PlatformIO.
The Waveshare's example code below compiles in ArduinoIDE without issue and properly outputs the 8Gb SD Card I have inserted into the display.
#include "waveshare_sd_card.h"
// Initial Setup
void setup(){
Serial.begin(115200);
Serial.println("Initialize IO expander");
/* Initialize IO expander */
ESP_IOExpander_CH422G *expander = new ESP_IOExpander_CH422G((i2c_port_t)I2C_MASTER_NUM, ESP_IO_EXPANDER_I2C_CH422G_ADDRESS, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO);
expander->init();
expander->begin();
Serial.println("Set the IO0-7 pin to output mode.");
expander->enableAllIO_Output();
expander->digitalWrite(TP_RST , HIGH);
expander->digitalWrite(LCD_RST , HIGH);
expander->digitalWrite(LCD_BL , HIGH);
// Use extended GPIO for SD card
expander->digitalWrite(SD_CS, LOW);
// Turn off backlight
expander->digitalWrite(LCD_BL, LOW);
// When USB_SEL is HIGH, it enables FSUSB42UMX chip and gpio19, gpio20 wired CAN_TX CAN_RX, and then don't use USB Function
expander->digitalWrite(USB_SEL, LOW);
// Initialize SPI
SPI.setHwCs(false);
SPI.begin(SD_CLK, SD_MISO, SD_MOSI, SD_SS);
if (!SD.begin()) {
Serial.println("Card Mount Failed"); // SD card mounting failed
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached"); // No SD card connected
return;
}
Serial.print("SD Card Type: "); // 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"); // Unknown Type
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize); // SD card size
// 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.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024)); // Total space
Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024)); // Used space
}
// Main Loop
void loop() {
}
In my PlatformIO project, I have attempted to port this code over in such a way that I can call SD_init(). However, when I do so, I either get system crashes (when trying to define a new IO Expander), or the LCD itself turns my UI blue (likely an issue on conflicting PINs or order of operations between the board and LCD init() functions and the SD_init() function).
Is there any way you help guide me on the proper way to initialize and utilize the SD Card on this board? For reference, my setup() function looks like this:
void setup()
{
Serial.begin(115200);
//createWifiKey();
delay(100); // Rumors say it helps avoid sporadical crashes after wakeup from deep-sleep
WiFi.mode(WIFI_STA);
delay(50);
Serial.print("MAC Address: ");
Serial.println(WiFi.macAddress());
myMac = WiFi.macAddress();
if (esp_now_init() == ESP_OK)
{
esp_now_register_recv_cb(OnDataRecv);
esp_now_register_send_cb(OnDataSent);
}
if (esp_now_set_pmk(esp_now_key) != ESP_OK)
{
Serial.println("Error setting ESP-NOW PMK");
}
// load UI
Serial.println("Initializing board");
Board *board = new Board();
board->init();
assert(board->begin());
Serial.println("Initializing LVGL");
lvgl_port_init(board->getLCD(), board->getTouch());
Serial.println("Creating UI");
ui_init(); // function defined in my ui.h file
initializeSdCard();
}
And here are the 2 helper functions for the SD Card:
bool SD_init(void) {
Serial.println("Initialize IO expander");
/* Initialize IO expander */
/*
ESP_IOExpander_CH422G *expander = new ESP_IOExpander_CH422G((i2c_port_t)I2C_MASTER_NUM, ESP_IO_EXPANDER_I2C_CH422G_ADDRESS, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO);
expander->init();
expander->begin();
Serial.println("Set the IO0-7 pin to output mode.");
expander->enableAllIO_Output();
expander->digitalWrite(TP_RST , HIGH);
expander->digitalWrite(LCD_RST , HIGH);
expander->digitalWrite(LCD_BL , HIGH);
// Use extended GPIO for SD card
expander->digitalWrite(SD_CS, LOW);
// Initialize SPI
SPI.setHwCs(false);
SPI.begin(SD_CLK, SD_MISO, SD_MOSI, SD_SS);
*/
/* The above code causes the board to go into a crash loop*/
if (!SD.begin()) {
Serial.println("Card Mount Failed"); // SD card mounting failed
return false;
}
return true;
}
void initializeSdCard()
{
sdCard = SD_init(); // global bool variable. Causes UI to be blue
if (sdCard)
{
// Check the total space and used space on the SD card
uint64_t totalBytes = SD.totalBytes();
uint64_t usedBytes = SD.usedBytes();
freePercentage = ((totalBytes - usedBytes) * 100) / totalBytes;
Serial.printf("SD Card Total Space: %llu bytes\n", totalBytes);
Serial.printf("SD Card Used Space: %llu bytes\n", usedBytes);
Serial.printf("SD Card Free Space: %d%%\n", freePercentage);
}
}
Any guidance you can provide is greatly appreciated. I see the expander being initialized in the esp_panel_board.cpp file, but I don't understand how to use that code to also initialize the SD Card. Thanks for your time.