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

📄 gpiodrv.c

📁 这是一个用于测试gpio 功能的小程序 这是一个用于测试gpio 功能的小程序
💻 C
字号:
/**********************************************
* *GEC-2410 develop board
**********************************************/

#include <linux/config.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/devfs_fs_kernel.h>

#include <asm/hardware.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.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 = { 
    	owner:	THIS_MODULE,
      	open: 	gpio_open , 
      	ioctl: 	gpio_ioctl ,
      	release: 	gpio_release , 
};

#define	LED1_ON( ) 	__raw_writel(__raw_readl(S3C2410_GPFDAT) & (~0x10), S3C2410_GPFDAT)   		   	
#define	LED2_ON( ) 	__raw_writel(__raw_readl(S3C2410_GPFDAT) & (~0x20), S3C2410_GPFDAT)		  	
#define	LED3_ON( ) 	__raw_writel(__raw_readl(S3C2410_GPFDAT) & (~0x40), S3C2410_GPFDAT)		   	
#define	LED4_ON( ) 	__raw_writel(__raw_readl(S3C2410_GPFDAT) & (~0x80), S3C2410_GPFDAT)		 	

#define	LED1_OFF( ) __raw_writel(__raw_readl(S3C2410_GPFDAT) | 0x10, S3C2410_GPFDAT)		
#define	LED2_OFF( ) __raw_writel(__raw_readl(S3C2410_GPFDAT) | 0x20, S3C2410_GPFDAT)		 
#define	LED3_OFF( ) __raw_writel(__raw_readl(S3C2410_GPFDAT) | 0x40, S3C2410_GPFDAT)		  
#define	LED4_OFF( ) __raw_writel(__raw_readl(S3C2410_GPFDAT) | 0x80, S3C2410_GPFDAT)		

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)
{

	__raw_writel(0x5500, S3C2410_GPFCON);	//GPFCON = 0x5500;
   	__raw_writel(0xff, S3C2410_GPFUP);		//GPFUP = 0xff ;
   
   	printk("open gpio devices\n"); 
	
   
   	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("...\n");
   	   	LedDisp();
	}
	printk("\n");
   }
   return 0;
}

static int gpio_release(struct inode *inode, struct file *filp)
{
	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 + -