This commit is contained in:
2026-02-26 14:08:17 +01:00
parent d48bc33530
commit b8b3e6ea44
9 changed files with 362 additions and 134 deletions

View File

@@ -1,10 +1,21 @@
#include <zephyr/fs/littlefs.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/pm/device.h>
#include <fs.h>
LOG_MODULE_REGISTER(buzz_fs, LOG_LEVEL_INF);
LOG_MODULE_REGISTER(buzz_fs, LOG_LEVEL_DBG);
#define STORAGE_PARTITION_ID FIXED_PARTITION_ID(littlefs_storage)
FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(fs_storage_data);
#define QSPI_FLASH_NODE DT_ALIAS(qspi_flash)
#if !DT_NODE_EXISTS(QSPI_FLASH_NODE)
#error "QSPI Flash alias not defined in devicetree"
#endif
static const struct device *flash_dev = DEVICE_DT_GET(QSPI_FLASH_NODE);
static volatile uint32_t open_count = 0;
static struct k_mutex flash_pm_lock;
static struct fs_mount_t fs_storage_mnt = {
.type = FS_LITTLEFS,
.fs_data = &fs_storage_data,
@@ -18,6 +29,90 @@ int fs_init(void) {
LOG_ERR("Error mounting filesystem: %d", rc);
return rc;
}
k_mutex_init(&flash_pm_lock);
LOG_DBG("Filesystem mounted successfully");
return 0;
}
}
int fs_pm_flash_suspend(void)
{
if (!device_is_ready(flash_dev)) {
return -ENODEV;
}
k_mutex_lock(&flash_pm_lock, K_FOREVER);
if (open_count > 0) {
open_count--;
if (open_count == 0) {
int rc = pm_device_action_run(flash_dev, PM_DEVICE_ACTION_SUSPEND);
if (rc < 0) {
LOG_WRN("Could not suspend flash: %d", rc);
} else {
LOG_DBG("Flash entered deep power-down");
}
}
}
k_mutex_unlock(&flash_pm_lock);
return 0;
}
int fs_pm_flash_resume(void)
{
if (!device_is_ready(flash_dev)) return -ENODEV;
k_mutex_lock(&flash_pm_lock, K_FOREVER);
if (open_count == 0) {
int rc = pm_device_action_run(flash_dev, PM_DEVICE_ACTION_RESUME);
if (rc == 0) {
k_busy_wait(50); // t-exit-dpd
LOG_DBG("Flash resumed");
}
}
open_count++;
k_mutex_unlock(&flash_pm_lock);
return 0;
}
int fs_pm_open(struct fs_file_t *file, const char *path, fs_mode_t mode)
{
int rc = fs_open(file, path, mode);
if (rc == 0)
{
fs_pm_flash_resume();
}
return rc;
}
int fs_pm_close(struct fs_file_t *file)
{
int rc = fs_close(file);
if (rc == 0)
{
fs_pm_flash_suspend();
}
return rc;
}
int fs_pm_opendir(struct fs_dir_t *dirp, const char *path)
{
int rc = fs_opendir(dirp, path);
if (rc == 0)
{
fs_pm_flash_resume();
}
return rc;
}
int fs_pm_closedir(struct fs_dir_t *dirp)
{
int rc = fs_closedir(dirp);
if (rc == 0)
{
fs_pm_flash_suspend();
}
return rc;
}