常用设备树操作函数
- 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]);
## 最后
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。**
**因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/27e013180ae5ae6d2529916e7db878cc.png)
![img](https://img-blog.csdnimg.cn/img_convert/3fcbde6b80c6b2d852b7cb66514ae6bf.jpeg)
![img](https://img-blog.csdnimg.cn/img_convert/5b54fd54caea69ab214ee1cbf5b6c8e6.png)
![img](https://img-blog.csdnimg.cn/img_convert/9af54d1dd5c8ddbfc43700ee859981c9.png)
![img](https://img-blog.csdnimg.cn/img_convert/f56d53976f328f66cd3b420280c21220.png)
![img](https://img-blog.csdnimg.cn/img_convert/14e06750cc90ef7d66a21383191b0eb2.png)
![](https://img-blog.csdnimg.cn/img_convert/f4f6698d252459c9daf03ed1e0d2f226.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!
习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!