Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226535
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-08 10:41
个人简介

爱莉清

文章分类

全部博文(80)

文章存档

2018年(1)

2017年(18)

2016年(49)

2015年(7)

2014年(5)

我的朋友

分类: 嵌入式

2014-11-09 11:27:25

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


/*use to creation a Device Nodes*/
static struct class *seconddrv_class;
static struct class_device *seconddrv_class_dev;


volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;




static int second_drv_open(struct inode *inode, struct file *file)
{


/*
* K1,K2,K3,K4对应GPF1、GPF4、GPF2、GPF0
*/


/* 配置GPF1、GPF4、GPF2、GPF0为输入引脚 */
*gpfcon &= ~((0x3<<(1*2)) | (0x3<<(4*2)) | (0x3<<(2*2)) | (0x3<<(0*2)));
return 0;


}




ssize_t second_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
/* 返回4个引脚的电平 */
unsigned char key_vals[4];
int regval;


if (size != sizeof(key_vals))
return -EINVAL;


regval = *gpfdat;
key_vals[0] = (regval & (1<<1)) ? 1 : 0;
key_vals[1] = (regval & (1<<4)) ? 1 : 0;
key_vals[2] = (regval & (1<<2)) ? 1 : 0;
key_vals[3] = (regval & (1<<0)) ? 1 : 0;


copy_to_user(buf, key_vals, sizeof(key_vals));

return sizeof(key_vals);
}
static struct file_operations second_drv_fops = {
    .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open     =  second_drv_open,     
.read    =  second_drv_read,   
};




int major;
static int second_drv_init(void)
{


major = register_chrdev(0, "second_drv", &second_drv_fops);/*第一个参数为0让系统自动分配主设备号*/

seconddrv_class = class_create(THIS_MODULE, "second_drv");
seconddrv_class_dev = class_device_create(seconddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* creation /dev/buttons */

gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1; /*指针加一地址加4*/
return 0;


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