OK6410驱动学习之first_drv

一、实验环境

              操作系统:ubuntu 12.04

             开发板:OK6410 (DDR:256   NAND Flash:2G)

             开发板linux内核:linux-2.6.36.2

二、简单字符设置驱动程序框架思路

           1.确定驱动程序的主设备号与设备名称(一个主设备号对应一个驱动)。

           2、驱动程序实现相关操作(读、写、控制)对就open、write、read、iocrl函数。

           3、定义file_operations结构体,并注册相应的操作函数名。如:

    static struct file_operations first_drv_fops = {
    .owner  = THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   = first_drv_open,     
	  .write	=	first_drv_write,	   
    };


          4、注册驱动程序到内核:告诉内核驱动程序的主设备号、设备名称、file_operations结构体。注册函数为register_chrdev()。

                如:

          register_chrdev(111, "first_drv", &first_drv_fops);
                         //111(主设备号), "first_drv"(设备名称), &first_drv_fops(file_operations结构体)


          5、定义驱动程序的入口函数。用module_init(first_drv_init)宏来修饰。注册驱动程序的函数通常放到入口函数中。在加载驱动程序到内核(insomd)时,将

                调用驱动程序的入口函数。实现驱动模块的加载。如:

                 

       static int first_drv_init(void)//入口函数
       {
	register_chrdev(111, "first_drv", &first_drv_fops);//注册字符设备
	return 0;
       }
       module_init(first_drv_init);//宏来修饰


           6、从内核卸载驱动程序:卸载驱动程序与注册驱动程序是一个相反的动作。用函数unregister_chrdev()如:

               

       unregister_chrdev(111, "first_drv"); // 卸载
                       //111, (主设备号),first_drv"(设备名称)

 

           7、定义驱动程序的出口函数。用module_exit(first_drv_exit)宏来修饰。卸载驱动程序的函数通常放到出口函数中。在内核卸载驱动程序(rmmd)时,将

                调用驱动程序的入口函数。实现驱动模块的加载。如:

              

       static void first_drv_exit(void)
       {
	unregister_chrdev(111, "first_drv"); // 卸载
          //return 0;
       }
       module_exit(first_drv_exit);

        
三、程序

/*********************************************************
*说明:第一个字符型驱动程序
*内核:2.6.36.2
**************************************************************/

#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 <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>

#include <mach/map.h>
#include <mach/regs-clock.h>
#include <mach/regs-gpio.h>

#include <plat/gpio-cfg.h>
#include <mach/gpio-bank-e.h>
#include <mach/gpio-bank-m.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;
}


/* 注册函数 */
static struct file_operations first_drv_fops = {
    .owner  = THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   = first_drv_open,     
	  .write	=	first_drv_write,	   
};

 // 注册, 告诉内核
static int first_drv_init(void)
{
	register_chrdev(111, "first_drv", &first_drv_fops);
	return 0;
}

static void first_drv_exit(void)
{
	unregister_chrdev(111, "first_drv"); // 卸载
  //return 0;
}


module_init(first_drv_init);//调用入口函数
module_exit(first_drv_exit);//释放入口函数

MODULE_LICENSE("GPL");


四、Makefile

KERN_DIR = /home/zhangyixin/linux_sys/linux-2.6.36.2
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


五、测试程序

#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;
}


 

 测试驱动程序时,要先创建“XXX”设置文件:

 

#mknod   /dev/xxx   c   111  0
//     设置名称   字符设备   主设备号  次设备号


 

 

            

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值