Zephyr Project API 4.2.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
mbox.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
12#ifndef ZEPHYR_INCLUDE_DRIVERS_MBOX_H_
13#define ZEPHYR_INCLUDE_DRIVERS_MBOX_H_
14
15#include <errno.h>
16#include <stdint.h>
17#include <stdlib.h>
18
19#include <zephyr/device.h>
20#include <zephyr/devicetree.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
83
85struct mbox_msg {
87 const void *data;
89 size_t size;
90};
91
99
128#define MBOX_DT_SPEC_GET(node_id, name) \
129 { \
130 .dev = DEVICE_DT_GET(DT_MBOX_CTLR_BY_NAME(node_id, name)), \
131 .channel_id = DT_MBOX_CHANNEL_BY_NAME(node_id, name), \
132 }
133
142#define MBOX_DT_SPEC_INST_GET(inst, name) \
143 MBOX_DT_SPEC_GET(DT_DRV_INST(inst), name)
144
161typedef void (*mbox_callback_t)(const struct device *dev,
162 mbox_channel_id_t channel_id, void *user_data,
163 struct mbox_msg *data);
164
175typedef int (*mbox_send_t)(const struct device *dev,
176 mbox_channel_id_t channel_id,
177 const struct mbox_msg *msg);
178
187typedef int (*mbox_mtu_get_t)(const struct device *dev);
188
201typedef int (*mbox_register_callback_t)(const struct device *dev,
202 mbox_channel_id_t channel_id,
203 mbox_callback_t cb, void *user_data);
204
215typedef int (*mbox_set_enabled_t)(const struct device *dev,
216 mbox_channel_id_t channel_id, bool enabled);
217
226typedef uint32_t (*mbox_max_channels_get_t)(const struct device *dev);
227
228__subsystem struct mbox_driver_api {
229 mbox_send_t send;
230 mbox_register_callback_t register_callback;
231 mbox_mtu_get_t mtu_get;
232 mbox_max_channels_get_t max_channels_get;
233 mbox_set_enabled_t set_enabled;
234};
235
245static inline bool mbox_is_ready_dt(const struct mbox_dt_spec *spec)
246{
247 return device_is_ready(spec->dev);
248}
249
269__syscall int mbox_send(const struct device *dev, mbox_channel_id_t channel_id,
270 const struct mbox_msg *msg);
271
272static inline int z_impl_mbox_send(const struct device *dev,
273 mbox_channel_id_t channel_id,
274 const struct mbox_msg *msg)
275{
276 const struct mbox_driver_api *api =
277 (const struct mbox_driver_api *)dev->api;
278
279 if (api->send == NULL) {
280 return -ENOSYS;
281 }
282
283 return api->send(dev, channel_id, msg);
284}
285
294static inline int mbox_send_dt(const struct mbox_dt_spec *spec,
295 const struct mbox_msg *msg)
296{
297 return mbox_send(spec->dev, spec->channel_id, msg);
298}
299
316static inline int mbox_register_callback(const struct device *dev,
317 mbox_channel_id_t channel_id,
318 mbox_callback_t cb,
319 void *user_data)
320{
321 const struct mbox_driver_api *api =
322 (const struct mbox_driver_api *)dev->api;
323
324 if (api->register_callback == NULL) {
325 return -ENOSYS;
326 }
327
328 return api->register_callback(dev, channel_id, cb, user_data);
329}
330
342static inline int mbox_register_callback_dt(const struct mbox_dt_spec *spec,
343 mbox_callback_t cb, void *user_data)
344{
345 return mbox_register_callback(spec->dev, spec->channel_id, cb,
346 user_data);
347}
348
369__syscall int mbox_mtu_get(const struct device *dev);
370
371static inline int z_impl_mbox_mtu_get(const struct device *dev)
372{
373 const struct mbox_driver_api *api =
374 (const struct mbox_driver_api *)dev->api;
375
376 if (api->mtu_get == NULL) {
377 return -ENOSYS;
378 }
379
380 return api->mtu_get(dev);
381}
382
391static inline int mbox_mtu_get_dt(const struct mbox_dt_spec *spec)
392{
393 return mbox_mtu_get(spec->dev);
394}
395
421__syscall int mbox_set_enabled(const struct device *dev,
422 mbox_channel_id_t channel_id, bool enabled);
423
424static inline int z_impl_mbox_set_enabled(const struct device *dev,
425 mbox_channel_id_t channel_id,
426 bool enabled)
427{
428 const struct mbox_driver_api *api =
429 (const struct mbox_driver_api *)dev->api;
430
431 if (api->set_enabled == NULL) {
432 return -ENOSYS;
433 }
434
435 return api->set_enabled(dev, channel_id, enabled);
436}
437
447static inline int mbox_set_enabled_dt(const struct mbox_dt_spec *spec,
448 bool enabled)
449{
450 return mbox_set_enabled(spec->dev, spec->channel_id, enabled);
451}
452
463__syscall uint32_t mbox_max_channels_get(const struct device *dev);
464
465static inline uint32_t z_impl_mbox_max_channels_get(const struct device *dev)
466{
467 const struct mbox_driver_api *api =
468 (const struct mbox_driver_api *)dev->api;
469
470 if (api->max_channels_get == NULL) {
471 return -ENOSYS;
472 }
473
474 return api->max_channels_get(dev);
475}
476
484static inline int mbox_max_channels_get_dt(const struct mbox_dt_spec *spec)
485{
486 return mbox_max_channels_get(spec->dev);
487}
488
491#ifdef __cplusplus
492}
493#endif
494
495#include <zephyr/syscalls/mbox.h>
496
497#endif /* ZEPHYR_INCLUDE_DRIVERS_MBOX_H_ */
Devicetree main header.
System error numbers.
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
static int mbox_set_enabled_dt(const struct mbox_dt_spec *spec, bool enabled)
Enable (disable) interrupts and callbacks for inbound channels from a struct mbox_dt_spec.
Definition mbox.h:447
int mbox_mtu_get(const struct device *dev)
Return the maximum number of bytes possible in an outbound message.
static int mbox_send_dt(const struct mbox_dt_spec *spec, const struct mbox_msg *msg)
Try to send a message over the MBOX device from a struct mbox_dt_spec.
Definition mbox.h:294
static int mbox_mtu_get_dt(const struct mbox_dt_spec *spec)
Return the maximum number of bytes possible in an outbound message from struct mbox_dt_spec.
Definition mbox.h:391
static int mbox_register_callback_dt(const struct mbox_dt_spec *spec, mbox_callback_t cb, void *user_data)
Register a callback function on a channel for incoming messages from a struct mbox_dt_spec.
Definition mbox.h:342
int mbox_set_enabled(const struct device *dev, mbox_channel_id_t channel_id, bool enabled)
Enable (disable) interrupts and callbacks for inbound channels.
static bool mbox_is_ready_dt(const struct mbox_dt_spec *spec)
Validate if MBOX device instance from a struct mbox_dt_spec is ready.
Definition mbox.h:245
static int mbox_register_callback(const struct device *dev, mbox_channel_id_t channel_id, mbox_callback_t cb, void *user_data)
Register a callback function on a channel for incoming messages.
Definition mbox.h:316
static int mbox_max_channels_get_dt(const struct mbox_dt_spec *spec)
Return the maximum number of channels from a struct mbox_dt_spec.
Definition mbox.h:484
uint32_t mbox_channel_id_t
Type for MBOX channel identifiers.
Definition mbox.h:82
uint32_t mbox_max_channels_get(const struct device *dev)
Return the maximum number of channels.
int mbox_send(const struct device *dev, mbox_channel_id_t channel_id, const struct mbox_msg *msg)
Try to send a message over the MBOX device.
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define NULL
Definition iar_missing_defs.h:20
ssize_t send(int sock, const void *buf, size_t len, int flags)
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
Runtime device structure (in ROM) per driver instance.
Definition device.h:510
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:516
MBOX specification from DT.
Definition mbox.h:93
const struct device * dev
MBOX device pointer.
Definition mbox.h:95
mbox_channel_id_t channel_id
Channel ID.
Definition mbox.h:97
Message struct (to hold data and its size).
Definition mbox.h:85
size_t size
Size of the data.
Definition mbox.h:89
const void * data
Pointer to the data sent in the message.
Definition mbox.h:87