Zephyr API Documentation  3.0.0
A Scalable Open Source RTOS
3.0.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
94typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
95
96#if defined(CONFIG_FLASH_PAGE_LAYOUT)
118typedef void (*flash_api_pages_layout)(const struct device *dev,
119 const struct flash_pages_layout **layout,
120 size_t *layout_size);
121#endif /* CONFIG_FLASH_PAGE_LAYOUT */
122
123typedef int (*flash_api_sfdp_read)(const struct device *dev, off_t offset,
124 void *data, size_t len);
125typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id);
126
127__subsystem struct flash_driver_api {
132#if defined(CONFIG_FLASH_PAGE_LAYOUT)
134#endif /* CONFIG_FLASH_PAGE_LAYOUT */
135#if defined(CONFIG_FLASH_JESD216_API)
138#endif /* CONFIG_FLASH_JESD216_API */
139};
140
163__syscall int flash_read(const struct device *dev, off_t offset, void *data,
164 size_t len);
165
166static inline int z_impl_flash_read(const struct device *dev, off_t offset,
167 void *data,
168 size_t len)
169{
170 const struct flash_driver_api *api =
171 (const struct flash_driver_api *)dev->api;
172
173 return api->read(dev, offset, data, len);
174}
175
194__syscall int flash_write(const struct device *dev, off_t offset,
195 const void *data,
196 size_t len);
197
198static inline int z_impl_flash_write(const struct device *dev, off_t offset,
199 const void *data, size_t len)
200{
201 const struct flash_driver_api *api =
202 (const struct flash_driver_api *)dev->api;
203 int rc;
204
205 rc = api->write(dev, offset, data, len);
206
207 return rc;
208}
209
231__syscall int flash_erase(const struct device *dev, off_t offset, size_t size);
232
233static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
234 size_t size)
235{
236 const struct flash_driver_api *api =
237 (const struct flash_driver_api *)dev->api;
238 int rc;
239
240 rc = api->erase(dev, offset, size);
241
242 return rc;
243}
244
246 off_t start_offset; /* offset from the base of flash address */
247 size_t size;
249};
250
251#if defined(CONFIG_FLASH_PAGE_LAYOUT)
261__syscall int flash_get_page_info_by_offs(const struct device *dev,
262 off_t offset,
263 struct flash_pages_info *info);
264
274__syscall int flash_get_page_info_by_idx(const struct device *dev,
275 uint32_t page_index,
276 struct flash_pages_info *info);
277
285__syscall size_t flash_get_page_count(const struct device *dev);
286
297typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
298
311void flash_page_foreach(const struct device *dev, flash_page_cb cb,
312 void *data);
313#endif /* CONFIG_FLASH_PAGE_LAYOUT */
314
315#if defined(CONFIG_FLASH_JESD216_API)
336__syscall int flash_sfdp_read(const struct device *dev, off_t offset,
337 void *data, size_t len);
338
339static inline int z_impl_flash_sfdp_read(const struct device *dev,
340 off_t offset,
341 void *data, size_t len)
342{
343 int rv = -ENOTSUP;
344 const struct flash_driver_api *api =
345 (const struct flash_driver_api *)dev->api;
346
347 if (api->sfdp_read != NULL) {
348 rv = api->sfdp_read(dev, offset, data, len);
349 }
350 return rv;
351}
352
364__syscall int flash_read_jedec_id(const struct device *dev, uint8_t *id);
365
366static inline int z_impl_flash_read_jedec_id(const struct device *dev,
367 uint8_t *id)
368{
369 int rv = -ENOTSUP;
370 const struct flash_driver_api *api =
371 (const struct flash_driver_api *)dev->api;
372
373 if (api->read_jedec_id != NULL) {
374 rv = api->read_jedec_id(dev, id);
375 }
376 return rv;
377}
378#endif /* CONFIG_FLASH_JESD216_API */
379
391__syscall size_t flash_get_write_block_size(const struct device *dev);
392
393static inline size_t z_impl_flash_get_write_block_size(const struct device *dev)
394{
395 const struct flash_driver_api *api =
396 (const struct flash_driver_api *)dev->api;
397
398 return api->get_parameters(dev)->write_block_size;
399}
400
401
413__syscall const struct flash_parameters *flash_get_parameters(const struct device *dev);
414
415static inline const struct flash_parameters *z_impl_flash_get_parameters(const struct device *dev)
416{
417 const struct flash_driver_api *api =
418 (const struct flash_driver_api *)dev->api;
419
420 return api->get_parameters(dev);
421}
422
423#ifdef __cplusplus
424}
425#endif
426
431#include <syscalls/flash.h>
432
433#endif /* ZEPHYR_INCLUDE_DRIVERS_FLASH_H_ */
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:297
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.
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:125
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:94
int(* flash_api_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition: flash.h:69
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:118
int(* flash_api_sfdp_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition: flash.h:123
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:450
const void * api
Definition: device.h:456
Definition: flash.h:127
flash_api_sfdp_read sfdp_read
Definition: flash.h:136
flash_api_get_parameters get_parameters
Definition: flash.h:131
flash_api_pages_layout page_layout
Definition: flash.h:133
flash_api_read read
Definition: flash.h:128
flash_api_write write
Definition: flash.h:129
flash_api_erase erase
Definition: flash.h:130
flash_api_read_jedec_id read_jedec_id
Definition: flash.h:137
Definition: flash.h:245
size_t size
Definition: flash.h:247
off_t start_offset
Definition: flash.h:246
uint32_t index
Definition: flash.h:248
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