Linux 3.14的裝置樹-ARM架構-4412平臺,最詳細的實戰開發程式碼(三)
本章節主要講解程式碼中如何使用裝置樹對應的介面
視訊講解,請點解如下連結:
Linux裝置樹驅動開發視訊-of解析dts節點的API-8
Linux ARM裝置樹驅動開發視訊-程式碼中獲取節點(9)
Linux ARM裝置樹驅動開發視訊-程式碼中獲取屬性(10)
=======================================================
OF提供的函式主要集中在drivers/of/目錄下,有address.c,base.c,device.c,fdt.c,irq.c,platform.c等等
1,根據deice_node結構的full_name
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 node的compatible匹配,返回匹配度
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,讀取該裝置的第index個irq號
unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
=======================================================
現在要在dts中新增如下節點,並且在程式碼中獲取到如下資訊:
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 4
static 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)
{
/*
[email protected]{
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("/[email protected]");
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");