Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
60 const size_t write_block_size;
61 uint8_t erase_value; /* Byte value of erased flash */
62};
63
73typedef int (*flash_api_read)(const struct device *dev, off_t offset,
74 void *data,
75 size_t len);
84typedef int (*flash_api_write)(const struct device *dev, off_t offset,
85 const void *data, size_t len);
86
95typedef int (*flash_api_erase)(const struct device *dev, off_t offset,
96 size_t size);
97
98typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
99
100#if defined(CONFIG_FLASH_PAGE_LAYOUT)
122typedef void (*flash_api_pages_layout)(const struct device *dev,
123 const struct flash_pages_layout **layout,
124 size_t *layout_size);
125#endif /* CONFIG_FLASH_PAGE_LAYOUT */
126
127typedef int (*flash_api_sfdp_read)(const struct device *dev, off_t offset,
128 void *data, size_t len);
129typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id);
130typedef int (*flash_api_ex_op)(const struct device *dev, uint16_t code,
131 const uintptr_t in, void *out);
132
133__subsystem struct flash_driver_api {
138#if defined(CONFIG_FLASH_PAGE_LAYOUT)
140#endif /* CONFIG_FLASH_PAGE_LAYOUT */
141#if defined(CONFIG_FLASH_JESD216_API)
144#endif /* CONFIG_FLASH_JESD216_API */
145#if defined(CONFIG_FLASH_EX_OP_ENABLED)
146 flash_api_ex_op ex_op;
147#endif /* CONFIG_FLASH_EX_OP_ENABLED */
148};
149
172__syscall int flash_read(const struct device *dev, off_t offset, void *data,
173 size_t len);
174
175static inline int z_impl_flash_read(const struct device *dev, off_t offset,
176 void *data,
177 size_t len)
178{
179 const struct flash_driver_api *api =
180 (const struct flash_driver_api *)dev->api;
181
182 return api->read(dev, offset, data, len);
183}
184
203__syscall int flash_write(const struct device *dev, off_t offset,
204 const void *data,
205 size_t len);
206
207static inline int z_impl_flash_write(const struct device *dev, off_t offset,
208 const void *data, size_t len)
209{
210 const struct flash_driver_api *api =
211 (const struct flash_driver_api *)dev->api;
212 int rc;
213
214 rc = api->write(dev, offset, data, len);
215
216 return rc;
217}
218
240__syscall int flash_erase(const struct device *dev, off_t offset, size_t size);
241
242static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
243 size_t size)
244{
245 const struct flash_driver_api *api =
246 (const struct flash_driver_api *)dev->api;
247 int rc;
248
249 rc = api->erase(dev, offset, size);
250
251 return rc;
252}
253
255 off_t start_offset; /* offset from the base of flash address */
256 size_t size;
258};
259
260#if defined(CONFIG_FLASH_PAGE_LAYOUT)
270__syscall int flash_get_page_info_by_offs(const struct device *dev,
271 off_t offset,
272 struct flash_pages_info *info);
273
283__syscall int flash_get_page_info_by_idx(const struct device *dev,
284 uint32_t page_index,
285 struct flash_pages_info *info);
286
294__syscall size_t flash_get_page_count(const struct device *dev);
295
306typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
307
320void flash_page_foreach(const struct device *dev, flash_page_cb cb,
321 void *data);
322#endif /* CONFIG_FLASH_PAGE_LAYOUT */
323
324#if defined(CONFIG_FLASH_JESD216_API)
345__syscall int flash_sfdp_read(const struct device *dev, off_t offset,
346 void *data, size_t len);
347
348static inline int z_impl_flash_sfdp_read(const struct device *dev,
349 off_t offset,
350 void *data, size_t len)
351{
352 int rv = -ENOTSUP;
353 const struct flash_driver_api *api =
354 (const struct flash_driver_api *)dev->api;
355
356 if (api->sfdp_read != NULL) {
357 rv = api->sfdp_read(dev, offset, data, len);
358 }
359 return rv;
360}
361
373__syscall int flash_read_jedec_id(const struct device *dev, uint8_t *id);
374
375static inline int z_impl_flash_read_jedec_id(const struct device *dev,
376 uint8_t *id)
377{
378 int rv = -ENOTSUP;
379 const struct flash_driver_api *api =
380 (const struct flash_driver_api *)dev->api;
381
382 if (api->read_jedec_id != NULL) {
383 rv = api->read_jedec_id(dev, id);
384 }
385 return rv;
386}
387#endif /* CONFIG_FLASH_JESD216_API */
388
400__syscall size_t flash_get_write_block_size(const struct device *dev);
401
402static inline size_t z_impl_flash_get_write_block_size(const struct device *dev)
403{
404 const struct flash_driver_api *api =
405 (const struct flash_driver_api *)dev->api;
406
407 return api->get_parameters(dev)->write_block_size;
408}
409
410
422__syscall const struct flash_parameters *flash_get_parameters(const struct device *dev);
423
424static inline const struct flash_parameters *z_impl_flash_get_parameters(const struct device *dev)
425{
426 const struct flash_driver_api *api =
427 (const struct flash_driver_api *)dev->api;
428
429 return api->get_parameters(dev);
430}
431
457__syscall int flash_ex_op(const struct device *dev, uint16_t code,
458 const uintptr_t in, void *out);
459
460/*
461 * Extended operation interface provides flexible way for supporting flash
462 * controller features. Code space is divided equally into Zephyr codes
463 * (MSb == 0) and vendor codes (MSb == 1). This way we can easily add extended
464 * operations to the drivers without cluttering the API or problems with API
465 * incompatibility. Extended operation can be promoted from vendor codes to
466 * Zephyr codes if the feature is available in most flash controllers and
467 * can be represented in the same way.
468 *
469 * It's not forbidden to have operation in Zephyr codes and vendor codes for
470 * the same functionality. In this case, vendor operation could provide more
471 * specific access when abstraction in Zephyr counterpart is insufficient.
472 */
473#define FLASH_EX_OP_VENDOR_BASE 0x8000
474#define FLASH_EX_OP_IS_VENDOR(c) ((c) & FLASH_EX_OP_VENDOR_BASE)
475
480 /*
481 * Reset flash device.
482 */
484};
485
486static inline int z_impl_flash_ex_op(const struct device *dev, uint16_t code,
487 const uintptr_t in, void *out)
488{
489#if defined(CONFIG_FLASH_EX_OP_ENABLED)
490 const struct flash_driver_api *api =
491 (const struct flash_driver_api *)dev->api;
492
493 if (api->ex_op == NULL) {
494 return -ENOTSUP;
495 }
496
497 return api->ex_op(dev, code, in, out);
498#else
499 ARG_UNUSED(dev);
500 ARG_UNUSED(code);
501 ARG_UNUSED(in);
502 ARG_UNUSED(out);
503
504 return -ENOSYS;
505#endif /* CONFIG_FLASH_EX_OP_ENABLED */
506}
507
508#ifdef __cplusplus
509}
510#endif
511
516#include <syscalls/flash.h>
517
518#endif /* ZEPHYR_INCLUDE_DRIVERS_FLASH_H_ */
Incorrect memory layout
Definition: bindesc.h:290
System error numbers.
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:306
int flash_ex_op(const struct device *dev, uint16_t code, const uintptr_t in, void *out)
Execute flash extended operation on given device.
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.
flash_ex_op_types
Enumeration for extra flash operations.
Definition: flash.h:479
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.
@ FLASH_EX_OP_RESET
Definition: flash.h:483
int(* flash_api_read_jedec_id)(const struct device *dev, uint8_t *id)
Definition: flash.h:129
int(* flash_api_erase)(const struct device *dev, off_t offset, size_t size)
Flash erase implementation handler type.
Definition: flash.h:95
const struct flash_parameters *(* flash_api_get_parameters)(const struct device *dev)
Definition: flash.h:98
int(* flash_api_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition: flash.h:73
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:122
int(* flash_api_sfdp_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition: flash.h:127
int(* flash_api_ex_op)(const struct device *dev, uint16_t code, const uintptr_t in, void *out)
Definition: flash.h:130
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:84
#define ENOSYS
Function not implemented.
Definition: errno.h:83
#define ENOTSUP
Unsupported value.
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
__UINTPTR_TYPE__ uintptr_t
Definition: stdint.h:105
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition: device.h:399
void * data
Address of the device instance private data.
Definition: device.h:409
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:405
Definition: flash.h:133
flash_api_sfdp_read sfdp_read
Definition: flash.h:142
flash_api_get_parameters get_parameters
Definition: flash.h:137
flash_api_pages_layout page_layout
Definition: flash.h:139
flash_api_read read
Definition: flash.h:134
flash_api_write write
Definition: flash.h:135
flash_api_erase erase
Definition: flash.h:136
flash_api_read_jedec_id read_jedec_id
Definition: flash.h:143
Definition: flash.h:254
size_t size
Definition: flash.h:256
off_t start_offset
Definition: flash.h:255
uint32_t index
Definition: flash.h:257
Definition: flash.h:35
size_t pages_size
Definition: flash.h:37
size_t pages_count
Definition: flash.h:36
Flash memory parameters.
Definition: flash.h:59
uint8_t erase_value
Definition: flash.h:61
const size_t write_block_size
Definition: flash.h:60