Zephyr API Documentation 4.0.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
6#ifndef ZEPHYR_INCLUDE_DRIVERS_MBOX_H_
7#define ZEPHYR_INCLUDE_DRIVERS_MBOX_H_
8
9#include <errno.h>
10#include <stdint.h>
11#include <stdlib.h>
12
13#include <zephyr/device.h>
14#include <zephyr/devicetree.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
77
79struct mbox_msg {
81 const void *data;
83 size_t size;
84};
85
93
122#define MBOX_DT_SPEC_GET(node_id, name) \
123 { \
124 .dev = DEVICE_DT_GET(DT_MBOX_CTLR_BY_NAME(node_id, name)), \
125 .channel_id = DT_MBOX_CHANNEL_BY_NAME(node_id, name), \
126 }
127
136#define MBOX_DT_SPEC_INST_GET(inst, name) \
137 MBOX_DT_SPEC_GET(DT_DRV_INST(inst), name)
138
155typedef void (*mbox_callback_t)(const struct device *dev,
156 mbox_channel_id_t channel_id, void *user_data,
157 struct mbox_msg *data);
158
169typedef int (*mbox_send_t)(const struct device *dev,
170 mbox_channel_id_t channel_id,
171 const struct mbox_msg *msg);
172
181typedef int (*mbox_mtu_get_t)(const struct device *dev);
182
195typedef int (*mbox_register_callback_t)(const struct device *dev,
196 mbox_channel_id_t channel_id,
197 mbox_callback_t cb, void *user_data);
198
209typedef int (*mbox_set_enabled_t)(const struct device *dev,
210 mbox_channel_id_t channel_id, bool enabled);
211
220typedef uint32_t (*mbox_max_channels_get_t)(const struct device *dev);
221
222__subsystem struct mbox_driver_api {
223 mbox_send_t send;
224 mbox_register_callback_t register_callback;
225 mbox_mtu_get_t mtu_get;
226 mbox_max_channels_get_t max_channels_get;
227 mbox_set_enabled_t set_enabled;
228};
229
239static inline bool mbox_is_ready_dt(const struct mbox_dt_spec *spec)
240{
241 return device_is_ready(spec->dev);
242}
243
263__syscall int mbox_send(const struct device *dev, mbox_channel_id_t channel_id,
264 const struct mbox_msg *msg);
265
266static inline int z_impl_mbox_send(const struct device *dev,
267 mbox_channel_id_t channel_id,
268 const struct mbox_msg *msg)
269{
270 const struct mbox_driver_api *api =
271 (const struct mbox_driver_api *)dev->api;
272
273 if (api->send == NULL) {
274 return -ENOSYS;
275 }
276
277 return api->send(dev, channel_id, msg);
278}
279
288static inline int mbox_send_dt(const struct mbox_dt_spec *spec,
289 const struct mbox_msg *msg)
290{
291 return mbox_send(spec->dev, spec->channel_id, msg);
292}
293
310static inline int mbox_register_callback(const struct device *dev,
311 mbox_channel_id_t channel_id,
312 mbox_callback_t cb,
313 void *user_data)
314{
315 const struct mbox_driver_api *api =
316 (const struct mbox_driver_api *)dev->api;
317
318 if (api->register_callback == NULL) {
319 return -ENOSYS;
320 }
321
322 return api->register_callback(dev, channel_id, cb, user_data);
323}
324
336static inline int mbox_register_callback_dt(const struct mbox_dt_spec *spec,
337 mbox_callback_t cb, void *user_data)
338{
339 return mbox_register_callback(spec->dev, spec->channel_id, cb,
340 user_data);
341}
342
363__syscall int mbox_mtu_get(const struct device *dev);
364
365static inline int z_impl_mbox_mtu_get(const struct device *dev)
366{
367 const struct mbox_driver_api *api =
368 (const struct mbox_driver_api *)dev->api;
369
370 if (api->mtu_get == NULL) {
371 return -ENOSYS;
372 }
373
374 return api->mtu_get(dev);
375}
376
385static inline int mbox_mtu_get_dt(const struct mbox_dt_spec *spec)
386{
387 return mbox_mtu_get(spec->dev);
388}
389
415__syscall int mbox_set_enabled(const struct device *dev,
416 mbox_channel_id_t channel_id, bool enabled);
417
418static inline int z_impl_mbox_set_enabled(const struct device *dev,
419 mbox_channel_id_t channel_id,
420 bool enabled)
421{
422 const struct mbox_driver_api *api =
423 (const struct mbox_driver_api *)dev->api;
424
425 if (api->set_enabled == NULL) {
426 return -ENOSYS;
427 }
428
429 return api->set_enabled(dev, channel_id, enabled);
430}
431
441static inline int mbox_set_enabled_dt(const struct mbox_dt_spec *spec,
442 bool enabled)
443{
444 return mbox_set_enabled(spec->dev, spec->channel_id, enabled);
445}
446
457__syscall uint32_t mbox_max_channels_get(const struct device *dev);
458
459static inline uint32_t z_impl_mbox_max_channels_get(const struct device *dev)
460{
461 const struct mbox_driver_api *api =
462 (const struct mbox_driver_api *)dev->api;
463
464 if (api->max_channels_get == NULL) {
465 return -ENOSYS;
466 }
467
468 return api->max_channels_get(dev);
469}
470
478static inline int mbox_max_channels_get_dt(const struct mbox_dt_spec *spec)
479{
480 return mbox_max_channels_get(spec->dev);
481}
482
485#ifdef __cplusplus
486}
487#endif
488
489#include <zephyr/syscalls/mbox.h>
490
491#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:441
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:288
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:385
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:336
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:239
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:310
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:478
uint32_t mbox_channel_id_t
Type for MBOX channel identifiers.
Definition mbox.h:76
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
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:411
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:417
MBOX specification from DT.
Definition mbox.h:87
const struct device * dev
MBOX device pointer.
Definition mbox.h:89
mbox_channel_id_t channel_id
Channel ID.
Definition mbox.h:91
Message struct (to hold data and its size).
Definition mbox.h:79
size_t size
Size of the data.
Definition mbox.h:83
const void * data
Pointer to the data sent in the message.
Definition mbox.h:81