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

📄 led-driver.bak.c

📁 嵌入式linux 设备驱动程序
💻 C
字号:
# include < linux/config.h >
# include < linux/init.h >
# include < linux/module.h >
# include < linux/fs.h >
# include < linux/iobuf.h >
# include < linux/kernel.h >
# include < linux/major.h >
# include < asm/uaccess.h >
# include < asm/hardware.h >
# include < asm/arch/cpu_3c2410.h >
# include < asm/io.h >
# include < linux/vmalloc.h >
# define IOPORT_MAJOR 220 /*定义主设备号*/
int gpio_open ( struct inode * , struct file * ) ; //函数声明
int gpio_release ( struct inode * , struct file * ) ;
int gpio_ct _ioctl ( struct inode * , struct file * , unsigned int , unsigned long ) ; 
static struct file_operations gpio_ctl_fops ={
ioctl : gpio_ctl_ioctl ,
Opell : gpio_open ,
release : gpio_release ,
} ;
/*所有的操作系统将硬件设备当作文件处理,所有外设的操作就封装在这个file_operations 结构体里
面就是文件的open / read / write / close 等操作,剩余的都放到一个ioctl 函数里面做处理。*/

# define LED1_ON ( ) ( GPFDAT &=~ Ox1O )
# define LED2_ON ( ) ( GPFDAT &=~ Ox2O )
# define LED3_ON ( ) ( GPFDAT &=~ Ox4O )
# define LED4_ON ( ) ( GPFDAT &=~ Ox8O )
# define LED1_OFF ( ) ( GPFDAT |= 0xl0 )
# define LED2_OFF ( ) ( GPFDAT |= 0x20 )
# define LED3_OFF ( ) ( GPFDAT |= 0x40)
# define LED4_OFF ( ) ( GPFDAT |= 0x80)
static int LedStatus ;
void LedSet ( int led )
{
	LedStatus = led ;
	if ( LedStatus &1 )
		LED1_ON ( ) ;
	else
		LED1_OFF ( ) ;
	if ( LedStatus & 2 )
		LED2_ON ( ) ;
	else
		LED2_OFF ( ) ;
	if ( LedStatus & 4 )
		LED3_ON ( ) ;
	else
		LED3_OFF ( ) ;
	if ( LedStatus & 8 )
		LED4_ON ( ) ;
	else
		LED4_OFF ( ) ;
}

void LedDisp ( void )
{
	LedSet ( 0x08 ) ; // LED 点亮/熄灭状态设置
	udelay ( ox500000 ) ; //延时函数
	LedSet ( 0x04 ) ; // LED 点亮/熄灭状态设置
	udelay ( Ox50000O ) ;
	LedSet ( 0x02 ) ; // LED 点亮/熄灭状态设置
	udelay ( Ox50000O ) ;
	LedSet ( 0x01 ) ; // LED 点亮/熄灭状态设置
	udelay ( Ox50000O ) ;
	LedSet ( 0x02 ) ; // LED 点亮/熄灭状态设置
	udelay ( Ox50000O ) ;
	LedSet ( 0x04 ) ; // LED 点亮/熄灭状态设置
	udelay ( Ox50000O ) ;
	LedSet ( 0x08 ) ; // LED 点亮/熄灭状态设置
	udelay ( Ox50000O ) ;
}

static int __init gpio_init ( void )
{
	int err=0 ;
	printk ( “ gpio _ init \ n “ ) ;
	err = register_chrdev (IOPORT_MAJOR ,“gpio”, &gpio_ctl_fops);
	if ( err )
	{
	printk ( “fail to register \n” ) ;
	return -1;
	}
	printk (“success to register\n”) ;
	return 0;
	}
	int gpio_open ( struct inode *inode , struct file *filp )
	{
	GPFCON=Ox5500
	GPFUP=0xff ;
	printk ( “open gpio devices\n” ) ;
	return 0;
}

int gpio_release ( struct inode *inode , struct file *filp )
{
	printk ( “release this device\n” ) ;
	return 0;
}
int gpio_ctl_ioctl ( struct inode *inode , struct file *filp , unsigned int command , unsigned
long arg )
{
	int err=0 ;
	if ( command ==1 )
	{
		while ( arg--)
		{
			LedDisp ( ) ;
			printk ( “… “) ;
		}
		printk ( “\ n” ) ;
		return 0;
	}
	return err ;
}
module_init(gpio_init);
module_exit(gpio_release);

⌨️ 快捷键说明

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