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,
  .read = button_drv_read,
};

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), NULL, DEVICE_NAME);

  gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  gpfdat = gpfcon + 1;
  gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
  gpgdat = gpgcon + 1;
  return 0;
}

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, keyVal, sizeof(keyVal));
    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]);
    }
  }

  return 0;
}

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

Linux 驱动——Button驱动1的更多相关文章

  1. Linux 驱动——Button驱动7(Timer)消抖

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  2. Linux 驱动——Button驱动6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  3. Linux 驱动——Button驱动5(atomic)原子量

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  4. Linux 驱动——Button驱动4(fasync)异步通知

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  5. Linux 驱动——Button驱动3(poll机制)

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  6. Linux 驱动——Button驱动2

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  7. Linux GPIO键盘驱动开发记录_OMAPL138

    Linux GPIO键盘驱动开发记录_OMAPL138 Linux基本配置完毕了,这几天开始着手Linux驱动的开发,从一个最简单的键盘驱动开始,逐步的了解开发驱动的过程有哪些.看了一下Linux3. ...

  8. linux块设备驱动之实例

    1.注册:向内核注册个块设备驱动,其实就是用主设备号告诉内核这个代表块设备驱动 sbull_major  =  register_blkdev(sbull_major, "sbull&quo ...

  9. Linux 视频设备驱动V4L2最常用的控制命令

    http://blog.csdn.net/shaolyh/article/details/6583226 Linux 视频设备驱动V4L2最常用的控制命令使用说明(1.02) 命令 功能 VIDIOC ...

随机推荐

  1. dynamics crm跳转到手机版本的页面

    https://community.dynamics.com/crm/f/117/t/210393 https://community.dynamics.com/crm/f/117/t/118414 ...

  2. react-redux中的数据传递

    1.connect connect用于连接React组件与 Redux store,其使用方法如下 connect([mapStateToProps], [mapDispatchToProps], [ ...

  3. (03) spring Boot 的配置

    1. spring boot 的核心配置 spring boot 项目建立之后,已经创建好了application.properties 配置文件 其实, 配置文件还支持*.yml 格式的: 2. 多 ...

  4. 回文的范围——算法面试刷题2(for google),考察前缀和

    如果一个正整数的十进制表示(没有前导零)是一个回文字符串(一个前后读取相同的字符串),那么它就是回文.例如,数字5, 77, 363, 4884, 11111, 12121和349943都是回文. 如 ...

  5. CentOS 7上安装PGI 2017编译器

    1. 安装PGI编译器 在PGI的官方网站的右上角,有一个社区免费版(Community Edition)的下载链接(GET PGI FOR FREE),根据操作系统选择合适的版本即可. 需要注意的是 ...

  6. php遍历数组7种方式(严格说是五种)

    数组: $arr = array(1,2,3,4,5); 第一种:foreach (最常见的) foreach ($arr as $v){ echo $v;} 第二种:for for($i=0;$i& ...

  7. Delphi indy线程控件TIdThreadComponent的使用

    当程序需要做耗时操作,例如访问数据库获取较多的数据.获取大文件MD5.网络访问数据量比较大.界面需要频繁刷新等等,都可以用线程来解决界面卡顿的问题,从而优化用户体验. 在知道TIdThreadComp ...

  8. Spring MVC扩展

    使用@ResonseBody实现异步请求时返回的数据对象的输出. 通过配置StringHttpMessageConverter消息转换器来解决JSON数据传递中出现的中文乱码问题. 在实际项目开发中, ...

  9. 小谈对Python的认知与期望

    18级新生,在大学之前并未接触过程序语言编程,在众多语言编程中只对C语言有个名字上认知.在上个学期初次了解到Python语言,计算机老师表示Python是现在编程语言中如雨后春笋般的发展飞速的计算机语 ...

  10. 《用Python写爬虫》学习笔记(一)

    注:纯文本内容,代码独立另写,属于本人学习总结,无任何商业用途,在此分享,如有错误,还望指教. 1.为什么需要爬虫? 答:目前网络API未完全放开,所以需要网络爬虫知识. 2.爬虫的合法性? 答:爬虫 ...