参考:https://zhuanlan.zhihu.com/p/112700249
上一篇:荔枝派zero驱动开发02:完善字符设备
下一篇:荔枝派zero驱动开发04:GPIO操作(寄存器方式)
概述
一句话概括:设备树是一种描述硬件参数的数据结构,与json并无本质区别,用于向内核传递信息。得益于Linux内核优秀的架构设计和丰富的源码,需要使用内核中的驱动代码时,编写匹配的设备树给出必要的配置信息即可,无需重复编写驱动代码;也可以实现在内核代码中预先编译驱动,在不改动内核的情况下配置设备树后就可以启用某项功能。
本文尝试在内核驱动中,实现一个获取和使用设备树信息的例程,仅演示向内核传递信息的作用
常用设备树操作函数
-
inline struct device_node *of_find_node_by_path(const char *path)
-
struct device_node *of_find_node_by_name(struct device_node *from, const char *name)
通过节点路径或节点名称查找节点
-
int of_property_read_string(struct device_node *np, const char *propname, const char **out_string)
用于读取属性中字符串值
-
int of_property_read_u32(const struct device_node *np, const char *propname, u32 *out_value)
-
int of_property_read_u32_array(const struct device_node *np, const char *propname, u32 *out_values, size_t sz)
用于读取整形值或数组,支持8位、16位、32位和64位
设备树修改
在根节点下添加测试节点,编译设备树并使用新的设备树文件启动系统
testnode {
compatible = "user,testnode";
status = "okay";
regs = <0x1000 0x100 0x3456 0x04>;
value = <789>;
testchildnode {
compatible = "user,childnode";
value = <0x666>;
};
};
驱动编写
源码附在文后,参考前述章节编译并生成devicetreebase.ko,将驱动拷贝到开发板加载测试
本篇比较简单,全部在module_init()中完成即可,无需注册字符设备相关操作
测试
源码
devicetreebase.c
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/of.h>
#include <linux/of_address.h>
// 入口与出口需使用__init和__exit修饰
static int __init chardev_init(void)
{
struct device_node *np;
uint32_t value;
uint32_t vals[4];
// 通过全路径查找节点
np = of_find_node_by_path("/testnode");
// np = of_find_node_by_name(NULL, "testnode"); // 通过名称查找节点
if (!np)
{
printk("Failed to find testnode\n");
return -EINVAL;
}
// 判断compatible属性
if (of_device_is_compatible(np, "user,testnode"))
{
const char *str;
// 提取并打印compatible属性
of_property_read_string(np, "compatible", &str);
printk("compatible find \"%s\"\n", str);
// 提取status属性
of_property_read_string(np, "status", &str);
printk("status find \"%s\"\n", str);
}
// 读取属性值(u32)
of_property_read_u32(np, "value", &value);
printk("value: %d\n", value);
// 读取数组
of_property_read_u32_array(np, "regs", vals, 4);
printk("Regs: 0x%08x 0x%08x 0x%08x 0x%08x\n", vals[0], vals[1], vals[2], vals[3]);
struct device_node *child_node;
// child_node = of_find_node_by_name(np, "testchildnode"); // 按名称查找np节点的子节点
// child_node = of_get_next_child(np,NULL); //迭代查找子节点
child_node = of_find_node_by_path("/testnode/testchildnode"); // 按全路径查找子节点
if (!child_node)
{
printk("Failed to find child node 'testchildnode'\n");
return -EINVAL;
}
of_property_read_u32(child_node, "value", &value);
printk(KERN_INFO "Child Node value: 0x%08x\n", value);
return 0;
}
static void __exit chardev_exit(void)
{
printk("exit()\r\n");
}
// 宏
module_init(chardev_init);
module_exit(chardev_exit);
// 固定
MODULE_LICENSE("GPL");
MODULE_AUTHOR("USER");
MODULE_INFO(intree, "Y");