Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
memc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_MEMC_H_
14#define ZEPHYR_INCLUDE_DRIVERS_MEMC_H_
15
24
25#include <zephyr/types.h>
26#include <stddef.h>
27#include <zephyr/device.h>
28#include <string.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
39
44typedef int (*memc_api_read)(const struct device *dev, uint32_t addr, uint8_t *data, size_t len);
45
50typedef int (*memc_api_write)(const struct device *dev, uint32_t addr, const uint8_t *data,
51 size_t len);
52
57typedef int (*memc_api_get_size)(const struct device *dev, uint64_t *size);
58
63typedef int (*memc_api_read_id)(const struct device *dev, uint8_t *id, size_t len);
64
69typedef void *(*memc_api_get_mem_base)(const struct device *dev);
70
86
88
101__syscall int memc_read(const struct device *dev, uint32_t addr, uint8_t *data, size_t len);
102
103static inline int z_impl_memc_read(const struct device *dev, uint32_t addr, uint8_t *data,
104 size_t len)
105{
106 if (!DEVICE_API_IS(memc, dev)) {
107 return -ENOTSUP;
108 }
109
110 const struct memc_driver_api *api = DEVICE_API_GET(memc, dev);
111
112 if (api->get_mem_base != NULL) {
113 void *base = api->get_mem_base(dev);
114
115 if (base != NULL) {
116 memcpy(data, (const uint8_t *)base + addr, len);
117 return 0;
118 }
119 }
120
121 if (api->read != NULL) {
122 return api->read(dev, addr, data, len);
123 }
124
125 return -ENOTSUP;
126}
127
140__syscall int memc_write(const struct device *dev, uint32_t addr, const uint8_t *data, size_t len);
141
142static inline int z_impl_memc_write(const struct device *dev, uint32_t addr, const uint8_t *data,
143 size_t len)
144{
145 if (!DEVICE_API_IS(memc, dev)) {
146 return -ENOTSUP;
147 }
148
149 const struct memc_driver_api *api = DEVICE_API_GET(memc, dev);
150
151 if (api->get_mem_base != NULL) {
152 void *base = api->get_mem_base(dev);
153
154 if (base != NULL) {
155 memcpy((uint8_t *)base + addr, data, len);
156 return 0;
157 }
158 }
159
160 if (api->write != NULL) {
161 return api->write(dev, addr, data, len);
162 }
163
164 return -ENOTSUP;
165}
166
179static inline void *memc_get_mem_base(const struct device *dev)
180{
181 if (!DEVICE_API_IS(memc, dev)) {
182 return NULL;
183 }
184
185 const struct memc_driver_api *api = DEVICE_API_GET(memc, dev);
186
187 if (api->get_mem_base == NULL) {
188 return NULL;
189 }
190
191 return api->get_mem_base(dev);
192}
193
204__syscall int memc_get_size(const struct device *dev, uint64_t *size);
205
206static inline int z_impl_memc_get_size(const struct device *dev, uint64_t *size)
207{
208 if (!DEVICE_API_IS(memc, dev)) {
209 return -ENOTSUP;
210 }
211
212 const struct memc_driver_api *api = DEVICE_API_GET(memc, dev);
213
214 if (api->get_size == NULL) {
215 return -ENOTSUP;
216 }
217
218 return api->get_size(dev, size);
219}
220
233__syscall int memc_read_id(const struct device *dev, uint8_t *id, size_t len);
234
235static inline int z_impl_memc_read_id(const struct device *dev, uint8_t *id, size_t len)
236{
237 if (!DEVICE_API_IS(memc, dev)) {
238 return -ENOTSUP;
239 }
240
241 const struct memc_driver_api *api = DEVICE_API_GET(memc, dev);
242
243 if (api->read_id == NULL) {
244 return -ENOTSUP;
245 }
246
247 return api->read_id(dev, id, len);
248}
249
251
252#ifdef __cplusplus
253}
254#endif
255
256#include <zephyr/syscalls/memc.h>
257
258#endif /* ZEPHYR_INCLUDE_DRIVERS_MEMC_H_ */
#define DEVICE_API_IS(_class, _dev)
Macro that evaluates to a boolean that can be used to check if a device is of a particular class.
Definition device.h:1406
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1425
void *(* memc_api_get_mem_base)(const struct device *dev)
Callback API for getting the base address of external memory.
Definition memc.h:69
int(* memc_api_read)(const struct device *dev, uint32_t addr, uint8_t *data, size_t len)
Callback API for reading from external memory.
Definition memc.h:44
int(* memc_api_get_size)(const struct device *dev, uint64_t *size)
Callback API for getting the size of external memory.
Definition memc.h:57
int(* memc_api_write)(const struct device *dev, uint32_t addr, const uint8_t *data, size_t len)
Callback API for writing to external memory.
Definition memc.h:50
int(* memc_api_read_id)(const struct device *dev, uint8_t *id, size_t len)
Callback API for reading the ID of external memory.
Definition memc.h:63
int memc_get_size(const struct device *dev, uint64_t *size)
Get the size of external memory.
int memc_read(const struct device *dev, uint32_t addr, uint8_t *data, size_t len)
Read data from external memory.
int memc_write(const struct device *dev, uint32_t addr, const uint8_t *data, size_t len)
Write data to external memory.
static void * memc_get_mem_base(const struct device *dev)
Get the memory-mapped base address of a MEMC device.
Definition memc.h:179
int memc_read_id(const struct device *dev, uint8_t *id, size_t len)
Read the ID of external memory.
#define ENOTSUP
Unsupported value.
Definition errno.h:114
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
void * memcpy(void *ZRESTRICT d, const void *ZRESTRICT s, size_t n)
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
void * data
Address of the device instance private data.
Definition device.h:523
<span class="mlabel">Driver Operations</span> MEMC driver operations
Definition memc.h:74
memc_api_get_mem_base get_mem_base
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition memc.h:80
memc_api_get_size get_size
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition memc.h:82
memc_api_read read
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition memc.h:76
memc_api_write write
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition memc.h:78
memc_api_read_id read_id
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition memc.h:84