Linux 驱动——从宏观上掌握基本框架
一、一个简单的驱动程序实例
led_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>
#define DEVICE_MAJOR 111
#define DRIVER_NAME "led_drv"
int led_open(struct inode *inode, struct file *file)
{
printk(KERN_EMERG "led_drv_open.\n");
return 0;
}
int led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
printk(KERN_EMERG "led_drv_write.\n");
return 0;
}
int led_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
printk(KERN_EMERG "led_drv_read.\n");
return 0;
}
static struct file_operations led_fopt = {
.open = led_open,
.write = led_write,
.read = led_read,
};
int __init led_init()
{
int ret;
ret = register_chrdev(DEVICE_MAJOR, DRIVER_NAME, &led_fopt); //注册驱动
if(ret<0)
{
printk(KERN_EMERG "can't register major number.\n");
return ret;
}
return 0;
}
void __exit led_exit()
{
unregister_chrdev(DEVICE_MAJOR, DRIVER_NAME); //卸载驱动
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
Makefile 文件:
obj-m += led_drv.o
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
使用 make 命令编译,生成 led_drv.ko, 拷贝 led_drv.ko 至 U 盘, 使用 mount /dev/sda1 /mnt/usb 命令加载 U 盘( mnt 下无 usb 目录需提前新建 )
加载模块 insmod led_drv.ko
卸载模块 rmmod led_drv
led_app 测试文件:
#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/led_dev", O_RDWR);
if(fd<0)
{
printf("can not open \n");
}
write(fd, &val, 4);
read(fd, &val, 4);
return 0;
}
使用 arm-linux-gcc -o led_app led_app.c 命令编译 ( 这里只有一个文件就不写 Makefile 了 ), 生成 led_app 拷贝至 U 盘, 并加载 U 盘
重新加载模块 insmod led_drv.ko
运行 ./led_app 发现控制台打印 can not open, 这是因为虽然有了设备驱动, 但是没有设备文件, 所以 open 失败, 因此要先创建设备文件
创建设备文件:
mknod /dev/led_dev c 111 0
其中led_dev为设备文件(设备节点)的名称, c表示次设备为字符设备, 111表示该设备所对应的主设备号为111, 0表示该设备的次设备号为0
运行 ./led_app, 控制台输出:
led_drv_open.
led_drv_write.
led_drv_read.
Linux 驱动——从宏观上掌握基本框架的更多相关文章
- 嵌入式Linux驱动笔记(十八)------浅析V4L2框架之ioctl【转】
转自:https://blog.csdn.net/Guet_Kite/article/details/78574781 权声明:本文为 风筝 博主原创文章,未经博主允许不得转载!!!!!!谢谢合作 h ...
- 【SpringMVC学习01】宏观上把握SpringMVC框架
springmvc是一个基于mvc的web框架,是spring框架的一个模块,所以springmvc和spring无需通过中间整合层进行整合.我们先来看下spring的一个架构模型,看springmv ...
- 【MyBatis学习01】宏观上把握MyBatis框架
今天开始学习mybatis框架,博客主要记录学习过程中的一些总结,如有错误之处,欢迎留言指正~先用mybatis的鸟鸟来镇个楼,咳咳~~ mybatis框架是一个持久层框架,是Apache下的顶级项目 ...
- Linux驱动修炼之道-RTC子系统框架与源码分析【转】
转自:http://helloyesyes.iteye.com/blog/1072433 努力成为linux kernel hacker的人李万鹏原创作品,为梦而战.转载请标明出处 http://bl ...
- Gt9xx芯片,在规格书+Linux驱动的基础上,移植为USB裸机经验。直接用开发板,不去碰硬件的坑。
1,用内核代码和规格书来印证数据格式: //命令3字节,IC地址 u8 end_cmd[] = {GTP_READ_COOR_ADDR >> , GTP_READ_COOR_ADDR &a ...
- Linux内核(17) - 高效学习Linux驱动开发
这本<Linux内核修炼之道>已经开卖(网上的链接为: 卓越.当当.china-pub ),虽然是严肃文学,但为了保证流畅性,大部分文字我还都是斟词灼句,反复的念几遍才写上去的,尽量考虑到 ...
- Linux 驱动框架---input子系统
input 子系统也是作为内核的一个字符设备模块存在的,所以他也是字符设备自然也会有字符设备的文件接口.input子系统的注册过程主要分为两步,先注册了一个input class然后再注册一个字符设备 ...
- linux驱动学习(八) i2c驱动架构(史上最全) davinc dm368 i2c驱动分析【转】
转自:http://blog.csdn.net/ghostyu/article/details/8094049 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 预备知识 lin ...
- linux驱动基础系列--linux spi驱动框架分析
前言 主要是想对Linux 下spi驱动框架有一个整体的把控,因此会忽略某些细节,同时里面涉及到的一些驱动基础,比如平台驱动.设备模型等也不进行详细说明原理.如果有任何错误地方,请指出,谢谢! spi ...
随机推荐
- 不同路由器下远程ssh登录Beaglebone系统(通过路由器端口转发,配合花生壳的DDNS功能)
使用场景: 一般家庭设备都是通过路由器中转连上互联网的,而且运营商给家庭宽带分配的IP地址也是动态的.随时可能变动的.所以当程序员们离开家之后,是很难直接和家里的设备进行交互的.但是通过TPLINK路 ...
- wrap
import 'package:flutter/material.dart'; void main() { runApp(MaterialApp(home: new MyApp())); } clas ...
- Docker Compose 一键部署LNMP
Docker Compose 一键部署LNMP 目录结构 [root@localhost ~]# tree compose_lnmp/ compose_lnmp/ ├── docker-compose ...
- FL studio的循环模式简介
在FL studio中,有一个非常有用的功能,它可以加快我们的工作进程,它就是循环模式. 通过频道循环,我们可以在单个模式中创建整个项目,然后使用“按频道分割”将它们分开,以便在播放列表中排列.通常情 ...
- Canonical Coin Systems【完全背包】
问题 C: Canonical Coin Systems 时间限制: 1 Sec 内存限制: 128 MB 提交: 200 解决: 31 [提交] [状态] [命题人:admin] 题目描述 A ...
- jenkins安装部署全过程
基本配置: 1.Linux安装配置jdk环境 1.1.上传到 Linux 服务器:例如: 上传至: cd /usr/local 1.2.解压: rpm -ivh jdk-8u111-linux-x64 ...
- jQuery validator plugin 之 custom methods 案例1:multi email
1.add method jQuery.validator.addMethod( "multiemail", function (value, element) { var ema ...
- wrk 安装使用
==================== 安装 ====================https://github.com/wg/wrk/wiki sudo yum -y groupinstall ...
- python 自定义异常
python2 #coding=utf- class CustomError(Exception): def __init__(self,ErrorInfo): self.er ...
- CentOS7.5安装Python3.7报错:configure: error: no acceptable C compiler found in $PATH --Python3
1.问题解析 报错信息中有这样一条:configure: error: no acceptable C compiler found in $PATH即:配置错误,在$path中找不到可接受的C编译器 ...