Yocto框架接触一段时间以后就会越来越感觉到它的优势。原有layer可以让我们非常方便的利用现有资源,各种模版也可以根据自己的需求任意添加需求,不论是应用程序,还是驱动程序,还是系统组件和驱动模块。
前面的帖子尝试新添加应用程序和驱动模块,这次我们尝试在现在kernel里面新加一个驱动程序,并读取dts的一些属性设置。
1)源代码和dts相关
kernel的根目录:warp7_yocto/build/tmp/work-shared/imx7s-warp/kernel-source
c文件:drivers/char/unflattendts.c
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #define unflattendts_tiP "unflattendts_tip"
- int g_nNum = 0;
- char unflattendts_string[64] = "unflattendts, unflattendts_show";
- int unflattendts_major = 217;
- int unflattendts_open(struct inode *inode, struct file *filep)
- {
- int nNum = 99;
- printk("unflattendts_open nNum=%d g_nNum=%dn", nNum, g_nNum);
- return 0;
- }
-
- static const struct file_operations unflattendts_fops = {
- .owner = THIS_MODULE,
- .open = unflattendts_open,
- };
- static ssize_t show_unf(struct device_driver *driver, char *buf)
- {
- if(NULL != buf)
- {
- /* Newline is not appended on purpose, for convenience of reader programs */
- snprintf(buf, PAGE_SIZE, "%sn", unflattendts_string);
- return strlen(buf);
- }
- return 0;
- }
- static DRIVER_ATTR(unflattendts, 0444, show_unf, NULL);
- static struct attribute *unflattendts_attrs[] = {
- &driver_attr_unflattendts.attr,
- NULL,
- };
- static struct attribute_group unflattendts_group = {
- .name = "unflattendts",
- .attrs = unflattendts_attrs,
- };
- static const struct attribute_group *unflattendts_groups[] = {
- &unflattendts_group,
- NULL,
- };
- static int unflattendts_probe(struct platform_device *pdev)
- {
- int nNum;
- int ret;
- const char *m_string;
- printk("-0000-unflattendts_proben");
- if(NULL == pdev)
- {
- printk("unflattendts_probe: unflattendts_probe failed, pdev is NULLn");
- return 0;
- }
- if(NULL == pdev->dev.of_node)
- {
- printk( "unflattendts_probe: unflattendts_probe failed, of_node is NULLn");
- return 0;
- }
- /* unflattendts_TIP*/
- if(of_property_read_bool(pdev->dev.of_node, unflattendts_TIP))
- {
- printk( "unflattendts_probe: dtsi<%s> is existingn", unflattendts_TIP);
- }
- if (of_property_read_u32(pdev->dev.of_node, "linux,code", &nNum))
- {
- printk(KERN_INFO "=11= nNum=%dn", nNum);
- }
- else
- {
- printk(KERN_INFO "=0101= nNum=%dn", nNum);
- }
- g_nNum = nNum;
- if (of_property_read_string(pdev->dev.of_node, "linux,string", &m_string))
- {
- strcpy(unflattendts_string, m_string);
- printk(KERN_INFO "=11= m_string=%s unflattendts_string=%sn", m_string, unflattendts_string);
- }
- else
- {
- printk(KERN_INFO "=12= m_string=%sn", m_string);
- }
- return 0;
- }
- static struct of_device_id unflattendts_info_match_table[] = {
- { .compatible = "fsl,unflattendts",},
- { },
- };
- static struct platform_driver unflattendts = {
- .driver = {
- .name = "unflattendts",
- .owner = THIS_MODULE,
- .of_match_table = unflattendts_info_match_table,
- },
- .probe = unflattendts_probe,
- .remove = NULL,
- };
- static int __init unflattendts_init(void)
- {
- printk("unflattendts_initn");
- return platform_driver_register(&unflattendts);
- }
- static void __exit unflattendts_exit(void)
- {
- platform_driver_unregister(&unflattendts);
- printk("unflattendts_exitn");
- }
- module_init(unflattendts_init);
- module_exit(unflattendts_exit);
- MODULE_LICENSE("GPL");
复制代码
Makefile:driver/char
obj-$(CONFIG_UNFLATTENDTS) += unflattendts.o
imx_v7_defconfig:arch/arm/configs
CONFIG_UNFLATTENDTS=y
dts文件:arch/arm/boot/dts/imx7s-warp.dts
- memory {
- reg = <0x80000000 0x40000000>;
- };
- unflattendts {
- compatible = "fsl,unflattendts";
- #address-cells = <3>;
- #size-cells = <1>;
- unflattendts_tip;
- linux,code = <20170111>;
- linux,string = "NXP fsl";
- };
- gpio-keys {
- compatible = "gpio-keys";
- pinctrl-0 = <&pinctrl_gpio>;
- autorepeat;
- back {
- label = "Back";
- gpios = <&gpio7 1 GPIO_ACTIVE_HIGH>;
- linux,code = ;
- gpio-key,wakeup;
- autorepeat;
- };
- };
复制代码
2)编译
- bitbake linux-warp7 -c compile -f
复制代码
3)下载运行
unflattendts_init
-0000-unflattendts_probe
unflattendts_probe: dtsi is existing
=0101= nNum=20170111
=12= m_string=NXP fsl
后面尝试把开发板自带WIFI使能起来。
|