linux内核驱动裁剪,内核裁剪,将自己写的驱动加入内核及按键驱动的代码

1.在/opt/下解压LINUX内核文件

2.cd

/opt/opt/EmbedSky/linux-2.6.30.4/drivers/input/keyboard

3.gedit

Kconfig

4.config

TQ2440_IRQ_BUTTON

tristate "EmbedSky TQ2440 Board user buttons"

depends on ARCH_S3C2440

default y if ARCH_S3C2440

help

IRQ Button for EmbedSky TQ2440 Board.

拷贝楼上粘贴如下

config

BLACK //这里改改

tristate "EmbedSky" //这里也改改

depends on ARCH_S3C2440

default y if ARCH_S3C2440

help

IRQ Button for EmbedSky TQ2440 Board.

4.gedit

Makefile

最后一行obj-$(CONFIG_BLACK) += keyIrq.o

5.cd 到linux-2.6.30.4这里进行裁剪系统,make

menuconfig->Device Drivers->Input device

support->Keybords->去掉系统的*,把自己的加上*(在最后一个) 用空格来切换添加删除

6.cp 自己代码到/drivers/input/keyboard下

7.cp

config_EmbedSky_W43 .config

8.cp

config_EmbedSky_W43 config

9../config

10.make

clean;make 已成功一大半

11.将编译得到的zImage.bin内核文件烧写进ARM板,要清除原来的,UBOOT,系统也要重新烧写(9,1,3,6 来一遍)

12.然后驱动代码如下(记得改makefile):

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include //class_create

#include //S3C2410_GPF1

//#include

#include

//#include

#include

//wait_event_interruptible

static

DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static struct class *thirddrv_class;

static struct device *thirddrv_device;

static struct pin_desc{

unsigned int pin;

unsigned int key_val;

};

static struct pin_desc pins_desc[4] = {

{S3C2410_GPF1,0x01},

{S3C2410_GPF4,0x02},

{S3C2410_GPF2,0x03},

{S3C2410_GPF0,0x04},

};

static int ev_press = 0;

static unsigned char key_val;

int major;

static irqreturn_t buttons_irq(int irq, void

*dev_id)

{

struct pin_desc *pindesc = (struct pin_desc

*)dev_id;

unsigned int pinval;

pinval =

s3c2410_gpio_getpin(pindesc->pin);

if(pinval)

{

key_val = 0x80 | (pindesc->key_val);

}

else

{

key_val = pindesc->key_val;

}

ev_press = 1;

wake_up_interruptible(&button_waitq);

return IRQ_HANDLED;

}

static int third_drv_open(struct inode *

inode, struct file * filp)

{

request_irq(IRQ_EINT1, buttons_irq,

IRQ_TYPE_EDGE_BOTH, "K1",&pins_desc[0]);

request_irq(IRQ_EINT4, buttons_irq,

IRQ_TYPE_EDGE_BOTH, "K2",&pins_desc[1]);

request_irq(IRQ_EINT2, buttons_irq,

IRQ_TYPE_EDGE_BOTH, "K3",&pins_desc[2]);

request_irq(IRQ_EINT0, buttons_irq,

IRQ_TYPE_EDGE_BOTH, "K4",&pins_desc[3]);

return 0;

}

static ssize_t third_drv_read(struct file

*file, char __user *user, size_t size,loff_t *ppos)

{

if (size != 1)

return -EINVAL;

wait_event_interruptible(button_waitq,

ev_press);

copy_to_user(user, &key_val, 1);

ev_press = 0;

return 1;

}

static int third_drv_close(struct inode

*inode, struct file *file)

{

free_irq(IRQ_EINT1,&pins_desc[0]);

free_irq(IRQ_EINT4,&pins_desc[1]);

free_irq(IRQ_EINT2,&pins_desc[2]);

free_irq(IRQ_EINT0,&pins_desc[3]);

return 0;

}

static const struct file_operations

third_drv_fops = {

.owner = THIS_MODULE,

.open = third_drv_open,

.read = third_drv_read,

.release  = third_drv_close,

};

static int third_drv_init(void)

{

major = register_chrdev(0, "third_drv",

&third_drv_fops);

thirddrv_class = class_create(THIS_MODULE,

"thirddrv");

thirddrv_device =

device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL,

"buttons");

return 0;

}

static void third_drv_exit(void)

{

unregister_chrdev(major, "third_drv");

device_unregister(thirddrv_device);

//卸载类下的设备

class_destroy(thirddrv_class); //卸载类

}

module_init(third_drv_init);

//用于修饰入口函数

module_exit(third_drv_exit);

//用于修饰出口函数

MODULE_AUTHOR("LWJ");

MODULE_DESCRIPTION("Just for Demon");

MODULE_LICENSE("GPL");

//遵循GPL协议

13.再创建一个.c文件,测试代码如下:

#include

#include

#include

#include

#include

int main(int argc ,char *argv[])

{

int fd;

unsigned char key_val;

fd = open("/dev/buttons",O_RDWR);

if (fd < 0)

{

printf("open error\n");

}

while(1)

{

read(fd,&key_val,1);

printf("key_val = 0x%x\n",key_val);

}

return 0;

}

14.接下来就是在系统上分别编译这两个代码

15.打开超级终端,重启系统,将得到的.ko和可执行文件发送到/home下

16.insmod

.ko

17../可执行文件

18.分别按上下左右按键,效果如下:

a4c26d1e5885305701be709a3d33442f.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值