字符设备驱动程序--LED驱动
编写驱动程序需要编写那些代码:
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驱动的更多相关文章
- 嵌入式Linux驱动学习之路(二十一)字符设备驱动程序总结和块设备驱动程序的引入
字符设备驱动程序 应用程序是调用C库中的open read write等函数.而为了操作硬件,所以引入了驱动模块. 构建一个简单的驱动,有一下步骤. 1. 创建file_operations 2. 申 ...
- Linux驱动实践:你知道【字符设备驱动程序】的两种写法吗?
作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...
- Linux 简单字符设备驱动程序 (自顶向下)
第零章:扯扯淡 特此总结一下写的一个简单字符设备驱动程序的过程,我要强调一下“自顶向下”这个介绍方法,因为我觉得这样更容易让没有接触过设备驱动程序的童鞋更容易理解,“自顶向下”最初从<计算机网络 ...
- ARM Linux字符设备驱动程序
1.主设备号和次设备号(二者一起为设备号): 一个字符设备或块设备都有一个主设备号和一个次设备号.主设备号用来标识与设备文件相连的驱动程序,用来反 映设备类型.次设备号被驱动程序用来辨别操作的是哪个 ...
- 浅析Linux字符设备驱动程序内核机制
前段时间在学习linux设备驱动的时候,看了陈学松著的<深入Linux设备驱动程序内核机制>一书. 说实话.这是一本非常好的书,作者不但给出了在设备驱动程序开发过程中的所须要的知识点(如对 ...
- 一个简单的演示用的Linux字符设备驱动程序
实现如下的功能:--字符设备驱动程序的结构及驱动程序需要实现的系统调用--可以使用cat命令或者自编的readtest命令读出"设备"里的内容--以8139网卡为例,演示了I/O端 ...
- 简单linux字符设备驱动程序
本文代码参考<LINUX设备驱动程序>第三章 字符设备驱动程序 本文中的“字符设备”是一段大小为PAGE_SIZE的内存空间 功能:向字符设备写入字符串:从字符设备读出字符串 代码: 1. ...
- LINUX设备驱动程序笔记(三)字符设备驱动程序
<一>.主设备号和次设备号 对字符设备的訪问时通过文件系统内的设备名称进行的.那些设备名称简单称之为文件系统树的节点,它们通常位于/dev文件夹. 字符设备驱动程 ...
- linux设备驱动程序--串行通信驱动框架分析
linux 串行通信接口驱动框架 在学习linux内核驱动时,不论是看linux相关的书籍,又或者是直接看linux的源码,总是能在linux中看到各种各样的框架,linux内核极其庞杂,linux各 ...
随机推荐
- Yii2-核心框架代码规范
1.概述 简单说,我们使用PSR-2兼容规范,所以应用于PSR-2的一切对我们的代码也同样适用. 文件必须使用 <?php 或 <?= 标签. 文件未尾应该有一个新行. PHP代码文件必须 ...
- JavaScript basics: 2 ways to get child elements with JavaScript
原文: https://blog.mrfrontend.org/2017/10/2-ways-get-child-elements-javascript/ Along the lines of oth ...
- phpunit与xdebug的使用
基本说明: 1.xdebug是程序猿在调试程序过程中使用的bug调试暴露工具 windows下安装: 1)下载php对应的dll文件,下载地址:https://xdebug.org/download. ...
- (转)Vue.use源码分析
我想有过vue开发经验的,对于vue.use并不陌生.当使用vue-resource或vue-router等全局组件时,必须通过Vue.use方法引入,才起作用.那么vue.use在组件引入之前到底做 ...
- oneapm的技术博客(简书),用来追溯群里的讨论,mark
http://www.jianshu.com/users/572133740c3f/latest_articles
- ubuntu14.04 配置tomcat8
ubuntu下配置tomcat的过程事实上和windows是差点儿相同的,以下一起来看一下怎样在ubuntu14.04中配置tomcat. 1.下载tomcat 地址:http://tomcat.ap ...
- react-native + teaset(Drawer)实现侧边菜单
1.代码 /** * 购物车 */ import React, {Component} from 'react'; import { View, Image, } from 'react-native ...
- spring揭秘读书笔记----spring的ioc容器之BeanFactory
spring的ioc容器是一种特殊的Ioc Service Provider(ioc服务提供者),如果把普通的ioc容器认为是工厂模式(其实很相似),那spring的ioc容器只是让这个工厂的功能更强 ...
- javascript学习笔记(三)
1.与命名空间相关的方法以及属性 2.任何支持style特性的HTML元素在Javascript中都有一个对应的style属性.这个属性是CSSStyleDecalration的实例, 包含着通过HT ...
- spring学习笔记(四)
1.aop编程 a.前置通知 .... <!-- 配置被代理的对象 --> <bean id="test1Service" class="com. ...