Linux 驱动——Led驱动2
led_drv.c驱动文件:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/uaccess.h> //包含copy_from_user函数
#include <linux/device.h> //包含class类相关的处理函数
#include <linux/fs.h> //包含file_operations结构体
#include <asm/io.h> //包含iomap函数iounmap函数
#define DRIVER_NAME "led_drv"
int major;
static struct class *led_drv_class;
static struct class_device *led_drv_class_dev[4];
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static int led_drv_open(struct inode *inode, struct file *file)
{
int minor = MINOR(inode->i_rdev); //获取设备的次设备号
switch(minor)
{
case 0:
{
*gpfcon &= ~(0x3<<(2*4));
*gpfcon |= (0x1<<(2*4));
*gpfcon &= ~(0x3<<(2*5));
*gpfcon |= (0x1<<(2*5));
*gpfcon &= ~(0x3<<(2*6));
*gpfcon |= (0x1<<(2*6));
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6)); //输出低电平
break;
}
case 1:
{
*gpfcon &= ~(0x3<<(2*4));
*gpfcon |= (0x1<<(2*4));
*gpfdat &= ~(0x1<<4);
break;
}
case 2:
{
*gpfcon &= ~(0x3<<(2*5));
*gpfcon |= (0x1<<(2*5));
*gpfdat &= ~(0x1<<5);
break;
}
case 3:
{
*gpfcon &= ~(0x3<<(2*6));
*gpfcon |= (0x1<<(2*6));
*gpfdat &= ~(0x1<<6);
break;
}
}
return 0;
}
static int led_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int minor = MINOR(file->f_dentry->d_inode->i_rdev); //获取设备的次设备号
char val, ret;
ret = copy_from_user(&val, buf, 1);
if(ret!=0)
{
printk("led_drv_write error 1 \n");
}
switch(minor)
{
case 0:
{
if(val==1)
{
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6)); //输出低电平, 灯亮
}
else
{
*gpfdat |= ((1<<4) | (1<<5) | (1<<6)); //输出高电平, 灯灭
}
break;
}
case 1:
{
if(val==1)
{
*gpfdat &= ~(1<<4);
}
else
{
*gpfdat |= (1<<4);
}
break;
}
case 2:
{
if(val==1)
{
*gpfdat &= ~(1<<5);
}
else
{
*gpfdat |= (1<<5);
}
break;
}
case 3:
{
if(val==1)
{
*gpfdat &= ~(1<<6);
}
else
{
*gpfdat |= (1<<6);
}
break;
}
}
return 0;
}
static struct file_operations led_drv_fopt = {
.owner = THIS_MODULE,
.open = led_drv_open,
.write = led_drv_write,
};
static int led_drv_init(void)
{
int i;
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
major = register_chrdev(0, DRIVER_NAME, &led_drv_fopt); //向内核中注册驱动
if(major<0)
{
printk(KERN_EMERG "can not obtain major number.\n");
return major;
}
//建立设备类别
led_drv_class = class_create(THIS_MODULE, DRIVER_NAME);
//分配次设备号,主设备号为major从设备号为0
led_drv_class_dev[0] = class_device_create(led_drv_class, NULL, MKDEV(major, 0), NULL, "leds");
//分配次设备号,主设备号为major从设备号为i
for(i=1; i<4; i++)
{
led_drv_class_dev[i] = class_device_create(led_drv_class, NULL, MKDEV(major, i), NULL, "led%d", i);
}
return 0;
}
static void led_drv_exit(void)
{
int i;
unregister_chrdev(major, DRIVER_NAME);
for(i=0; i<4; i++)
{
class_device_unregister(led_drv_class_dev[i]);
}
class_destroy(led_drv_class);
iounmap(gpfcon);
}
module_init(led_drv_init);
module_exit(led_drv_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:
rm -rf *.o *.ko *.order *.symvers *.mod.c
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;
char *filename;
filename = argv[1];
fd = open(filename, O_RDWR);
if(fd<0)
{
printf("error, can't open %s\n", filename);
return 0;
}
if(strcmp("on", argv[2])==0)
{
val = 1;
write(fd, &val, 1);
}
else if(strcmp("off", argv[2])==0)
{
val = 0;
write(fd, &val, 1);
}
return 0;
}
编译生成led_drv.ko和led_app, 运行./led_app /dev/leds on
./led_app /dev/leds off
./led_app /dev/led1 on
./led_app /dev/led1 off
./led_app /dev/led2 on
./led_app /dev/led2 off
./led_app /dev/led3 on
./led_app /dev/led3 off
可以看到不同的效果
Linux 驱动——Led驱动2的更多相关文章
- (笔记)linux设备驱动--LED驱动
linux设备驱动--LED驱动 最近正在学习设备驱动开发,因此打算写一个系列博客,即是对自己学习的一个总结,也是对自己的一个督促,有不对,不足,需要改正的地方还望大家指出,而且希望结识志同道合的朋友 ...
- Linux 驱动——Led驱动1
led_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/init ...
- 自己动手写最简单的Android驱动---LED驱动的编写【转】
本文转载自:http://blog.csdn.net/k_linux_man/article/details/7023824 转载注明出处,作者:K_Linux_Man, 薛凯 山东中医药大学,给文章 ...
- linux设备驱动归纳总结(五):4.写个简单的LED驱动【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-84693.html linux设备驱动归纳总结(五):4.写个简单的LED驱动 xxxxxxxxxxx ...
- 全志A33 linux led驱动编程(附实测参考代码)
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 开发平台 * 芯灵思Sinl ...
- Linux驱动之LED驱动编写
从上到下,一个软件系统可以分为:应用程序.操作系统(内核).驱动程序.结构图如下:我们需要做的就是写出open.read.write等驱动层的函数.一个LED驱动的步骤如下: 1.查看原理图,确定需要 ...
- 【Linux开发】linux设备驱动归纳总结(五):4.写个简单的LED驱动
linux设备驱动归纳总结(五):4.写个简单的LED驱动 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- 嵌入式Linux学习笔记(三) 字符型设备驱动--LED的驱动开发
在成功构建了一个能够运行在开发板平台的系统后,下一步就要正式开始应用的开发(这里前提是有一定的C语言基础,对ARM体系的软/硬件,这部分有疑问可能要参考其它教程),根据需求仔细分解任务,可以发现包含的 ...
- linux驱动之LED驱动
通过之前的学习,了解到linux驱动编写的流程是:先通过注册函数注册我们编写的入口函数,然后在入口函数中获取设备号->注册字符设备->自动创建设备节点->获取设备树信息,最后通过销毁 ...
随机推荐
- hadoop 安装之 hadoop、hive环境配置
总结了一下hadoop的大致安装过程,按照master . slave的hadoop主从类别,以及root和hadoop集群用户两种角色,以职责图的方式展现,更加清晰一些
- 第二天:python的函 数、循环和条件、类
https://uqer.io/community/share/54c8af17f9f06c276f651a54 第一天学习了Python的基本操作,以及几种主要的容器类型,今天学习python的函数 ...
- http/1.0/1.1/2.0与https的比较
HTTP是HyperText Transfer Protocol的缩写,译为超文本传输协议.是一种应用于OSI七层模型中应用层的协议,是我们平常互联网网络通信传输的基础.它的作用就是规定了服务器和客户 ...
- node.js http接口调试时请求串行特性分析
缘起: 产品业务上有个类数据库服务的请求时间比较长(类似mysql的sql查询),为了优化减少并发时的请求数,做了一个并发时共用请求的优化. 通过单元测试后,想通过手动模拟看下效果,发现优化一直不能生 ...
- vue使用动态渲染v-model输入框无法输入内容
最近使用ElementUI框架,在动态渲染表单的时候,表单框无法输入内容,但是绑定model的数据是会发生变化 解决方法: 将动态生成的表单对象,深拷贝到 data 对象中 <el-date-p ...
- ECC算法软件保护中的应用
椭圆曲线在软件注册保护的应用 我们知道将公开密钥算法作为软件注册算法的好处是Cracker很难通过跟踪验证算法得到注册机.下面,将简介一种利用Fp(a,b)椭圆曲线进行软件注册的方法. 软件作者按如下 ...
- iis设置http重置到https
http://www.cnblogs.com/tangge/p/4259749.html 1.购买SSL证书,参考:http://www.cnblogs.com/yipu/p/3722135.html ...
- linux安装mysql(shell一键安装)
1. 相关文件(install_mysql.sh.my.cnf.mysqld相关内容在文中最后面) 2. 将上面的文件上传到linux服务器某一目录下 3.给install_mysql.sh赋执行权限 ...
- ESP系列MQTT数据通信
1.使用一个深圳四博智联科技有限公司的NODEMCU开发板. 2.下载MQTT的SDK压缩包,请查看附件. 3.用官方提供的Eclipse打开MQTT的sdk开发包. 4.打开include文件夹中的 ...
- vim打开txt文件看到^@字符
'\0'是不可见字符,使用vim编辑器查看的文本文件中如果包含'\0'字符,vim会自动将'\0'字符转换为^@字符. 看下面的代码: #include <stdio.h> #includ ...