Skip to content

Commit

Permalink
Updating external flash sample
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwolff committed Sep 26, 2024
1 parent 106db85 commit 04b496e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@
full-ohms = <(100000 + 100000)>;
power-gpios = <&gpio0 25 GPIO_ACTIVE_HIGH>;
};

fstab {
compatible = "zephyr,fstab";
lfs: lfs {
compatible = "zephyr,fstab,littlefs";
mount-point = "/lfs";
partition = <&lfs_partition>;
read-size = <16>;
prog-size = <16>;
cache-size = <64>;
lookahead-size = <32>;
block-cycles = <512>;
automount;
};
};
};

&adc {
Expand Down Expand Up @@ -176,6 +191,16 @@
t-enter-dpd = <30000>;
t-exit-dpd = <30000>;
jedec-id = [ ef 40 16 ];
partitions {
compatible = "fixed-partitions";
#address-cells = <1>;
#size-cells = <1>;

lfs_partition: partition@0 {
label = "lfs_partition";
reg = <0x00000000 DT_SIZE_M(4)>;
};
};
};
};

Expand Down
10 changes: 10 additions & 0 deletions samples/external_flash/pm_static.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
external_flash:
address: 0x0
end_address: 0x400000
region: external_flash
size: 0x400000
littlefs_storage:
address: 0x0
device: W25Q32JV
region: external_flash
size: 0x400000
3 changes: 1 addition & 2 deletions samples/external_flash/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CONFIG_MAIN_STACK_SIZE=2048
CONFIG_SPI=y
CONFIG_SPI_NOR=y
CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
CONFIG_SPI_NOR_SFDP_RUNTIME=y

# Enable flash operations.
CONFIG_FLASH=y
Expand All @@ -24,6 +25,4 @@ CONFIG_LOG_MODE_IMMEDIATE=y

# Partition manager settings
CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=n
CONFIG_PM_PARTITION_REGION_LITTLEFS_EXTERNAL=y
CONFIG_PM_PARTITION_SIZE_LITTLEFS=0x400000
CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y
69 changes: 24 additions & 45 deletions samples/external_flash/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,38 @@ LOG_MODULE_REGISTER(main);
/* Matches LFS_NAME_MAX */
#define MAX_PATH_LEN 255

FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(storage);
static struct fs_mount_t lfs_storage_mnt = {
.type = FS_LITTLEFS,
.fs_data = &storage,
.storage_dev = (void *)FLASH_AREA_ID(littlefs_storage),
.mnt_point = "/lfs",
};

static int nor_storage_erase(void)
#define PARTITION_NODE DT_NODELABEL(lfs)
FS_FSTAB_DECLARE_ENTRY(PARTITION_NODE);

struct fs_mount_t *mp =
&FS_FSTAB_ENTRY(PARTITION_NODE);

static int littlefs_flash_erase(unsigned int id)
{
int err;
struct fs_mount_t *mp = &lfs_storage_mnt;
unsigned int id = (uintptr_t)mp->storage_dev;
const struct flash_area *pfa;
int rc;

/* Unmount if mounted */
fs_unmount(&lfs_storage_mnt);

err = flash_area_open(id, &pfa);
if (err < 0)
rc = flash_area_open(id, &pfa);
if (rc < 0)
{
LOG_ERR("FAIL: unable to find flash area %u: %d",
id, err);
return err;
LOG_ERR("FAIL: unable to find flash area %u: %d\n",
id, rc);
return rc;
}

LOG_INF("Area %u at 0x%x for %u bytes",
id, (unsigned int)pfa->fa_off,
(unsigned int)pfa->fa_size);
LOG_PRINTK("Area %u at 0x%x on %s for %u bytes\n",
id, (unsigned int)pfa->fa_off, pfa->fa_dev->name,
(unsigned int)pfa->fa_size);

LOG_INF("Erasing flash area ... ");
err = flash_area_erase(pfa, 0, pfa->fa_size);
LOG_INF("Done. Code: %i", err);

if (err)
/* Optional wipe flash contents */
if (IS_ENABLED(CONFIG_APP_WIPE_STORAGE))
{
LOG_ERR("Failed to erase storage. (err: %d)", err);
return err;
rc = flash_area_erase(pfa, 0, pfa->fa_size);
LOG_ERR("Erasing flash area ... %d", rc);
}

flash_area_close(pfa);

return 0;
return rc;
}

int nor_storage_init(void)
Expand All @@ -75,12 +64,7 @@ int nor_storage_init(void)
struct fs_dirent dirent;
struct fs_statvfs sbuf;

/* Mount filesystem */
err = fs_mount(&lfs_storage_mnt);
if (err)
return err;

err = fs_statvfs(lfs_storage_mnt.mnt_point, &sbuf);
err = fs_statvfs(mp->mnt_point, &sbuf);
if (err < 0)
LOG_ERR("statvfs: %d", err);

Expand All @@ -94,15 +78,10 @@ int nor_storage_init(void)
{
LOG_INF("Erasing storage!");

err = nor_storage_erase();
err = littlefs_flash_erase((uintptr_t)mp->storage_dev);
if (err < 0)
return err;

/* Re-mount */
err = fs_mount(&lfs_storage_mnt);
if (err)
return err;

/* Create folder */
err = fs_mkdir(NOR_STORAGE_ERASED_ON_BOOT);
if (err)
Expand Down Expand Up @@ -167,7 +146,7 @@ int main(void)
{
int err;

LOG_INF("External Flash on %s\n", CONFIG_BOARD);
LOG_INF("External Flash on %s", CONFIG_BOARD);

err = nor_storage_init();
if (err < 0)
Expand Down

0 comments on commit 04b496e

Please sign in to comment.