Linux 驱动——Button驱动1

Linux 驱动——Button驱动1,第1张

概述button_drv.c驱动文件:   #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <asm/io.h>           //包含iomap函数iounmap函数 #include <asm/uaccess.h>         //包含copy_from_user函数

button_drv.c驱动文件:

 

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/io.h>           //包含iomap函数IoUnmap函数
#include <asm/uaccess.h>         //包含copy_from_user函数
#include <linux/device.h>        //包含class类相关的处理函数
#include <linux/fs.h>           //包含file_operations结构体

 

#define DRIVER_name "button_drv"
#define DEVICE_name "button_dev"

 

int major;

 

static struct class *buttondrv_class;
static struct class_device *buttondrv_class_dev;

 

volatile unsigned long *gpfcon = NulL;
volatile unsigned long *gpfdat = NulL;
volatile unsigned long *gpgcon = NulL;
volatile unsigned long *gpgdat = NulL;

 

static int button_drv_open(struct inode *inode,struct file *file)
{
  *gpfcon &= ~((0x3<<(0*2)) | (0x3<<(2*2)));
  *gpgcon &= ~((0x3<<(3*2)) | (0x3<<(11*2)));
  return 0;
}

 

static int button_drv_read(struct file * file,char __user * userbuf,size_t count,loff_t * off)
  int ret;
  unsigned char keyval[4];
  unsigned long keyvals;

  if(count!=sizeof(keyval))
  {
    printk("button_drv_read error 1 \n");
  }

  keyvals = *gpfdat;
  keyval[0] = ((keyvals>>0) & 1);
  keyval[1] = ((keyvals>>2) & 1);
  keyvals = *gpgdat;
  keyval[2] = ((keyvals>>3) & 1);
  keyval[3] = ((keyvals>>11) & 1);
  
  ret = copy_to_user(userbuf,keyval,sizeof(keyval));
  if(ret<0)
    printk("button_drv_read error 2 \n");
  return sizeof(keyval);
static struct file_operations button_drv_fops = {
  .owner = THIS_MODulE,
  .open = button_drv_open,courIEr; Font-size: 18px;">   .read = button_drv_read,courIEr; Font-size: 18px;">};

 

static int button_drv_init(voID)
  major = register_chrdev(0,DRIVER_name,&button_drv_fops);

 

  buttondrv_class = class_create(THIS_MODulE,DEVICE_name);
  buttondrv_class_dev = class_device_create(buttondrv_class,NulL,MKDEV(major,0),DEVICE_name);

 

  gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
  gpfdat = gpfcon + 1;
  gpgcon = (volatile unsigned long *)ioremap(0x56000060,courIEr; Font-size: 18px;">   gpgdat = gpgcon + 1;
static voID button_drv_exit(voID)
  unregister_chrdev(major,DRIVER_name);
  class_device_unregister(buttondrv_class_dev);
  class_destroy(buttondrv_class);
  IoUnmap(gpfcon);
  IoUnmap(gpgcon);
module_init(button_drv_init);
module_exit(button_drv_exit);

 

MODulE_liCENSE("GPL");

Makefile文件:

obj-m += button_drv.o

KERN_DIR = /work/system/linux-2.6.22.6

all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
rm -rf *.o *.ko *.order *.symvers *.mod.c

button_app.c文件:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main(int argc,char **argv)
  int fd;
  int val;
  char *filename;
  unsigned char keyval[4];

  filename = argv[1];
  fd = open(filename,O_RDWR);
  if(fd<0)
    printf("can not open \n");
  while(1)
    read(fd,courIEr; Font-size: 18px;">     if(!keyval[0] || !keyval[1] || !keyval[2] || !keyval[3])
    {
      printf("key pressed %d %d %d %d \n",keyval[0],keyval[1],keyval[2],keyval[3]);
    }
  }

}

编译生成button_drv.ko文件和button_app文件,运行:./button_app /dev/button_dev

总结

以上是内存溢出为你收集整理的Linux 驱动——Button驱动1全部内容,希望文章能够帮你解决Linux 驱动——Button驱动1所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1031571.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存