Linux 3.14的设备树-ARM架构-4412平台,最详细的实战开发代码(三)

来源:互联网 发布:麻将游戏app源码 编辑:程序博客网 时间:2024/05/22 12:19

本章节主要讲解代码中如何使用设备树对应的接口

视频讲解,请点解如下链接:

Linux设备树驱动开发视频-of解析dts节点的API-8

 https://v.qq.com/x/page/v0378tv6esg.html

Linux ARM设备树驱动开发视频-代码中获取节点(9)

 

https://v.qq.com/x/page/x0378fw9wxh.html

Linux ARM设备树驱动开发视频-代码中获取属性(10)

https://v.qq.com/x/page/a0381pzcqeu.html

=======================================================

OF提供的函数主要集中在drivers/of/目录下,有address.c,base.c,device.c,fdt.c,irq.c,platform.c等等


1,根据deice_node结构的full_name参数,在全局链表of_allnodes中,查找合适的device_node

struct device_node *of_find_node_by_path(const char *path)

 

2,根据property结构的name参数,在指定的device node中查找合适的property

struct property *of_find_property(const struct device_node *np,const char *name,int *lenp)

3,根据compat参数与device nodecompatible匹配,返回匹配度

int of_device_is_compatible(const struct device_node *device,const char *compat)

4,获得父节点的device node

struct device_node *of_get_parent(const struct device_node *node)

5,根据属性名propname,读出该属性的数组中sz个属性值给out_values

int of_property_read_u32_array(const struct device_node *np,const char *propname,,u8 *out_values, size_t sz)


6,读取该设备的第indexirq

unsigned int irq_of_parse_and_map(struct device_node *dev, int index)

=======================================================

现在要在dts中添加如下节点,并且在代码中获取到如下信息:

test_nod@12345678{

                compatible = "test,farsight";

                reg = <0xa2345678 0x24

                         0xb3456780 0x24>;

                testprop,mytest;

                test_list_string = "red fish", "blue fish";

                interrupt-parent = <&gpx1>;  // 因为按键接到了gpx1_1

                interrupts = <1 2>;  

                // 因为按键接到了gpx1_1,如果接到gpx2_1,那么就是<2 2>

        };


#include <linux/init.h>#include <linux/module.h>#include <linux/of.h>#include <linux/of_irq.h>#include <linux/interrupt.h>#define U32_DATA_LEN 4static  int is_good;static int irqno;irqreturn_t key_irq_handler(int irqno, void *devid){printk("------------------------key pressed \n");return IRQ_HANDLED;}static int __init dt_drv_init(void){/* test_nod@12345678{                compatible = "farsight,test";                reg = <0x12345678 0x24                        0x87654321 0x24>;                testprop,mytest;                test_list_string = "red fish","fly fish", "blue fish";                 interrupt-parent = <&gpx1>;                interrupts = <1 4>;        };*/// 在代码中获取节点的所有信息//先把节点获取到struct device_node *np = NULL;np = of_find_node_by_path("/test_nod@12345678");if(np){printk("find test node ok\n");printk("node name = %s\n", np->name);printk("node full name = %s\n", np->full_name);}else{printk("find test node failed\n");}//获取到节点中的属性struct property *prop = NULL;prop = of_find_property(np, "compatible",NULL);if(prop){printk("find compatible ok\n");printk("compatible value = %s\n", prop->value);printk("compatible name = %s\n", prop->name);}else{printk("find compatible failed\n");}if(of_device_is_compatible(np, "farsight,test")){printk("we have a compatible named farsight,test\n");}//读取到属性中的整数的数组u32 regdata[U32_DATA_LEN];int ret;ret = of_property_read_u32_array(np, "reg", regdata, U32_DATA_LEN);if(!ret){int i;for(i=0; i<U32_DATA_LEN; i++)printk("----regdata[%d] = 0x%x\n", i,regdata[i]);}else{printk("get reg data failed\n");}//读取到属性中的字符串的数组const char *pstr[3];int i;for(i=0; i<3; i++){ret = of_property_read_string_index(np, "test_list_string", i, &pstr[i]);if(!ret){printk("----pstr[%d] = %s\n", i,pstr[i]);}else{printk("get pstr data failed\n");}}// 属性的值为空,实际可以用于设置标志if(of_find_property(np, "testprop,mytest", NULL)){is_good = 1;printk("is_good = %d\n", is_good);}// 获取到中断的号码irqno = irq_of_parse_and_map(np, 0);printk("-----irqno = %d\n", irqno);//验证中断号码是否有效ret = request_irq(irqno, key_irq_handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "key_irq", NULL);if(ret){printk("request_irq error\n");return -EBUSY;}return 0;}static void __exit dt_drv_exit(void){free_irq(irqno,  NULL);}module_init(dt_drv_init);module_exit(dt_drv_exit);MODULE_LICENSE("GPL");


原创粉丝点击