内核源代码中的一个示例表明了一种可能的解决方案。
如果您的节点属性由 n 元组列表组成,则of_property_read_u32_index()可用于获取第 i 个元组的单个 u32 参数:
num_args = 4;
if (!of_get_property(np, "relays", &tmp))
return NULL;
num_relays = tmp / (sizeof(u32) * num_args);
for (i = 0; i < num_relays; i++) {
offset = i * num_args;
if (of_property_read_u32_index(np, "relays", offset, &arg0))
goto err;
if (of_property_read_u32_index(np, "relays", offset + 1, &arg1))
goto err;
if (of_property_read_u32_index(np, "relays", offset + 2, &arg2))
goto err;
if (of_property_read_u32_index(np, "relays", offset + 3, &arg3))
goto err;
}
驱动程序 drivers/clk/at91/clk-pll.c使用类似的代码从atmel,pll-clk-output-ranges
属性中检索值:
pmc: pmc@fffffc00 {
plla: pllack {
atmel,clk-input-range = <2000000 32000000>;
atmel,pll-clk-output-ranges = <745000000 800000000 0 0>,
<695000000 750000000 1 0>,
<645000000 700000000 2 0>,
<595000000 650000000 3 0>,
<545000000 600000000 0 1>,
<495000000 550000000 1 1>,
<445000000 500000000 2 1>,
<400000000 450000000 3 1>;
};
...
};
附录
似乎属性定义中的尖括号对编译后的dtb中的整数数组没有影响。
以下两个属性定义:
node1 {
xxx = <745000000 800000000 0 0>,
<695000000 750000000 1 0>,
<645000000 700000000 2 0>,
<595000000 650000000 3 0>,
<545000000 600000000 0 1>,
<495000000 550000000 1 1>,
<445000000 500000000 2 1>,
<400000000 450000000 3 1>;
};
node2 {
xxx = < 745000000 800000000 0 0
695000000 750000000 1 0
645000000 700000000 2 0
595000000 650000000 3 0
545000000 600000000 0 1
495000000 550000000 1 1
445000000 500000000 2 1
400000000 450000000 3 1 >;
};
从/proc/device-tree/转储时具有相同的值:
# hexdump -C node1/xxx
00000000 2c 67 cc 40 2f af 08 00 00 00 00 00 00 00 00 00 |,g.@/...........|
00000010 29 6c db c0 2c b4 17 80 00 00 00 01 00 00 00 00 |)l..,...........|
00000020 26 71 eb 40 29 b9 27 00 00 00 00 02 00 00 00 00 |&q.@).'.........|
00000030 23 76 fa c0 26 be 36 80 00 00 00 03 00 00 00 00 |#v..&.6.........|
00000040 20 7c 0a 40 23 c3 46 00 00 00 00 00 00 00 00 01 | |.@#.F.........|
00000050 1d 81 19 c0 20 c8 55 80 00 00 00 01 00 00 00 01 |.... .U.........|
00000060 1a 86 29 40 1d cd 65 00 00 00 00 02 00 00 00 01 |..)@..e.........|
00000070 17 d7 84 00 1a d2 74 80 00 00 00 03 00 00 00 01 |......t.........|
00000080
# hexdump -C node2/xxx
00000000 2c 67 cc 40 2f af 08 00 00 00 00 00 00 00 00 00 |,g.@/...........|
00000010 29 6c db c0 2c b4 17 80 00 00 00 01 00 00 00 00 |)l..,...........|
00000020 26 71 eb 40 29 b9 27 00 00 00 00 02 00 00 00 00 |&q.@).'.........|
00000030 23 76 fa c0 26 be 36 80 00 00 00 03 00 00 00 00 |#v..&.6.........|
00000040 20 7c 0a 40 23 c3 46 00 00 00 00 00 00 00 00 01 | |.@#.F.........|
00000050 1d 81 19 c0 20 c8 55 80 00 00 00 01 00 00 00 01 |.... .U.........|
00000060 1a 86 29 40 1d cd 65 00 00 00 00 02 00 00 00 01 |..)@..e.........|
00000070 17 d7 84 00 1a d2 74 80 00 00 00 03 00 00 00 01 |......t.........|
00000080
# cmp node1/xxx node2/xxx
#
在任一版本的属性中,尖括号(又名 V 形)和逗号似乎都被忽略了,并且这些值被简单地存储为整数的一维数组(具有大端字节序)。
附录 2
上述结果由 DeviceTree 规范(版本 v0.2-18-g8466c3b-dirty)第 6.3 节中的两个语句确认:
单元数组(即由 32 位组成的信息单元)由围绕空格分隔的 C 样式整数列表的尖括号表示
和
(属性)值可能有几个逗号分隔的组件,它们连接在一起。
因此,作为单个数组的属性值与多个连接的数组无法区分。
使用 n 元组列表纯粹是为了美观和可读性。
在 dtb 级别无法识别数据的“组织”(作为 n 元组)。
因此,n
n 元组(或“单元列表”)的大小应该在另一个属性中传递给驱动程序,或者在绑定文档中指定(当在驱动程序中硬编码时)。