7#ifndef ZEPHYR_INCLUDE_DEVICE_H_
8#define ZEPHYR_INCLUDE_DEVICE_H_
70#define DEVICE_HANDLE_SEP INT16_MIN
77#define DEVICE_HANDLE_ENDS INT16_MAX
80#define DEVICE_HANDLE_NULL 0
82#define Z_DEVICE_MAX_NAME_LEN 48
104#define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
114#define Z_DEVICE_DT_DEV_NAME(node_id) _CONCAT(dts_ord_, DT_DEP_ORD(node_id))
119#define Z_DEVICE_STATE_NAME(dev_name) _CONCAT(__devstate_, dev_name)
162#define DEVICE_DEFINE(dev_name, drv_name, init_fn, pm_device, \
163 data_ptr, cfg_ptr, level, prio, api_ptr) \
164 Z_DEVICE_STATE_DEFINE(DT_INVALID_NODE, dev_name) \
165 Z_DEVICE_DEFINE(DT_INVALID_NODE, dev_name, drv_name, init_fn, \
167 data_ptr, cfg_ptr, level, prio, api_ptr, \
168 &Z_DEVICE_STATE_NAME(dev_name))
181#define DEVICE_DT_NAME(node_id) \
182 DT_PROP_OR(node_id, label, DT_NODE_FULL_NAME(node_id))
225#define DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
226 data_ptr, cfg_ptr, level, prio, \
228 Z_DEVICE_STATE_DEFINE(node_id, Z_DEVICE_DT_DEV_NAME(node_id)) \
229 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_NAME(node_id), \
230 DEVICE_DT_NAME(node_id), init_fn, \
232 data_ptr, cfg_ptr, level, prio, \
234 &Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_NAME(node_id)), \
246#define DEVICE_DT_INST_DEFINE(inst, ...) \
247 DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
264#define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_NAME(node_id))
282#define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
293#define DEVICE_DT_INST_GET(inst) DEVICE_DT_GET(DT_DRV_INST(inst))
311#define DEVICE_DT_GET_ANY(compat) \
312 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
313 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
333#define DEVICE_DT_GET_ONE(compat) \
334 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
335 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
336 (ZERO_OR_COMPILE_ERROR(0)))
350#define DEVICE_DT_GET_OR_NULL(node_id) \
351 COND_CODE_1(DT_NODE_HAS_STATUS(node_id, okay), \
352 (DEVICE_DT_GET(node_id)), (NULL))
364#define DEVICE_GET(name) (&DEVICE_NAME_GET(name))
380#define DEVICE_DECLARE(name) static const struct device DEVICE_NAME_GET(name)
389#define DEVICE_INIT_DT_GET(node_id) (&Z_INIT_ENTRY_NAME(DEVICE_DT_NAME_GET(node_id)))
398#define DEVICE_INIT_GET(name) (&Z_INIT_ENTRY_NAME(DEVICE_NAME_GET(name)))
426#ifdef CONFIG_HAS_DYNAMIC_DEVICE_HANDLES
427#define Z_DEVICE_HANDLES_CONST
429#define Z_DEVICE_HANDLES_CONST const
455#ifdef CONFIG_PM_DEVICE
457 struct pm_device *pm;
473 extern const struct device __device_start[];
493static inline const struct device *
496 extern const struct device __device_start[];
497 extern const struct device __device_end[];
498 const struct device *dev = NULL;
499 size_t numdev = __device_end - __device_start;
501 if ((dev_handle > 0) && ((
size_t)dev_handle <= numdev)) {
502 dev = &__device_start[dev_handle - 1];
598 while (region != 1) {
645 while (region != 2) {
766size_t z_device_get_all_static(
const struct device * *devices);
782bool z_device_is_ready(
const struct device *dev);
801static inline bool z_impl_device_is_ready(
const struct device *dev)
803 return z_device_is_ready(dev);
814#define Z_DEVICE_HANDLE_NAME(node_id, dev_name) \
815 _CONCAT(__devicehdl_, \
816 COND_CODE_1(DT_NODE_EXISTS(node_id), \
820#define Z_DEVICE_EXTRA_HANDLES(...) \
821 FOR_EACH_NONEMPTY_TERM(IDENTITY, (,), __VA_ARGS__)
829#define Z_DEVICE_STATE_DEFINE(node_id, dev_name) \
830 static struct device_state Z_DEVICE_STATE_NAME(dev_name) \
831 __attribute__((__section__(".z_devstate")));
836#define Z_DEVICE_DEFINE_PRE(node_id, dev_name, ...) \
837 Z_DEVICE_DEFINE_HANDLES(node_id, dev_name, __VA_ARGS__)
878#define Z_DEVICE_DEFINE_HANDLES(node_id, dev_name, ...) \
879 extern Z_DEVICE_HANDLES_CONST device_handle_t \
880 Z_DEVICE_HANDLE_NAME(node_id, dev_name)[]; \
881 Z_DEVICE_HANDLES_CONST device_handle_t \
882 __aligned(sizeof(device_handle_t)) \
883 __attribute__((__weak__, \
884 __section__(".__device_handles_pass1"))) \
885 Z_DEVICE_HANDLE_NAME(node_id, dev_name)[] = { \
886 COND_CODE_1(DT_NODE_EXISTS(node_id), ( \
887 DT_DEP_ORD(node_id), \
888 DT_REQUIRES_DEP_ORDS(node_id) \
890 DEVICE_HANDLE_NULL, \
893 Z_DEVICE_EXTRA_HANDLES(__VA_ARGS__) \
895 COND_CODE_1(DT_NODE_EXISTS(node_id), \
896 (DT_SUPPORTS_DEP_ORDS(node_id)), ()) \
899#define Z_DEVICE_DEFINE_INIT(node_id, dev_name) \
900 .handles = Z_DEVICE_HANDLE_NAME(node_id, dev_name),
905#define Z_DEVICE_DEFINE(node_id, dev_name, drv_name, init_fn, pm_device,\
906 data_ptr, cfg_ptr, level, prio, api_ptr, state_ptr, ...) \
907 Z_DEVICE_DEFINE_PRE(node_id, dev_name, __VA_ARGS__) \
908 COND_CODE_1(DT_NODE_EXISTS(node_id), (), (static)) \
909 const Z_DECL_ALIGN(struct device) \
910 DEVICE_NAME_GET(dev_name) __used \
911 __attribute__((__section__(".z_device_" #level STRINGIFY(prio)"_"))) = { \
913 .config = (cfg_ptr), \
915 .state = (state_ptr), \
916 .data = (data_ptr), \
917 IF_ENABLED(CONFIG_PM_DEVICE, (.pm = pm_device,)) \
918 Z_DEVICE_DEFINE_INIT(node_id, dev_name) \
920 BUILD_ASSERT(sizeof(Z_STRINGIFY(drv_name)) <= Z_DEVICE_MAX_NAME_LEN, \
921 Z_STRINGIFY(DEVICE_NAME_GET(drv_name)) " too long"); \
922 Z_INIT_ENTRY_DEFINE(DEVICE_NAME_GET(dev_name), init_fn, \
923 (&DEVICE_NAME_GET(dev_name)), level, prio)
936#define Z_MAYBE_DEVICE_DECLARE_INTERNAL(node_id) \
937 extern const struct device DEVICE_DT_NAME_GET(node_id);
947#include <syscalls/device.h>
ZTEST_BMEM int count
Definition: main.c:33
volatile int rv
Definition: main.c:45
const struct device * device_get_binding(const char *name)
Get a const struct device* from its name field.
int16_t device_handle_t
Type used to represent a "handle" for a device.
Definition: device.h:63
static const device_handle_t * device_required_handles_get(const struct device *dev, size_t *count)
Get the device handles for devicetree dependencies of this device.
Definition: device.h:550
static const device_handle_t * device_supported_handles_get(const struct device *dev, size_t *count)
Get the set of handles that this device supports.
Definition: device.h:636
static device_handle_t device_handle_get(const struct device *dev)
Get the handle for a given device.
Definition: device.h:470
#define DEVICE_HANDLE_NULL
Flag value used to identify an unknown device.
Definition: device.h:80
#define DEVICE_HANDLE_SEP
Flag value used in lists of device handles to separate distinct groups.
Definition: device.h:70
int device_required_foreach(const struct device *dev, device_visitor_callback_t visitor_cb, void *context)
Visit every device that dev directly requires.
static const struct device * device_from_handle(device_handle_t dev_handle)
Get the device corresponding to a handle.
Definition: device.h:494
int(* device_visitor_callback_t)(const struct device *dev, void *context)
Prototype for functions used when iterating over a set of devices.
Definition: device.h:527
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
#define DEVICE_HANDLE_ENDS
Flag value used in lists of device handles to indicate the end of the list.
Definition: device.h:77
static const device_handle_t * device_injected_handles_get(const struct device *dev, size_t *count)
Get the device handles for injected dependencies of this device.
Definition: device.h:589
int device_supported_foreach(const struct device *dev, device_visitor_callback_t visitor_cb, void *context)
Visit every device that dev directly supports.
DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_EMUL_DECLARE_INTERNAL)
static ZTEST_BMEM volatile int ret
Definition: k_float_disable.c:28
Definitions of various linker Sections.
__INT16_TYPE__ int16_t
Definition: stdint.h:73
Runtime device dynamic structure (in RAM) per driver instance.
Definition: device.h:408
bool initialized
Definition: device.h:421
unsigned int init_res
Definition: device.h:416
Runtime device structure (in ROM) per driver instance.
Definition: device.h:435
const char * name
Definition: device.h:437
void * data
Definition: device.h:445
const device_handle_t * handles
Definition: device.h:453
const void * api
Definition: device.h:441
struct device_state * state
Definition: device.h:443
const void * config
Definition: device.h:439