3288学习笔记------驱动设备树DTS的简介与例子

DTS文档的位置

在上一节课hello.c基础上改动

git log

git show

查看改动部分

 1、改变了DTS

2、改变了hello.c

 具体如下:

在hello.c 中,增加了必要的头文件

在hello.c 中,增加了探针函数firefly_hello_probe  -------平台驱动一开始进入的函数

在hello.c 中,增加退出函数firefly_hello_remove

在hello.c 中,增加了结构体,

衔接DTS和驱动的结构体,非常关键

在hello.c 中,增加了平台驱动的结构体,

hello.c代码

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#ifdef CONFIG_OF
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>
#endif


#define GPIO_LOW 0
#define GPIO_HIGH 1

static int firefly_hello_probe(struct platform_device *pdev) 
{
    int ret = -1;
	int i;
    int gpio,flag;
	struct device_node *hello_node = pdev->dev.of_node;

	printk("%s-%d: enter\n",__FUNCTION__,__LINE__);

	gpio = of_get_named_gpio_flags(hello_node,"led", 0,&flag);
	if (!gpio_is_valid(gpio)){
		printk("hello: invalid gpio : %d\n",gpio);
		return -1;
	}
    ret = gpio_request(gpio, "hello_led");
	if (ret != 0) {
		gpio_free(gpio);
		return -EIO;
	}

	gpio_direction_output(gpio, GPIO_HIGH);

	for(i=0; i < 10; i++)
	{
		gpio_set_value(gpio,GPIO_LOW);
		mdelay(500);
		gpio_set_value(gpio,GPIO_HIGH);
		mdelay(500);
	}

	printk("%s-%d: exit\n",__FUNCTION__,__LINE__);

	return 0;  //return Ok
}


static int firefly_hello_remove(struct platform_device *pdev)
{
    return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id of_firefly_hello_match[] = {
	{ .compatible = "firefly,hello_led" },
	{ /* Sentinel */ }
};
#endif

static struct platform_driver firefly_hello_driver = {
	.probe		= firefly_hello_probe,  
	.remove		= firefly_hello_remove, 
	.driver		= {
		.name	= "firefly_hello",
		.owner	= THIS_MODULE,
#ifdef CONFIG_OF
		.of_match_table	= of_firefly_hello_match,   
#endif
	},

};

static int __init hello_init(void)  
{
    printk(KERN_INFO "Enter %s\n", __FUNCTION__);
    return platform_driver_register(&firefly_hello_driver);  
    return 0;
}

static void __exit hello_exit(void)  
{
	platform_driver_unregister(&firefly_hello_driver);
    printk("Exit Hello world\n");
}

subsys_initcall(hello_init); 
module_exit(hello_exit);   



MODULE_AUTHOR("sai <271319925@qq.com>");
MODULE_DESCRIPTION("Firefly hello driver");
MODULE_LICENSE("GPL");

DTS代码中添加的代码

hello-led{
		compatible = "firefly,hello_led";
		led= <&gpio8 GPIO_A2 GPIO_ACTIVE_LOW>;
		status = "okay";
	};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值