Z_DEVICE_DEFINE,定义设备实例并声明其初始化函数,由驱动程序使用
#define Z_DEVICE_DEFINE(node_id, dev_name, drv_name, init_fn, pm_device,\
data_ptr, cfg_ptr, level, prio, api_ptr, state_ptr, ...) \
Z_DEVICE_DEFINE_PRE(node_id, dev_name, __VA_ARGS__) \
COND_CODE_1(DT_NODE_EXISTS(node_id), (), (static)) \
const Z_DECL_ALIGN(struct device) \
DEVICE_NAME_GET(dev_name) __used \
__attribute__((__section__(".z_device_" #level STRINGIFY(prio)"_"))) = { \
.name = drv_name, \
.config = (cfg_ptr), \
.api = (api_ptr), \
.state = (state_ptr), \
.data = (data_ptr), \
COND_CODE_1(CONFIG_PM_DEVICE, (.pm = pm_device,), ()) \
Z_DEVICE_DEFINE_INIT(node_id, dev_name) \
}; \
BUILD_ASSERT(sizeof(Z_STRINGIFY(drv_name)) <= Z_DEVICE_MAX_NAME_LEN, \
Z_STRINGIFY(DEVICE_NAME_GET(drv_name)) " too long"); \
Z_INIT_ENTRY_DEFINE(DEVICE_NAME_GET(dev_name), init_fn, \
(&DEVICE_NAME_GET(dev_name)), level, prio)#ifdef __cplusplus
}
DEVICE_DT_GET,获取设备句柄
#define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
DT_REG_ADDR_BY_IDX,获取指定设备节点的寄存器基址
#define DT_REG_ADDR_BY_IDX(node_id, idx) \
DT_CAT(node_id, _REG_IDX_##idx##_VAL_ADDRESS)
DT_PROP_BY_PHANDLE_IDX,获取实例属性值
#define DT_PROP_BY_PHANDLE_IDX(node_id, phs, idx, prop) \
DT_PROP(DT_PHANDLE_BY_IDX(node_id, phs, idx), prop)
举例:
Example devicetree fragment:
n1: node-1 {
foo = <&n2 &n3>;
};
n2: node-2 {
bar = <42>;
};
n3: node-3 {
baz = <43>;
};
Example usage:
#define N1 DT_NODELABEL(n1)
DT_PROP_BY_PHANDLE_IDX(N1, foo, 0, bar) // 42
DT_PROP_BY_PHANDLE_IDX(N1, foo, 1, baz) // 43
DT_PROP,获取设备节点属性
#define DT_PROP(node_id, prop) DT_CAT(node_id, _P_##prop)
DT_IRQ_BY_IDX,在索引处获取中断说明符内的值
#define DT_IRQ_BY_IDX(node_id, idx, cell) \
DT_CAT(node_id, _IRQ_IDX_##idx##_VAL_##cell)
举例:
Example devicetree fragment:
my-serial: serial@... {
interrupts = < 33 0 >, < 34 1 >;
};
#define SERIAL DT_NODELABEL(my_serial)
Example usage Value
------------- -----
DT_IRQ_BY_IDX(SERIAL, 0, irq) 33
DT_IRQ_BY_IDX(SERIAL, 0, priority) 0
DT_IRQ_BY_IDX(SERIAL, 1, irq, 34
DT_IRQ_BY_IDX(SERIAL, 1, priority) 1
DT_INST,获取兼容的实例的节点标识符
#define DT_INST(inst, compat) UTIL_CAT(DT_N_INST, DT_DASH(inst, compat))
DT_NODELABEL,获取节点标签的节点标识符
#define DT_NODELABEL(label) DT_CAT(DT_N_NODELABEL_, label)
举例:
serial1: serial@40001000 {
label = "UART_0";
status = "okay";
current-speed = <115200>;
...
};
You can use DT_NODELABEL(serial1) to get a node identifier for the serial@40001000 node
DT_PROP(DT_NODELABEL(serial1), current_speed) // 115200
DT_FOREACH_PROP_ELEM,为属性“prop”的值中的每个元素调用“fn”
#define DT_FOREACH_PROP_ELEM(node_id, prop, fn) \
DT_CAT4(node_id, _P_, prop, _FOREACH_PROP_ELEM)(fn)
举例:
n: node {
my-ints = <1 2 3>;
};
#define TIMES_TWO(node_id, prop, idx) \
(2 * DT_PROP_BY_IDX(node_id, prop, idx)),
int array[] = {
DT_FOREACH_PROP_ELEM(DT_NODELABEL(n), my_ints, TIMES_TWO)
};
int array[] = { (2 * 1), (2 * 2), (2 * 3) };
DEVICE_DT_GET,通过@p node_id获取指向设备对象的指针
#define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))