Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
flash.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 Nordic Semiconductor ASA
3 * Copyright (c) 2016 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
13#ifndef ZEPHYR_INCLUDE_DRIVERS_FLASH_H_
14#define ZEPHYR_INCLUDE_DRIVERS_FLASH_H_
15
23#include <zephyr/types.h>
24#include <stddef.h>
25#include <sys/types.h>
26#include <device.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#if defined(CONFIG_FLASH_PAGE_LAYOUT)
34 size_t pages_count; /* count of pages sequence of the same size */
35 size_t pages_size;
36};
37#endif /* CONFIG_FLASH_PAGE_LAYOUT */
38
56 const size_t write_block_size;
57 uint8_t erase_value; /* Byte value of erased flash */
58};
59
69typedef int (*flash_api_read)(const struct device *dev, off_t offset,
70 void *data,
71 size_t len);
80typedef int (*flash_api_write)(const struct device *dev, off_t offset,
81 const void *data, size_t len);
82
91typedef int (*flash_api_erase)(const struct device *dev, off_t offset,
92 size_t size);
93
94/* This API is deprecated and will be removed in Zephyr 2.8. */
95typedef int (*flash_api_write_protection)(const struct device *dev,
96 bool enable);
97typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
98
99#if defined(CONFIG_FLASH_PAGE_LAYOUT)
121typedef void (*flash_api_pages_layout)(const struct device *dev,
122 const struct flash_pages_layout **layout,
123 size_t *layout_size);
124#endif /* CONFIG_FLASH_PAGE_LAYOUT */
125
126typedef int (*flash_api_sfdp_read)(const struct device *dev, off_t offset,
127 void *data, size_t len);
128typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id);
129
130__subsystem struct flash_driver_api {
136#if defined(CONFIG_FLASH_PAGE_LAYOUT)
138#endif /* CONFIG_FLASH_PAGE_LAYOUT */
139#if defined(CONFIG_FLASH_JESD216_API)
142#endif /* CONFIG_FLASH_JESD216_API */
143};
144
167__syscall int flash_read(const struct device *dev, off_t offset, void *data,
168 size_t len);
169
170static inline int z_impl_flash_read(const struct device *dev, off_t offset,
171 void *data,
172 size_t len)
173{
174 const struct flash_driver_api *api =
175 (const struct flash_driver_api *)dev->api;
176
177 return api->read(dev, offset, data, len);
178}
179
198__syscall int flash_write(const struct device *dev, off_t offset,
199 const void *data,
200 size_t len);
201
202static inline int z_impl_flash_write(const struct device *dev, off_t offset,
203 const void *data, size_t len)
204{
205 const struct flash_driver_api *api =
206 (const struct flash_driver_api *)dev->api;
207 int rc;
208
209 /* write protection management in this function exists for keeping
210 * compatibility with out-of-tree drivers which are not aligned jet
211 * with write-protection API depreciation.
212 * This will be removed with flash_api_write_protection handler type.
213 */
214 if (api->write_protection != NULL) {
215 rc = api->write_protection(dev, false);
216 if (rc) {
217 return rc;
218 }
219 }
220
221 rc = api->write(dev, offset, data, len);
222
223 if (api->write_protection != NULL) {
224 (void) api->write_protection(dev, true);
225 }
226
227 return rc;
228}
229
251__syscall int flash_erase(const struct device *dev, off_t offset, size_t size);
252
253static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
254 size_t size)
255{
256 const struct flash_driver_api *api =
257 (const struct flash_driver_api *)dev->api;
258 int rc;
259
260 /* write protection management in this function exists for keeping
261 * compatibility with out-of-tree drivers which are not aligned jet
262 * with write-protection API depreciation.
263 * This will be removed with flash_api_write_protection handler type.
264 */
265 if (api->write_protection != NULL) {
266 rc = api->write_protection(dev, false);
267 if (rc) {
268 return rc;
269 }
270 }
271
272 rc = api->erase(dev, offset, size);
273
274 if (api->write_protection != NULL) {
275 (void) api->write_protection(dev, true);
276 }
277
278 return rc;
279}
280
297__deprecated
298__syscall int flash_write_protection_set(const struct device *dev,
299 bool enable);
300
301static inline int z_impl_flash_write_protection_set(const struct device *dev,
302 bool enable)
303{
304 ARG_UNUSED(dev);
305 ARG_UNUSED(enable);
306
307 return 0;
308}
309
311 off_t start_offset; /* offset from the base of flash address */
312 size_t size;
314};
315
316#if defined(CONFIG_FLASH_PAGE_LAYOUT)
326__syscall int flash_get_page_info_by_offs(const struct device *dev,
327 off_t offset,
328 struct flash_pages_info *info);
329
339__syscall int flash_get_page_info_by_idx(const struct device *dev,
340 uint32_t page_index,
341 struct flash_pages_info *info);
342
350__syscall size_t flash_get_page_count(const struct device *dev);
351
362typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
363
376void flash_page_foreach(const struct device *dev, flash_page_cb cb,
377 void *data);
378#endif /* CONFIG_FLASH_PAGE_LAYOUT */
379
380#if defined(CONFIG_FLASH_JESD216_API)
401__syscall int flash_sfdp_read(const struct device *dev, off_t offset,
402 void *data, size_t len);
403
404static inline int z_impl_flash_sfdp_read(const struct device *dev,
405 off_t offset,
406 void *data, size_t len)
407{
408 int rv = -ENOTSUP;
409 const struct flash_driver_api *api =
410 (const struct flash_driver_api *)dev->api;
411
412 if (api->sfdp_read != NULL) {
413 rv = api->sfdp_read(dev, offset, data, len);
414 }
415 return rv;
416}
417
429__syscall int flash_read_jedec_id(const struct device *dev, uint8_t *id);
430
431static inline int z_impl_flash_read_jedec_id(const struct device *dev,
432 uint8_t *id)
433{
434 int rv = -ENOTSUP;
435 const struct flash_driver_api *api =
436 (const struct flash_driver_api *)dev->api;
437
438 if (api->read_jedec_id != NULL) {
439 rv = api->read_jedec_id(dev, id);
440 }
441 return rv;
442}
443#endif /* CONFIG_FLASH_JESD216_API */
444
456__syscall size_t flash_get_write_block_size(const struct device *dev);
457
458static inline size_t z_impl_flash_get_write_block_size(const struct device *dev)
459{
460 const struct flash_driver_api *api =
461 (const struct flash_driver_api *)dev->api;
462
463 return api->get_parameters(dev)->write_block_size;
464}
465
466
478__syscall const struct flash_parameters *flash_get_parameters(const struct device *dev);
479
480static inline const struct flash_parameters *z_impl_flash_get_parameters(const struct device *dev)
481{
482 const struct flash_driver_api *api =
483 (const struct flash_driver_api *)dev->api;
484
485 return api->get_parameters(dev);
486}
487
488#ifdef __cplusplus
489}
490#endif
491
496#include <syscalls/flash.h>
497
498#endif /* ZEPHYR_INCLUDE_DRIVERS_FLASH_H_ */
void
Definition: eswifi_shell.c:15
volatile int rv
Definition: main.c:45
int flash_erase(const struct device *dev, off_t offset, size_t size)
Erase part or all of a flash memory.
const struct flash_parameters * flash_get_parameters(const struct device *dev)
Get pointer to flash_parameters structure.
void flash_page_foreach(const struct device *dev, flash_page_cb cb, void *data)
Iterate over all flash pages on a device.
bool(* flash_page_cb)(const struct flash_pages_info *info, void *data)
Callback type for iterating over flash pages present on a device.
Definition: flash.h:362
int flash_write(const struct device *dev, off_t offset, const void *data, size_t len)
Write buffer into flash memory.
int flash_sfdp_read(const struct device *dev, off_t offset, void *data, size_t len)
Read data from Serial Flash Discoverable Parameters.
int flash_read(const struct device *dev, off_t offset, void *data, size_t len)
Read data from flash.
size_t flash_get_write_block_size(const struct device *dev)
Get the minimum write block size supported by the driver.
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.
int flash_read_jedec_id(const struct device *dev, uint8_t *id)
Read the JEDEC ID from a compatible flash device.
int flash_write_protection_set(const struct device *dev, bool enable)
Enable or disable write protection for a flash memory.
size_t flash_get_page_count(const struct device *dev)
Get the total number of flash pages.
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.
int(* flash_api_read_jedec_id)(const struct device *dev, uint8_t *id)
Definition: flash.h:128
int(* flash_api_erase)(const struct device *dev, off_t offset, size_t size)
Flash erase implementation handler type.
Definition: flash.h:91
const struct flash_parameters *(* flash_api_get_parameters)(const struct device *dev)
Definition: flash.h:97
int(* flash_api_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition: flash.h:69
int(* flash_api_write_protection)(const struct device *dev, bool enable)
Definition: flash.h:95
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.
Definition: flash.h:121
int(* flash_api_sfdp_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition: flash.h:126
int(* flash_api_write)(const struct device *dev, off_t offset, const void *data, size_t len)
Flash write implementation handler type.
Definition: flash.h:80
#define ENOTSUP
Definition: errno.h:115
#define bool
Definition: stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
Runtime device structure (in ROM) per driver instance.
Definition: device.h:367
const void * api
Definition: device.h:373
Definition: flash.h:130
flash_api_sfdp_read sfdp_read
Definition: flash.h:140
flash_api_get_parameters get_parameters
Definition: flash.h:135
flash_api_pages_layout page_layout
Definition: flash.h:137
flash_api_read read
Definition: flash.h:131
flash_api_write write
Definition: flash.h:132
flash_api_write_protection write_protection
Definition: flash.h:134
flash_api_erase erase
Definition: flash.h:133
flash_api_read_jedec_id read_jedec_id
Definition: flash.h:141
Definition: flash.h:310
size_t size
Definition: flash.h:312
off_t start_offset
Definition: flash.h:311
uint32_t index
Definition: flash.h:313
Definition: flash.h:33
size_t pages_size
Definition: flash.h:35
size_t pages_count
Definition: flash.h:34
Definition: flash.h:55
uint8_t erase_value
Definition: flash.h:57
const size_t write_block_size
Definition: flash.h:56
static fdata_t data[2]
Definition: test_fifo_contexts.c:15