From d63a401cd50efd7a6a7310e3d1f4b3b33ee9603f Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Mon, 2 Mar 2026 23:59:54 +0100 Subject: [PATCH] SDMMC driver WIP --- .../source/drivers/esp32_sdmmc.cpp | 50 ++++++++----------- .../source/drivers/esp32_sdmmc_fs.cpp | 12 +++-- Platforms/platform-esp32/source/module.cpp | 8 ++- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/Platforms/platform-esp32/source/drivers/esp32_sdmmc.cpp b/Platforms/platform-esp32/source/drivers/esp32_sdmmc.cpp index 2222bf48..aa8b5599 100644 --- a/Platforms/platform-esp32/source/drivers/esp32_sdmmc.cpp +++ b/Platforms/platform-esp32/source/drivers/esp32_sdmmc.cpp @@ -7,7 +7,7 @@ #include "tactility/drivers/gpio_descriptor.h" #include #include -#include +#include #define TAG "esp32_sdmmc" @@ -16,32 +16,16 @@ extern "C" { -error_t mount(Device* device) { - return ERROR_NONE; -} - -error_t unmount(Device* device) { - return ERROR_NONE; -} - -bool is_mounted(Device* device) { - return true; -} - -error_t get_mount_path(Device*, char* out_path) { - return ERROR_NONE; -} - -static const FileSystemApi sdmmc_filesystem_api = { - .mount = mount, - .unmount = unmount, - .is_mounted = is_mounted, - .get_mount_path = get_mount_path -}; - struct Esp32SdmmcInternal { RecursiveMutex mutex = {}; bool initialized = false; + char fs_device_name[16] = "esp32_sdmmc_fs0"; + Device fs_device = { + .name = fs_device_name, + .config = nullptr, + .parent = nullptr, + .internal = nullptr + }; // Pin descriptors GpioDescriptor* pin_clk_descriptor = nullptr; @@ -122,7 +106,11 @@ static error_t start(Device* device) { return ERROR_RESOURCE; } - // TODO: filesystem + // Create filesystem child device + auto* fs_device = &data->fs_device; + fs_device->parent = device; + fs_device->config = sdmmc_config; + check(device_construct_add_start(fs_device, "espressif,esp32-sdmmc-fs") == ERROR_NONE); data->initialized = true; return ERROR_NONE; @@ -130,14 +118,18 @@ static error_t start(Device* device) { static error_t stop(Device* device) { ESP_LOGI(TAG, "stop %s", device->name); - auto* driver_data = GET_DATA(device); + auto* data = GET_DATA(device); auto* dts_config = GET_CONFIG(device); - // TODO: filesystem + // Create filesystem child device + auto* fs_device = &data->fs_device; + check(device_stop(fs_device) == ERROR_NONE); + check(device_remove(fs_device) == ERROR_NONE); + check(device_destruct(fs_device) == ERROR_NONE); - driver_data->cleanup_pins(); + data->cleanup_pins(); device_set_driver_data(device, nullptr); - delete driver_data; + delete data; return ERROR_NONE; } diff --git a/Platforms/platform-esp32/source/drivers/esp32_sdmmc_fs.cpp b/Platforms/platform-esp32/source/drivers/esp32_sdmmc_fs.cpp index e7487e69..1e9188d7 100644 --- a/Platforms/platform-esp32/source/drivers/esp32_sdmmc_fs.cpp +++ b/Platforms/platform-esp32/source/drivers/esp32_sdmmc_fs.cpp @@ -74,7 +74,7 @@ error_t mount(Device* device, const char* mount_path) { } else { LOG_E(TAG, "Mounting failed: %s", esp_err_to_name(result)); } - return false; + return ERROR_UNDEFINED; } data->mount_path = mount_path; @@ -117,7 +117,9 @@ static error_t start(Device* device) { device_set_driver_data(device, data); - // TODO: filesystem + if (mount(device, "/sdcard") != ERROR_NONE) { + LOG_E(TAG, "Failed to mount SD card"); + } return ERROR_NONE; } @@ -126,7 +128,11 @@ static error_t stop(Device* device) { ESP_LOGI(TAG, "stop %s", device->name); auto* driver_data = GET_DATA(device); - // TODO: filesystem + if (is_mounted(device)) { + if (unmount(device) != ERROR_NONE) { + LOG_E(TAG, "Failed to unmount SD card"); + } + } device_set_driver_data(device, nullptr); delete driver_data; diff --git a/Platforms/platform-esp32/source/module.cpp b/Platforms/platform-esp32/source/module.cpp index f75fbb09..3d1ce73e 100644 --- a/Platforms/platform-esp32/source/module.cpp +++ b/Platforms/platform-esp32/source/module.cpp @@ -7,6 +7,8 @@ extern "C" { extern Driver esp32_gpio_driver; extern Driver esp32_i2c_driver; extern Driver esp32_i2s_driver; +extern Driver esp32_sdmmc_driver; +extern Driver esp32_sdmmc_fs_driver; extern Driver esp32_spi_driver; extern Driver esp32_uart_driver; @@ -16,6 +18,8 @@ static error_t start() { check(driver_construct_add(&esp32_gpio_driver) == ERROR_NONE); check(driver_construct_add(&esp32_i2c_driver) == ERROR_NONE); check(driver_construct_add(&esp32_i2s_driver) == ERROR_NONE); + check(driver_construct_add(&esp32_sdmmc_driver) == ERROR_NONE); + check(driver_construct_add(&esp32_sdmmc_fs_driver) == ERROR_NONE); check(driver_construct_add(&esp32_spi_driver) == ERROR_NONE); check(driver_construct_add(&esp32_uart_driver) == ERROR_NONE); return ERROR_NONE; @@ -27,12 +31,14 @@ static error_t stop() { check(driver_remove_destruct(&esp32_gpio_driver) == ERROR_NONE); check(driver_remove_destruct(&esp32_i2c_driver) == ERROR_NONE); check(driver_remove_destruct(&esp32_i2s_driver) == ERROR_NONE); + check(driver_remove_destruct(&esp32_sdmmc_driver) == ERROR_NONE); + check(driver_remove_destruct(&esp32_sdmmc_fs_driver) == ERROR_NONE); check(driver_remove_destruct(&esp32_spi_driver) == ERROR_NONE); check(driver_remove_destruct(&esp32_uart_driver) == ERROR_NONE); return ERROR_NONE; } -struct Module platform_esp32_module = { +Module platform_esp32_module = { .name = "platform-esp32", .start = start, .stop = stop,