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
146
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
237
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 = DEVICE_API_GET(mbox, dev);
277
278 if (api->send == NULL) {
279 return -ENOSYS;
280 }
281
282 return api->send(dev, channel_id, msg);
283}
284
293static inline int mbox_send_dt(const struct mbox_dt_spec *spec,
294 const struct mbox_msg *msg)
295{
296 return mbox_send(spec->dev, spec->channel_id, msg);
297}
298
315static inline int mbox_register_callback(const struct device *dev,
316 mbox_channel_id_t channel_id,
317 mbox_callback_t cb,
318 void *user_data)
319{
320 const struct mbox_driver_api *api = DEVICE_API_GET(mbox, dev);
321
322 if (api->register_callback == NULL) {
323 return -ENOSYS;
324 }
325
326 return api->register_callback(dev, channel_id, cb, user_data);
327}
328
340static inline int mbox_register_callback_dt(const struct mbox_dt_spec *spec,
341 mbox_callback_t cb, void *user_data)
342{
343 return mbox_register_callback(spec->dev, spec->channel_id, cb,
344 user_data);
345}
346
367__syscall int mbox_mtu_get(const struct device *dev);
368
369static inline int z_impl_mbox_mtu_get(const struct device *dev)
370{
371 const struct mbox_driver_api *api = DEVICE_API_GET(mbox, dev);
372
373 if (api->mtu_get == NULL) {
374 return -ENOSYS;
375 }
376
377 return api->mtu_get(dev);
378}
379
388static inline int mbox_mtu_get_dt(const struct mbox_dt_spec *spec)
389{
390 return mbox_mtu_get(spec->dev);
391}
392
418__syscall int mbox_set_enabled(const struct device *dev,
419 mbox_channel_id_t channel_id, bool enabled);
420
421static inline int z_impl_mbox_set_enabled(const struct device *dev,
422 mbox_channel_id_t channel_id,
423 bool enabled)
424{
425 const struct mbox_driver_api *api = DEVICE_API_GET(mbox, dev);
426
427 if (api->set_enabled == NULL) {
428 return -ENOSYS;
429 }
430
431 return api->set_enabled(dev, channel_id, enabled);
432}
433
443static inline int mbox_set_enabled_dt(const struct mbox_dt_spec *spec,
444 bool enabled)
445{
446 return mbox_set_enabled(spec->dev, spec->channel_id, enabled);
447}
448
459__syscall uint32_t mbox_max_channels_get(const struct device *dev);
460
461static inline uint32_t z_impl_mbox_max_channels_get(const struct device *dev)
462{
463 const struct mbox_driver_api *api = DEVICE_API_GET(mbox, dev);
464
465 if (api->max_channels_get == NULL) {
466 return -ENOSYS;
467 }
468
469 return api->max_channels_get(dev);
470}
471
479static inline int mbox_max_channels_get_dt(const struct mbox_dt_spec *spec)
480{
481 return mbox_max_channels_get(spec->dev);
482}
483
485
486#ifdef __cplusplus
487}
488#endif
489
490#include <zephyr/syscalls/mbox.h>
491
492#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:1375
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:443
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:293
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:388
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:340
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:315
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:479
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:513
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