华清远见上海中心22071

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
/*
mynode@0x12345678{
    compatible = "hqyj,mynode";
    astring = "hello 22071";
    uint  = <0xaabbccdd 0x11223344>;
    binarry = [00 0c 27 b9  f9 be];
    mixed = "hello", [11 22], <0x12345678>;
 }
*/

struct device_node *node;

static int __init demo_init(void)
{
    int res, i;
    unsigned int value1, value2;
    unsigned int value[10] = {0};
    const char *buf;
    unsigned char arr[128] = "";
    node = of_find_node_by_path("/mynode@0x12345678");
    if (NULL == node)
    {
        printk("访问设备树节点失败\n");
        return -EFAULT;
    }
    printk("success to find node\n");

   
    res = of_property_read_u32_index(node, "uint", 0, &value1);
    if (res)
    {
        printk("获取数值失败\n(err %d)", -res);
        return -res;
    }

    res = of_property_read_u32_index(node, "uint", 1, &value2);
    if (res)
    {
        printk("获取数值失败(err %d)\n", -res);
        return -res;
    }
    printk("mynode uint = %#x, %#x\n", value1, value2);

    
    res = of_property_read_variable_u32_array(node, "uint", value, 0, 2);
    if (res < 0)
    {
        printk("获取数值失败(err %d)\n", -res);
        return -res;
    }
    for (i = 0; i < res; i++)
    {
        printk("mynode uint = %#x\n", value[i]);
    }

  
    res = of_property_read_string(node, "astring", &buf);
    if (res)
    {
        printk("获取数值失败(err %d)\n", -res);
        return -res;
    }
    printk("mynode astring = %s\n", buf);

    res = of_property_read_variable_u8_array(node, "binarry", arr, 1, 8);
    if (res < 0)
    {
        printk("获取数值失败(err %d)\n", -res);
        return -res;
    }
    for (i = 0; i < res; i++)
    {
        printk("mynode binary = %#x\n", arr[i]);
    }

    res = of_property_read_u8_array(node, "compatible", arr, 12);
    if (res)
    {
        printk("获取数值失败(err %d)\n", -res);
        return -res;
    }
    for (i = 0; i < 12; i++)
    {
        printk("mynode compatible = %c\n", arr[i]);
    }

    return 0;
}

static void __exit demo_exit(void)
{
}

module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值