在x-86-Linux环境下搭建Mini2440内核驱动编译环境并编译内核过程
- 搭建arm-linux-gcc交叉编译环境(略);
- 建立x86-Linux(Ubuntu)下mini2440开发环境并编译内核驱动;
- 将mini2440开发板Linux内核源代码复制到Ubuntn系统任意合法位置;
- 打开终端,进入root用户;
- 在/opt目录下创建/FriendlyARM/mini2440,并将Mini2440开发板内核源码压缩包复制到mini2440目录下;
- 编写LED内核驱动代码:
#include<mach/regs-gpio.h> #include<mach/hardware.h> #include<linux/miscdevice.h> #include<linux/delay.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/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/irq.h> #include<asm/uaccess.h> #include<asm/atomic.h> #include<asm/unistd.h>
#define DEVICE_NAME "led_dev"
static unsigned long led_table[]={ S3C2410_GPB(5), S3C2410_GPB(6), S3C2410_GPB(7), S3C2410_GPB(8), };
static unsigned int led_cfg_table[]={ S3C2410_GPIO_OUTPUT, S3C2410_GPIO_OUTPUT, S3C2410_GPIO_OUTPUT, S3C2410_GPIO_OUTPUT, };
static int sbc2440_leds_ioctl( struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg ){ switch(cmd){ case 0: case 1: if(arg>4){ return -EINVAL; } s3c2410_gpio_setpin(led_table[arg],!cmd); return 0; default: return -EINVAL; } }
static struct file_operations dev_fops={ .owner= THIS_MODULE, .ioctl = sbc2440_leds_ioctl, };
static struct miscdevice misc ={ .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &dev_fops, };
static int __init dev_init(void){ int ret; int i;
printk(KERN_ALERT "Hello, led_dev is installed!\n");
for(i=0;i<4;i++){ s3c2410_gpio_cfgpin(led_table[i],led_cfg_table[i]); s3c2410_gpio_setpin(led_table[i],0); } ret =misc_register(&misc);
printk(DEVICE_NAME"\t Yipingjian_led_dev\n"); return ret; }
static void __exit dev_exit(void){ printk(KERN_ALERT"Good-bye,led_dev is removed!\n"); misc_deregister(&misc); }
module_init(dev_init); module_exit(dev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Yipingjian Inc."); |
进入/opt/FriendlyARM/mini2440/linux-2.6.32.2/drivers/char,将编写好的驱动文件led_dev.c复制到该目录下。打开Makefile,添加如阴影所示,并保存:
进入/opt/FriendlyARM/mini2440/linux-2.6.32.2,输入命令:make modules,编译内核,
可以看到/opt/FriendlyARM/mini2440/linux-2.6.32.2/drivers/char目录下,出现编译成功的.ko文件: