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

📄 gpiodrv.c

📁 1、将makefile中的 CROSS =/usr/local/arm/2.95.3/bin/arm-linux- CFLAGS+=-I/usr/local/src/2.4.18-rmk7/inc
💻 C
字号:
/**********************************************
* *GEC-2410 develop board
**********************************************/

#include <linux/config.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h> 
#include <linux/major.h>
#include <asm/uaccess.h> 
#include <asm/hardware.h>
#include <asm/io.h> 
#include <linux/delay.h>

#define	IOPORT_MAJOR 	220   //mknod /dev/gpio  c 220  0
      
static int gpio_open ( struct inode * , struct file * ); //
static int gpio_release ( struct inode * , struct file * ); 
static int gpio_ioctl ( struct inode * , struct file * , unsigned int , unsigned long);
static struct file_operations gpio_ctl_fops = { 
      open: gpio_open , 
      ioctl: gpio_ioctl ,
      release: gpio_release , 
};

#define	LED1_ON( ) 	( GPFDAT &=~ 0x10 )	   	
#define	LED2_ON( ) 	( GPFDAT &=~ 0x20 )	  	
#define	LED3_ON( ) 	( GPFDAT &=~ 0x40 )	   	
#define	LED4_ON( ) 	( GPFDAT &=~ 0x80 )	 	

#define	LED1_OFF( ) 	( GPFDAT |= 0x10 )	   
#define	LED2_OFF( ) 	( GPFDAT |= 0x20 )	  
#define	LED3_OFF( ) 	( GPFDAT |= 0x40 )	   
#define	LED4_OFF( ) 	( GPFDAT |= 0x80)	

static void LedSet ( unsigned char led )	
{ 
	unsigned char  LedStatus ;
  	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( );
}

static void LedDisp ( void ) 
{ 
  LedSet( 0x08 ); 
  mdelay(1000);
  
  LedSet( 0x04 ); 
  mdelay(1000); 
  
  LedSet( 0x02 ); 
  mdelay(1000);
  
  LedSet( 0x01 ) ; 
  mdelay(1000);

  LedSet( 0x00 ); 
  mdelay(1000);

   LedSet( 0x01 ) ; 
  mdelay(1000);
  
  LedSet( 0x02 ); 
  mdelay(1000); 
  
  LedSet( 0x04 ); 
  mdelay(1000); 
  
  LedSet( 0x08 ); 
  mdelay(1000); 

  LedSet( 0x00 ); 
  mdelay(1000);
} 

static int gpio_open(struct inode *inode , struct file *file)
{
   	GPFCON = 0x5500;
   	GPFUP = 0xff ;
   
   	printk("open gpio devices\n"); 
	
	MOD_INC_USE_COUNT;
   
   	return 0;
   
}

static int gpio_ioctl(struct inode *inode, struct file *file, unsigned int command ,unsigned long arg)
{
   printk("gpio_ioctl called,with command=%d\n", command);
   if(command==0)
   {
   	while( arg-- )   {
		printk("...");
   	   	LedDisp();
	}
	printk("\n");
   }
   return 0;
}

static int gpio_release(struct inode *inode, struct file *filp)
{
	MOD_DEC_USE_COUNT;
	printk("device closed!!!\n");
	
	return 0;
}

static int __init gpio_init(void)
{
   int err=0;
   
   printk("gpio_init\n");
   err = register_chrdev(IOPORT_MAJOR, "gpio",  &gpio_ctl_fops);
   if(err<0)
   {
   	printk("fail to register\n");   	
   	return -1;
   }
   printk("success to register!!!\n");
   
   return 0;
}

static void  __exit gpio_exit(void)
{
   printk ( "release this device!!!\n" );
   unregister_chrdev(IOPORT_MAJOR, "gpio");	
}

module_init(gpio_init);
module_exit(gpio_exit);


⌨️ 快捷键说明

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