天天看點

FL2440字元裝置驅動開發之查詢按鍵

上一篇連接配接位址:http://blog.csdn.net/qq_21792169/article/details/48414687

驅動程式:

#include <linux/module.h>

#include <linux/kernel.h>

#include <linux/fs.h>

#include <linux/init.h>

#include <linux/device.h>

//#include <linux/devfs_fs_kernel.h>

#include <linux/miscdevice.h>

#include <linux/delay.h>

#include <asm/irq.h>

#include <asm/io.h>

#include <asm/uaccess.h>

#include <mach/regs-gpio.h>

#include <mach/hardware.h>

#include <linux/device.h> 

#include <linux/gpio.h>

//volatile unsigned long *gpfcon=null;

//volatile unsigned long *gpfdat=null;

#define device_name  "buttons"

static struct class *buttons;

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

{

/* 配置gpf0,2,3,4為輸入引腳 */

//*gpfcon = 0x3ffc0c;// gpf0,gpf2,gpf3,gpf4設定為輸入

unsigned long temp;

    temp=__raw_readl(s3c2410_gpfcon);

    temp=0x3ffc0c;

__raw_writel(temp,s3c2410_gpfcon);

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 = __raw_readl(s3c2410_gpfdat);

key_vals[0] = (regval & (1<<0)) ? 1 : 0;

key_vals[1] = (regval & (1<<2)) ? 1 : 0;

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

key_vals[3] = (regval & (1<<4)) ? 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);

buttons = class_create(this_module, device_name);

device_create(buttons, null, mkdev(major, 0), null, "buttons"); 

      // gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);

//gpfdat = gpfcon + 1;

    return 0;

static void second_drv_exit(void)

unregister_chrdev(major, "second_drv");

device_destroy(buttons,mkdev(major, 0));

class_destroy(buttons);

//iounmap(gpfcon);

module_init(second_drv_init);

module_exit(second_drv_exit);

module_license("gpl");

測試程式:

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

int main(int argc, char **argv)

int fd;

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]);

makefile其他的部分是不變的,隻需要修改檔案名字就可以了。

obj-m :=test.o

kerneldir ?= /home/work/linux/linux-2.6.28.7

pwd := $(shell pwd)

default:

$(make) -c $(kerneldir) m=$(pwd) modules

clean:

rm -f  *o  *.mod.o  *mod.c  *.symvers *.order

繼續閱讀