博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【总结】设备树语法及常用API函数【转】
阅读量:6977 次
发布时间:2019-06-27

本文共 3443 字,大约阅读时间需要 11 分钟。

本文转载自:

一、DTS编写语法

 

二、常用函数

设备树函数思路是:
uboot启动时将设备树地址传给内核,内核解析设备树,并创建设备,初始化相关属性,驱动中通过of_get_XXX函数去获取设备树加载时创建的设备。想要知道of函数做了什么,就去追踪这个函数最后调用了什么,同时也就知道了内核解析设备树的时候为我们创建了什么。
 
(1)of_get_named_gpio
/**
 * include/of_gpio.h
 * of_get_named_gpio - 从设备树中提取gpio口
 * @np - 设备节点指针
 * @propname - 属性名
 * @index - gpio口引脚标号 
 * 成功:得到GPIO口编号;失败:负数,绝对值是错误码
 */
int of_get_named_gpio(struct device_node *np, const char *propname, int index);
(2)gpio_to_irq
/**
 * include/gpio.h
 * PIN值转换为相应的IRQ值,中断编号可以传给request_irq()和free_irq()
 * @gpio - gpio口引脚标号 
 * 成功:得到GPIO口编号
 */
static inline int gpio_to_irq(unsigned gpio)
(3)devm_request_any_context_irq
/**
 * 注册中断
 */
devm_request_any_context_irq
(4)of_match_ptr
/**
 * 匹配设备树上的参数,将设备int_demo_dt_ids与驱动int_demo_driver联系起来
 * 系统会根据设备树种定义的compatible参数比较驱动中的int_demo_dt_ids中定义的 .compatible 参数
 */
of_match_ptr(int_demo_dt_ids)
例子:
static const struct of_device_id int_demo_dt_ids[] = {  
    { .compatible = "tiny4412,interrupt_demo", },  
    {},  
};  
  
MODULE_DEVICE_TABLE(of, int_demo_dt_ids);  
  
static struct platform_driver int_demo_driver = {  
    .driver        = {  
        .name      = "interrupt_demo",  
        .of_match_table    = of_match_ptr(int_demo_dt_ids),  
    },  
    .probe         = int_demo_probe,  
    .remove        = int_demo_remove,  
};  
 
(5)of_get_property
/*
 */drivers/of/base.c
 * Find a property with a given name for a given node
 * and return the value.
 * 通过给定的设备节点和属性名字得到value。
 */
const void *of_get_property(const struct device_node *np, const char *name,
int *lenp)
{
struct property *pp = of_find_property(np, name, lenp);
return pp ? pp->value : NULL;
}
 
(6)devm_pinctrl_get
获取一个pinctrl句柄,参数是dev是包含这个pin的device结构体即xxx这个设备的device
获取设备操作句柄(设备模型中的struct device)的pin control state holder(struct pinctrl)
/** 
 * struct devm_pinctrl_get() - Resource managed pinctrl_get() 
 * @dev: the device to obtain the handle for 
 * 
 * If there is a need to explicitly destroy the returned struct pinctrl, 
 * devm_pinctrl_put() should be used, rather than plain pinctrl_put(). 
 */  
struct pinctrl *devm_pinctrl_get(struct device *dev)  
(7)pinctrl_lookup_state
获取这个pin对应pin_state(引脚状态-turnon_tes/turnoff_tes)
/** 
 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle 
 * @p: the pinctrl handle to retrieve the state from 
 * @name: the state name to retrieve 
 */  
struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)  
(8)pinctrl_select_state
设置引脚为为某个stata -- turnon_tes/turnoff_tes
/** 
 * pinctrl_select_state() - select/activate/program a pinctrl state to HW 
 * @p: the pinctrl handle for the device that requests configuration 
 * @state: the state handle to select/activate/program 
 */  
int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)  
(9)of_get_named_gpio
得到GPIO的编号
./**
 * include/of_gpio.h
 * of_get_named_gpio - 从设备树中提取gpio口
 * @np - 设备节点指针
 * @propname - 属性名
 * @index - gpio口引脚标号 
 * 成功:得到GPIO口编号int型;失败:负数,绝对值是错误码
 */
int of_get_named_gpio(struct device_node *np, const char *propname, int index);
of_get_named_gpio:此函数是解析设备树的函数,我们通过这个函数去解析设备树,tiny4412,int_gpio1 = <&gpx3 2 GPIO_ACTIVE_HIGH>; 
跟踪下去会发现这个函数掉用了list = of_get_property(np, "tiny4412,int_gpio2", &size);设备树解析是创界了设备节点,现在通过这个函数去获取属性。
(10)devm_gpio_request_one
获取一个GPIO并初始化属性
/**
 * devm_gpio_request_one - request a single GPIO with initial setup
 * @dev:   device to request for
 * @gpio:  the GPIO number
 * @flags:  GPIO configuration as specified by GPIOF_*
 * @label:  a literal description string of this GPIO
 */
int devm_gpio_request_one(struct device *dev, unsigned gpio,
 unsigned long flags, const char *label)
你可能感兴趣的文章
通过改进算法来优化程序性能的真实案例(Ransac)
查看>>
head命令
查看>>
软件开发经验总结(一)细节决定软件的成败
查看>>
python tar.gz格式压缩、解压
查看>>
JNDI概述(转载)
查看>>
利用java反射机制 读取配置文件 实现动态类载入以及动态类型转换
查看>>
第 7 章 项目运作
查看>>
PYTHON黑帽编程1.5 使用WIRESHARK练习网络协议分析
查看>>
.NET平台开源项目速览(18)C#平台JSON实体类生成器JSON C# Class Generator
查看>>
C# 格式串(收藏)
查看>>
浅谈SQL Server中统计对于查询的影响
查看>>
WF4 Beta,RC版文章总结
查看>>
WPF 与Surface 2.0 SDK 亲密接触–LibraryContainer 篇
查看>>
C# 对应 Oracle 存储过程 的 SYS_REFCURSOR 应该 传入什么类型的参数?
查看>>
Unity3D移植到自己的Android程序
查看>>
【转】用示例说明索引数据块中出现热块的场景,并给出解决方案
查看>>
HDU 2034 人见人爱A-B
查看>>
【AngularJS】—— 12 独立作用域
查看>>
使用工作集(Working Set)整理项目
查看>>
MailMail、RegeX等程序的云端版
查看>>