Linux 驱动——Led驱动1
led_drv.c驱动文件:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/io.h> //包含 iomap 和 iounmap 函数
#include <asm/uaccess.h> //包含 copy_from_user 函数
#include <linux/device.h> //包含 class 类相关的处理函数
#include <linux/fs.h> //包含 file_operations 结构体
#define DRIVER_NAME "led_drv"
#define DEVICE_NAME "led_dev"
int major;
static struct class *leddrv_class;
static struct class_device *leddrv_class_dev;
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static int led_drv_open(struct inode *inode, struct file *file)
{
*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
return 0;
}
static ssize_t led_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val, ret;
ret = copy_from_user(&val, buf, count);
if(ret!=0)
{
printk("led_drv_write error 1 \n");
}
if (val == 1)
{
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6)); //点灯
}
else
{
*gpfdat |= (1<<4) | (1<<5) | (1<<6); //灭灯
}
return 0;
}
static struct file_operations led_drv_fops = {
.owner = THIS_MODULE,
.open = led_drv_open,
.write = led_drv_write,
};
static int led_drv_init(void)
{
major = register_chrdev(0, DRIVER_NAME, &led_drv_fops);
leddrv_class = class_create(THIS_MODULE, DRIVER_NAME); //创建设备类
leddrv_class_dev = class_device_create(leddrv_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME); //在设备类下创建设备文件其实也就是设备节点,
//创建好的设备文件(设备节点)可以在中/dev
//下查看, 如果创建成功,则在/dev下应该有
//led_dev这个设备文件名,或者说是有
//led_dev这个设备节点
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
static void led_drv_exit(void)
{
unregister_chrdev(major, DRIVER_NAME);
class_device_unregister(leddrv_class_dev);
class_destroy(leddrv_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 = 1;
fd = open("/dev/led_dev", O_RDWR);
if(fd<0)
{
printf("can not open \n");
}
if(argc != 2)
{
printf("%s <on|off> \n");
return 0;
}
if(strcmp(argv[1], "on") == 0)
{
val = 1;
}
else
{
val = 0;
}
write(fd, &val, 4);
return 0;
}
编译led_drv.c与led_app.c文件, 加载编译后的模块, 运行./led_app on 或 ./led_app off可以看到灯的亮灭。
Linux 驱动——Led驱动1的更多相关文章
- (笔记)linux设备驱动--LED驱动
linux设备驱动--LED驱动 最近正在学习设备驱动开发,因此打算写一个系列博客,即是对自己学习的一个总结,也是对自己的一个督促,有不对,不足,需要改正的地方还望大家指出,而且希望结识志同道合的朋友 ...
- Linux 驱动——Led驱动2
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驱动编写的流程是:先通过注册函数注册我们编写的入口函数,然后在入口函数中获取设备号->注册字符设备->自动创建设备节点->获取设备树信息,最后通过销毁 ...
随机推荐
- Luogu P1010 幂次方
[橙题不会做系列]QAQ 是李老师上课的题目-- 这题最开始想法是打表做.事实证明这样做也可以( 老师用的是位运算-- 这种一步步分解也能想到用递归qwq #include <algorithm ...
- poj1985和poj1849(树的直径)
题目传送门:poj1985 树是连通无环图,树上任意两点之间的路径是唯一的.定义树上任 意两点u, v的距离为u到v路径上边权的和.树的直径MN为树上最长路 径,即点M和N是树上距离最远的两个点. 题 ...
- msf下的各种生成payload命令
Often one of the most useful (and to the beginner underrated) abilities of Metasploit is the msfpayl ...
- 论文笔记:Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks
Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks ICML 2017 Paper:https://arxiv.org/ ...
- Linux下调试.Net core(1):lldb的安装
windows下,我们对于.net程序发生Crash,资源泄露,死锁等问题的分析,有神器windbg,那现在我们的.net core程序运行在linux上时,该怎么进行对对Core Dump文件进行分 ...
- 解决 ln -s 软链接产生的Too many levels of symbolic links错误
参考: ln -s 软链接产生Too many levels of symbolic links错误 解决 ln -s 软链接产生的Too many levels of symbolic links错 ...
- CSS中正确理解浮动以及clear:both的关系
要注意以下几点: 1. 浮动元素会被自动设置成块级元素,相当于给元素设置了display:block(块级元素能设置宽和高,而行内元素则不可以). 2. 浮动元素后边的非浮动元素显示问题. 3. 多个 ...
- 浅谈JS中的typeof和instanceof的区别
JS中的typeof和instanceof常用来判断一个变量是否为空,或者是什么类型. typeof typeof运算符返回一个用来表示表达式的数据类型的字符串. typeof一般返回以下几个字符串: ...
- 整理this笔记
1.在浏览器全局环境中this指向的是Window console.log(this); //Window 2.在事件处理函数中的this,这个事件是由谁触发,this就指向谁 3.直接执行一个函数的 ...
- redhat7.2安全基线BI
(一) Redhat linux7.2安全基线基本型(BI) 1. 密码复杂度策略 /etc/pam.d/system-auth文件中,增加内容 password requisite pam_ ...