内核裁剪,将自己写的驱动加入内核及按键驱动的代码(arm TQ2440)

来源:互联网 发布:37传奇霸业羽毛数据 编辑:程序博客网 时间:2024/05/19 05:01
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/*这里改成自己的.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内核文件烧写进[color=rgb(68, 68, 68) !important]ARM板,要清除原来的,UBOOT,系统也要重新烧写(9,1,3,6 来一遍)
12.驱动代码如下(记得改makefile)
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/gpio.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/module.h>
#include <linux/device.h> //class_create
#include <mach/regs-gpio.h>        //S3C2410_GPF1
//#include <asm/arch/regs-gpio.h>  
#include <mach/hardware.h>
//#include <asm/hardware.h>
#include <linux/interrupt.h>  //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;

/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
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)
{
/*  K1 ---- EINT1,K2 ---- EINT4,K3 ---- EINT2,K4 ---- EINT0
  *  配置GPF1、GPF4、GPF2、GPF0为相应的外部中断引脚
  *  IRQT_BOTHEDGE应该改为IRQ_TYPE_EDGE_BOTH
*/
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;
/* 当没有按键按下时,休眠。
* 即ev_press = 0;
* 当有按键按下时,发生中断,在中断处理函数会唤醒
* 即ev_press = 1;
* 唤醒后,接着继续将数据通过copy_to_user函数传递给应用程序
*/
wait_event_interruptible(button_waitq, ev_press);
copy_to_user(user, &key_val, 1);
/* 将ev_press清零 */
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;
}

/* File operations struct for character device */
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)
{
/* 主设备号设置为0表示由系统自动分配主设备号 */
major = register_chrdev(0, "third_drv", &third_drv_fops);

/* 创建thirddrv类 */
thirddrv_class = class_create(THIS_MODULE, "thirddrv");

/* 在thirddrv类下创建buttons设备,供应用程序打开设备*/
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 <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

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.分别按上下左右按键,效果如下:
1 0
原创粉丝点击