7#ifndef ZEPHYR_INCLUDE_DEVICE_H_
8#define ZEPHYR_INCLUDE_DEVICE_H_
60#define DEVICE_HANDLE_SEP INT16_MIN
67#define DEVICE_HANDLE_ENDS INT16_MAX
70#define DEVICE_HANDLE_NULL 0
72#define Z_DEVICE_MAX_NAME_LEN 48
96#define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
110#define SYS_DEVICE_DEFINE(drv_name, init_fn, level, prio) \
111 __DEPRECATED_MACRO SYS_INIT(init_fn, level, prio)
121#define Z_DEVICE_DT_DEV_NAME(node_id) _CONCAT(dts_ord_, DT_DEP_ORD(node_id))
126#define Z_DEVICE_STATE_NAME(dev_name) _CONCAT(__devstate_, dev_name)
134#define Z_DEVICE_STATE_DEFINE(node_id, dev_name) \
135 static struct device_state Z_DEVICE_STATE_NAME(dev_name) \
136 __attribute__((__section__(".z_devstate")));
181#define DEVICE_DEFINE(dev_name, drv_name, init_fn, pm_device, \
182 data_ptr, cfg_ptr, level, prio, api_ptr) \
183 Z_DEVICE_STATE_DEFINE(DT_INVALID_NODE, dev_name) \
184 Z_DEVICE_DEFINE(DT_INVALID_NODE, dev_name, drv_name, init_fn, \
186 data_ptr, cfg_ptr, level, prio, api_ptr, \
187 &Z_DEVICE_STATE_NAME(dev_name))
202#define DEVICE_DT_NAME(node_id) \
203 DT_PROP_OR(node_id, label, DT_NODE_FULL_NAME(node_id))
248#define DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
249 data_ptr, cfg_ptr, level, prio, \
251 Z_DEVICE_STATE_DEFINE(node_id, Z_DEVICE_DT_DEV_NAME(node_id)) \
252 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_NAME(node_id), \
253 DEVICE_DT_NAME(node_id), init_fn, \
255 data_ptr, cfg_ptr, level, prio, \
257 &Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_NAME(node_id)), \
271#define DEVICE_DT_INST_DEFINE(inst, ...) \
272 DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
291#define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_NAME(node_id))
311#define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
323#define DEVICE_DT_INST_GET(inst) DEVICE_DT_GET(DT_DRV_INST(inst))
343#define DEVICE_DT_GET_ANY(compat) \
344 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
345 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
367#define DEVICE_DT_GET_ONE(compat) \
368 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
369 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
370 (ZERO_OR_COMPILE_ERROR(0)))
386#define DEVICE_DT_GET_OR_NULL(node_id) \
387 COND_CODE_1(DT_NODE_HAS_STATUS(node_id, okay), \
388 (DEVICE_DT_GET(node_id)), (NULL))
402#define DEVICE_GET(name) (&DEVICE_NAME_GET(name))
419#define DEVICE_DECLARE(name) static const struct device DEVICE_NAME_GET(name)
469#ifdef CONFIG_PM_DEVICE
471 struct pm_device *
const pm;
487 extern const struct device __device_start[];
507static inline const struct device *
510 extern const struct device __device_start[];
511 extern const struct device __device_end[];
512 const struct device *dev = NULL;
513 size_t numdev = __device_end - __device_start;
515 if ((dev_handle > 0) && ((
size_t)dev_handle <= numdev)) {
516 dev = &__device_start[dev_handle - 1];
613 while (region != 2) {
734size_t z_device_get_all_static(
const struct device * *devices);
750bool z_device_is_ready(
const struct device *dev);
769static inline bool z_impl_device_is_ready(
const struct device *dev)
771 return z_device_is_ready(dev);
787__deprecated
static inline int z_device_usable_check(
const struct device *dev)
789 return z_device_is_ready(dev) ? 0 : -
ENODEV;
815#define Z_DEVICE_HANDLE_NAME(node_id, dev_name) \
816 _CONCAT(__devicehdl_, \
817 COND_CODE_1(DT_NODE_EXISTS(node_id), \
821#define Z_DEVICE_EXTRA_HANDLES(...) \
822 FOR_EACH_NONEMPTY_TERM(IDENTITY, (,), __VA_ARGS__)
830#define Z_DEVICE_STATE_DEFINE(node_id, dev_name) \
831 static struct device_state Z_DEVICE_STATE_NAME(dev_name) \
832 __attribute__((__section__(".z_devstate")));
837#define Z_DEVICE_DEFINE_PRE(node_id, dev_name, ...) \
838 Z_DEVICE_DEFINE_HANDLES(node_id, dev_name, __VA_ARGS__)
879#define Z_DEVICE_DEFINE_HANDLES(node_id, dev_name, ...) \
880 extern const device_handle_t \
881 Z_DEVICE_HANDLE_NAME(node_id, dev_name)[]; \
882 const device_handle_t \
883 __aligned(sizeof(device_handle_t)) \
884 __attribute__((__weak__, \
885 __section__(".__device_handles_pass1"))) \
886 Z_DEVICE_HANDLE_NAME(node_id, dev_name)[] = { \
887 COND_CODE_1(DT_NODE_EXISTS(node_id), ( \
888 DT_DEP_ORD(node_id), \
889 DT_REQUIRES_DEP_ORDS(node_id) \
891 DEVICE_HANDLE_NULL, \
894 Z_DEVICE_EXTRA_HANDLES(__VA_ARGS__) \
896 COND_CODE_1(DT_NODE_EXISTS(node_id), \
897 (DT_SUPPORTS_DEP_ORDS(node_id)), ()) \
900#define Z_DEVICE_DEFINE_INIT(node_id, dev_name) \
901 .handles = Z_DEVICE_HANDLE_NAME(node_id, dev_name),
906#define Z_DEVICE_DEFINE(node_id, dev_name, drv_name, init_fn, pm_device,\
907 data_ptr, cfg_ptr, level, prio, api_ptr, state_ptr, ...) \
908 Z_DEVICE_DEFINE_PRE(node_id, dev_name, __VA_ARGS__) \
909 COND_CODE_1(DT_NODE_EXISTS(node_id), (), (static)) \
910 const Z_DECL_ALIGN(struct device) \
911 DEVICE_NAME_GET(dev_name) __used \
912 __attribute__((__section__(".z_device_" #level STRINGIFY(prio)"_"))) = { \
914 .config = (cfg_ptr), \
916 .state = (state_ptr), \
917 .data = (data_ptr), \
918 COND_CODE_1(CONFIG_PM_DEVICE, (.pm = pm_device,), ()) \
919 Z_DEVICE_DEFINE_INIT(node_id, dev_name) \
921 BUILD_ASSERT(sizeof(Z_STRINGIFY(drv_name)) <= Z_DEVICE_MAX_NAME_LEN, \
922 Z_STRINGIFY(DEVICE_NAME_GET(drv_name)) " too long"); \
923 Z_INIT_ENTRY_DEFINE(DEVICE_NAME_GET(dev_name), init_fn, \
924 (&DEVICE_NAME_GET(dev_name)), level, prio)
931#include <device_extern.h>
933#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:53
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:564
static int device_usable_check(const struct device *dev)
Determine whether a device is ready for use.
Definition: device.h:802
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:604
static device_handle_t device_handle_get(const struct device *dev)
Get the handle for a given device.
Definition: device.h:484
#define DEVICE_HANDLE_NULL
Flag value used to identify an unknown device.
Definition: device.h:70
#define DEVICE_HANDLE_SEP
Flag value used in lists of device handles to separate distinct groups.
Definition: device.h:60
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:508
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:541
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:67
int device_supported_foreach(const struct device *dev, device_visitor_callback_t visitor_cb, void *context)
Visit every device that dev directly supports.
#define ENODEV
Definition: errno.h:58
static ZTEST_BMEM volatile int ret
Definition: k_float_disable.c:28
Definitions of various linker Sections.
__INT16_TYPE__ int16_t
Definition: stdint.h:43
Runtime device dynamic structure (in RAM) per driver instance.
Definition: device.h:429
bool initialized
Definition: device.h:442
unsigned int init_res
Definition: device.h:437
Runtime device structure (in ROM) per driver instance.
Definition: device.h:450
const char * name
Definition: device.h:452
const void * api
Definition: device.h:456
struct device_state *const state
Definition: device.h:458
const device_handle_t *const handles
Definition: device.h:468
void *const data
Definition: device.h:460
const void * config
Definition: device.h:454