【实践】第一个驱动

实验环境: 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

更多精彩资讯,请扫码关注。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值