嵌入式Linux系統:驅動編程_LED驅動

        LED驅動寫好並註冊到系統後,上層應用通過設備節點“/dev/hello_ctl”操作LED可以像訪問普通文件一樣直接使用open、close、ioctl函數實現。

        如下圖所示,應用比較簡單,調用延時函數,首先將 Led 點亮三秒,然後再滅掉三秒,再點亮。

 

驅動程序編寫

1、定義hello_open、hello_ioctl、hello_release等函數;

 

2、定義file_operations結構體hello_ops;

 

3、定義miscdevice結構體hello_dev上層應用中用到的設備節點“/dev/hello_ctl”與本結構體中name字段一致,見宏定義 #define DEVICE_NAME "hello_ctl",設備節點在Linux系統 “/dev”目錄下);

 

4、在hello_probe函數中初始化GPIO並調用函數misc_register註冊雜項設備節點;在hello_release函數中並調用函數misc_deregister卸載雜項設備節點。

 

5、定義platform_driver結構體hello_driver;

 

6、在模塊入口和出口調用函數 platform_driver_register 和platform_driver_unregister註冊和卸載驅動;

 

 

 

        如下,是完整的LED驅動的代碼:leds.c

        以下代碼實現的功能是將驅動“hello_ctl”註冊到平臺總線(見博文:嵌入式Linux系統:驅動編程_註冊驅動到平臺總線  )並生成雜項設備節點“hello_ctl”(見博文:嵌入式Linux系統:驅動編程_生成雜項設備節點 ),上層應用通過設備節點“/dev/hello_ctl”操作LED可以像訪問普通文件一樣直接使用open、close、ioctl函數實現。

        前提是已將設備“hello_ctl”註冊到平臺總線(見博文:嵌入式Linux系統:驅動編程_註冊設備到平臺總線 )。

 

#include <linux/init.h>
#include <linux/module.h>

/*驅動註冊的頭文件,包含驅動的結構體和註冊和卸載的函數*/
#include <linux/platform_device.h>
/*註冊雜項設備頭文件*/
#include <linux/miscdevice.h>
/*註冊設備節點的文件結構體*/
#include <linux/fs.h>

/*Linux中申請GPIO的頭文件*/
#include <linux/gpio.h>
/*三星平臺的GPIO配置函數頭文件*/
/*三星平臺EXYNOS系列平臺,GPIO配置參數宏定義頭文件*/
#include <plat/gpio-cfg.h>
#include <mach/gpio.h>
/*三星平臺4412平臺,GPIO宏定義頭文件*/
#include <mach/gpio-exynos4.h>

#define DRIVER_NAME "hello_ctl"

#define DEVICE_NAME "hello_ctl"


MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");

static long hello_ioctl( struct file *files, unsigned int cmd, unsigned long arg){
	printk("cmd is %d,arg is %d\n",cmd,arg);
	
	if(cmd > 1){
		printk(KERN_EMERG "cmd is 0 or 1\n");
	}
	if(arg > 1){
		printk(KERN_EMERG "arg is only 1\n");
	}
	
	gpio_set_value(EXYNOS4_GPL2(0),cmd);
	
	return 0;
}

static int hello_release(struct inode *inode, struct file *file){
	printk(KERN_EMERG "hello release\n");
	return 0;
}

static int hello_open(struct inode *inode, struct file *file){
	printk(KERN_EMERG "hello open\n");
	return 0;
}


static struct file_operations hello_ops = {
	.owner = THIS_MODULE,
	.open = hello_open,
	.release = hello_release,
	.unlocked_ioctl = hello_ioctl,
};

static  struct miscdevice hello_dev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = DEVICE_NAME,
	.fops = &hello_ops,
};


static int hello_probe(struct platform_device *pdv){
	int ret;
	
	printk(KERN_EMERG "\tinitialized\n");
	
	ret = gpio_request(EXYNOS4_GPL2(0),"LEDS");
	if(ret < 0){
		printk(KERN_EMERG "gpio_request EXYNOS4_GPL2(0) failed!\n");
		return ret;
	}
	
	s3c_gpio_cfgpin(EXYNOS4_GPL2(0),S3C_GPIO_OUTPUT);
	
	gpio_set_value(EXYNOS4_GPL2(0),0);
	
	misc_register(&hello_dev);
	
	return 0;
}

static int hello_remove(struct platform_device *pdv){
	
	printk(KERN_EMERG "\tremove\n");
	misc_deregister(&hello_dev);
	return 0;
}

static void hello_shutdown(struct platform_device *pdv){
	
	;
}

static int hello_suspend(struct platform_device *pdv,pm_message_t pmt){
	
	return 0;
}

static int hello_resume(struct platform_device *pdv){
	
	return 0;
}

struct platform_driver hello_driver = {
	.probe = hello_probe,
	.remove = hello_remove,
	.shutdown = hello_shutdown,
	.suspend = hello_suspend,
	.resume = hello_resume,
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	}
};


static int hello_init(void)
{
	int DriverState;
	
	printk(KERN_EMERG "HELLO WORLD enter!\n");
	DriverState = platform_driver_register(&hello_driver);
	
	printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
	return 0;
}


static void hello_exit(void)
{
	printk(KERN_EMERG "HELLO WORLD exit!\n");
	
	platform_driver_unregister(&hello_driver);	
}

module_init(hello_init);
module_exit(hello_exit);

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章