Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1526945
  • 博文数量: 329
  • 博客积分: 2773
  • 博客等级: 少校
  • 技术积分: 4219
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:17
个人简介

淡定从容,宁静致远

文章分类

全部博文(329)

文章存档

2016年(4)

2015年(50)

2014年(68)

2013年(45)

2012年(162)

分类: LINUX

2012-09-17 08:54:45

     //This file for explain how to use a simple driver  
 
 
#include
#include
#include
#include
#include
 
 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
 
#include
 
// RELAY DEVICE MAJOR
#define SIMPLE_RELAY_MAJOR    154
#define OURS_RELAY_DEBUG  
 
#define VERSION         "PXA270RP-V1.00-090224"
//define LED STATUS
#define RELAY_ON  0
#define RELAY_OFF 1
 
struct realy_dev
{  
 struct cdev cdev;
   unsigned char value;
};
struct realy_dev *realy_devp;
 
static int realy_major=SIMPLE_RELAY_MAJOR;
 
 
void showversion(void)
{
        printk("*********************************************\n");
        printk("\t %s \t\n", VERSION);
        printk("*********************************************\n\n");
 
}
 
 
// ------------------- READ ------------------------
ssize_t SIMPLE_RELAY_read (struct file * file ,char * buf, size_t count, loff_t * f_ops)
{
    #ifdef OURS_RELAY_DEBUG
        printk ("SIMPLE_RELAY_read [ --kernel--]\n");
    #endif               
 
    return count;
}    
 
// ------------------- WRITE -----------------------
ssize_t SIMPLE_RELAY_write (struct file * file ,const char * buf, size_t count, loff_t * f_ops)
{
    #ifdef OURS_RELAY_DEBUG
            printk ("SIMPLE_RELAY_write [ --kernel--]\n");
        #endif
 
    return count;
}    
 
// ------------------- IOCTL -----------------------
ssize_t SIMPLE_RELAY_ioctl (struct inode * inode ,struct file * file, unsigned int cmd, unsigned long data)
{
     
#ifdef OURS_RELAY_DEBUG
            printk ("SIMPLE_RELAY_ioctl [ --kernel--]\n");
        #endif
 
     switch(cmd)
 {
   case RELAY_ON: {GPCR0 |=(0x1<<9);
                    break;}
  case RELAY_OFF: {GPSR0 |=(0x1<<9);
                     ;break;}
 
   default:
  {printk("led control:no cmd run\n");
   return (-ENOTTY);
  }
 
  }
    
    return 0;
}
 
// ------------------- OPEN ------------------------
ssize_t SIMPLE_RELAY_open (struct inode * inode ,struct file * file)
{
    #ifdef OURS_RELAY_DEBUG
            printk ("SIMPLE_RELAY_open [ --kernel--]\n");
        #endif
    
    
    return 0;
}    
 
// ------------------- RELEASE/CLOSE ---------------
ssize_t SIMPLE_RELAY_release (struct inode  * inode ,struct file * file)
{
    #ifdef OURS_RELAY_DEBUG
            printk ("SIMPLE_RELAY_release [ --kernel--]\n");
        #endif
 
 
    return 0;
}
 
// -------------------------------------------------
struct file_operations realy_fops ={
 
    
    .open=        SIMPLE_RELAY_open,
    
    .read=        SIMPLE_RELAY_read,
    
    .write=        SIMPLE_RELAY_write,
    
    .ioctl=             SIMPLE_RELAY_ioctl,
    
    .release=    SIMPLE_RELAY_release,
    
};
 
static void realy_setup_cdev(struct realy_dev *dev,int index)
{  
 int err, devno=MKDEV(realy_major,index);
    cdev_init(&dev->cdev,&realy_fops);
     dev->cdev.owner=THIS_MODULE;
   dev->cdev.ops=&realy_fops;
  err=cdev_add(&dev->cdev,devno,1);
    if(err)
    printk(KERN_NOTICE "Error %d adding realy%d",err,index);
}
 
int realy_init(void)
{
int result;
 
dev_t dev=MKDEV(realy_major,0);
if(realy_major)
 result=register_chrdev_region(dev,1,"RELAY_ctl");
else
{
result=alloc_chrdev_region(&dev,0,1,"RELAY_ctl");
realy_major=MAJOR(dev);
}
if(result<0)
return result;
 
 
realy_devp =kmalloc (sizeof(struct realy_dev),GFP_KERNEL);
if (!realy_devp)
{ result= - ENOMEM;
 goto fail_malloc;
}
memset(realy_devp,0,sizeof(struct realy_dev));
 
 realy_setup_cdev(realy_devp,0);
  //init RELAY
    
        GAFR0_U &=(0xfff3ffff); //SET GPIO9 GPIO MODE
    GPDR0 |= (0x1<<9); // SET GPIO9 OUTPUT MODE
    GPSR0 |= (0x1<<9); // OFF THE RELAY
 
 
printk("realy_init\n");  
showversion();  
return 0;
 
fail_malloc:unregister_chrdev_region(dev,1);
return result;
}
void realy_cleanup(void)
{
 cdev_del(&realy_devp->cdev);
  kfree(realy_devp);
  unregister_chrdev_region(MKDEV(realy_major,0),1);
  printk("realy_cleanup\n");
}
 
MODULE_DESCRIPTION("simple realy driver module");
MODULE_AUTHOR("guliangzeng");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_ALIAS("realy module");
 
module_init(realy_init);
module_exit(realy_cleanup);

#include
#include
#include

#include       // open() close()
#include      // rea() write()

#define DEVICE_NAME "/dev/RELAY_ctl"


//------------------------------------- main ----------------------------------------------------------------
int main(void)
{
        int fd;
    int ret;
    int i;

        printf("\nstart relay_driver test\n\n");

        fd = open(DEVICE_NAME, O_RDWR);
   
    printf("fd = %d\n",fd);
   

        if (fd == -1)
        {
                printf("open device %s error\n",DEVICE_NAME);
        }
        else
        {


        for(i=0;i<5;i++)
         {    ioctl(fd,0);
            sleep(1);
            ioctl(fd,1);
            sleep(1);

        }
            // close
        ret = close(fd);

        printf ("ret=%d\n",ret);
        printf ("close gpio_led_driver test\n");
        }

        return 0;
}// end main


阅读(911) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~