Chinaunix首页 | 论坛 | 博客
  • 博客访问: 105032
  • 博文数量: 50
  • 博客积分: 3120
  • 博客等级: 中校
  • 技术积分: 800
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-25 16:40
文章存档

2011年(1)

2009年(9)

2008年(40)

我的朋友

分类: LINUX

2008-09-25 17:13:37

KEY测试(查询)
作好了LED,现在做一个按键,板子的按键是接在PIOB0-3,只需要稍微改动一下就可以了,程序如下
#include
#include
#include
#include
#include
#include /* get the user-level API */
#include
#include
#include
#include
#include "at91_pio.h"
MODULE_LICENSE("GPL");
static void __iomem *pio_base;
size_t key_read (struct file *filp, char __user *buf, size_t count, loff_t *pos){
      printk("watchdog read.\r\n");
      return 0;
}
ssize_t key_write (struct file *filp, const char __user *buf, size_t count,off_t *pos){
      printk("watchdog write.\r\n");
      return count;
}
int key_ioctl (struct inode *inode, struct file *filp,unsigned int cmd, unsigned long arg){
      arg=readl(pio_base + PIO_PDSR)&0x0000000f;
      //printk("led ioctl cmd:%d%ld\r\n",cmd,arg);
      return arg;
}
static struct file_operations key_fops = {
      .owner = THIS_MODULE,
      .read   = key_read,
      .write = key_write,
      .ioctl = key_ioctl,
};
static int key_init(void){
      int result;
      printk(KERN_ALERT "key_test:%s,%s\r\n",__DATE__,__TIME__);
      pio_base = ioremap(0xFFFFF600,512);       
      writel(0x0000000f, pio_base + PIO_PUER);    
      writel(0x0000000f, pio_base + PIO_PER);     
      writel(0x0000000f, pio_base + PIO_IDR);
     
      result = register_chrdev(61, "key", &key_fops);
      if (result < 0){
          printk("register device fail.\r\n");
return result;
      }
      return 0;
}
static void key_exit(void){
printk("stop key\r\n");
      unregister_chrdev(61, "key");
      iounmap(pio_base);
      printk(KERN_ALERT "stop clear key\r\n");
}
module_init(key_init);
module_exit(key_exit);
同样编译好后,在运行之前,先要创建一个设备文件
# mknod "/dev/led" c 61 1
即创建一个主设备号为61,次设备号为1名称为KEY的字符型设备。
然后运行这个函数,按下按键,即可看见终端会有相应的显示。
阅读(387) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~