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. WDA 新SELECT OPTION

    感谢肖总的提示,不然还不知道wda的新select option... 使用前提:SE24类:CL_WDR_SELECT_OPTIONS_20 核对了多个版本,ERP系统版本要S4 1701往上,CR ...

  2. IPhone手机常用的一些连

    1.查看固件可下载链接. ipsw.me 2.查看手机型号(是global还是GSM). https://ipsw.me/device-finder 3.系统降级链接. https://jingyan ...

  3. OneNote无法打开链接出现错误:你的组织策略阻止我们为你完成此操作

    首先打开注册表编辑器,按键盘win+r,调出运行窗口,输入regedit打开注册表编辑器 打开HKEY_CURRENT_USER\Software 打开\Classes 最后选中.html,在默认选项 ...

  4. 一、Git简介

    由于工作需要,代码版本控制工具由SVN换为了Git.Git对于我来说,很陌生,不清楚其工作原理,所以查询了一些资料,将自己所得知的内容来汇总一下,梳理一下这方面的知识,同时也给其他想了解Git的童鞋们 ...

  5. socket应用(vue、node.js、M站)

    socket应用(vue.node.js.M站) 前言:我们在做一些项目的时候需要做到实时变化, 比如我们有时候有需求会要求我们做一个类似于聊天室的页面 比如有些时候我们对某些东西进行点赞和刷票,需要 ...

  6. win10优化工具哪个好

    不少刚安装好win10系统的用户觉得win10系统很卡,这是因为你没有进行win10优化导致的,今天小编就来跟各位介绍几款win10优化,希望对您的优化工作有帮助. 我们安装好电脑操作系统如果直接使用 ...

  7. 关于以base64编码形式上传图片

    第一次用,记录一下 HTML中的样式 HTML代码 1 <li class="orther" style="padding-top: 15px;"> ...

  8. number类型精度分析

    numbe类型的可设置的取值范围: number无限定 number(6)是6位整数 number(6,2)是4位整数,精确到两位小数,最多6位.四舍五入 number(6,-2)是6位整数,精确到百 ...

  9. C#如何在生成文件夹或者文件时候自动重命名

    如果你在一个文件夹里面, 连续添加文件夹或者文件(不改名字), 那么系统会自动加上(1),(2),(3)... 这个效果我在网上搜不到, 自己写一下也不太难. 文件夹的:(不断点击按钮就有效果) pr ...

  10. js截取固定长度字符串,多余字符显示...

    function cutstr(str, len) { var str_length = 0; var str_len = 0; str_cut = new String(); str_len = s ...