s3c2440驱动程序_/*功能:使用查询方式读取按键值

来源:互联网 发布:sql server 实训报告 编辑:程序博客网 时间:2024/06/04 17:43
/*功能:使用查询方式读取按键值
2016年5月4日16:21:44
*/
#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_devs[4];


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<<2*3) | (0x3 <<11*2));
return 0;
}




/*函数的参数是用户空间的函数的参数*/
static ssize_t second_drv_read(struct file *file, const __user char *buffer,size_t count, loff_t *ppos)
{
if(count != sizeof(key_vals)
return -EINVAL;//返回错误值

/*返回四个引脚的电平状态*/
unsigned char key_vals[4];
int regval;
/*读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;


/*用户空间和内核空间之间是通过参数来传递的,
static ssize_t second_drv_read(struct file *file, const __user char *buffer,size_t count, loff_t *ppos)
函数的参数是用户空间的,内核空间的参数是我们自己定义的,他们之间传递参数都是用指针来实现的
*/
copy_to_user(buffer ,key_vals,sizeof(key_vals));
return sizeof(key_vals);





}




/*入口函数*/
int major;
static int second_drv_init (void)
{
major = register_chrdev(0,"secod_drv",&second_drv_fops);


seconddrv_class = class_create(THIS_MODULE, "secod_drv");//创建个类

seconddrv_class_devs = class_device_create(seconddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */



gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
gpfdat = gpfcon + 1;//指向0x56000054


gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
gpgdat = gpgcon + 1;//指向0x56000064


return 0;


}






/*出口函数*/
int major;
static int second_drv_exit (void)
{
unregister_chrdev(major,"secod_drv");

class_device_unregister(seconddrv_class_devs);
 
class_destroy(seconddrv_class);



iounmap(gpfcon);
iounmap(gpgcon);
return 0;


}




static struct file_operations second_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   second_drv_open,    //表示将first_drv_open函数的地址赋给file_operations结构体中open函数,
.read =second_drv_read, //以后执行 first_drv_open函数时会自动调用file_operations结构体中的open函数
};


/*修饰*/
modules_init(second_drv_init);
modules_exit(second_drv_exit);


MODULE_LICENSE("GPL");




//测试函数




/*功能:使用查询方式读取按键值
2016年5月4日16:21:44
*/


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


/*my_first_drv on    打开
my_first_drv off  灭灯
*/
int main (int argc,char ** argv)
{
int fd;
int count = 0;
unsigned char key_vals [4];
fd = open("/dev/buttons",O_RDWR);


if(fd < 0)
{
printf("can't open !\n");


}


while (1)
{
// static ssize_t second_drv_read(struct file *file, const __user char *buffer,size_t count, loff_t *ppos)


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",count++,key_vals[0],key_vals[1] ,key_vals[2],key_vals[3]);


}


}




return 0;


}
/*
一般函数是可以带参数的,main函数也不例外,格式通常采用这种固定形式。
由于main
不被其他函数调用,所以不能从程序中获取参数。实际上参数是执行时从操作系统上获取?
模琣rgc是参数个数,argv是参数列表。
当我们要运行一个可执行文件时,在DOS
提示符下键入文件名,再输入实际参数即可把这些实参传送到main的形参中去。


argc是你主程序参数的个数。


argv[0]是你编译出来执行时候程序的名字。
argv[1].....是你主程序需要的参数。
举例说明:如下程序 aa.c
#include<stdio.h>
#include<stdlib.h>
#include<stdring.h>
int main(int argc, char *argv[])
{
        printf("%d\n",argc);
        printf("%s\n",argv[0]);
        printf("%s\n",argv[1]);
        printf("%s\n",argv[2]);
        return 0;
}
编译: gcc -o hello aa.c (也就是编译出来的执行文件叫hello,这是linux
上的编译方式)
执行: hello aa  bb
结果:
2
hello
aa
bb


*/






/*
#include<fcntl.h>
int open(constchar*pathname,intflags);
int open(constchar*pathname,intflags,mode_tmode);
返回值:成功则返回文件描述符,否则返回-1


对于open函数来说,第三个参数仅当创建新文件时(即 使用了O_CREAT 
时)才使用,用于指定文件的访问权限位(access permission bits)。pathname 
是待打开/创建文件的POSIX路径名(如/home/user/a.cpp);flags 
用于指定文件的打开/创建模式,这个参数可由以下常量(定义于fcntl.h
)通过逻辑位或逻辑构成。


1
2
3


O_RDONLY只读模式
O_WRONLY只写模式
O_RDWR读写模式




write: int write(int fd, char * buf, unsigned count);


write函数将buf中的count字节内容写入文件描述符fd
.成功时返回写的字节数.失败时返回-1. 并设置errno变量. 在网络程序中,
当我们向套接字文件描述符写时有俩种可能.  
1)write的返回值大于0,表示写了部分或者是全部的数据.  
2)返回的值小于0,此时出现了错误.我们要根据错误类型来处理.  如果错误为EINTR
表示在写的时候出现了中断错误.  
如果为EPIPE表示网络连接出现了问题(对方已经关闭了连接).




*/


/*测试结果是:can't open!
因为在板子上不存在/dev/xxx,如果想要在板子上测试成功的话
需要使用mknod /dev/xxx c 111 0
c 代表字符   111 代表主设备号 0 代表次设备号
mknod /dev/xxx c 111 0 这行命令代表创建设备节点,
把/dev/xxx设备设为字符设备,主设备号为111,次设备号为0;




*/






0 0