编写驱动程序需要编写那些代码:

1、硬件相关的驱动程序

2、Makefile的编译程序

3、还需要编写一个相关的测试程序

比如说:一个摄像头驱动程序

1、驱动程序的编写,需要编写一些硬件相关的操作,编译Makefile

2、安装、运行、卸载驱动程序(insmod ***、。./*** 、remod *** )。

3、使用这个驱动程序:需要一个测试程序,如QQ(测试程序)打开摄像头。

编写驱动程序框架:

APP:(测试程序)                         open         read         write         .........

-------------------------------------------------------------------------------------------

内核                                           sys_open    sys_read  sys_writ    sys_.......

-------------------------------------------------------------------------------------------

驱动程序                          入口函数:

注册一个结构体:register("  ",&file_operation);

/* 这里执行相关的硬件操作 */

struct   file_operation{

.open      =    open_,

.read       =    read_,

.write       =    write_,

}

出口函数:

----------------------------------------------------------------------------------------------------------------------------------------------------

驱动程序的编写步骤包括:

入口函数

static int first_drv_init(void)

{major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核}

出口函数

static void first_drv_exit(void)

{
  unregister_chrdev(major, "first_drv"); // 卸载

}

构造一个file_operation结构体

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

相关的修饰:让内核知道这是个特殊的函数,作为驱动用

module_init(first_drv_init);
module_exit(first_drv_exit);

在加上一个协议:应为Linux为开源的,所以要遵循一些协议

MODULE_LICENSE("GPL");

剩下的就是对结构体里面的open、read、write函数进行硬件操作,如open对硬件引脚的定义、设置,read读取硬件引脚寄存器的状态,如灯是开还是关,write就是对相关硬件寄存器的操作,比如对相关数据寄存器写0/1来控制LED灯的亮灭。

相关硬件操作:比如对于LED灯简单硬件操作而言

根据芯片手册、原理图、确定硬件,操作相关寄存器对设置相关的引脚。然后设置相关的数据寄存器对引脚的控制。

上层测试程序会调用open、read、write函数最终会调用到驱动程序file_opreation结果体里面的open、read、write函数进而对硬件如LED灯的点亮操作。

相关代码如下

fist_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");

编写: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

编译成功后,把程序放到开发板文件系统目录下,执行insmod  文件名  装载驱动,运行驱动程序为./文件名   如果想卸载执行remod 文件名命令

测试程序:

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

字符设备驱动程序--LED驱动的更多相关文章

  1. 嵌入式Linux驱动学习之路(二十一)字符设备驱动程序总结和块设备驱动程序的引入

    字符设备驱动程序 应用程序是调用C库中的open read write等函数.而为了操作硬件,所以引入了驱动模块. 构建一个简单的驱动,有一下步骤. 1. 创建file_operations 2. 申 ...

  2. Linux驱动实践:你知道【字符设备驱动程序】的两种写法吗?

    作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...

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

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

  4. ARM Linux字符设备驱动程序

    1.主设备号和次设备号(二者一起为设备号): 一个字符设备或块设备都有一个主设备号和一个次设备号.主设备号用来标识与设备文件相连的驱动程序,用来反  映设备类型.次设备号被驱动程序用来辨别操作的是哪个 ...

  5. 浅析Linux字符设备驱动程序内核机制

    前段时间在学习linux设备驱动的时候,看了陈学松著的<深入Linux设备驱动程序内核机制>一书. 说实话.这是一本非常好的书,作者不但给出了在设备驱动程序开发过程中的所须要的知识点(如对 ...

  6. 一个简单的演示用的Linux字符设备驱动程序

    实现如下的功能:--字符设备驱动程序的结构及驱动程序需要实现的系统调用--可以使用cat命令或者自编的readtest命令读出"设备"里的内容--以8139网卡为例,演示了I/O端 ...

  7. 简单linux字符设备驱动程序

    本文代码参考<LINUX设备驱动程序>第三章 字符设备驱动程序 本文中的“字符设备”是一段大小为PAGE_SIZE的内存空间 功能:向字符设备写入字符串:从字符设备读出字符串 代码: 1. ...

  8. LINUX设备驱动程序笔记(三)字符设备驱动程序

          <一>.主设备号和次设备号        对字符设备的訪问时通过文件系统内的设备名称进行的.那些设备名称简单称之为文件系统树的节点,它们通常位于/dev文件夹. 字符设备驱动程 ...

  9. linux设备驱动程序--串行通信驱动框架分析

    linux 串行通信接口驱动框架 在学习linux内核驱动时,不论是看linux相关的书籍,又或者是直接看linux的源码,总是能在linux中看到各种各样的框架,linux内核极其庞杂,linux各 ...

随机推荐

  1. poj 1426 Find The Multiple (bfs 搜索)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 729 ...

  2. Struts + Spring + Hibernate 进阶开端(一)

    Long Long ago,就听说过SSH,起初还以为是一个东东,具体内容更是不详,总觉得高端大气上档次,经过学习之后才发现,不仅仅是高大上,更是低调奢华有内涵,经过一段时间的研究和学习SSH框架的基 ...

  3. 使用 原生js 制作插件 (javaScript音乐播放器)

    1.引用页面 index.html <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  4. Linux信号(signal) 机制分析(转)

    [摘要]本文分析了Linux内核对于信号的实现机制和应用层的相关处理.首先介绍了软中断信号的本质及信号的两种不同分类方法尤其是不可靠信号的原理.接着分析了内核对于信号的处理流程包括信号的触发/注册/执 ...

  5. 使用AVPlayer制作一个播放器

    代码地址如下:http://www.demodashi.com/demo/11685.html AVPlayer 是一个强大的视频播放器,可以播放多种格式的视频,缺点是没有控制界面,需要自己去实现. ...

  6. 记一次elementUI Icon 加载无效的问题。并且提示错误 Failed to decode downloaded font:

    问题在于webpack的loader中.检查了一下发现有两个相同的file-loader的配置,删除其中一个即可.

  7. vue 父子组件属性传递

    父子组件属性传递 注意:0.谁被引用,谁就算子组件  1.属性命名最好完全小写,否则需要如下格式转换:myAttr == my-attr 2.引入的vue组件后必须通过 components 注册才能 ...

  8. VC驿站黑客编程(关机,重新启动,注销)

    此程序在VS2013下编译通过,假设换到编译器,大家能够稍作改动使用 #include<Windows.h> #include<tchar.h> #include"r ...

  9. 内核并发管理---spin lock

    自旋锁最初是为了在smp系统上使用而设计. 1.在单处理器非抢占模式下,自旋锁不做任何事情. #ifdef CONFIG_PREEMPT_COUNT     //支持抢占模式 #define pree ...

  10. Django学习之模板标签和变量

    safe过滤器和{% autoescape %}标签 首先看这样一个例子: views.py中: c = '<h3>更上一层楼</h3>' render(request,'te ...