diff --git a/Devices/lilygo-tdeck/lilygo,tdeck.dts b/Devices/lilygo-tdeck/lilygo,tdeck.dts index e5448ee00..adccef932 100644 --- a/Devices/lilygo-tdeck/lilygo,tdeck.dts +++ b/Devices/lilygo-tdeck/lilygo,tdeck.dts @@ -45,8 +45,8 @@ compatible = "espressif,esp32-spi"; host = ; cs-gpios = <&gpio0 12 GPIO_FLAG_NONE>, // Display - <&gpio0 9 GPIO_FLAG_NONE>, // Radio - <&gpio0 39 GPIO_FLAG_NONE>; // SD card + <&gpio0 39 GPIO_FLAG_NONE>, // SD card + <&gpio0 9 GPIO_FLAG_NONE>; // Radio pin-mosi = <&gpio0 41 GPIO_FLAG_NONE>; pin-miso = <&gpio0 38 GPIO_FLAG_NONE>; pin-sclk = <&gpio0 40 GPIO_FLAG_NONE>; @@ -55,8 +55,9 @@ compatible = "display-placeholder"; }; - sdcard@2 { + sdcard@1 { compatible = "espressif,esp32-sdspi"; + status = "disabled"; // Must be started after display frequency-khz = <20000>; }; }; diff --git a/Devices/lilygo-tlora-pager/lilygo,tlora-pager.dts b/Devices/lilygo-tlora-pager/lilygo,tlora-pager.dts index b72631095..94dde1ebb 100644 --- a/Devices/lilygo-tlora-pager/lilygo,tlora-pager.dts +++ b/Devices/lilygo-tlora-pager/lilygo,tlora-pager.dts @@ -35,20 +35,21 @@ spi0 { compatible = "espressif,esp32-spi"; host = ; - cs-gpios = <&gpio0 21 GPIO_FLAG_NONE>, // SD card - <&gpio0 36 GPIO_FLAG_NONE>; // Display + cs-gpios = <&gpio0 36 GPIO_FLAG_NONE>, // Display + <&gpio0 21 GPIO_FLAG_NONE>; // SD card pin-mosi = <&gpio0 34 GPIO_FLAG_NONE>; pin-miso = <&gpio0 33 GPIO_FLAG_NONE>; pin-sclk = <&gpio0 35 GPIO_FLAG_NONE>; - - sdcard@0 { - compatible = "espressif,esp32-sdspi"; - frequency-khz = <20000>; - }; - - display@1 { + + display@0 { compatible = "display-placeholder"; }; + + sdcard@1 { + compatible = "espressif,esp32-sdspi"; + status = "disabled"; // Must be started after display + frequency-khz = <20000>; + }; }; // ES8311 diff --git a/Devices/m5stack-core2/m5stack,core2.dts b/Devices/m5stack-core2/m5stack,core2.dts index 95c6c13b9..0d166c2e8 100644 --- a/Devices/m5stack-core2/m5stack,core2.dts +++ b/Devices/m5stack-core2/m5stack,core2.dts @@ -53,20 +53,21 @@ spi0 { compatible = "espressif,esp32-spi"; host = ; - cs-gpios = <&gpio0 4 GPIO_FLAG_NONE>, // SD card - <&gpio0 5 GPIO_FLAG_NONE>; // Display + cs-gpios = <&gpio0 5 GPIO_FLAG_NONE>, // Display + <&gpio0 4 GPIO_FLAG_NONE>; // SD card pin-mosi = <&gpio0 23 GPIO_FLAG_NONE>; pin-miso = <&gpio0 38 GPIO_FLAG_NONE>; pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>; - sdcard@0 { - compatible = "espressif,esp32-sdspi"; - frequency-khz = <20000>; - }; - - display@1 { + display@0 { compatible = "display-placeholder"; }; + + sdcard@1 { + compatible = "espressif,esp32-sdspi"; + status = "disabled"; // Must be started after display + frequency-khz = <20000>; + }; }; // NS4168: Speaker and microphone diff --git a/Devices/m5stack-cores3/m5stack,cores3.dts b/Devices/m5stack-cores3/m5stack,cores3.dts index c9057f907..e9d49036c 100644 --- a/Devices/m5stack-cores3/m5stack,cores3.dts +++ b/Devices/m5stack-cores3/m5stack,cores3.dts @@ -78,20 +78,21 @@ spi0 { compatible = "espressif,esp32-spi"; host = ; - cs-gpios = <&gpio0 4 GPIO_FLAG_NONE>, // SD card - <&gpio0 3 GPIO_FLAG_NONE>; // Display + cs-gpios = <&gpio0 3 GPIO_FLAG_NONE>, // Display + <&gpio0 4 GPIO_FLAG_NONE>; // SD card pin-mosi = <&gpio0 37 GPIO_FLAG_NONE>; pin-miso = <&gpio0 35 GPIO_FLAG_NONE>; pin-sclk = <&gpio0 36 GPIO_FLAG_NONE>; - - sdcard@0 { - compatible = "espressif,esp32-sdspi"; - frequency-khz = <20000>; - }; - - display@1 { + + display@0 { compatible = "display-placeholder"; }; + + sdcard@1 { + compatible = "espressif,esp32-sdspi"; + status = "disabled"; // Must be started after display + frequency-khz = <20000>; + }; }; // TODO: Enable speaker via ES7210 I2C: https://github.com/m5stack/M5Unified/blob/a6256725481f1bc366655fa48cf03b6095e30ad1/src/M5Unified.cpp#L417 diff --git a/Devices/m5stack-stackchan/m5stack,stackchan.dts b/Devices/m5stack-stackchan/m5stack,stackchan.dts index b20828af4..3029e89ea 100644 --- a/Devices/m5stack-stackchan/m5stack,stackchan.dts +++ b/Devices/m5stack-stackchan/m5stack,stackchan.dts @@ -112,19 +112,20 @@ spi0 { compatible = "espressif,esp32-spi"; host = ; - cs-gpios = <&gpio0 4 GPIO_FLAG_NONE>, // SD card - <&gpio0 3 GPIO_FLAG_NONE>; // Display + cs-gpios = <&gpio0 3 GPIO_FLAG_NONE>, // Display + <&gpio0 4 GPIO_FLAG_NONE>; // SD card pin-mosi = <&gpio0 37 GPIO_FLAG_NONE>; pin-miso = <&gpio0 35 GPIO_FLAG_NONE>; pin-sclk = <&gpio0 36 GPIO_FLAG_NONE>; - sdcard@0 { - compatible = "espressif,esp32-sdspi"; - frequency-khz = <20000>; + display@0 { + compatible = "display-placeholder"; }; - display@1 { - compatible = "display-placeholder"; + sdcard@1 { + compatible = "espressif,esp32-sdspi"; + status = "disabled"; // Must be started after display + frequency-khz = <20000>; }; }; diff --git a/Devices/simulator/Source/Simulator.cpp b/Devices/simulator/Source/Simulator.cpp index b60d84566..0e44471d3 100644 --- a/Devices/simulator/Source/Simulator.cpp +++ b/Devices/simulator/Source/Simulator.cpp @@ -1,9 +1,7 @@ #include "hal/SdlDisplay.h" #include "hal/SdlKeyboard.h" #include "hal/SimulatorPower.h" -#include "hal/SimulatorSdCard.h" -#include // LVGL #include #define TAG "hardware" @@ -15,7 +13,6 @@ static std::vector> createDevices() { std::make_shared(), std::make_shared(), std::make_shared(), - std::make_shared() }; } diff --git a/Devices/simulator/Source/hal/SimulatorSdCard.h b/Devices/simulator/Source/hal/SimulatorSdCard.h deleted file mode 100644 index 796a35fcd..000000000 --- a/Devices/simulator/Source/hal/SimulatorSdCard.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#include "Tactility/hal/sdcard/SdCardDevice.h" -#include -#include - -using tt::hal::sdcard::SdCardDevice; - -class SimulatorSdCard final : public SdCardDevice { - - State state; - std::shared_ptr lock; - std::string mountPath; - -public: - - SimulatorSdCard() : SdCardDevice(MountBehaviour::AtBoot), - state(State::Unmounted), - lock(std::make_shared()) - {} - - std::string getName() const override { return "Mock SD Card"; } - std::string getDescription() const override { return ""; } - - bool mount(const std::string& newMountPath) override { - state = State::Mounted; - mountPath = newMountPath; - return true; - } - - bool unmount() override { - state = State::Unmounted; - mountPath = ""; - return true; - } - - std::string getMountPath() const override { return mountPath; } - - std::shared_ptr getLock() const override { return lock; } - - State getState(TickType_t timeout) const override { return state; } -}; diff --git a/Platforms/platform-esp32/source/drivers/esp32_sdmmc.cpp b/Platforms/platform-esp32/source/drivers/esp32_sdmmc.cpp index b1fe993ab..4e259c40a 100644 --- a/Platforms/platform-esp32/source/drivers/esp32_sdmmc.cpp +++ b/Platforms/platform-esp32/source/drivers/esp32_sdmmc.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -177,7 +178,7 @@ Driver esp32_sdmmc_driver = { .start_device = start, .stop_device = stop, .api = nullptr, - .device_type = nullptr, + .device_type = &SDCARD_TYPE, .owner = &platform_esp32_module, .internal = nullptr }; diff --git a/Platforms/platform-esp32/source/drivers/esp32_sdspi.cpp b/Platforms/platform-esp32/source/drivers/esp32_sdspi.cpp index 38b542785..7b4bf1531 100644 --- a/Platforms/platform-esp32/source/drivers/esp32_sdspi.cpp +++ b/Platforms/platform-esp32/source/drivers/esp32_sdspi.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -165,7 +166,7 @@ Driver esp32_sdspi_driver = { .start_device = start, .stop_device = stop, .api = nullptr, - .device_type = nullptr, + .device_type = &SDCARD_TYPE, .owner = &platform_esp32_module, .internal = nullptr }; diff --git a/Tactility/Include/Tactility/hal/sdcard/SdCardDevice.h b/Tactility/Include/Tactility/hal/sdcard/SdCardDevice.h deleted file mode 100644 index d57ffdb73..000000000 --- a/Tactility/Include/Tactility/hal/sdcard/SdCardDevice.h +++ /dev/null @@ -1,84 +0,0 @@ -#pragma once - -#include - -#include -#include - - -struct FileSystem; -namespace tt::hal::sdcard { - -/** - * Warning: getLock() does not have to be used when calling any of the functions of this class. - * The lock is only used for file access on the path where the SD card is mounted. - * This is mainly used when accessing the SD card on a shared SPI bus. - */ -class SdCardDevice : public Device { - -public: - - enum class State { - Mounted, - Unmounted, - Error, - Timeout // Failed to retrieve state due to timeout - }; - - enum class MountBehaviour { - AtBoot, /** Only mount at boot */ - Anytime /** Mount/dismount any time */ - }; - -private: - - MountBehaviour mountBehaviour; - FileSystem* fileSystem; - -public: - - explicit SdCardDevice(MountBehaviour mountBehaviour); - ~SdCardDevice() override; - - Type getType() const final { return Type::SdCard; }; - - /** - * Mount the device. - * @param mountPath the path to mount at - * @return true on successful mount - */ - virtual bool mount(const std::string& mountPath) = 0; - - /** - * Unmount the device. - * @return true on successful unmount - */ - virtual bool unmount() = 0; - - virtual State getState(TickType_t timeout = kernel::MAX_TICKS) const = 0; - - /** @return empty string when not mounted or the mount path if mounted */ - virtual std::string getMountPath() const = 0; - - /** @return non-null lock, used by code that wants to access files on the mount path of this SD card */ - virtual std::shared_ptr getLock() const = 0; - - /** @return the MountBehaviour of this device */ - virtual MountBehaviour getMountBehaviour() const { return mountBehaviour; } - - /** @return true if the SD card was mounted, returns false when it was not or when a timeout happened. */ - bool isMounted(TickType_t timeout = kernel::MAX_TICKS) const { return getState(timeout) == State::Mounted; } -}; - -/** Return the SdCard device if the path is within the SdCard mounted path (path std::string::starts_with() check), otherwise return nullptr */ -std::shared_ptr find(const std::string& path); - -/** - * Attempt to find an SD card that the specified belongs to, - * and returns its lock if the SD card is mounted. Otherwise it returns nullptr. - * @param[in] a path on a file system (e.g. file, directory, etc.) - * @return the lock of a mounted SD card or otherwise null - */ -std::shared_ptr findSdCardLock(const std::string& path); - -} // namespace tt::hal diff --git a/Tactility/Private/Tactility/hal/SdCard.h b/Tactility/Private/Tactility/hal/SdCard.h new file mode 100644 index 000000000..d09379a44 --- /dev/null +++ b/Tactility/Private/Tactility/hal/SdCard.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +#include + +namespace tt::hal::sdcard { + +/** + * Attempt to find an SD card that the specified belongs to, + * and returns its lock if the SD card is mounted. Otherwise it returns nullptr. + * @param[in] a path on a file system (e.g. file, directory, etc.) + * @return the lock of a mounted SD card or otherwise null + */ +std::shared_ptr findSdCardLock(const std::string& path); + +void mountAll(); + +} diff --git a/Tactility/Private/Tactility/hal/sdcard/SdCardMounting.h b/Tactility/Private/Tactility/hal/sdcard/SdCardMounting.h deleted file mode 100644 index 163e5a7ed..000000000 --- a/Tactility/Private/Tactility/hal/sdcard/SdCardMounting.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -namespace tt::hal::sdcard { - -void mountAll(); - -} diff --git a/Tactility/Source/app/files/State.cpp b/Tactility/Source/app/files/State.cpp index 86f88a594..033746e3c 100644 --- a/Tactility/Source/app/files/State.cpp +++ b/Tactility/Source/app/files/State.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include diff --git a/Tactility/Source/app/fileselection/State.cpp b/Tactility/Source/app/fileselection/State.cpp index 7dfa38ca8..0e2419bd5 100644 --- a/Tactility/Source/app/fileselection/State.cpp +++ b/Tactility/Source/app/fileselection/State.cpp @@ -1,7 +1,6 @@ #include "Tactility/app/fileselection/State.h" #include -#include "Tactility/hal/sdcard/SdCardDevice.h" #include #include #include diff --git a/Tactility/Source/file/FileLock.cpp b/Tactility/Source/file/FileLock.cpp index 3af36820b..17715cab8 100644 --- a/Tactility/Source/file/FileLock.cpp +++ b/Tactility/Source/file/FileLock.cpp @@ -1,7 +1,6 @@ #include "Tactility/file/FileLock.h" -#include -#include +#include namespace tt::file { diff --git a/Tactility/Source/hal/Hal.cpp b/Tactility/Source/hal/Hal.cpp index df7840ac3..7f7af1c07 100644 --- a/Tactility/Source/hal/Hal.cpp +++ b/Tactility/Source/hal/Hal.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include diff --git a/Tactility/Source/hal/sdcard/SdCard.cpp b/Tactility/Source/hal/sdcard/SdCard.cpp index 63aaa74bc..38339593d 100644 --- a/Tactility/Source/hal/sdcard/SdCard.cpp +++ b/Tactility/Source/hal/sdcard/SdCard.cpp @@ -1,12 +1,11 @@ -#include "Tactility/hal/sdcard/SdCardDevice.h" - #include #include +#include #include -#include #include +#include namespace tt::hal::sdcard { @@ -38,4 +37,14 @@ std::shared_ptr findSdCardLock(const std::string& path) { return ctx.result; } +void mountAll() { + device_for_each_of_type(&SDCARD_TYPE, nullptr, [](::Device* device, void*) -> bool { + if (!device_is_ready(device)) { + if (device_start(device) != ERROR_NONE) { + } + } + return true; + }); +} + } diff --git a/Tactility/Source/hal/sdcard/SdCardDevice.cpp b/Tactility/Source/hal/sdcard/SdCardDevice.cpp deleted file mode 100644 index 44fbd2843..000000000 --- a/Tactility/Source/hal/sdcard/SdCardDevice.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include - -#include - -#include - -namespace tt::hal::sdcard { - -static error_t mount(void* data) { - auto* device = static_cast(data); - auto path = device->getMountPath(); - if (!device->mount(path)) return ERROR_UNDEFINED; - return ERROR_NONE; -} - -static error_t unmount(void* data) { - auto* device = static_cast(data); - if (!device->unmount()) return ERROR_UNDEFINED; - return ERROR_NONE; -} - -static bool is_mounted(void* data) { - auto* device = static_cast(data); - return device->isMounted(); -} - -static error_t get_path(void* data, char* out_path, size_t out_path_size) { - auto* device = static_cast(data); - const auto mount_path = device->getMountPath(); - if (mount_path.size() >= out_path_size) return ERROR_BUFFER_OVERFLOW; - if (mount_path.empty()) return ERROR_INVALID_STATE; - strncpy(out_path, mount_path.c_str(), out_path_size); - return ERROR_NONE; -} - -FileSystemApi sdCardDeviceApi = { - .mount = mount, - .unmount = unmount, - .is_mounted = is_mounted, - .get_path = get_path -}; - -SdCardDevice::SdCardDevice(MountBehaviour mountBehaviour) : mountBehaviour(mountBehaviour) { - fileSystem = file_system_add(&sdCardDeviceApi, this); - check(fileSystem != nullptr); -} - -SdCardDevice::~SdCardDevice() { - file_system_remove(fileSystem); -} - -} diff --git a/Tactility/Source/hal/sdcard/SdCardMounting.cpp b/Tactility/Source/hal/sdcard/SdCardMounting.cpp deleted file mode 100644 index b7152117c..000000000 --- a/Tactility/Source/hal/sdcard/SdCardMounting.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include - -#include - -#include - -namespace tt::hal::sdcard { - -static const auto LOGGER = Logger("SdCardMounting"); -constexpr auto* TT_SDCARD_MOUNT_POINT = "/sdcard"; - -static void mount(const std::shared_ptr& sdcard, const std::string& path) { - LOGGER.info("Mounting sdcard at {}", path); - if (!sdcard->mount(path)) { - LOGGER.warn("SD card mount failed for {} (init can continue)", path); - } -} - -static std::string getMountPath(int index, int count) { - return (count == 1) ? TT_SDCARD_MOUNT_POINT : std::format("{}{}", TT_SDCARD_MOUNT_POINT, index); -} - -void mountAll() { - const auto sdcards = hal::findDevices(Device::Type::SdCard); - // Numbered mount path name - for (int i = 0; i < sdcards.size(); i++) { - auto sdcard = sdcards[i]; - if (!sdcard->isMounted() && sdcard->getMountBehaviour() == SdCardDevice::MountBehaviour::AtBoot) { - std::string mount_path = getMountPath(i, sdcards.size()); - mount(sdcard, mount_path); - } - } -} - -} diff --git a/Tactility/Source/service/webserver/WebServerService.cpp b/Tactility/Source/service/webserver/WebServerService.cpp index 95aeebb67..5675f689b 100644 --- a/Tactility/Source/service/webserver/WebServerService.cpp +++ b/Tactility/Source/service/webserver/WebServerService.cpp @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include diff --git a/Tactility/Source/service/wifi/WifiBootSplashInit.cpp b/Tactility/Source/service/wifi/WifiBootSplashInit.cpp index 86625684c..1f7fa261f 100644 --- a/Tactility/Source/service/wifi/WifiBootSplashInit.cpp +++ b/Tactility/Source/service/wifi/WifiBootSplashInit.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include #include diff --git a/Tactility/Source/settings/BootSettings.cpp b/Tactility/Source/settings/BootSettings.cpp index 29cdbcf42..061e35933 100644 --- a/Tactility/Source/settings/BootSettings.cpp +++ b/Tactility/Source/settings/BootSettings.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include diff --git a/TactilityKernel/include/tactility/drivers/sdcard.h b/TactilityKernel/include/tactility/drivers/sdcard.h new file mode 100644 index 000000000..6931f1683 --- /dev/null +++ b/TactilityKernel/include/tactility/drivers/sdcard.h @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +extern const struct DeviceType SDCARD_TYPE; + +#ifdef __cplusplus +} +#endif diff --git a/TactilityKernel/source/drivers/sdcard.cpp b/TactilityKernel/source/drivers/sdcard.cpp new file mode 100644 index 000000000..c49a8d78f --- /dev/null +++ b/TactilityKernel/source/drivers/sdcard.cpp @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: Apache-2.0 +#include + +extern "C" { + +const struct DeviceType SDCARD_TYPE { + .name = "sdcard" +}; + +}