实验环境: JZ2440
###1、编写驱动程序
文件:first_drv.v
#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 int first_drv_open(struct inode *inode,struct file *file)
{
printk("first_drv_open\n");
return 0;
}
static ssize_t first_drv_write(struct file *file,const char __user *buf,size_t count,loff_t *ppos )
{
printk("first_drv_write\n");
return 0;
}
struct file_operations first_drv_fops = {
.owner = THIS_MODULE,
.open = first_drv_open,
.write = first_drv_write
};
int first_drv_init(void)
{
register_chrdev(111,"first drv",&first_drv_fops);
return 0;
}
void first_drv_exit(void)
{
unregister_chrdev(111,"first drv");
}
module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");
###2、编写Makefile
文件:Makefile
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += first_drv.o
###3、编写测试代码
文件:firstdrvtest.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
int val = 1;
fd = open("/dev/xxx",O_RDWR);
if(fd < 0)
{
printf("can't open!\n");
}
write(fd,&val,4);
return 0;
}
###4、编译驱动程序和测试程序
驱动程序直接make即可生成first_drv.ko;
测试程序编译:
arm-linux-gcc -o firstdrvtest firstdrvtest.c
###5、加载驱动模块
insmod firstdrv.ko
###6、创建设备节点
mknod /dev/xxx c 111 0
###7、执行测试程序
./firstdrvtest