SD card improvements (#214)

- Implement SD card locking logic and helper functions
- Fix issue with running ELF apps from SD card: this would crash when launched from the AppList
- Reduce Boot app wait time to 1 second
- Speed up boot by about 0.1 second by moving app&service registration to the Boot app
- Files app now uses proper SD card mount point name (and multiple SD cards)
- Removed `TT_SCREENSHOT_MODE`
This commit is contained in:
Ken Van Hoeylandt
2025-02-09 12:01:01 +01:00
committed by GitHub
parent fd1e31dec4
commit a7a3b17ff6
15 changed files with 128 additions and 56 deletions
@@ -5,6 +5,7 @@
#include <ranges>
#include <string>
#include <vector>
#include <cassert>
namespace tt::hal {
@@ -6,9 +6,6 @@
namespace tt::hal {
#define TT_SDCARD_MOUNT_NAME "sdcard"
#define TT_SDCARD_MOUNT_POINT "/sdcard"
class SdCard : public Device {
public:
@@ -36,12 +33,35 @@ public:
Type getType() const final { return Type::SdCard; };
virtual bool mount(const char* mountPath) = 0;
virtual bool mount(const std::string& mountPath) = 0;
virtual bool unmount() = 0;
virtual State getState() const = 0;
/** Return empty string when not mounted or the mount path if mounted */
virtual std::string getMountPath() const = 0;
virtual std::shared_ptr<Lockable> getLockable() const = 0;
virtual MountBehaviour getMountBehaviour() const { return mountBehaviour; }
bool isMounted() const { return getState() == State::Mounted; }
};
} // namespace
/** Return the SdCard device if the path is within the SdCard mounted path (path std::string::starts_with() check)*/
std::shared_ptr<SdCard> _Nullable findSdCard(const std::string& path);
/**
* Acquires an SD card lock if the path is an SD card path.
* Always calls the function, but doesn't lock if the path is not an SD card path.
*/
template<typename ReturnType>
inline ReturnType withSdCardLock(const std::string& path, std::function<ReturnType()> fn) {
auto sdcard = hal::findSdCard(path);
std::unique_ptr<ScopedLockableUsage> scoped_lockable;
if (sdcard != nullptr) {
scoped_lockable = sdcard->getLockable()->scoped();
scoped_lockable->lock(portMAX_DELAY);
}
return fn();
}
} // namespace tt::hal
@@ -15,7 +15,7 @@ namespace tt::hal {
/**
* SD card interface at the default SPI interface
*/
class SpiSdCard : public tt::hal::SdCard {
class SpiSdCard final : public tt::hal::SdCard {
public:
struct Config {
Config(
@@ -24,7 +24,7 @@ public:
gpio_num_t spiPinWp,
gpio_num_t spiPinInt,
MountBehaviour mountBehaviourAtBoot,
std::shared_ptr<Lockable> lockable = nullptr,
std::shared_ptr<Lockable> lockable = std::make_shared<Mutex>(),
std::vector<gpio_num_t> csPinWorkAround = std::vector<gpio_num_t>(),
spi_host_device_t spiHost = SPI2_HOST,
int spiFrequencyKhz = SDMMC_FREQ_DEFAULT
@@ -37,7 +37,9 @@ public:
lockable(std::move(lockable)),
csPinWorkAround(std::move(csPinWorkAround)),
spiHost(spiHost)
{}
{
assert(this->lockable != nullptr);
}
int spiFrequencyKhz;
gpio_num_t spiPinCs; // Clock
@@ -56,12 +58,12 @@ public:
private:
std::string mountPoint;
std::string mountPath;
sdmmc_card_t* card = nullptr;
std::shared_ptr<Config> config;
bool applyGpioWorkAround();
bool mountInternal(const char* mount_point);
bool mountInternal(const std::string& mountPath);
public:
@@ -73,8 +75,12 @@ public:
std::string getName() const final { return "SD Card"; }
std::string getDescription() const final { return "SD card via SPI interface"; }
bool mount(const char* mountPath) override;
bool unmount() override;
bool mount(const std::string& mountPath) final;
bool unmount() final;
std::string getMountPath() const final { return mountPath; }
std::shared_ptr<Lockable> getLockable() const final { return config->lockable; }
State getState() const override;
sdmmc_card_t* _Nullable getCard() { return card; }
+2
View File
@@ -9,6 +9,8 @@
#define TAG "hal"
#define TT_SDCARD_MOUNT_POINT "/sdcard"
namespace tt::hal {
void init(const Configuration& configuration) {
+17
View File
@@ -0,0 +1,17 @@
#include "Tactility/hal/SdCard.h"
#include "Tactility/hal/Device.h"
namespace tt::hal {
std::shared_ptr<SdCard> _Nullable findSdCard(const std::string& path) {
auto sdcards = findDevices<SdCard>(Device::Type::SdCard);
for (auto& sdcard : sdcards) {
if (sdcard->isMounted() && path.starts_with(sdcard->getMountPath())) {
return sdcard;
}
}
return nullptr;
}
}
+10 -10
View File
@@ -50,8 +50,8 @@ bool SpiSdCard::applyGpioWorkAround() {
return true;
}
bool SpiSdCard::mountInternal(const char* mountPoint) {
TT_LOG_I(TAG, "Mounting %s", mountPoint);
bool SpiSdCard::mountInternal(const std::string& newMountPath) {
TT_LOG_I(TAG, "Mounting %s", newMountPath.c_str());
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = config->formatOnMountFailed,
@@ -76,7 +76,7 @@ bool SpiSdCard::mountInternal(const char* mountPoint) {
host.max_freq_khz = config->spiFrequencyKhz;
host.slot = config->spiHost;
esp_err_t result = esp_vfs_fat_sdspi_mount(mountPoint, &host, &slot_config, &mount_config, &card);
esp_err_t result = esp_vfs_fat_sdspi_mount(newMountPath.c_str(), &host, &slot_config, &mount_config, &card);
if (result != ESP_OK) {
if (result == ESP_FAIL) {
@@ -87,22 +87,22 @@ bool SpiSdCard::mountInternal(const char* mountPoint) {
return false;
}
this->mountPoint = mountPoint;
mountPath = newMountPath;
return true;
}
bool SpiSdCard::mount(const char* mount_point) {
bool SpiSdCard::mount(const std::string& newMountPath) {
if (!applyGpioWorkAround()) {
TT_LOG_E(TAG, "Failed to set SPI CS pins high. This is a pre-requisite for mounting.");
return false;
}
if (mountInternal(mount_point)) {
if (mountInternal(newMountPath)) {
sdmmc_card_print_info(stdout, card);
return true;
} else {
TT_LOG_E(TAG, "Mount failed for %s", mount_point);
TT_LOG_E(TAG, "Mount failed for %s", newMountPath.c_str());
return false;
}
}
@@ -113,12 +113,12 @@ bool SpiSdCard::unmount() {
return false;
}
if (esp_vfs_fat_sdcard_unmount(mountPoint.c_str(), card) == ESP_OK) {
mountPoint = "";
if (esp_vfs_fat_sdcard_unmount(mountPath.c_str(), card) == ESP_OK) {
mountPath = "";
card = nullptr;
return true;
} else {
TT_LOG_E(TAG, "Unmount failed for %s", mountPoint.c_str());
TT_LOG_E(TAG, "Unmount failed for %s", mountPath.c_str());
return false;
}
}