Skip to content

Commit a85f733

Browse files
pdgendtnashif
authored andcommitted
nvmem: Add flash support
Allow flash devices to be accessed using the NVMEM API. Note that it simply uses the read/write API functions. Erasing should be handled by the application. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent e784a03 commit a85f733

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

subsys/nvmem/Kconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ config NVMEM_EEPROM
1111
default y
1212
depends on EEPROM
1313

14+
config NVMEM_FLASH
15+
bool "NVMEM (read) support for flash devices"
16+
default y
17+
depends on FLASH
18+
19+
config NVMEM_FLASH_WRITE
20+
bool "NVMEM write support for flash devices"
21+
depends on NVMEM_FLASH
22+
help
23+
Allow writing to NVMEM cells in flash devices.
24+
25+
An explicit option is added for this as it's non-trivial
26+
to write to flash devices requiring out-of-bands erase or
27+
taking write-block sizes into account.
28+
1429
module = NVMEM
1530
module-str = nvmem
1631
source "subsys/logging/Kconfig.template.log_config"

subsys/nvmem/nvmem.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <errno.h>
88
#include <zephyr/drivers/eeprom.h>
9+
#include <zephyr/drivers/flash.h>
910
#include <zephyr/nvmem.h>
1011
#include <zephyr/sys/__assert.h>
1112

@@ -21,6 +22,10 @@ int nvmem_cell_read(const struct nvmem_cell *cell, void *buf, off_t off, size_t
2122
return eeprom_read(cell->dev, cell->offset + off, buf, len);
2223
}
2324

25+
if (IS_ENABLED(CONFIG_NVMEM_FLASH) && DEVICE_API_IS(flash, cell->dev)) {
26+
return flash_read(cell->dev, cell->offset + off, buf, len);
27+
}
28+
2429
return -ENXIO;
2530
}
2631

@@ -40,5 +45,9 @@ int nvmem_cell_write(const struct nvmem_cell *cell, const void *buf, off_t off,
4045
return eeprom_write(cell->dev, cell->offset + off, buf, len);
4146
}
4247

48+
if (IS_ENABLED(CONFIG_NVMEM_FLASH_WRITE) && DEVICE_API_IS(flash, cell->dev)) {
49+
return flash_write(cell->dev, cell->offset + off, buf, len);
50+
}
51+
4352
return -ENXIO;
4453
}

0 commit comments

Comments
 (0)