⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 led.c

📁 一个简单的字符设备的驱动--led的驱动,对于学习linux驱动有一定的帮助!
💻 C
字号:
/* 	the led driver for matrix v5	date: 2007.03.16	by jjj.*///-------------------------------------#include <linux/module.h>#include <linux/fs.h>#include <linux/string.h>#include <linux/init.h>#include <linux/device.h>#include <linux/interrupt.h>#include <linux/rtc.h>#include <linux/bcd.h>#include <linux/delay.h>#include <linux/devfs_fs_kernel.h> #include <asm/hardware.h>#include <asm/uaccess.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/rtc.h>#include <asm/mach/time.h>#include <asm/hardware/clock.h>#include <asm/arch/regs-gpio.h> #define 		LED1		0x01#define 		LED2		0x02#define 		LED3		0x04#define 		LED4		0x08static int matrix_led_major = 0;static unsigned char led_status = 0;static void set_led_status(unsigned char status){	unsigned char led_status_changed;	led_status_changed = led_status ^ (status & 0x0F);	led_status = status & 0x0F;	if(led_status_changed != 0x00)	{		if(led_status_changed & LED1)		{			if(led_status & LED1)				s3c2410_gpio_setpin(S3C2410_GPB7,0);			else				s3c2410_gpio_setpin(S3C2410_GPB7,1);		}		if(led_status_changed & LED2)		{			if(led_status & LED2)				s3c2410_gpio_setpin(S3C2410_GPB8,0);			else				s3c2410_gpio_setpin(S3C2410_GPB8,1);		}			if(led_status_changed & LED3)		{			if(led_status & LED3)				s3c2410_gpio_setpin(S3C2410_GPB9,0);			else				s3c2410_gpio_setpin(S3C2410_GPB9,1);		}			if(led_status_changed & LED4)		{			if(led_status & LED4)				s3c2410_gpio_setpin(S3C2410_GPB10,0);			else				s3c2410_gpio_setpin(S3C2410_GPB10,1);		}	}}static int matrix_led_open(struct inode *inode,struct file *filp){	return 0;}static int matrix_led_release(struct inode *inode,struct file *filp){	return 0;}static ssize_t matrix_led_read(struct file *filp,char *buff,size_t count,loff_t *offp){
	copy_to_user(buff,(char *)&led_status,sizeof(unsigned char));	return sizeof(unsigned char);}static ssize_t matrix_led_write(struct file *filp,const char *buff,size_t count,loff_t *offp){	unsigned char status;	if(count == 1)	{
		if(copy_from_user(&status,buff,sizeof(unsigned char)))
			return -EFAULT;		set_led_status(status);		return sizeof(unsigned char);	}
	else		return -EFAULT;}static struct file_operations matrix_led_fops = { 	.owner 	= THIS_MODULE, 	.open   	= matrix_led_open,	.read   	= matrix_led_read,	.write	= matrix_led_write,	.release	= matrix_led_release,}; static int __init matrix_led_init(void){   int ret;   ret = register_chrdev(0,"led",&matrix_led_fops);	if(ret < 0) 	{		printk("matrix_led: can't get major number\n");		return ret;	}	matrix_led_major = ret;	#ifdef CONFIG_DEVFS_FS	ret = devfs_mk_cdev(MKDEV(matrix_led_major,0),S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,"led");	if(ret)	{		unregister_chrdev(matrix_led_major,"led"); 		printk("matrix_led: can't make char device fo devfs\n");		return ret;	}	#endif	s3c2410_gpio_cfgpin(S3C2410_GPB7,S3C2410_GPB7_OUTP); 	s3c2410_gpio_cfgpin(S3C2410_GPB8,S3C2410_GPB8_OUTP); 	s3c2410_gpio_cfgpin(S3C2410_GPB9,S3C2410_GPB9_OUTP); 	s3c2410_gpio_cfgpin(S3C2410_GPB10,S3C2410_GPB10_OUTP); 	s3c2410_gpio_setpin(S3C2410_GPB7,1);	s3c2410_gpio_setpin(S3C2410_GPB8,1);	s3c2410_gpio_setpin(S3C2410_GPB9,1);	s3c2410_gpio_setpin(S3C2410_GPB10,1);	led_status = 0x00;	printk("led: the matrix led initialized\n");	return 0;}static void __exit matrix_led_exit(void){	set_led_status(~(LED1 | LED2 | LED3 | LED4));	#ifdef CONFIG_DEVFS_FS		devfs_remove("led");	#endif	unregister_chrdev(matrix_led_major,"led");	printk("led: the matrix led driver exit\n");}module_init(matrix_led_init);module_exit(matrix_led_exit);MODULE_ALIAS("matrix_led"); MODULE_DESCRIPTION("The Matrix LED Driver");MODULE_AUTHOR("jjj.<jjjstudio@163.com>");MODULE_LICENSE("GPL");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -