Unified flash block management of internal flash and SPI flash
Overview
The flash block manager provides APIs for internal/external flash operation. The mapping of logical flash block and physical flash devices is defined in the custom device tree or overlay file. Flash blocks on multiple flash devices are identified by a unique label defined in partitions
node of device tree. The flash block operation is compatible with Oasis flash block API so that facilities of flash operation code can be ported easily.
const void* Addr(size_t pos, size_t len);
bool Erase();
bool Write( size_t pos, const void *buf, size_t len );
void Lock();
void Unlock();
Internal flash
The device driver of internal MCU flash is already well supported by Zephyr. The only work is to wrap flash API with Oasis flash block API. The internal flash is recognized as supporting memory-mapped access if CONFIG_FLASH_BASE_ADDRESS
and DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL
is detected, where Addr()
API would directly access memory space at [base + offset] rather than allocating buffer for flash read operation.
External flash (SPI)
The SPI flash driver needs to support JEDEC SPI NOR flash which is already supported in Zephyr. The device tree/overlay needs to define flash node as sub-node of SPI node. The compatible
name must be jedec,spi-nor
. The Microchip SST26 flash needs to define requires-ulbpr
attribute for Global Block Protection Unlock (0x98)
.
Recommended partitioning and label name
configuration
calibration
coredump-partition
nvs-0 / nvs-1
log-0 / log-1 / log-2 / log-3 / ...
fpga0-0 / fpga0-1
fpga1-0 / fpga1-1
image-0 / image-1
Core dump partition
The core dump feature requires a special device bindings property write-block-size
which is defined by most internal flash drivers, while this property is not supported by jedec,spi-nor
flash driver. In order to workaround this problem, a iivi,spi-nor
binding is created that derives from jedec,spi-nor
and includes the write-block-size
. Since most SPI flash devices support 256 bytes write, the default value of this property is set to 256.
Linux/Windows implementation
For non-Zephyr applications, flash blocks are mapped to files named by the same convention as label names in device tree. When application runs, files are loaded or created if they don't exist. These special files are treated as they were flash blocks on a flash device. More specifically,
- When offset of Write API exceeds file size,
\xFF
bytes would be inserted to fill the gap. - When offset of Read API exceeds file size,
\xFF
bytes is returned (without modifying the file).
Test command
-
ftest map
- Displays mapping of flash blocks including physical flash device information -
ftest <label> e
- Erase the flash block -
ftest <label> r [offset] [length]
- Read data from flash. Default values:[offset = 0] [length = CONFIG_FLASH_CACHE_MAX_SIZE]
-
ftest <label> w <offset> <count> <data>
- Write repeatedcount
ofdata
to flash -
ftest <label> x
- Download a file to flash block through Xmodem