Flash¶
Overview¶
Flash offset concept
Offsets used by the user API are expressed in relation to the flash memory beginning address. This rule shall be applied to all flash controller regular memory that layout is accessible via API for retrieving the layout of pages (see option:CONFIG_FLASH_PAGE_LAYOUT).
An exception from the rule may be applied to a vendor-specific flash dedicated-purpose region (such a region obviously can’t be covered under API for retrieving the layout of pages).
User API Reference¶
-
group
flash_interface
FLASH Interface.
Typedefs
-
typedef bool (*
flash_page_cb
)(const struct flash_pages_info *info, void *data)¶ Callback type for iterating over flash pages present on a device.
The callback should return true to continue iterating, and false to halt.
- Return
True to continue iteration, false to halt iteration.
- See
- Parameters
info
: Information for current pagedata
: Private data for callback
Functions
-
int
flash_read
(const struct device *dev, off_t offset, void *data, size_t len)¶ Read data from flash.
All flash drivers support reads without alignment restrictions on the read offset, the read size, or the destination address.
- Return
0 on success, negative errno code on fail.
- Parameters
dev
: : flash devoffset
: : Offset (byte aligned) to readdata
: : Buffer to store read datalen
: : Number of bytes to read.
-
int
flash_write
(const struct device *dev, off_t offset, const void *data, size_t len)¶ Write buffer into flash memory.
All flash drivers support a source buffer located either in RAM or SoC flash, without alignment restrictions on the source address. Write size and offset must be multiples of the minimum write block size supported by the driver.
Prior to the invocation of this API, the flash_write_protection_set needs to be called first to disable the write protection.
- Return
0 on success, negative errno code on fail.
- Parameters
dev
: : flash deviceoffset
: : starting offset for the writedata
: : data to writelen
: : Number of bytes to write
-
int
flash_erase
(const struct device *dev, off_t offset, size_t size)¶ Erase part or all of a flash memory.
Acceptable values of erase size and offset are subject to hardware-specific multiples of page size and offset. Please check the API implemented by the underlying sub driver, for example by using flash_get_page_info_by_offs() if that is supported by your flash driver.
Prior to the invocation of this API, the flash_write_protection_set needs to be called first to disable the write protection.
- Return
0 on success, negative errno code on fail.
- See
- See
- Parameters
dev
: : flash deviceoffset
: : erase area starting offsetsize
: : size of area to be erased
-
int
flash_write_protection_set
(const struct device *dev, bool enable)¶ Enable or disable write protection for a flash memory.
This API is required to be called before the invocation of write or erase API. Any calls to flash_write() or flash_erase() that do not first disable write protection using this function result in undefined behavior. Usage Example:
flash_write_protection_set(flash_dev, false); flash_erase(flash_dev, page_offset, page_size); flash_write_protection_set(flash_dev, false); flash_write(flash_dev, offset, data, sizeof(data)); flash_write_protection_set(flash_dev, true); // enable is recommended
Please note that on some flash components, the write protection is automatically turned on again by the device after the completion of each call to flash_write or flash_erase(). Therefore, portable programs must disable write protection using this function before each call to flash_erase() or flash_write().
For some flash devices, this function may implement a no-operation, as some flash hardware does not support write protection, or may not support it in a manner that is compatible with this API. For these drivers, this function always returns success.
- Return
0 on success, negative errno code on fail.
- Parameters
dev
: : flash deviceenable
: : enable or disable flash write protection
-
int
flash_get_page_info_by_offs
(const struct device *dev, off_t offset, struct flash_pages_info *info)¶ Get the size and start offset of flash page at certain flash offset.
- Return
0 on success, -EINVAL if page of the offset doesn’t exist.
- Parameters
dev
: flash deviceoffset
: Offset within the pageinfo
: Page Info structure to be filled
-
int
flash_get_page_info_by_idx
(const struct device *dev, uint32_t page_index, struct flash_pages_info *info)¶ Get the size and start offset of flash page of certain index.
- Return
0 on success, -EINVAL if page of the index doesn’t exist.
- Parameters
dev
: flash devicepage_index
: Index of the page. Index are counted from 0.info
: Page Info structure to be filled
-
size_t
flash_get_page_count
(const struct device *dev)¶ Get the total number of flash pages.
- Return
Number of flash pages.
- Parameters
dev
: flash device
-
void
flash_page_foreach
(const struct device *dev, flash_page_cb cb, void *data)¶ Iterate over all flash pages on a device.
This routine iterates over all flash pages on the given device, ordered by increasing start offset. For each page, it invokes the given callback, passing it the page’s information and a private data object.
- Parameters
dev
: Device whose pages to iterate overcb
: Callback to invoke for each flash pagedata
: Private data for callback function
-
int
flash_sfdp_read
(const struct device *dev, off_t offset, void *data, size_t len)¶ Read data from Serial Flash Discoverable Parameters.
This routine reads data from a serial flash device compatible with the JEDEC JESD216 standard for encoding flash memory characteristics.
Availability of this API is conditional on selecting
CONFIG_FLASH_JESD216_API
and support of that functionality in the driver underlyingdev
.- Parameters
dev
: device from which parameters will be readoffset
: address within the SFDP region containing data of interestdata
: where the data to be read will be placedlen
: the number of bytes of data to be read
- Return Value
0
: on success-ENOTSUP
: if the flash driver does not support SFDP accessnegative
: values for other errors.
-
int
flash_read_jedec_id
(const struct device *dev, uint8_t *id)¶ Read the JEDEC ID from a compatible flash device.
- Parameters
dev
: device from which id will be readid
: pointer to a buffer of at least 3 bytes into which id will be stored
- Return Value
0
: on successful store of 3-byte JEDEC id-ENOTSUP
: if flash driver doesn’t support this functionnegative
: values for other errors
-
size_t
flash_get_write_block_size
(const struct device *dev)¶ Get the minimum write block size supported by the driver.
The write block size supported by the driver might differ from the write block size of memory used because the driver might implements write-modify algorithm.
- Return
write block size in bytes.
- Parameters
dev
: flash device
-
const struct flash_parameters *
flash_get_parameters
(const struct device *dev)¶ Get pointer to flash_parameters structure.
Returned pointer points to a structure that should be considered constant through a runtime, regardless if it is defined in RAM or Flash. Developer is free to cache the structure pointer or copy its contents.
- Return
pointer to flash_parameters structure characteristic for the device.
-
struct
flash_parameters
¶ - #include <flash.h>
Flash memory parameters. Contents of this structure suppose to be filled in during flash device initialization and stay constant through a runtime.
-
struct
flash_pages_info
¶ - #include <flash.h>
-
typedef bool (*
Implementation interface API Reference¶
-
group
flash_internal_interface
FLASH internal Interface.
Typedefs
-
typedef int (*
flash_api_write
)(const struct device *dev, off_t offset, const void *data, size_t len)¶
-
typedef const struct flash_parameters *(*
flash_api_get_parameters
)(const struct device *dev)¶
-
typedef void (*
flash_api_pages_layout
)(const struct device *dev, const struct flash_pages_layout **layout, size_t *layout_size)¶ Retrieve a flash device’s layout.
A flash device layout is a run-length encoded description of the pages on the device. (Here, “page” means the smallest erasable area on the flash device.)
For flash memories which have uniform page sizes, this routine returns an array of length 1, which specifies the page size and number of pages in the memory.
Layouts for flash memories with nonuniform page sizes will be returned as an array with multiple elements, each of which describes a group of pages that all have the same size. In this case, the sequence of array elements specifies the order in which these groups occur on the device.
- Parameters
dev
: Flash device whose layout to retrieve.layout
: The flash layout will be returned in this argument.layout_size
: The number of elements in the returned layout.
-
struct
flash_pages_layout
¶ - #include <flash.h>
-
struct
flash_driver_api
¶ - #include <flash.h>
-
typedef int (*