一.系统功能框架

U-boot : 启动内核

linux kernel: 启动应用

应用: open,read,write 都是通过C库实现,汇编就相当于swi val,引发中断,通过系统调用接口在异常中断调用不同处理函数(VFS)。

二.字符设备驱动框架

1.编写驱动:open , read, write 等功能函数的实现:

   static int led_drv_open(struct inode *inode, struct file *file){
      printk("led_drv_open\n");
      return 0;

   }

   static int led_drv_write(struct file *file, const char __user *buf, size_t count, loff_t* ppos){
      printk("led_drv_write\n");
      return 0;
   }

2.注册驱动:

  ①构造file_operations结构:  
      static struct file_operation led_drv_fops = {
        .owner = THIS_MODULE,
        .open = led_drv_open,
        .write = led_drv_write,
      }

  ②注册驱动:
    入口函数: 

           int major; 
         int led_drv_init(void){
            major = register_chrdev(0, "led_drv", &led_drv_fops);  //注册字符设备,major-主设备号 mior-次设备号,app根据设备类型和主设备号调用具体驱动。
            //创建设备节点:
            //... class_create(THIS_MODULE, "led_drv");
            //... clase_device_create(...);
            return 0;
         }
    修饰入口函数: module_init(led_drv_init);
    
    出口函数:void led_drv_exit(void){
            unregister_chrdev(major, "led_drv");  
         }
    修饰出口函数: module_exit(led_drv_exit);

  ③编译:

      Makefile :

        KERN_DIR=/work/system/linux-2.6.22.6  //本地编译过的linux源码目录
        all:
            make -C $(KERN_DIR) M=`pwd` modules
        clean:
            make -C $(KERN_DIR) M=`pwd` modules clean
            rm -rf modules.order
        obj-m  += led_drv.o

  ④加载:

      insmod led_drv.ko
      cat /proc/devices

  

三.參考代碼:

驱动程序:first_drv.c

#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 struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev; volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL; static int first_drv_open(struct inode *inode, struct file *file)
{
//printk("first_drv_open\n");
/* 配置GPF4,5,6为输出 */
*gpfcon &= ~((0x3<<(*)) | (0x3<<(*)) | (0x3<<(*)));
*gpfcon |= ((0x1<<(*)) | (0x1<<(*)) | (0x1<<(*)));
return ;
} static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val; //printk("first_drv_write\n"); copy_from_user(&val, buf, count); // copy_to_user(); if (val == )
{
// 点灯
*gpfdat &= ~((<<) | (<<) | (<<));
}
else
{
// 灭灯
*gpfdat |= (<<) | (<<) | (<<);
} return ;
} static struct file_operations first_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = first_drv_open,
.write = first_drv_write,
}; int major;
static int first_drv_init(void)
{
major = register_chrdev(, "first_drv", &first_drv_fops); // 注册, 告诉内核 firstdrv_class = class_create(THIS_MODULE, "firstdrv"); firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, ), NULL, "xyz"); /* /dev/xyz */ gpfcon = (volatile unsigned long *)ioremap(0x56000050, );
gpfdat = gpfcon + ; return ;
} static void first_drv_exit(void)
{
unregister_chrdev(major, "first_drv"); // 卸载 class_device_unregister(firstdrv_class_dev);
class_destroy(firstdrv_class);
iounmap(gpfcon);
} module_init(first_drv_init);
module_exit(first_drv_exit); MODULE_LICENSE("GPL");

测试程序:firstdrvtest.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h> /* firstdrvtest on
* firstdrvtest off
*/
int main(int argc, char **argv)
{
int fd;
int val = ;
fd = open("/dev/xyz", O_RDWR);
if (fd < )
{
printf("can't open!\n");
}
if (argc != )
{
printf("Usage :\n");
printf("%s <on|off>\n", argv[]);
return ;
} if (strcmp(argv[], "on") == )
{
val = ;
}
else
{
val = ;
} write(fd, &val, );
return ;
}

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

 

    

      

        

Linux学习 :字符设备框架的更多相关文章

  1. linux学习--字符设备驱动

    目录 1.字符设备驱动抽象结构 2.设备号及设备节点 2.1 设备号分配与管理 2.2 设备节点的生成 3.打开设备文件 linux驱动有基本的接口进行注册和卸载,这里不再做详细说明,本文主要关注li ...

  2. linux kernel 字符设备详解

    有关Linux kernel 字符设备分析: 参考:http://blog.jobbole.com/86531/ 一.linux kernel 将设备分为3大类,字符设备,块设备,网络设备. 字符设备 ...

  3. Linux实现字符设备驱动的基础步骤

    Linux应用层想要操作kernel层的API,比方想操作相关GPIO或寄存器,能够通过写一个字符设备驱动来实现. 1.先在rootfs中的 /dev/ 下生成一个字符设备.注意主设备号 和 从设备号 ...

  4. 【转】Linux高级字符设备之Poll操作

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275559.html 在用户程序中,select()和poll()也是与设备阻塞与非阻塞 ...

  5. Linux高级字符设备驱动

    转载:http://www.linuxidc.com/Linux/2012-05/60469p4.htm 1.什么是Poll方法,功能是什么? 2.Select系统调用(功能)      Select ...

  6. Linux 简单字符设备驱动程序 (自顶向下)

    第零章:扯扯淡 特此总结一下写的一个简单字符设备驱动程序的过程,我要强调一下“自顶向下”这个介绍方法,因为我觉得这样更容易让没有接触过设备驱动程序的童鞋更容易理解,“自顶向下”最初从<计算机网络 ...

  7. linux驱动---字符设备的注册register_chrdev说起

    首先我们在注册函数里面调用了register_chrdev(MEM_MAJOR,"mem",&memory_fops),向内核注册了一个字符设备. 第一个参数是主设备号,0 ...

  8. linux 高级字符设备驱动 ioctl操作介绍 例程分析实现【转】

    转自:http://my.oschina.net/u/274829/blog/285014 1,ioctl介绍 ioctl控制设备读写数据以及关闭等. 用户空间函数原型:int ioctl(int f ...

  9. Linux高级字符设备驱动 poll方法(select多路监控原理与实现)

    1.什么是Poll方法,功能是什么? 2.Select系统调用(功能)      Select系统调用用于多路监控,当没有一个文件满足要求时,select将阻塞调用进程.      int selec ...

随机推荐

  1. spring4+websocket+nginx详细配置

    实现的版本jdk1.7.0_25, tomcat7.0.47.0, Tengine/2.1.1 (nginx/1.6.2), servlet3.0, spring4.2.2 使用maven导入版本3. ...

  2. C语言程序设计进阶 第1周编程题

    第1周编程题 查看帮助 返回 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数 ...

  3. java面向对象的语言

    对象:真实存在唯一的事物. 类: 实际就是对某种类型事物的共性属性与行为的抽取. 抽象的概念.... 车 我们从小的时候就不断的接触很多的对象,我们的大脑就会把 这些具备相同属性与行为的事物进行分类. ...

  4. c/c++中两颗璀璨的明珠

    1.指针: 函数指针做函数参数 回调函数 语法现象 意义 实现什么效果 (1).间接赋值成立的三个条件 a.两个变量 b.建立关联 c. *p-> (2).函数指针做函数参数 a.调用的角度去理 ...

  5. gulp 插件

    原文链接:http://www.mamicode.com/info-detail-517085.html gulp是什么? http://gulpjs.com/ 相信你会明白的! 与著名的构建工具gr ...

  6. Filter过滤器的写法

    http://pengenjing.iteye.com/blog/1607248 这里写的过滤器用的是适配器模式,思路为: 先写一个类实现Filter,然后在让你写的过滤器来继承自这个类: 步骤:1. ...

  7. mybatis-缓存1

    以下转自:http://www.cnblogs.com/weidiao/p/5469046.html mybatis有两级缓存机制,一级缓存默认开启,可以在手动关闭:二级缓存默认关闭,可以手动开启.一 ...

  8. 一个div相对于外层的div水平和垂直居中

    我自己感觉,第四种比较常用 <title>无标题文档</title><style>        .parent {           width:800px; ...

  9. OneProxy自动剔除延迟节点

    在多种情况下,MySQL主从节点之间可能存在延迟.比如,主库的写入能力强于从库的写入能力.从库单线程复制.从库复制出错导致相关进程停止.为了保证数据的时效性,OneProxy提供了复制时效性检查,用于 ...

  10. centos6.5安装lnmp环境

    1.安装nignx的源,默认cenots6没有的. rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-cent ...