1 /*
2 * Copyright (c) 2019 Vestas Wind Systems A/S
3 *
4 * Heavily based on drivers/flash.h which is:
5 * Copyright (c) 2017 Nordic Semiconductor ASA
6 * Copyright (c) 2016 Intel Corporation
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11 /**
12 * @file
13 * @brief Public API for EEPROM drivers
14 */
15
16 #ifndef ZEPHYR_INCLUDE_DRIVERS_EEPROM_H_
17 #define ZEPHYR_INCLUDE_DRIVERS_EEPROM_H_
18
19 /**
20 * @brief EEPROM Interface
21 * @defgroup eeprom_interface EEPROM Interface
22 * @since 2.1
23 * @version 1.0.0
24 * @ingroup io_interfaces
25 * @{
26 */
27
28 #include <zephyr/types.h>
29 #include <stddef.h>
30 #include <sys/types.h>
31 #include <zephyr/device.h>
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /**
38 * @cond INTERNAL_HIDDEN
39 *
40 * For internal driver use only, skip these in public documentation.
41 */
42
43 /**
44 * @brief Callback API upon reading from the EEPROM.
45 * See @a eeprom_read() for argument description
46 */
47 typedef int (*eeprom_api_read)(const struct device *dev, off_t offset,
48 void *data,
49 size_t len);
50
51 /**
52 * @brief Callback API upon writing to the EEPROM.
53 * See @a eeprom_write() for argument description
54 */
55 typedef int (*eeprom_api_write)(const struct device *dev, off_t offset,
56 const void *data, size_t len);
57
58 /**
59 * @brief Callback API upon getting the EEPROM size.
60 * See @a eeprom_get_size() for argument description
61 */
62 typedef size_t (*eeprom_api_size)(const struct device *dev);
63
64 __subsystem struct eeprom_driver_api {
65 eeprom_api_read read;
66 eeprom_api_write write;
67 eeprom_api_size size;
68 };
69
70 /** @endcond */
71
72 /**
73 * @brief Read data from EEPROM
74 *
75 * @param dev EEPROM device
76 * @param offset Address offset to read from.
77 * @param data Buffer to store read data.
78 * @param len Number of bytes to read.
79 *
80 * @return 0 on success, negative errno code on failure.
81 */
82 __syscall int eeprom_read(const struct device *dev, off_t offset, void *data,
83 size_t len);
84
z_impl_eeprom_read(const struct device * dev,off_t offset,void * data,size_t len)85 static inline int z_impl_eeprom_read(const struct device *dev, off_t offset,
86 void *data, size_t len)
87 {
88 const struct eeprom_driver_api *api =
89 (const struct eeprom_driver_api *)dev->api;
90
91 return api->read(dev, offset, data, len);
92 }
93
94 /**
95 * @brief Write data to EEPROM
96 *
97 * @param dev EEPROM device
98 * @param offset Address offset to write data to.
99 * @param data Buffer with data to write.
100 * @param len Number of bytes to write.
101 *
102 * @return 0 on success, negative errno code on failure.
103 */
104 __syscall int eeprom_write(const struct device *dev, off_t offset,
105 const void *data,
106 size_t len);
107
z_impl_eeprom_write(const struct device * dev,off_t offset,const void * data,size_t len)108 static inline int z_impl_eeprom_write(const struct device *dev, off_t offset,
109 const void *data, size_t len)
110 {
111 const struct eeprom_driver_api *api =
112 (const struct eeprom_driver_api *)dev->api;
113
114 return api->write(dev, offset, data, len);
115 }
116
117 /**
118 * @brief Get the size of the EEPROM in bytes
119 *
120 * @param dev EEPROM device.
121 *
122 * @return EEPROM size in bytes.
123 */
124 __syscall size_t eeprom_get_size(const struct device *dev);
125
z_impl_eeprom_get_size(const struct device * dev)126 static inline size_t z_impl_eeprom_get_size(const struct device *dev)
127 {
128 const struct eeprom_driver_api *api =
129 (const struct eeprom_driver_api *)dev->api;
130
131 return api->size(dev);
132 }
133
134 #ifdef __cplusplus
135 }
136 #endif
137
138 /**
139 * @}
140 */
141
142 #include <zephyr/syscalls/eeprom.h>
143
144 #endif /* ZEPHYR_INCLUDE_DRIVERS_EEPROM_H_ */
145