写一个简单查询的按键驱动

前言:

把JZ2440翻了出来,无聊写写笔记。

 

正文:

内核版本:Linux 2.6

硬件图:button接在GPF0这个引脚上。

 

驱动程序:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>


#define	BUTTON_DRIVER_NAME		"button_driver"

static int button_driver_open(struct inode *inode, struct file *file);
ssize_t button_driver_read(struct file *file, char __user *buf, size_t size, loff_t *ppos);


static int button_driver_major = 0;

static const struct file_operations button_driver_file_ops = {
	.owner = THIS_MODULE,
	.open = button_driver_open,
	.read = button_driver_read,
	};





static int button_driver_open(struct inode *inode, struct file *file)
{
	printk("button_driver_open\r\n");
	s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPIO_INPUT);
	s3c2410_gpio_pullup(S3C2410_GPF0, 0);	// 0: pullup off
	return 0;
}


ssize_t button_driver_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
	unsigned int val = 0;
	
	//printk("button_driver_read\r\n");
	
	if(size != 4)	return -EINVAL;
	
	val = s3c2410_gpio_getpin(S3C2410_GPF0);
	copy_to_user(buf, &val, 4);
	
	return size;
}

static struct class *button_class;
static struct class_device *button_dev_class;


static int __init button_driver_init(void)
{
	printk("button_drive_init\r\n");
	button_driver_major = register_chrdev(0, BUTTON_DRIVER_NAME, &button_driver_file_ops);
	button_class = class_create(THIS_MODULE, BUTTON_DRIVER_NAME);
	button_dev_class = class_device_create(button_class, NULL, MKDEV(button_driver_major, 0), NULL, BUTTON_DRIVER_NAME);

	return 0;
}

static void __exit button_driver_exit(void)
{
	printk("button_drive_exit\r\n");

	class_device_destroy(button_class, MKDEV(button_driver_major, 0));
	class_destroy(button_class);
	unregister_chrdev(button_driver_major, BUTTON_DRIVER_NAME);
}


module_init(button_driver_init);
module_exit(button_driver_exit);
MODULE_LICENSE("GPL");





 

MakeFile编写:

KERN_DIR = /home/sudaroot/JZ2440/Kernel/linux-2.6.22.6

all:
	make -C $(KERN_DIR) M=`pwd` modules 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order

obj-m	+= button_driver.o

编译驱动程序命令:

#make all

生成button_driver.ko

 

 

 

测试程序:

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>



int main(int argc, char** argv)
{
	int fd = 0;
	unsigned int val = 1;

	fd = open("/dev/button_driver", O_RDWR);
	if(fd < 0)
	{
		printf("open fail\r\n");
		return 1;
	}

	while(1)
	{
		read(fd, &val, 4);
		if(val == 0)
		{
			printf("Pressed\r\n");
		}
	}

	return 0;
}

编译测试程序命令:

#arm-linux-gcc buttonAPP.c -o buttonAPP

 

 

开发板测试:

先把前面编好的驱动模块程序button_driver.ko 和 测试程序buttonAPP复制到开发板上。

装载驱动模块,运行测试程序。

命令行运行详情,按下按键:

 

卸载驱动:

 

 

 

全篇完。

本人是一个嵌入式未入门小白,博客仅仅代表我个人主观见解,记录成长笔记。
笔记是以最简单的方式,只展示最核心的原理。
若有与 大神大大 见解有歧义,我绝对坚信 大神大大 见解是对的,我的是错的。
若无积分等无法下载源码,留下邮箱。
感谢~!

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值