4.在Yocto框架添加驱动程序验证
时间:10-02
整理:3721RD
点击:
Yocto框架接触一段时间以后就会越来越感觉到它的优势。原有layer可以让我们非常方便的利用现有资源,各种模版也可以根据自己的需求任意添加需求,不论是应用程序,还是驱动程序,还是系统组件和驱动模块。
前面的帖子尝试新添加应用程序和驱动模块,这次我们尝试在现在kernel里面新加一个驱动程序,并读取dts的一些属性设置。
1)源代码和dts相关
kernel的根目录:warp7_yocto/build/tmp/work-shared/imx7s-warp/kernel-source
c文件:drivers/char/unflattendts.c
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
2)编译
3)下载运行
unflattendts_init
-0000-unflattendts_probe
unflattendts_probe: dtsi<unflattendts_tip> is existing
=0101= nNum=20170111
=12= m_string=NXP fsl
后面尝试把开发板自带WIFI使能起来。
前面的帖子尝试新添加应用程序和驱动模块,这次我们尝试在现在kernel里面新加一个驱动程序,并读取dts的一些属性设置。
1)源代码和dts相关
kernel的根目录:warp7_yocto/build/tmp/work-shared/imx7s-warp/kernel-source
c文件:drivers/char/unflattendts.c
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/device.h>
- #include <linux/platform_device.h>
- #include <linux/sysfs.h>
- #include <linux/slab.h>
- #include <linux/kernel.h>
- #include <linux/kobject.h>
- #include <linux/string.h>
- #include <linux/of.h>
- #include <linux/cdev.h>
- #include <linux/fs.h>
- #include <linux/vmalloc.h>
- #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=%d\n", 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, "%s\n", 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_probe\n");
- if(NULL == pdev)
- {
- printk("unflattendts_probe: unflattendts_probe failed, pdev is NULL\n");
- return 0;
- }
- if(NULL == pdev->dev.of_node)
- {
- printk( "unflattendts_probe: unflattendts_probe failed, of_node is NULL\n");
- return 0;
- }
- /* unflattendts_TIP*/
- if(of_property_read_bool(pdev->dev.of_node, unflattendts_TIP))
- {
- printk( "unflattendts_probe: dtsi<%s> is existing\n", unflattendts_TIP);
- }
- if (of_property_read_u32(pdev->dev.of_node, "linux,code", &nNum))
- {
- printk(KERN_INFO "=11= nNum=%d\n", nNum);
- }
- else
- {
- printk(KERN_INFO "=0101= nNum=%d\n", 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=%s\n", m_string, unflattendts_string);
- }
- else
- {
- printk(KERN_INFO "=12= m_string=%s\n", 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_init\n");
- return platform_driver_register(&unflattendts);
- }
- static void __exit unflattendts_exit(void)
- {
- platform_driver_unregister(&unflattendts);
- printk("unflattendts_exit\n");
- }
- 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 = <KEY_BACK>;
- gpio-key,wakeup;
- autorepeat;
- };
- };
2)编译
- bitbake linux-warp7 -c compile -f
3)下载运行
unflattendts_init
-0000-unflattendts_probe
unflattendts_probe: dtsi<unflattendts_tip> is existing
=0101= nNum=20170111
=12= m_string=NXP fsl
后面尝试把开发板自带WIFI使能起来。