Zephyr API Documentation 4.4.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
11
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
80
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
150
165typedef void (*mbox_callback_t)(const struct device *dev,
166 mbox_channel_id_t channel_id, void *user_data,
167 struct mbox_msg *data);
168
179typedef int (*mbox_send_t)(const struct device *dev,
180 mbox_channel_id_t channel_id,
181 const struct mbox_msg *msg);
182
191typedef int (*mbox_mtu_get_t)(const struct device *dev);
192
205typedef int (*mbox_register_callback_t)(const struct device *dev,
206 mbox_channel_id_t channel_id,
207 mbox_callback_t cb, void *user_data);
208
219typedef int (*mbox_set_enabled_t)(const struct device *dev,
220 mbox_channel_id_t channel_id, bool enabled);
221
230typedef uint32_t (*mbox_max_channels_get_t)(const struct device *dev);
231
247
249
257static inline bool mbox_is_ready_dt(const struct mbox_dt_spec *spec)
258{
259 return device_is_ready(spec->dev);
260}
261
281__syscall int mbox_send(const struct device *dev, mbox_channel_id_t channel_id,
282 const struct mbox_msg *msg);
283
284static inline int z_impl_mbox_send(const struct device *dev,
285 mbox_channel_id_t channel_id,
286 const struct mbox_msg *msg)
287{
288 const struct mbox_driver_api *api = DEVICE_API_GET(mbox, dev);
289
290 if (api->send == NULL) {
291 return -ENOSYS;
292 }
293
294 return api->send(dev, channel_id, msg);
295}
296
305static inline int mbox_send_dt(const struct mbox_dt_spec *spec,
306 const struct mbox_msg *msg)
307{
308 return mbox_send(spec->dev, spec->channel_id, msg);
309}
310
327static inline int mbox_register_callback(const struct device *dev,
328 mbox_channel_id_t channel_id,
330 void *user_data)
331{
332 const struct mbox_driver_api *api = DEVICE_API_GET(mbox, dev);
333
334 if (api->register_callback == NULL) {
335 return -ENOSYS;
336 }
337
338 return api->register_callback(dev, channel_id, cb, user_data);
339}
340
352static inline int mbox_register_callback_dt(const struct mbox_dt_spec *spec,
353 mbox_callback_t cb, void *user_data)
354{
355 return mbox_register_callback(spec->dev, spec->channel_id, cb,
356 user_data);
357}
358
379__syscall int mbox_mtu_get(const struct device *dev);
380
381static inline int z_impl_mbox_mtu_get(const struct device *dev)
382{
383 const struct mbox_driver_api *api = DEVICE_API_GET(mbox, dev);
384
385 if (api->mtu_get == NULL) {
386 return -ENOSYS;
387 }
388
389 return api->mtu_get(dev);
390}
391
400static inline int mbox_mtu_get_dt(const struct mbox_dt_spec *spec)
401{
402 return mbox_mtu_get(spec->dev);
403}
404
430__syscall int mbox_set_enabled(const struct device *dev,
431 mbox_channel_id_t channel_id, bool enabled);
432
433static inline int z_impl_mbox_set_enabled(const struct device *dev,
434 mbox_channel_id_t channel_id,
435 bool enabled)
436{
437 const struct mbox_driver_api *api = DEVICE_API_GET(mbox, dev);
438
439 if (api->set_enabled == NULL) {
440 return -ENOSYS;
441 }
442
443 return api->set_enabled(dev, channel_id, enabled);
444}
445
455static inline int mbox_set_enabled_dt(const struct mbox_dt_spec *spec,
456 bool enabled)
457{
458 return mbox_set_enabled(spec->dev, spec->channel_id, enabled);
459}
460
471__syscall uint32_t mbox_max_channels_get(const struct device *dev);
472
473static inline uint32_t z_impl_mbox_max_channels_get(const struct device *dev)
474{
475 const struct mbox_driver_api *api = DEVICE_API_GET(mbox, dev);
476
477 if (api->max_channels_get == NULL) {
478 return -ENOSYS;
479 }
480
481 return api->max_channels_get(dev);
482}
483
491static inline int mbox_max_channels_get_dt(const struct mbox_dt_spec *spec)
492{
493 return mbox_max_channels_get(spec->dev);
494}
495
497
498#ifdef __cplusplus
499}
500#endif
501
502#include <zephyr/syscalls/mbox.h>
503
504#endif /* ZEPHYR_INCLUDE_DRIVERS_MBOX_H_ */
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1425
Devicetree main header.
System error numbers.
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
int(* mbox_send_t)(const struct device *dev, mbox_channel_id_t channel_id, const struct mbox_msg *msg)
Callback API to send MBOX messages.
Definition mbox.h:179
int(* mbox_set_enabled_t)(const struct device *dev, mbox_channel_id_t channel_id, bool enabled)
Callback API upon enablement of interrupts.
Definition mbox.h:219
uint32_t(* mbox_max_channels_get_t)(const struct device *dev)
Callback API to get maximum number of channels.
Definition mbox.h:230
int(* mbox_register_callback_t)(const struct device *dev, mbox_channel_id_t channel_id, mbox_callback_t cb, void *user_data)
Callback API upon registration.
Definition mbox.h:205
void(* mbox_callback_t)(const struct device *dev, mbox_channel_id_t channel_id, void *user_data, struct mbox_msg *data)
Callback API for incoming MBOX messages.
Definition mbox.h:165
int(* mbox_mtu_get_t)(const struct device *dev)
Callback API to get maximum data size.
Definition mbox.h:191
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:455
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:305
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:400
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:352
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:257
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:327
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:491
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
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
<span class="mlabel">Driver Operations</span> MBOX driver operations
Definition mbox.h:235
mbox_send_t send
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition mbox.h:237
mbox_mtu_get_t mtu_get
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition mbox.h:241
mbox_max_channels_get_t max_channels_get
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition mbox.h:243
mbox_register_callback_t register_callback
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition mbox.h:239
mbox_set_enabled_t set_enabled
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition mbox.h:245
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