Zephyr Project API  3.3.0
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 <errno.h>
24
25#include <zephyr/types.h>
26#include <stddef.h>
27#include <sys/types.h>
28#include <zephyr/device.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#if defined(CONFIG_FLASH_PAGE_LAYOUT)
36 size_t pages_count; /* count of pages sequence of the same size */
37 size_t pages_size;
38};
39#endif /* CONFIG_FLASH_PAGE_LAYOUT */
40
58 const size_t write_block_size;
59 uint8_t erase_value; /* Byte value of erased flash */
60};
61
71typedef int (*flash_api_read)(const struct device *dev, off_t offset,
72 void *data,
73 size_t len);
82typedef int (*flash_api_write)(const struct device *dev, off_t offset,
83 const void *data, size_t len);
84
93typedef int (*flash_api_erase)(const struct device *dev, off_t offset,
94 size_t size);
95
96typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
97
98#if defined(CONFIG_FLASH_PAGE_LAYOUT)
120typedef void (*flash_api_pages_layout)(const struct device *dev,
121 const struct flash_pages_layout **layout,
122 size_t *layout_size);
123#endif /* CONFIG_FLASH_PAGE_LAYOUT */
124
125typedef int (*flash_api_sfdp_read)(const struct device *dev, off_t offset,
126 void *data, size_t len);
127typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id);
128
129__subsystem struct flash_driver_api {
134#if defined(CONFIG_FLASH_PAGE_LAYOUT)
136#endif /* CONFIG_FLASH_PAGE_LAYOUT */
137#if defined(CONFIG_FLASH_JESD216_API)
140#endif /* CONFIG_FLASH_JESD216_API */
141};
142
165__syscall int flash_read(const struct device *dev, off_t offset, void *data,
166 size_t len);
167
168static inline int z_impl_flash_read(const struct device *dev, off_t offset,
169 void *data,
170 size_t len)
171{
172 const struct flash_driver_api *api =
173 (const struct flash_driver_api *)dev->api;
174
175 return api->read(dev, offset, data, len);
176}
177
196__syscall int flash_write(const struct device *dev, off_t offset,
197 const void *data,
198 size_t len);
199
200static inline int z_impl_flash_write(const struct device *dev, off_t offset,
201 const void *data, size_t len)
202{
203 const struct flash_driver_api *api =
204 (const struct flash_driver_api *)dev->api;
205 int rc;
206
207 rc = api->write(dev, offset, data, len);
208
209 return rc;
210}
211
233__syscall int flash_erase(const struct device *dev, off_t offset, size_t size);
234
235static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
236 size_t size)
237{
238 const struct flash_driver_api *api =
239 (const struct flash_driver_api *)dev->api;
240 int rc;
241
242 rc = api->erase(dev, offset, size);
243
244 return rc;
245}
246
248 off_t start_offset; /* offset from the base of flash address */
249 size_t size;
251};
252
253#if defined(CONFIG_FLASH_PAGE_LAYOUT)
263__syscall int flash_get_page_info_by_offs(const struct device *dev,
264 off_t offset,
265 struct flash_pages_info *info);
266
276__syscall int flash_get_page_info_by_idx(const struct device *dev,
277 uint32_t page_index,
278 struct flash_pages_info *info);
279
287__syscall size_t flash_get_page_count(const struct device *dev);
288
299typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
300
313void flash_page_foreach(const struct device *dev, flash_page_cb cb,
314 void *data);
315#endif /* CONFIG_FLASH_PAGE_LAYOUT */
316
317#if defined(CONFIG_FLASH_JESD216_API)
338__syscall int flash_sfdp_read(const struct device *dev, off_t offset,
339 void *data, size_t len);
340
341static inline int z_impl_flash_sfdp_read(const struct device *dev,
342 off_t offset,
343 void *data, size_t len)
344{
345 int rv = -ENOTSUP;
346 const struct flash_driver_api *api =
347 (const struct flash_driver_api *)dev->api;
348
349 if (api->sfdp_read != NULL) {
350 rv = api->sfdp_read(dev, offset, data, len);
351 }
352 return rv;
353}
354
366__syscall int flash_read_jedec_id(const struct device *dev, uint8_t *id);
367
368static inline int z_impl_flash_read_jedec_id(const struct device *dev,
369 uint8_t *id)
370{
371 int rv = -ENOTSUP;
372 const struct flash_driver_api *api =
373 (const struct flash_driver_api *)dev->api;
374
375 if (api->read_jedec_id != NULL) {
376 rv = api->read_jedec_id(dev, id);
377 }
378 return rv;
379}
380#endif /* CONFIG_FLASH_JESD216_API */
381
393__syscall size_t flash_get_write_block_size(const struct device *dev);
394
395static inline size_t z_impl_flash_get_write_block_size(const struct device *dev)
396{
397 const struct flash_driver_api *api =
398 (const struct flash_driver_api *)dev->api;
399
400 return api->get_parameters(dev)->write_block_size;
401}
402
403
415__syscall const struct flash_parameters *flash_get_parameters(const struct device *dev);
416
417static inline const struct flash_parameters *z_impl_flash_get_parameters(const struct device *dev)
418{
419 const struct flash_driver_api *api =
420 (const struct flash_driver_api *)dev->api;
421
422 return api->get_parameters(dev);
423}
424
425#ifdef __cplusplus
426}
427#endif
428
433#include <syscalls/flash.h>
434
435#endif /* ZEPHYR_INCLUDE_DRIVERS_FLASH_H_ */
System error numbers.
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:299
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:127
int(* flash_api_erase)(const struct device *dev, off_t offset, size_t size)
Flash erase implementation handler type.
Definition: flash.h:93
const struct flash_parameters *(* flash_api_get_parameters)(const struct device *dev)
Definition: flash.h:96
int(* flash_api_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition: flash.h:71
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:120
int(* flash_api_sfdp_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition: flash.h:125
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:82
#define ENOTSUP
Definition: errno.h:115
__INTPTR_TYPE__ off_t
Definition: types.h:36
#define bool
Definition: stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
Runtime device structure (in ROM) per driver instance.
Definition: device.h:378
const void * api
Definition: device.h:384
Definition: flash.h:129
flash_api_sfdp_read sfdp_read
Definition: flash.h:138
flash_api_get_parameters get_parameters
Definition: flash.h:133
flash_api_pages_layout page_layout
Definition: flash.h:135
flash_api_read read
Definition: flash.h:130
flash_api_write write
Definition: flash.h:131
flash_api_erase erase
Definition: flash.h:132
flash_api_read_jedec_id read_jedec_id
Definition: flash.h:139
Definition: flash.h:247
size_t size
Definition: flash.h:249
off_t start_offset
Definition: flash.h:248
uint32_t index
Definition: flash.h:250
Definition: flash.h:35
size_t pages_size
Definition: flash.h:37
size_t pages_count
Definition: flash.h:36
Definition: flash.h:57
uint8_t erase_value
Definition: flash.h:59
const size_t write_block_size
Definition: flash.h:58
static fdata_t data[2]
Definition: test_fifo_contexts.c:15