linux GPIO驱动输入模式读按键(二)

一:驱动实现轮询读取gpio,提供读和写操作方法,用于测试

/button_drv.c*******/

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/gpio.h>
 
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
 

/*  0x01  OUT 	//0x00	 IN*/
/*  0x01 HIGH  	//0x00	 LOW*/

/*
//GPIO1_0    led0
#define GPIO1_0_DIR  0x12141400
#define GPIO1_0_DATA 0x12141004

//GPIO1_1    led1
#define GPIO1_1_DIR  0x12141400
#define GPIO1_1_DATA 0x12141008
*/

//GPIO2_0    key
#define GPIO2_0_DIR  0x12142400
#define GPIO2_0_DATA 0x12142004

volatile unsigned int *pGPIO2_0_DIR;
volatile unsigned int *pGPIO2_0_DATA;

#define BUFFER_SIZE 50
char kbuf[BUFFER_SIZE];	// 内核空间的buf
char kcharbuf;	// 内核空间的buf
struct hi3559av100_button
{
   
	int dev_major ;
	struct class *cls;
	struct device *dev;
	int value;
};
struct hi3559av100_button *button_dev;

static int button_drv_open(struct inode *inode, struct file *filp)
{
   
	printk("%s\n", __FUNCTION__);
	*pGPIO2_0_DIR  &= ~(0x1<<0); //set 0  in		 
	return 0;
}
static int button_drv_close(struct inode *inode, struct file *filp)
{
   
	printk("%s\n", __FUNCTION__);
	*pGPIO2_0_DIR  &= ~(0x1<<0); //set 0  in
	return 0;
}
static ssize_t button_drv_read(struct file *filp,  char __user *ubuf, size_t count, loff_t *ppos)
{
   
	printk("%s\n", __FUNCTION__);
	int ret = -1;

	memset(kbuf, 0, sizeof(kbuf));
	kbuf[0] = *pGPIO2_0_DATA;
	printk("%s  kbuf:%d\n", __FUNCTION__,kbuf[0]);

	ret = copy_to_user(ubuf, kbuf, count);
	if (ret) {
   
		printk(KERN_ERR "copy_to_user fail\n");
		return -EINVAL;
	} else if(ret == 0) {
   
		printk(KERN_INFO "copy_to_user success\n");
	}

	return ret;
}
static ssize_t button_drv_write(struct file *file, const char __user *u
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值