arm 驱动基础:查询方式的按键驱动程序
来源:互联网 发布:9.3.3cydia数据库红字 编辑:程序博客网 时间:2024/04/28 19:36
http://www.cnblogs.com/ITmelody/archive/2012/05/16/2504069.html
原理图:
驱动程序代码:
second_drv.c
#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>static struct class *seconddrv_class;static struct class_device *seconddrv_class_dev;volatile unsigned long *gpfcon;volatile unsigned long *gpfdat;volatile unsigned long *gpgcon;volatile unsigned long *gpgdat;static int second_drv_open(struct inode *inode, struct file *file){ /* 配置GPF0,2为输入引脚 */ *gpfcon &= ~((0x3<<(0*2)) | (0x3<<(2*2))); /* 配置GPG3,11为输入引脚 */ *gpgcon &= ~((0x3<<(3*2)) | (0x3<<(11*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; /* 读GPF0,2 */ regval = *gpfdat; key_vals[0] = (regval & (1<<0)) ? 1 : 0; key_vals[1] = (regval & (1<<2)) ? 1 : 0; /* 读GPG3,11 */ regval = *gpgdat; key_vals[2] = (regval & (1<<3)) ? 1 : 0; key_vals[3] = (regval & (1<<11)) ? 1 : 0; copy_to_user(buf, key_vals, sizeof(key_vals)); return sizeof(key_vals);}static struct file_operations sencod_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", &sencod_drv_fops); seconddrv_class = class_create(THIS_MODULE, "second_drv"); seconddrv_class_dev = class_device_create(seconddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */ gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16); gpfdat = gpfcon + 1; gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16); gpgdat = gpgcon + 1; return 0;}static void second_drv_exit(void){ unregister_chrdev(major, "second_drv"); class_device_unregister(seconddrv_class_dev); class_destroy(seconddrv_class); iounmap(gpfcon); iounmap(gpgcon); return 0;}module_init(second_drv_init);module_exit(second_drv_exit);MODULE_LICENSE("GPL");
应用程序代码:
seconddrvtest.c
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>/* seconddrvtest */int main(int argc, char **argv){ int fd; unsigned char key_vals[4]; int cnt = 0; fd = open("/dev/buttons", O_RDWR); if (fd < 0) { printf("can't open!\n"); } while (1) { read(fd, key_vals, sizeof(key_vals)); if (!key_vals[0] || !key_vals[1] || !key_vals[2] || !key_vals[3]) { printf("%04d key pressed: %d %d %d %d\n", cnt++, key_vals[0], key_vals[1], key_vals[2], key_vals[3]); } } return 0;}
- arm 驱动基础:查询方式的按键驱动程序
- 查询方式的按键驱动程序
- 字符设备驱动之按键处理一(查询方式的按键驱动程序)
- 按键驱动程序之查询方式的总结
- 跟着韦东山老师学字符设备驱动之查询方式的按键驱动程序分析
- 字符设备驱动程序之查询方式的按键驱动程序
- linux下查询方式的按键驱动
- 按键驱动的恩恩怨怨之查询方式
- 字符设备驱动程序之中断方式的按键驱动
- arm9按键驱动:查询的方式获取按键值
- 字符设备驱动--查询方式的按键驱动
- 按键驱动_查询方式
- 第12课第3节 字符设备驱动程序之查询方式的按键驱动程序
- 6410之写查询方式的按键驱动
- Linux设备驱动开发基础---字符设备驱动程序开发之基于中断的按键驱动
- 按键驱动——查询方式实现
- S3C6410中断方式查询按键值驱动
- 07-S3C2440驱动学习(一)嵌入式linux字符设备驱动-查询+中断+引入poll机制的按键驱动程序
- ArcGIS10.1使用OleDB连接
- JSP的九大内置对象
- ZOJ 1354 Extended Lights Out
- linux xp双系统修复
- spice use usb device qemu part support
- arm 驱动基础:查询方式的按键驱动程序
- 指针常量和常量指针(指向常量的指针)
- 老年人养生:有意识的进行左手摇蒲扇 有助于预防脑溢血 健康养生
- InputStream 和OutputStream
- Windows api 的学习和开发 二
- python实现模拟登录
- 《Linux大鹏的Linux五分钟系列》笔记
- POI API整理
- 利用cutetFTP软件在win7和虚拟主机上的linux系统进行文本传输