一.系统功能框架

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. 《简明python教程》笔记二

    面向对象的编程: 类和对象是面向对象编程的两个主要方面.类创建一个新类型,而对象是这个类的实例.对象可以使用普通的属于对象的变量存储数据.属于一个对象或类的变量被称为域.对象也可以使用属于类的函数来具 ...

  2. 关于url中的#-----hash

    前言:不知道你们对url地址中的#一开始是怎么理解的,反正我以前一直都是默认那就是本页面中该id的位置.今天看了篇文章,才把这个真正透彻理解. 1,#涵义 #代表网页中的一个位置.其右面的字符,就是该 ...

  3. django框架代码基础

    urls.py 导入相对应的模块from django.conf.urls import url,includefrom django.contrib import adminfrom son1.vi ...

  4. mybatis原理

    http://blog.csdn.net/column/details/mybatis-principle.html?page=1

  5. samba完美安装

    感觉是一个相当强大的东西. Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件.它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务.为客户机/服务器型协议,客户机通过该协议 ...

  6. ios回调函数的标准实现:protocol+delegate

    一.项目结构

  7. 不刷新页面获取HTML进行显示

    $.ajax({ url: "请求地址",          dataType: "text",         type: "GET"   ...

  8. C/C++面试题总结

    腾讯阿里面试题总结:1. 多态机制2. 排序算法(快排.堆排)3. 程序内存分配4. unix多线程5. 哈希查找6. oop特点7. 素数(优化)8. 字符串掩膜操作(内存紧凑)9. 多边形相交10 ...

  9. hihoCoder挑战赛25

    萌新第一次打hihoCoder的比赛有点慌 T1 T1并不是特别难想到dp就好做了 显而易见的是一个01背包问题 Code: #include <cstdio> #include < ...

  10. 点击空白处div消失的方法

    这是做的js页面的一部分,也是上一篇文章中加载json格式数据后展示的效果界面. 现在的问题是:点击南京市后会弹出下面的白色的框,点击框右上角的X号后会关闭白色的框,现在想点击白色的框周围的任一地方, ...